diff options
author | David Young <dyoung@hdfgroup.org> | 2020-02-03 17:59:14 (GMT) |
---|---|---|
committer | David Young <dyoung@hdfgroup.org> | 2020-02-03 17:59:14 (GMT) |
commit | 986c7451a03aa8d54ce3b57ffd2e394665d360cc (patch) | |
tree | f026b4f18a17976c4f2072f5082bfa2654547b06 /src/H5TS.c | |
parent | 0147d2493eed235ba31077a6da38608ce2fe152a (diff) | |
download | hdf5-986c7451a03aa8d54ce3b57ffd2e394665d360cc.zip hdf5-986c7451a03aa8d54ce3b57ffd2e394665d360cc.tar.gz hdf5-986c7451a03aa8d54ce3b57ffd2e394665d360cc.tar.bz2 |
Replace pthread_self_ulong() with H5TS_thread_id(). The POSIX Threads
implementation ought to be portable to any system that has POSIX
Threads. On Windows, I use the same API call as before.
Diffstat (limited to 'src/H5TS.c')
-rw-r--r-- | src/H5TS.c | 119 |
1 files changed, 119 insertions, 0 deletions
@@ -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; + unsigned long id; +}; + +/* Pointer to first free thread ID record or NULL. */ +static h5_tid_t *tid_next_free = NULL; +static unsigned long 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 satisfies the + * following properties: + * + * 1 1 <= ID <= ULONG_MAX + * 2 The 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. + */ +unsigned long +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 != ULONG_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 */ + +unsigned long +H5TS_thread_id(void) +{ + return 0; +} + #endif /* H5_HAVE_THREADSAFE */ |