diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs-tests/test_libhdfs_threaded.c b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs-tests/test_libhdfs_threaded.c index f80200df0f..8d4b74302d 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs-tests/test_libhdfs_threaded.c +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs-tests/test_libhdfs_threaded.c @@ -17,8 +17,11 @@ */ #include "expect.h" +#include "exception.h" #include "hdfs/hdfs.h" +#include "jni_helper.h" #include "native_mini_dfs.h" +#include "os/mutexes.h" #include "os/thread.h" #include @@ -329,6 +332,23 @@ static int checkFailures(struct tlhThreadInfo *ti, int tlhNumThreads) return EXIT_FAILURE; } +int testRecursiveJvmMutex() { + jthrowable jthr; + JNIEnv *env = getJNIEnv(); + if (!env) { + fprintf(stderr, "testRecursiveJvmMutex: getJNIEnv failed\n"); + return -EIO; + } + jthr = newRuntimeError(env, "Dummy error to print for testing"); + + /* printExceptionAndFree() takes the jvmMutex within */ + mutexLock(&jvmMutex); + printExceptionAndFree(env, jthr, PRINT_EXC_ALL, "testRecursiveJvmMutex"); + mutexUnlock(&jvmMutex); + + return 0; +} + /** * Test that we can write a file with libhdfs and then read it back */ @@ -341,6 +361,12 @@ int main(void) 1, /* doFormat */ }; + /* Check that the recursive mutex works as expected */ + if (testRecursiveJvmMutex() < 0) { + fprintf(stderr, "testRecursiveJvmMutex failed\n"); + return EXIT_FAILURE; + } + tlhNumThreadsStr = getenv("TLH_NUM_THREADS"); if (!tlhNumThreadsStr) { tlhNumThreadsStr = "3"; diff --git a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/mutexes.c b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/mutexes.c index c4c2f26213..20dafaa020 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/mutexes.c +++ b/hadoop-hdfs-project/hadoop-hdfs-native-client/src/main/native/libhdfs/os/posix/mutexes.c @@ -22,7 +22,14 @@ #include mutex hdfsHashMutex = PTHREAD_MUTEX_INITIALIZER; -mutex jvmMutex = PTHREAD_MUTEX_INITIALIZER; +mutex jvmMutex; +pthread_mutexattr_t jvmMutexAttr; + +__attribute__((constructor)) static void init() { + pthread_mutexattr_init(&jvmMutexAttr); + pthread_mutexattr_settype(&jvmMutexAttr, PTHREAD_MUTEX_RECURSIVE); + pthread_mutex_init(&jvmMutex, &jvmMutexAttr); +} int mutexLock(mutex *m) { int ret = pthread_mutex_lock(m);