Skip to content
Myr

jv_thread.h

This file

Path

src/jv_thread.h

76 lines · 1.7 KiB · C

Last changed by

Not known.

History of this file

Take it

1#ifndef JV_THREAD_H2#define JV_THREAD_H34#ifdef WIN325#ifndef __MINGW32__6#include <windows.h>7#include <winnt.h>8#include <errno.h>910/* Copied from Heimdal: pthread-like mutexes for WIN32 -- see lib/base/heimbase.h in Heimdal */11typedef struct pthread_mutex {12    HANDLE      h;13} pthread_mutex_t;1415#define PTHREAD_MUTEX_INITIALIZER { INVALID_HANDLE_VALUE }1617static inline int18pthread_mutex_init(pthread_mutex_t *m)19{20    m->h = CreateSemaphore(NULL, 1, 1, NULL);21    if (m->h == INVALID_HANDLE_VALUE)22        return EAGAIN;23    return 0;24}2526static inline int27pthread_mutex_lock(pthread_mutex_t *m)28{29    HANDLE h, new_h;30    int created = 0;3132    h = InterlockedCompareExchangePointer(&m->h, m->h, m->h);33    if (h == INVALID_HANDLE_VALUE || h == NULL) {34        created = 1;35        new_h = CreateSemaphore(NULL, 0, 1, NULL);36        if (new_h == INVALID_HANDLE_VALUE)37            return EAGAIN;38        if (InterlockedCompareExchangePointer(&m->h, new_h, h) != h) {39            created = 0;40            CloseHandle(new_h);41        }42    }43    if (!created)44        WaitForSingleObject(m->h, INFINITE);45    return 0;46}4748static inline int49pthread_mutex_unlock(pthread_mutex_t *m)50{51    if (ReleaseSemaphore(m->h, 1, NULL) == FALSE)52        return EPERM;53    return 0;54}55static inline int56pthread_mutex_destroy(pthread_mutex_t *m)57{58    HANDLE h;5960    h = InterlockedCompareExchangePointer(&m->h, INVALID_HANDLE_VALUE, m->h);61    if (h != INVALID_HANDLE_VALUE)62        CloseHandle(h);63    return 0;64}6566typedef unsigned long pthread_key_t;67int pthread_key_create(pthread_key_t *, void (*)(void *));68int pthread_setspecific(pthread_key_t, void *);69void *pthread_getspecific(pthread_key_t);70#else71#include <pthread.h>72#endif73#else74#include <pthread.h>75#endif76#endif /* JV_THREAD_H */