lmdb/0001-Patch-for-LMDB-to-use-robust-mutexes.patch

94 lines
3.9 KiB
Diff
Raw Normal View History

diff -wruN -x '*~' -x '*.o' -x '*.a' -x '*.so' -x '*.so.[0-9]' -x autom4te.cache -x .deps -x .libs ../orig-liblmdb/configure.ac ./configure.ac
--- ../orig-liblmdb/configure.ac 2014-07-28 17:44:41.873398363 +0200
+++ ./configure.ac 2014-07-28 17:47:06.143497590 +0200
@@ -11,5 +11,12 @@ m4_ifdef([AM_SILENT_RULES], [AM_SILENT_RULES([yes])])
m4_ifdef([AM_PROG_AR], [AM_PROG_AR])
LT_INIT
AC_CHECK_HEADERS([sys/file.h])
+AC_CHECK_LIB([pthread], [pthread_mutex_lock])
+AC_CHECK_LIB([pthreadGC2], [pthread_mutex_lock])
+AS_IF([test "$ac_cv_lib_pthread" = "no" && test "$ac_cv_lib_pthreadGC2" = "no"],
+[
+ AC_MSG_ERROR([Could not find pthreads library])
+])
+AC_CHECK_FUNCS([pthread_mutexattr_settype pthread_mutexattr_setrobust])
AC_CONFIG_FILES([Makefile])
AC_OUTPUT
diff -wruN -x '*~' -x '*.o' -x '*.a' -x '*.so' -x '*.so.[0-9]' -x autom4te.cache -x .deps -x .libs ../orig-liblmdb/mdb.c ./mdb.c
--- ../orig-liblmdb/mdb.c 2014-01-31 09:23:13.341427766 +0100
+++ ./mdb.c 2014-07-28 17:44:55.145959267 +0200
@@ -215,6 +215,7 @@ extern int cacheflush(char *addr, int nbytes, int cache);
#define MDB_USE_HASH 1
#define MDB_PIDLOCK 0
#define THREAD_RET DWORD
+#define EOWNERDEAD 130
#define pthread_t HANDLE
#define pthread_mutex_t HANDLE
#define pthread_cond_t HANDLE
@@ -226,7 +227,7 @@ extern int cacheflush(char *addr, int nbytes, int cache);
#define pthread_getspecific(x) TlsGetValue(x)
#define pthread_setspecific(x,y) (TlsSetValue(x,y) ? 0 : ErrCode())
#define pthread_mutex_unlock(x) ReleaseMutex(*x)
-#define pthread_mutex_lock(x) WaitForSingleObject(*x, INFINITE)
+#define pthread_mutex_lock(x) (WaitForSingleObject(x, INFINITE) == WAIT_ABANDONED ? EOWNERDEAD : 0) // masks FAIL and TIMEOUT error, but acceptable
#define pthread_cond_signal(x) SetEvent(*x)
#define pthread_cond_wait(cond,mutex) do{SignalObjectAndWait(*mutex, *cond, INFINITE, FALSE); WaitForSingleObject(*mutex, INFINITE);}while(0)
#define THREAD_CREATE(thr,start,arg) thr=CreateThread(NULL,0,start,arg,0,NULL)
@@ -2529,7 +2530,20 @@ mdb_txn_renew0(MDB_txn *txn)
env->me_live_reader = 1;
}
- LOCK_MUTEX_R(env);
+ rc = LOCK_MUTEX_R(env);
+ switch (rc)
+ {
+ case 0:
+ break;
+#ifdef HAVE_PTHREAD_MUTEXATTR_SETROBUST
+ case EOWNERDEAD:
+ // we cannot recover the state, so mark mutex as unusable
+ UNLOCK_MUTEX_R(env);
+ // FALLTHROUGH
+#endif
+ default:
+ return MDB_PANIC;
+ }
nr = ti->mti_numreaders;
for (i=0; i<nr; i++)
if (ti->mti_readers[i].mr_pid == 0)
@@ -2563,7 +2577,20 @@ mdb_txn_renew0(MDB_txn *txn)
txn->mt_dbxs = env->me_dbxs; /* mostly static anyway */
} else {
if (ti) {
- LOCK_MUTEX_W(env);
+ rc = LOCK_MUTEX_W(env);
+ switch (rc)
+ {
+ case 0:
+ break;
+#ifdef HAVE_PTHREAD_MUTEXATTR_SETROBUST
+ case EOWNERDEAD:
+ // we cannot recover the state, so mark mutex as unusable
+ UNLOCK_MUTEX_W(env);
+ // FALLTHROUGH
+#endif
+ default:
+ return MDB_PANIC;
+ }
txn->mt_txnid = ti->mti_txnid;
meta = env->me_metas[txn->mt_txnid & 1];
@@ -4458,6 +4485,12 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl)
pthread_mutexattr_t mattr;
if ((rc = pthread_mutexattr_init(&mattr))
+#ifdef HAVE_PTHREAD_MUTEXATTR_SETTYPE
+ || (rc = pthread_mutexattr_settype(&mattr, PTHREAD_MUTEX_ERRORCHECK))
+#endif
+#ifdef HAVE_PTHREAD_MUTEXATTR_SETROBUST
+ || (rc = pthread_mutexattr_setrobust(&mattr, PTHREAD_MUTEX_ROBUST))
+#endif
|| (rc = pthread_mutexattr_setpshared(&mattr, PTHREAD_PROCESS_SHARED))
|| (rc = pthread_mutex_init(&env->me_txns->mti_mutex, &mattr))
|| (rc = pthread_mutex_init(&env->me_txns->mti_wmutex, &mattr)))