diff options
Diffstat (limited to 'src/thread.c')
| -rw-r--r-- | src/thread.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/src/thread.c b/src/thread.c index c3e536c..92edf5a 100644 --- a/src/thread.c +++ b/src/thread.c @@ -373,6 +373,74 @@ void bh_thread_pool_worker(void *arg) } } + +bh_spinlock_t *bh_spinlock_new(void) +{ + bh_spinlock_t *result; + + result = malloc(sizeof(*result)); + if (result) + bh_spinlock_init(result); + + return result; +} + +void bh_spinlock_free(bh_spinlock_t *lock) +{ + bh_spinlock_destroy(lock); + free(lock); +} + +int bh_tls_alloc(bh_generic_cb_t cleanup) +{ + bh_tls_info_t *info; + int result; + + /* Fetch TLS managment info */ + info = bh_tls_info(); + result = BH_ERROR; + + /* Get new counter value and setup cleanup function */ + bh_spinlock_lock(&info->lock); + if (info->counter < BH_MAX_TLS) + { + result = info->counter++; + info->cleanup[result] = cleanup; + } + bh_spinlock_unlock(&info->lock); + + /* Return slot id */ + return result; +} + +void *bh_tls_get(int slot) +{ + return bh_tls_fetch()->data[slot]; +} + +void bh_tls_set(int slot, void *data) +{ + bh_tls_fetch()->data[slot] = data; +} + +void bh_tls_cleanup(void) +{ + bh_tls_info_t *info; + bh_tls_t *tls; + size_t i; + + /* Fetch TLS managment info and TLS itself */ + info = bh_tls_info(); + tls = bh_tls_fetch(); + + /* Call cleanup for every slot */ + for (i = 0; i < info->counter; i++) + if (info->cleanup[i]) + info->cleanup[i](tls->data[i]); + + /* Free tls */ + free(tls); +} /** * \} */
\ No newline at end of file |
