| 1 |
/* |
| 2 |
* Copyright(C) 1999-2020 National Technology & Engineering Solutions |
| 3 |
* of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 4 |
* NTESS, the U.S. Government retains certain rights in this software. |
| 5 |
* |
| 6 |
* See packages/seacas/LICENSE for details |
| 7 |
*/ |
| 8 |
|
| 9 |
#define _GNU_SOURCE |
| 10 |
#include "exodusII.h" |
| 11 |
#if defined(EXODUS_THREADSAFE) |
| 12 |
#include <pthread.h> |
| 13 |
|
| 14 |
#include "exodusII_int.h" |
| 15 |
|
| 16 |
/* NOTE: All code in this file was originally based on the thread-safe |
| 17 |
* code from the hdf5 library. |
| 18 |
*/ |
| 19 |
|
| 20 |
/* Global variable definitions */ |
| 21 |
pthread_once_t EX_first_init_g = PTHREAD_ONCE_INIT; |
| 22 |
pthread_key_t EX_errval_key_g; |
| 23 |
|
| 24 |
EX_mutex_t EX_g; |
| 25 |
|
| 26 |
static void ex_key_destructor(void *key_val) |
| 27 |
{ |
| 28 |
if (key_val != NULL) |
| 29 |
free(key_val); |
| 30 |
} |
| 31 |
|
| 32 |
#define ex_err_abort(status, message) \ |
| 33 |
do { \ |
| 34 |
fprintf(stderr, "%s in file %s at line %d: %s\n", message, __FILE__, __LINE__, \ |
| 35 |
strerror(status)); \ |
| 36 |
abort(); \ |
| 37 |
} while (0) |
| 38 |
|
| 39 |
void exi_pthread_first_thread_init(void) |
| 40 |
{ |
| 41 |
int err = pthread_mutexattr_init(&EX_g.attribute); |
| 42 |
if (err != 0) { |
| 43 |
ex_err_abort(err, "Mutex Attr Init"); |
| 44 |
} |
| 45 |
|
| 46 |
err = pthread_mutexattr_settype(&EX_g.attribute, PTHREAD_MUTEX_RECURSIVE); |
| 47 |
if (err != 0) { |
| 48 |
ex_err_abort(err, "Mutex Attr Set Type"); |
| 49 |
} |
| 50 |
|
| 51 |
err = pthread_mutex_init(&EX_g.atomic_lock, &EX_g.attribute); |
| 52 |
if (err != 0) { |
| 53 |
ex_err_abort(err, "Mutex Init"); |
| 54 |
} |
| 55 |
|
| 56 |
/* initialize key for thread-specific error stacks */ |
| 57 |
err = pthread_key_create(&EX_errval_key_g, ex_key_destructor); |
| 58 |
if (err != 0) { |
| 59 |
ex_err_abort(err, "Create errval key"); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
int exi_mutex_lock(EX_mutex_t *mutex) |
| 64 |
{ |
| 65 |
int ret_value = pthread_mutex_lock(&mutex->atomic_lock); |
| 66 |
if (ret_value != 0) { |
| 67 |
ex_err_abort(ret_value, "Lock mutex"); |
| 68 |
} |
| 69 |
return ret_value; |
| 70 |
} |
| 71 |
|
| 72 |
int exi_mutex_unlock(EX_mutex_t *mutex, const char *func, int line) |
| 73 |
{ |
| 74 |
int ret_value = pthread_mutex_unlock(&mutex->atomic_lock); |
| 75 |
if (ret_value != 0) { |
| 76 |
char errmsg[MAX_ERR_LENGTH]; |
| 77 |
snprintf(errmsg, MAX_ERR_LENGTH, "ERROR: Invalid unlock mutex call from line %d of '%s'", line, |
| 78 |
func); |
| 79 |
ex_err_abort(ret_value, errmsg); |
| 80 |
} |
| 81 |
return ret_value; |
| 82 |
} |
| 83 |
|
| 84 |
EX_errval_t *exerrval_get(void) |
| 85 |
{ |
| 86 |
EX_errval_t *ex_errval_local = (EX_errval_t *)pthread_getspecific(EX_errval_key_g); |
| 87 |
if (!ex_errval_local) { |
| 88 |
/* |
| 89 |
* First time thread calls library - create new value and associate |
| 90 |
* with key |
| 91 |
*/ |
| 92 |
ex_errval_local = (EX_errval_t *)calloc(1, sizeof(EX_errval_t)); |
| 93 |
pthread_setspecific(EX_errval_key_g, (void *)ex_errval_local); |
| 94 |
} |
| 95 |
|
| 96 |
return ex_errval_local; |
| 97 |
} |
| 98 |
#else |
| 99 |
void ex_dummy() {} |
| 100 |
#endif |