summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJordan Henderson <jhenderson@hdfgroup.org>2020-02-05 20:15:16 (GMT)
committerJordan Henderson <jhenderson@hdfgroup.org>2020-02-05 22:07:33 (GMT)
commitbe0cd0b64aef3f64b15d5fc89cd15213ad939763 (patch)
tree40f8887cb3125a297f67327097a97793c7b3d4be /src
parent469c56d8a9378bd9ff74d6456aa9d8e598867da4 (diff)
parent00608a8756330fc387cde90c397562103c857671 (diff)
downloadhdf5-be0cd0b64aef3f64b15d5fc89cd15213ad939763.zip
hdf5-be0cd0b64aef3f64b15d5fc89cd15213ad939763.tar.gz
hdf5-be0cd0b64aef3f64b15d5fc89cd15213ad939763.tar.bz2
Merge branch 'develop' of ssh://git@bitbucket.hdfgroup.org:7999/~jhenderson/hdf5.git into develop
Diffstat (limited to 'src')
-rw-r--r--src/H5CS.c6
-rw-r--r--src/H5Eint.c9
-rw-r--r--src/H5TS.c119
-rw-r--r--src/H5TSprivate.h3
-rw-r--r--src/H5private.h5
-rw-r--r--src/H5win32defs.h4
6 files changed, 125 insertions, 21 deletions
diff --git a/src/H5CS.c b/src/H5CS.c
index 0a3dcce..3fccce4 100644
--- a/src/H5CS.c
+++ b/src/H5CS.c
@@ -145,11 +145,7 @@ H5CS_print_stack(const H5CS_t *fstack, FILE *stream)
HDfprintf(stream, "HDF5-DIAG: Function stack from %s ", H5_lib_vers_info_g);
/* try show the process or thread id in multiple processes cases*/
-#ifdef H5_HAVE_THREADSAFE
- HDfprintf(stream, "thread %lu.", HDpthread_self_ulong());
-#else /* H5_HAVE_THREADSAFE */
- HDfprintf(stream, "thread 0.");
-#endif /* H5_HAVE_THREADSAFE */
+ HDfprintf(stream, "thread %" PRIu64 ".", H5TS_thread_id());
if(fstack && fstack->nused>0)
HDfprintf(stream, " Back trace follows.");
HDfputc('\n', stream);
diff --git a/src/H5Eint.c b/src/H5Eint.c
index acd5b28..2aedc17 100644
--- a/src/H5Eint.c
+++ b/src/H5Eint.c
@@ -37,6 +37,7 @@
#include "H5Epkg.h" /* Error handling */
#include "H5Iprivate.h" /* IDs */
#include "H5MMprivate.h" /* Memory management */
+#include "H5TSprivate.h" /* Thread stuff */
/****************/
@@ -259,10 +260,8 @@ H5E__walk1_cb(int n, H5E_error1_t *err_desc, void *client_data)
else
HDfprintf(stream, "thread 0");
} /* end block */
-#elif defined(H5_HAVE_THREADSAFE)
- HDfprintf(stream, "thread %lu", (unsigned long)HDpthread_self_ulong());
#else
- HDfprintf(stream, "thread 0");
+ HDfprintf(stream, "thread %" PRIu64, H5TS_thread_id());
#endif
HDfprintf(stream, ":\n");
} /* end if */
@@ -391,10 +390,8 @@ H5E__walk2_cb(unsigned n, const H5E_error2_t *err_desc, void *client_data)
else
HDfprintf(stream, "thread 0");
} /* end block */
-#elif defined(H5_HAVE_THREADSAFE)
- HDfprintf(stream, "thread %lu", (unsigned long)HDpthread_self_ulong());
#else
- HDfprintf(stream, "thread 0");
+ HDfprintf(stream, "thread %" PRIu64, H5TS_thread_id());
#endif
HDfprintf(stream, ":\n");
} /* end if */
diff --git a/src/H5TS.c b/src/H5TS.c
index 10e14d5..7a801e2 100644
--- a/src/H5TS.c
+++ b/src/H5TS.c
@@ -37,6 +37,31 @@ H5TS_key_t H5TS_funcstk_key_g;
H5TS_key_t H5TS_apictx_key_g;
H5TS_key_t H5TS_cancel_key_g;
+#ifndef H5_HAVE_WIN_THREADS
+
+/* An h5_tid_t is a record of a thread identifier that is
+ * available for reuse.
+ */
+struct _tid;
+typedef struct _tid h5_tid_t;
+
+struct _tid {
+ h5_tid_t *next;
+ uint64_t id;
+};
+
+/* Pointer to first free thread ID record or NULL. */
+static h5_tid_t *tid_next_free = NULL;
+static uint64_t tid_next_id = 0;
+
+/* Mutual exclusion for access to tid_next_free and tid_next_id. */
+static pthread_mutex_t tid_mtx;
+
+/* Key for thread-local storage of the thread ID. */
+static H5TS_key_t tid_key;
+
+#endif /* H5_HAVE_WIN_THREADS */
+
/*--------------------------------------------------------------------------
* NAME
@@ -68,6 +93,89 @@ H5TS_key_destructor(void *key_val)
#ifndef H5_HAVE_WIN_THREADS
+/* When a thread shuts down, put its ID record on the free list. */
+static void
+tid_destructor(void *_v)
+{
+ h5_tid_t *tid = _v;
+
+ if (tid == NULL)
+ return;
+
+ /* XXX I can use mutexes in destructors, right? */
+ /* TBD use an atomic CAS */
+ pthread_mutex_lock(&tid_mtx);
+ tid->next = tid_next_free;
+ tid_next_free = tid;
+ pthread_mutex_unlock(&tid_mtx);
+}
+
+/* Initialize for integer thread identifiers. */
+static void
+tid_init(void)
+{
+ pthread_mutex_init(&tid_mtx, NULL);
+ pthread_key_create(&tid_key, tid_destructor);
+}
+
+/* Return an integer identifier, ID, for the current thread satisfying the
+ * following properties:
+ *
+ * 1 1 <= ID <= UINT64_MAX
+ * 2 ID is constant over the thread's lifetime.
+ * 3 No two threads share an ID during their lifetimes.
+ * 4 A thread's ID is available for reuse as soon as it is joined.
+ *
+ * ID 0 is reserved. H5TS_thread_id() returns 0 if the library was not built
+ * with thread safety or if an error prevents it from assigning an ID.
+ */
+uint64_t
+H5TS_thread_id(void)
+{
+ h5_tid_t *tid = pthread_getspecific(tid_key);
+ h5_tid_t proto_tid;
+
+ /* An ID is already assigned. */
+ if (tid != NULL)
+ return tid->id;
+
+ /* An ID is *not* already assigned: reuse an ID that's on the
+ * free list, or else generate a new ID.
+ *
+ * Allocating memory while holding a mutex is bad form, so
+ * point `tid` at `proto_tid` if we need to allocate some
+ * memory.
+ */
+ pthread_mutex_lock(&tid_mtx);
+ if ((tid = tid_next_free) != NULL)
+ tid_next_free = tid->next;
+ else if (tid_next_id != UINT64_MAX) {
+ tid = &proto_tid;
+ tid->id = ++tid_next_id;
+ }
+ pthread_mutex_unlock(&tid_mtx);
+
+ /* If a prototype ID record was established, copy it to the heap. */
+ if (tid == &proto_tid) {
+ if ((tid = HDmalloc(sizeof(*tid))) != NULL)
+ *tid = proto_tid;
+ }
+
+ if (tid == NULL)
+ return 0;
+
+ /* Finish initializing the ID record and set a thread-local pointer
+ * to it.
+ */
+ tid->next = NULL;
+ if (pthread_setspecific(tid_key, tid) != 0) {
+ tid_destructor(tid);
+ return 0;
+ }
+
+ return tid->id;
+}
+
/*--------------------------------------------------------------------------
* NAME
* H5TS_pthread_first_thread_init
@@ -104,6 +212,9 @@ H5TS_pthread_first_thread_init(void)
pthread_cond_init(&H5_g.init_lock.cond_var, NULL);
H5_g.init_lock.lock_count = 0;
+ /* Initialize integer thread identifiers. */
+ tid_init();
+
/* initialize key for thread-specific error stacks */
pthread_key_create(&H5TS_errstk_key_g, H5TS_key_destructor);
@@ -528,5 +639,13 @@ H5TS_create_thread(void *(*func)(void *), H5TS_attr_t *attr, void *udata)
} /* H5TS_create_thread */
+#else /* H5_HAVE_THREADSAFE */
+
+uint64_t
+H5TS_thread_id(void)
+{
+ return 0;
+}
+
#endif /* H5_HAVE_THREADSAFE */
diff --git a/src/H5TSprivate.h b/src/H5TSprivate.h
index 9e093a6..f22ed52 100644
--- a/src/H5TSprivate.h
+++ b/src/H5TSprivate.h
@@ -68,7 +68,7 @@ H5_DLL void H5TS_win32_process_exit(void);
H5_DLL herr_t H5TS_win32_thread_enter(void);
H5_DLL herr_t H5TS_win32_thread_exit(void);
-
+#define H5TS_thread_id() ((uint64_t)GetCurrentThreadId())
#else /* H5_HAVE_WIN_THREADS */
@@ -102,6 +102,7 @@ typedef pthread_once_t H5TS_once_t;
#define H5TS_mutex_init(mutex) pthread_mutex_init(mutex, NULL)
#define H5TS_mutex_lock_simple(mutex) pthread_mutex_lock(mutex)
#define H5TS_mutex_unlock_simple(mutex) pthread_mutex_unlock(mutex)
+H5_DLL uint64_t H5TS_thread_id(void);
#endif /* H5_HAVE_WIN_THREADS */
diff --git a/src/H5private.h b/src/H5private.h
index 41cef16..f0f3687 100644
--- a/src/H5private.h
+++ b/src/H5private.h
@@ -1549,11 +1549,6 @@ extern char *strdup(const char *s);
#define HDpthread_self() pthread_self()
#endif /* HDpthread_self */
-/* Use this version of pthread_self for printing the thread ID */
-#ifndef HDpthread_self_ulong
- #define HDpthread_self_ulong() ((unsigned long)pthread_self())
-#endif /* HDpthread_self_ulong */
-
/* Macro for "stringizing" an integer in the C preprocessor (use H5_TOSTRING) */
/* (use H5_TOSTRING, H5_STRINGIZE is just part of the implementation) */
#define H5_STRINGIZE(x) #x
diff --git a/src/H5win32defs.h b/src/H5win32defs.h
index 0a0bd37..00df56d 100644
--- a/src/H5win32defs.h
+++ b/src/H5win32defs.h
@@ -163,10 +163,6 @@ extern "C" {
/* Non-POSIX functions */
-/* Don't use actual pthread_self on Windows because the return
- * type cannot be cast as a ulong like other systems. */
-#define HDpthread_self_ulong() ((unsigned long)GetCurrentThreadId())
-
#ifndef H5_HAVE_MINGW
#define HDftruncate(F,L) _chsize_s(F,L)
#define HDfseek(F,O,W) _fseeki64(F,O,W)