Implement TLS and spinlocks, fix bug in Windows threads
This commit is contained in:
68
src/thread.c
68
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);
|
||||
}
|
||||
/**
|
||||
* \}
|
||||
*/
|
||||
Reference in New Issue
Block a user