diff options
author | Quincey Koziol <koziol@koziol.gov> | 2020-06-26 23:57:38 (GMT) |
---|---|---|
committer | Quincey Koziol <koziol@koziol.gov> | 2020-06-26 23:57:38 (GMT) |
commit | e767f44e953047297fece364218147c908e8b585 (patch) | |
tree | d4b6909f14b78bb732b6947adc46ac3cd3ca54f6 /src/H5TS.c | |
parent | 949649a2b6e00297cbdc49437e70a27e455e92d2 (diff) | |
parent | a08ab621febde7b09e4d86eab80cb029c123e9f6 (diff) | |
download | hdf5-e767f44e953047297fece364218147c908e8b585.zip hdf5-e767f44e953047297fece364218147c908e8b585.tar.gz hdf5-e767f44e953047297fece364218147c908e8b585.tar.bz2 |
Merge remote-tracking branch 'origin/develop' into monotonic_timer
Diffstat (limited to 'src/H5TS.c')
-rw-r--r-- | src/H5TS.c | 197 |
1 files changed, 178 insertions, 19 deletions
@@ -34,8 +34,34 @@ H5TS_once_t H5TS_first_init_g = PTHREAD_ONCE_INIT; #endif /* H5_HAVE_WIN_THREADS */ H5TS_key_t H5TS_errstk_key_g; 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 H5TS_tid_t is a record of a thread identifier that is + * available for reuse. + */ +struct _tid; +typedef struct _tid H5TS_tid_t; + +struct _tid { + H5TS_tid_t *next; + uint64_t id; +}; + +/* Pointer to first free thread ID record or NULL. */ +static H5TS_tid_t *H5TS_tid_next_free = NULL; +static uint64_t H5TS_tid_next_id = 0; + +/* Mutual exclusion for access to H5TS_tid_next_free and H5TS_tid_next_id. */ +static pthread_mutex_t H5TS_tid_mtx; + +/* Key for thread-local storage of the thread ID. */ +static H5TS_key_t H5TS_tid_key; + +#endif /* H5_HAVE_WIN_THREADS */ + /*-------------------------------------------------------------------------- * NAME @@ -69,6 +95,127 @@ H5TS_key_destructor(void *key_val) /*-------------------------------------------------------------------------- * NAME + * H5TS_tid_destructor + * + * USAGE + * H5TS_tid_destructor() + * + * RETURNS + * + * DESCRIPTION + * When a thread shuts down, put its ID record on the free list. + * + *-------------------------------------------------------------------------- + */ +static void +H5TS_tid_destructor(void *_v) +{ + H5TS_tid_t *tid = _v; + + if (tid == NULL) + return; + + /* TBD use an atomic CAS */ + pthread_mutex_lock(&H5TS_tid_mtx); + tid->next = H5TS_tid_next_free; + H5TS_tid_next_free = tid; + pthread_mutex_unlock(&H5TS_tid_mtx); +} + +/*-------------------------------------------------------------------------- + * NAME + * H5TS_tid_init + * + * USAGE + * H5TS_tid_init() + * + * RETURNS + * + * DESCRIPTION + * Initialize for integer thread identifiers. + * + *-------------------------------------------------------------------------- + */ +static void +H5TS_tid_init(void) +{ + pthread_mutex_init(&H5TS_tid_mtx, NULL); + pthread_key_create(&H5TS_tid_key, H5TS_tid_destructor); +} + +/*-------------------------------------------------------------------------- + * NAME + * H5TS_thread_id + * + * USAGE + * uint64_t id = H5TS_thread_id() + * + * RETURNS + * Return an integer identifier, ID, for the current thread. + * + * DESCRIPTION + * The ID satisfies 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) +{ + H5TS_tid_t *tid = pthread_getspecific(H5TS_tid_key); + H5TS_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(&H5TS_tid_mtx); + if ((tid = H5TS_tid_next_free) != NULL) + H5TS_tid_next_free = tid->next; + else if (H5TS_tid_next_id != UINT64_MAX) { + tid = &proto_tid; + tid->id = ++H5TS_tid_next_id; + } + pthread_mutex_unlock(&H5TS_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(H5TS_tid_key, tid) != 0) { + H5TS_tid_destructor(tid); + return 0; + } + + return tid->id; +} + +/*-------------------------------------------------------------------------- + * NAME * H5TS_pthread_first_thread_init * * USAGE @@ -103,12 +250,18 @@ 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. */ + H5TS_tid_init(); + /* initialize key for thread-specific error stacks */ pthread_key_create(&H5TS_errstk_key_g, H5TS_key_destructor); /* initialize key for thread-specific function stacks */ pthread_key_create(&H5TS_funcstk_key_g, H5TS_key_destructor); + /* initialize key for thread-specific API contexts */ + pthread_key_create(&H5TS_apictx_key_g, H5TS_key_destructor); + /* initialize key for thread cancellability mechanism */ pthread_key_create(&H5TS_cancel_key_g, H5TS_key_destructor); } @@ -139,7 +292,7 @@ herr_t H5TS_mutex_lock(H5TS_mutex_t *mutex) { #ifdef H5_HAVE_WIN_THREADS - EnterCriticalSection( &mutex->CriticalSection); + EnterCriticalSection( &mutex->CriticalSection); return 0; #else /* H5_HAVE_WIN_THREADS */ herr_t ret_value = pthread_mutex_lock(&mutex->atomic_lock); @@ -147,7 +300,7 @@ H5TS_mutex_lock(H5TS_mutex_t *mutex) if (ret_value) return ret_value; - if(mutex->lock_count && pthread_equal(HDpthread_self(), mutex->owner_thread)) { + if(mutex->lock_count && pthread_equal(pthread_self(), mutex->owner_thread)) { /* already owned by self - increment count */ mutex->lock_count++; } else { @@ -156,11 +309,11 @@ H5TS_mutex_lock(H5TS_mutex_t *mutex) pthread_cond_wait(&mutex->cond_var, &mutex->atomic_lock); /* After we've received the signal, take ownership of the mutex */ - mutex->owner_thread = HDpthread_self(); + mutex->owner_thread = pthread_self(); mutex->lock_count = 1; } - return pthread_mutex_unlock(&mutex->atomic_lock); + return pthread_mutex_unlock(&mutex->atomic_lock); #endif /* H5_HAVE_WIN_THREADS */ } @@ -191,7 +344,7 @@ H5TS_mutex_unlock(H5TS_mutex_t *mutex) #ifdef H5_HAVE_WIN_THREADS /* Releases ownership of the specified critical section object. */ LeaveCriticalSection(&mutex->CriticalSection); - return 0; + return 0; #else /* H5_HAVE_WIN_THREADS */ herr_t ret_value = pthread_mutex_lock(&mutex->atomic_lock); @@ -210,7 +363,7 @@ H5TS_mutex_unlock(H5TS_mutex_t *mutex) ret_value = err; } /* end if */ - return ret_value; + return ret_value; #endif /* H5_HAVE_WIN_THREADS */ } /* H5TS_mutex_unlock */ @@ -247,23 +400,22 @@ H5TS_cancel_count_inc(void) return SUCCEED; #else /* H5_HAVE_WIN_THREADS */ H5TS_cancel_t *cancel_counter; - herr_t ret_value = SUCCEED; + herr_t ret_value = SUCCEED; - cancel_counter = (H5TS_cancel_t *)H5TS_get_thread_local_value(H5TS_cancel_key_g); + cancel_counter = (H5TS_cancel_t *)H5TS_get_thread_local_value(H5TS_cancel_key_g); if (!cancel_counter) { /* * First time thread calls library - create new counter and associate * with key. - * + * * Don't use H5MM calls here since the destructor has to use HDfree in * order to avoid codestack calls. */ cancel_counter = (H5TS_cancel_t *)HDcalloc(1, sizeof(H5TS_cancel_t)); if (!cancel_counter) { - H5E_push_stack(NULL, "H5TS_cancel_count_inc", __FILE__, __LINE__, - H5E_ERR_CLS_g, H5E_RESOURCE, H5E_NOSPACE, "memory allocation failed"); + HERROR(H5E_RESOURCE, H5E_NOSPACE, "memory allocation failed"); return FAIL; } @@ -277,7 +429,7 @@ H5TS_cancel_count_inc(void) ++cancel_counter->cancel_count; - return ret_value; + return ret_value; #endif /* H5_HAVE_WIN_THREADS */ } @@ -307,19 +459,19 @@ H5TS_cancel_count_inc(void) herr_t H5TS_cancel_count_dec(void) { -#ifdef H5_HAVE_WIN_THREADS +#ifdef H5_HAVE_WIN_THREADS /* unsupported; will just return 0 */ return SUCCEED; #else /* H5_HAVE_WIN_THREADS */ - register H5TS_cancel_t *cancel_counter; + register H5TS_cancel_t *cancel_counter; herr_t ret_value = SUCCEED; - cancel_counter = (H5TS_cancel_t *)H5TS_get_thread_local_value(H5TS_cancel_key_g); + cancel_counter = (H5TS_cancel_t *)H5TS_get_thread_local_value(H5TS_cancel_key_g); if (cancel_counter->cancel_count == 1) ret_value = pthread_setcancelstate(cancel_counter->previous_state, NULL); - --cancel_counter->cancel_count; + --cancel_counter->cancel_count; return ret_value; #endif /* H5_HAVE_WIN_THREADS */ @@ -339,7 +491,7 @@ H5TS_cancel_count_dec(void) * *-------------------------------------------------------------------------- */ -H5_DLL BOOL CALLBACK +H5_DLL BOOL CALLBACK H5TS_win32_process_enter(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex) { BOOL ret_value = TRUE; @@ -356,6 +508,9 @@ H5TS_win32_process_enter(PINIT_ONCE InitOnce, PVOID Parameter, PVOID *lpContex) ret_value = FALSE; #endif /* H5_HAVE_CODESTACK */ + if(TLS_OUT_OF_INDEXES == (H5TS_apictx_key_g = TlsAlloc())) + ret_value = FALSE; + return ret_value; } /* H5TS_win32_process_enter() */ #endif /* H5_HAVE_WIN_THREADS */ @@ -421,10 +576,10 @@ H5TS_win32_process_exit(void) /* Clean up per-process thread local storage */ TlsFree(H5TS_errstk_key_g); - #ifdef H5_HAVE_CODESTACK TlsFree(H5TS_funcstk_key_g); #endif /* H5_HAVE_CODESTACK */ + TlsFree(H5TS_apictx_key_g); return; } /* H5TS_win32_process_exit() */ @@ -469,6 +624,10 @@ H5TS_win32_thread_exit(void) LocalFree((HLOCAL)lpvData); #endif /* H5_HAVE_CODESTACK */ + lpvData = TlsGetValue(H5TS_apictx_key_g); + if(lpvData) + LocalFree((HLOCAL)lpvData); + return ret_value; } /* H5TS_win32_thread_exit() */ #endif /* H5_HAVE_WIN_THREADS */ @@ -494,7 +653,7 @@ H5TS_create_thread(void *(*func)(void *), H5TS_attr_t *attr, void *udata) { H5TS_thread_t ret_value; -#ifdef H5_HAVE_WIN_THREADS +#ifdef H5_HAVE_WIN_THREADS /* When calling C runtime functions, you should use _beginthread or * _beginthreadex instead of CreateThread. Threads created with |