123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427 |
- #include "precompile.h"
- #include "toolkit.h"
- #include "internal.h"
- TOOLKIT_API int toolkit_key_create(toolkit_key_t* key)
- {
- key->tls_index = TlsAlloc();
- if (key->tls_index == TLS_OUT_OF_INDEXES)
- return TOOLKIT_ENOMEM;
- return 0;
- }
- TOOLKIT_API void toolkit_key_delete(toolkit_key_t* key)
- {
- if (TlsFree(key->tls_index) == FALSE)
- abort();
- key->tls_index = TLS_OUT_OF_INDEXES;
- }
- TOOLKIT_API void* toolkit_key_get(toolkit_key_t* key)
- {
- void* value;
- value = TlsGetValue(key->tls_index);
- if (value == NULL)
- if (GetLastError() != ERROR_SUCCESS)
- abort();
- return value;
- }
- TOOLKIT_API void toolkit_key_set(toolkit_key_t* key, void* value)
- {
- if (TlsSetValue(key->tls_index, value) == FALSE)
- abort();
- }
- static toolkit_key_t toolkit__current_thread_key;
- static toolkit_once_t toolkit__current_thread_init_guard = TOOLKIT_ONCE_INIT;
- static void toolkit__init_current_thread_key(void)
- {
- if (toolkit_key_create(&toolkit__current_thread_key))
- abort();
- }
- struct thread_ctx {
- void (*entry)(void* arg);
- void* arg;
- toolkit_thread_t self;
- };
- static UINT __stdcall toolkit__thread_start(void* arg)
- {
- struct thread_ctx* ctx_p;
- struct thread_ctx ctx;
- ctx_p = arg;
- ctx = *ctx_p;
- FREE(ctx_p);
- toolkit_once(&toolkit__current_thread_init_guard, toolkit__init_current_thread_key);
- toolkit_key_set(&toolkit__current_thread_key, (void*)ctx.self);
- ctx.entry(ctx.arg);
- return 0;
- }
- TOOLKIT_API int toolkit_thread_create_ex(toolkit_thread_t* tid, const toolkit_thread_option_t* params,
- toolkit_thread_cb entry, void* arg)
- {
- struct thread_ctx* ctx;
- int err;
- HANDLE thread;
- SYSTEM_INFO sysinfo;
- size_t stack_size;
- size_t pagesize;
- stack_size = params->flags & 1 ? params->stack_size : 0;
- if (stack_size != 0) {
- GetNativeSystemInfo(&sysinfo);
- pagesize = (size_t)sysinfo.dwPageSize;
- /* Round up to the nearest page boundary. */
- stack_size = (stack_size + pagesize - 1) & ~(pagesize - 1);
- if ((unsigned)stack_size != stack_size)
- return TOOLKIT_EINVAL;
- }
- ctx = MALLOC_T(struct thread_ctx);
- if (ctx == NULL)
- return TOOLKIT_ENOMEM;
- ctx->entry = entry;
- ctx->arg = arg;
- /* Create the thread in suspended state so we have a chance to pass
- * its own creation handle to it */
- thread = (HANDLE)_beginthreadex(NULL,
- (unsigned)stack_size,
- toolkit__thread_start,
- ctx,
- CREATE_SUSPENDED,
- NULL);
- if (thread == NULL) {
- err = errno;
- FREE(ctx);
- }
- else {
- err = 0;
- *tid = thread;
- ctx->self = thread;
- ResumeThread(thread);
- }
- switch (err) {
- case 0:
- return 0;
- case EACCES:
- return TOOLKIT_EACCES;
- case EAGAIN:
- return TOOLKIT_EAGAIN;
- case EINVAL:
- return TOOLKIT_EINVAL;
- }
- return TOOLKIT_EIO;
- }
- TOOLKIT_API int toolkit_thread_create(toolkit_thread_t* tid, toolkit_thread_cb entry, void* arg)
- {
- toolkit_thread_option_t params;
- params.flags = 0;
- return toolkit_thread_create_ex(tid, ¶ms, entry, arg);
- }
- TOOLKIT_API int toolkit_thread_join(toolkit_thread_t* tid)
- {
- if (WaitForSingleObject(*tid, INFINITE))
- return -1;
- else {
- CloseHandle(*tid);
- *tid = 0;
- MemoryBarrier(); /* For feature parity with pthread_join(). */
- return 0;
- }
- }
- TOOLKIT_API int toolkit_thread_equal(const toolkit_thread_t* t1, const toolkit_thread_t* t2)
- {
- return *t1 == *t2;
- }
- TOOLKIT_API toolkit_thread_t toolkit_thread_self(void)
- {
- toolkit_once(&toolkit__current_thread_init_guard, toolkit__init_current_thread_key);
- return (toolkit_thread_t)toolkit_key_get(&toolkit__current_thread_key);
- }
- int toolkit_rwlock_init(toolkit_rwlock_t* rwlock) {
- /* Initialize the semaphore that acts as the write lock. */
- HANDLE handle = CreateSemaphoreW(NULL, 1, 1, NULL);
- if (handle == NULL)
- return toolkit_translate_sys_error(GetLastError());
- rwlock->state_.write_semaphore_ = handle;
- /* Initialize the critical section protecting the reader count. */
- InitializeCriticalSection(&rwlock->state_.num_readers_lock_);
- /* Initialize the reader count. */
- rwlock->state_.num_readers_ = 0;
- return 0;
- }
- void toolkit_rwlock_destroy(toolkit_rwlock_t* rwlock) {
- DeleteCriticalSection(&rwlock->state_.num_readers_lock_);
- CloseHandle(rwlock->state_.write_semaphore_);
- }
- void toolkit_rwlock_rdlock(toolkit_rwlock_t* rwlock) {
- /* Acquire the lock that protects the reader count. */
- EnterCriticalSection(&rwlock->state_.num_readers_lock_);
- /* Increase the reader count, and lock for write if this is the first
- * reader.
- */
- if (++rwlock->state_.num_readers_ == 1) {
- DWORD r = WaitForSingleObject(rwlock->state_.write_semaphore_, INFINITE);
- if (r != WAIT_OBJECT_0)
- toolkit_fatal_error(GetLastError(), "WaitForSingleObject");
- }
- /* Release the lock that protects the reader count. */
- LeaveCriticalSection(&rwlock->state_.num_readers_lock_);
- }
- int toolkit_rwlock_tryrdlock(toolkit_rwlock_t* rwlock) {
- int err;
- if (!TryEnterCriticalSection(&rwlock->state_.num_readers_lock_))
- return TOOLKIT_EBUSY;
- err = 0;
- if (rwlock->state_.num_readers_ == 0) {
- /* Currently there are no other readers, which means that the write lock
- * needs to be acquired.
- */
- DWORD r = WaitForSingleObject(rwlock->state_.write_semaphore_, 0);
- if (r == WAIT_OBJECT_0)
- rwlock->state_.num_readers_++;
- else if (r == WAIT_TIMEOUT)
- err = TOOLKIT_EBUSY;
- else if (r == WAIT_FAILED)
- toolkit_fatal_error(GetLastError(), "WaitForSingleObject");
- }
- else {
- /* The write lock has already been acquired because there are other
- * active readers.
- */
- rwlock->state_.num_readers_++;
- }
- LeaveCriticalSection(&rwlock->state_.num_readers_lock_);
- return err;
- }
- void toolkit_rwlock_rdunlock(toolkit_rwlock_t* rwlock) {
- EnterCriticalSection(&rwlock->state_.num_readers_lock_);
- if (--rwlock->state_.num_readers_ == 0) {
- if (!ReleaseSemaphore(rwlock->state_.write_semaphore_, 1, NULL))
- toolkit_fatal_error(GetLastError(), "ReleaseSemaphore");
- }
- LeaveCriticalSection(&rwlock->state_.num_readers_lock_);
- }
- void toolkit_rwlock_wrlock(toolkit_rwlock_t* rwlock) {
- DWORD r = WaitForSingleObject(rwlock->state_.write_semaphore_, INFINITE);
- if (r != WAIT_OBJECT_0)
- toolkit_fatal_error(GetLastError(), "WaitForSingleObject");
- }
- int toolkit_rwlock_trywrlock(toolkit_rwlock_t* rwlock)
- {
- DWORD r = WaitForSingleObject(rwlock->state_.write_semaphore_, 0);
- if (r == WAIT_OBJECT_0)
- return 0;
- else if (r == WAIT_TIMEOUT)
- return TOOLKIT_EBUSY;
- else {
- toolkit_fatal_error(GetLastError(), "WaitForSingleObject");
- return TOOLKIT_UNKNOWN;
- }
- }
- void toolkit_rwlock_wrunlock(toolkit_rwlock_t* rwlock)
- {
- if (!ReleaseSemaphore(rwlock->state_.write_semaphore_, 1, NULL))
- toolkit_fatal_error(GetLastError(), "ReleaseSemaphore");
- }
- int toolkit_sem_init(toolkit_sem_t* sem, unsigned int value) {
- *sem = CreateSemaphore(NULL, value, INT_MAX, NULL);
- if (*sem == NULL)
- return toolkit_translate_sys_error(GetLastError());
- else
- return 0;
- }
- void toolkit_sem_destroy(toolkit_sem_t* sem)
- {
- if (!CloseHandle(*sem))
- abort();
- }
- void toolkit_sem_post(toolkit_sem_t* sem)
- {
- if (!ReleaseSemaphore(*sem, 1, NULL))
- abort();
- }
- void toolkit_sem_wait(toolkit_sem_t* sem)
- {
- if (WaitForSingleObject(*sem, INFINITE) != WAIT_OBJECT_0)
- abort();
- }
- int toolkit_sem_trywait(toolkit_sem_t* sem)
- {
- DWORD r = WaitForSingleObject(*sem, 0);
- if (r == WAIT_OBJECT_0)
- return 0;
- if (r == WAIT_TIMEOUT)
- return TOOLKIT_EAGAIN;
- abort();
- return -1; /* Satisfy the compiler. */
- }
- int toolkit_cond_init(toolkit_cond_t* cond)
- {
- InitializeConditionVariable(&cond->cond_var);
- return 0;
- }
- void toolkit_cond_destroy(toolkit_cond_t* cond)
- {
- /* nothing to do */
- (void)&cond;
- }
- void toolkit_cond_signal(toolkit_cond_t* cond)
- {
- WakeConditionVariable(&cond->cond_var);
- }
- void toolkit_cond_broadcast(toolkit_cond_t* cond)
- {
- WakeAllConditionVariable(&cond->cond_var);
- }
- void toolkit_cond_wait(toolkit_cond_t* cond, toolkit_mutex_t* mutex)
- {
- if (!SleepConditionVariableCS(&cond->cond_var, mutex, INFINITE))
- abort();
- }
- int toolkit_cond_timedwait(toolkit_cond_t* cond, toolkit_mutex_t* mutex, uint64_t timeout)
- {
- if (SleepConditionVariableCS(&cond->cond_var, mutex, (DWORD)(timeout / 1e6)))
- return 0;
- if (GetLastError() != ERROR_TIMEOUT)
- abort();
- return TOOLKIT_ETIMEDOUT;
- }
- int toolkit_barrier_init(toolkit_barrier_t* barrier, unsigned int count)
- {
- int err;
- barrier->n = count;
- barrier->count = 0;
- err = toolkit_mutex_init(&barrier->mutex);
- if (err)
- return err;
- err = toolkit_sem_init(&barrier->turnstile1, 0);
- if (err)
- goto error2;
- err = toolkit_sem_init(&barrier->turnstile2, 1);
- if (err)
- goto error;
- return 0;
- error:
- toolkit_sem_destroy(&barrier->turnstile1);
- error2:
- toolkit_mutex_destroy(&barrier->mutex);
- return err;
- }
- void toolkit_barrier_destroy(toolkit_barrier_t* barrier)
- {
- toolkit_sem_destroy(&barrier->turnstile2);
- toolkit_sem_destroy(&barrier->turnstile1);
- toolkit_mutex_destroy(&barrier->mutex);
- }
- int toolkit_barrier_wait(toolkit_barrier_t* barrier)
- {
- int serial_thread;
- toolkit_mutex_lock(&barrier->mutex);
- if (++barrier->count == barrier->n) {
- toolkit_sem_wait(&barrier->turnstile2);
- toolkit_sem_post(&barrier->turnstile1);
- }
- toolkit_mutex_unlock(&barrier->mutex);
- toolkit_sem_wait(&barrier->turnstile1);
- toolkit_sem_post(&barrier->turnstile1);
- toolkit_mutex_lock(&barrier->mutex);
- serial_thread = (--barrier->count == 0);
- if (serial_thread) {
- toolkit_sem_wait(&barrier->turnstile1);
- toolkit_sem_post(&barrier->turnstile2);
- }
- toolkit_mutex_unlock(&barrier->mutex);
- toolkit_sem_wait(&barrier->turnstile2);
- toolkit_sem_post(&barrier->turnstile2);
- return serial_thread;
- }
|