⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ethread.h

📁 OTP是开放电信平台的简称
💻 H
📖 第 1 页 / 共 2 页
字号:
#ifndef ETHR_HAVE_OPTIMIZED_ATOMIC_OPSstatic ETHR_INLINE intethr_atomic_init(ethr_atomic_t *var, long i){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *var = (ethr_atomic_t) i);}static ETHR_INLINE intethr_atomic_set(ethr_atomic_t *var, long i){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *var = (ethr_atomic_t) i);}static ETHR_INLINE intethr_atomic_read(ethr_atomic_t *var, long *i){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *i = (long) *var);}static ETHR_INLINE intethr_atomic_inctest(ethr_atomic_t *incp, long *testp){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(incp, *testp = (long) ++(*incp));}static ETHR_INLINE intethr_atomic_dectest(ethr_atomic_t *decp, long *testp){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(decp, *testp = (long) --(*decp));}static ETHR_INLINE intethr_atomic_add(ethr_atomic_t *var, long incr){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *var += incr);}       static ETHR_INLINE intethr_atomic_addtest(ethr_atomic_t *incp, long i, long *testp){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(incp, *incp += i; *testp = *incp);}static ETHR_INLINE intethr_atomic_inc(ethr_atomic_t *incp){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(incp, ++(*incp));}static ETHR_INLINE intethr_atomic_dec(ethr_atomic_t *decp){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(decp, --(*decp));}static ETHR_INLINE intethr_atomic_and_old(ethr_atomic_t *var, long mask, long *old){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *old = *var; *var &= mask);}static ETHR_INLINE intethr_atomic_or_old(ethr_atomic_t *var, long mask, long *old){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *old = *var; *var |= mask);}static ETHR_INLINE intethr_atomic_xchg(ethr_atomic_t *var, long new, long *old){    ETHR_ATOMIC_OP_FALLBACK_IMPL__(var, *old = *var; *var = new);}   #endif /* #ifndef ETHR_HAVE_OPTIMIZED_ATOMIC_OPS */#ifndef ETHR_HAVE_OPTIMIZED_LOCKSstatic ETHR_INLINE intethr_spinlock_init(ethr_spinlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    return pthread_spin_init(&lock->spnlck, 0);#else    return pthread_mutex_init(&lock->mtx, NULL);#endif}static ETHR_INLINE intethr_spinlock_destroy(ethr_spinlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    return pthread_spin_destroy(&lock->spnlck);#else    return pthread_mutex_destroy(&lock->mtx);#endif}static ETHR_INLINE intethr_spin_unlock(ethr_spinlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    return pthread_spin_unlock(&lock->spnlck);#else    return pthread_mutex_unlock(&lock->mtx);#endif}static ETHR_INLINE intethr_spin_lock(ethr_spinlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    return pthread_spin_lock(&lock->spnlck);#else    return pthread_mutex_lock(&lock->mtx);#endif}static ETHR_INLINE intethr_rwlock_init(ethr_rwlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    lock->counter = 0;    return pthread_spin_init(&lock->spnlck, 0);#else    return pthread_rwlock_init(&lock->rwlck, NULL);#endif}static ETHR_INLINE intethr_rwlock_destroy(ethr_rwlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    return pthread_spin_destroy(&lock->spnlck);#else    return pthread_rwlock_destroy(&lock->rwlck);#endif}static ETHR_INLINE intethr_read_unlock(ethr_rwlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    int res = pthread_spin_lock(&lock->spnlck);    if (res != 0)	return res;    lock->counter--;    return pthread_spin_unlock(&lock->spnlck);#else    return pthread_rwlock_unlock(&lock->rwlck);#endif}static ETHR_INLINE intethr_read_lock(ethr_rwlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    int locked = 0;    do {	int res = pthread_spin_lock(&lock->spnlck);	if (res != 0)	    return res;	if ((lock->counter & ETHR_RWLOCK_WRITERS) == 0) {	    lock->counter++;	    locked = 1;	}	res = pthread_spin_unlock(&lock->spnlck);	if (res != 0)	    return res;    } while (!locked);    return 0;#else    return pthread_rwlock_rdlock(&lock->rwlck);#endif}static ETHR_INLINE intethr_write_unlock(ethr_rwlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    lock->counter = 0;    return pthread_spin_unlock(&lock->spnlck);#else    return pthread_rwlock_unlock(&lock->rwlck);#endif}static ETHR_INLINE intethr_write_lock(ethr_rwlock_t *lock){#ifdef ETHR_HAVE_PTHREAD_SPIN_LOCK    while (1) {	int res = pthread_spin_lock(&lock->spnlck);	if (res != 0)	    return res;	lock->counter |= ETHR_RWLOCK_WRITERS;	if (lock->counter == ETHR_RWLOCK_WRITERS)	    return 0;	res = pthread_spin_unlock(&lock->spnlck);	if (res != 0)	    return res;    }    return 0;#else    return pthread_rwlock_wrlock(&lock->rwlck);#endif}#endif /* ETHR_HAVE_OPTIMIZED_LOCKS */#endif /* #ifdef ETHR_TRY_INLINE_FUNCS */#elif defined(ETHR_WIN32_THREADS)/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\ * The native win32 threads implementation                                   *\*                                                                           */#ifdef WIN32_LEAN_AND_MEAN#  define ETHR_WIN32_LEAN_AND_MEAN_ALREADY_DEFINED#else#  define WIN32_LEAN_AND_MEAN#endif#include <windows.h>#ifndef ETHR_WIN32_LEAN_AND_MEAN_ALREADY_DEFINED#  undef WIN32_LEAN_AND_MEAN#endif#ifdef ETHR_HAVE_TRYENTERCRITICALSECTION#  define ETHR_HAVE_ETHR_MUTEX_TRYLOCK ETHR_HAVE_TRYENTERCRITICALSECTION#endif#ifdef ETHR_EXTENDED_LIB#error "extended functionallity not implemented yet..."#endif#ifndef EWOULDBLOCK#  define EWOULDBLOCK (10035) /* WSAEWOULDBLOCK */#endif#ifndef ETIMEDOUT#  define ETIMEDOUT (10060) /* WSAETIMEDOUT */#endif/* Types */typedef long ethr_tid; /* thread id type */typedef struct {    int initialized;    CRITICAL_SECTION cs;#if ETHR_XCHK    int is_rec_mtx;#endif} ethr_mutex;typedef struct cnd_wait_event__ cnd_wait_event_;typedef struct {    int initialized;    CRITICAL_SECTION cs;    cnd_wait_event_ *queue;    cnd_wait_event_ *queue_end;} ethr_cond;/* Static initializers */#define ETHR_MUTEX_INITER {0}#define ETHR_COND_INITER {0}#define ETHR_REC_MUTEX_INITER ETHR_MUTEX_INITER#define ETHR_HAVE_ETHR_REC_MUTEX_INIT 1typedef DWORD ethr_tsd_key;#undef ETHR_HAVE_ETHR_SIG_FUNCStypedef LONG ethr_atomic_t;#ifdef ETHR_TRY_INLINE_FUNCSint ethr_fake_static_mutex_init(ethr_mutex *mtx);#ifdef ETHR_HAVE_ETHR_MUTEX_TRYLOCKstatic ETHR_INLINE intethr_mutex_trylock(ethr_mutex *mtx){    if (!mtx->initialized) {	int res = ethr_fake_static_mutex_init(mtx);	if (res != 0)	    return res;    }    if (TryEnterCriticalSection(&mtx->cs))	return 0;    else	return EBUSY;}#endifstatic ETHR_INLINE intethr_mutex_lock(ethr_mutex *mtx){    if (!mtx->initialized) {	int res = ethr_fake_static_mutex_init(mtx);	if (res != 0)	    return res;    }    EnterCriticalSection(&mtx->cs);    return 0;}static ETHR_INLINE intethr_mutex_unlock(ethr_mutex *mtx){    LeaveCriticalSection(&mtx->cs);    return 0;}static ETHR_INLINE intethr_atomic_inctest(ethr_atomic_t *incp, long *testp){    *testp = (long) InterlockedIncrement(incp);    return 0;}static ETHR_INLINE intethr_atomic_dectest(ethr_atomic_t *decp, long *testp){    *testp = (long) InterlockedDecrement(decp);    return 0;}static ETHR_INLINE intethr_atomic_inc(ethr_atomic_t *incp){    (void) InterlockedIncrement(incp);    return 0;}static ETHR_INLINE intethr_atomic_dec(ethr_atomic_t *decp){    (void) InterlockedDecrement(decp);    return 0;}#endif /* #ifdef ETHR_TRY_INLINE_FUNCS */#else /* No supportet thread lib found */#ifdef ETHR_NO_SUPP_THR_LIB_NOT_FATAL#define ETHR_NO_THREAD_LIB#else#error "No supported thread lib found"#endif#endif#ifndef EWOULDBLOCK#  define EWOULDBLOCK EAGAIN#endif#ifndef ETIMEDOUT#  define ETIMEDOUT EAGAIN#endif/* ENOTSUP: same as in sys.h */#ifndef ENOTSUP#  ifdef EOPNOTSUPP#    define ENOTSUP EOPNOTSUPP#  else#    define ENOTSUP -1738659#  endif#endiftypedef struct {    void *(*alloc)(size_t);    void *(*realloc)(void *, size_t);    void (*free)(void *);    void *(*thread_create_prepare_func)(void);    void (*thread_create_parent_func)(void *);    void (*thread_create_child_func)(void *);} ethr_init_data;#define ETHR_INIT_DATA_DEFAULT_INITER {malloc, realloc, free, NULL, NULL, NULL}typedef struct {    int detached;			/* boolean (default false) */    int suggested_stack_size;		/* kilo words (default sys dependent) */} ethr_thr_opts;#define ETHR_THR_OPTS_DEFAULT_INITER {0, -1}int ethr_init(ethr_init_data *);int ethr_install_exit_handler(void (*funcp)(void));int ethr_thr_create(ethr_tid *, void * (*)(void *), void *, ethr_thr_opts *);int ethr_thr_join(ethr_tid, void **);int ethr_thr_detach(ethr_tid);void ethr_thr_exit(void *);ethr_tid ethr_self(void);int ethr_equal_tids(ethr_tid, ethr_tid);int ethr_mutex_init(ethr_mutex *);#ifdef ETHR_HAVE_ETHR_REC_MUTEX_INITint ethr_rec_mutex_init(ethr_mutex *);#endifint ethr_mutex_destroy(ethr_mutex *);int ethr_mutex_set_forksafe(ethr_mutex *);int ethr_mutex_unset_forksafe(ethr_mutex *);#ifndef ETHR_TRY_INLINE_FUNCS#ifdef ETHR_HAVE_ETHR_MUTEX_TRYLOCKint ethr_mutex_trylock(ethr_mutex *);#endifint ethr_mutex_lock(ethr_mutex *);int ethr_mutex_unlock(ethr_mutex *);#endifint ethr_cond_init(ethr_cond *);int ethr_cond_destroy(ethr_cond *);int ethr_cond_signal(ethr_cond *);int ethr_cond_broadcast(ethr_cond *);int ethr_cond_wait(ethr_cond *, ethr_mutex *);int ethr_cond_timedwait(ethr_cond *, ethr_mutex *, ethr_timeval *);#ifdef ETHR_EXTENDED_LIBint ethr_rwmutex_init(ethr_rwmutex *);int ethr_rwmutex_destroy(ethr_rwmutex *);#ifndef ETHR_TRY_INLINE_FUNCSint ethr_rwmutex_tryrlock(ethr_rwmutex *);int ethr_rwmutex_rlock(ethr_rwmutex *);int ethr_rwmutex_runlock(ethr_rwmutex *);int ethr_rwmutex_tryrwlock(ethr_rwmutex *);int ethr_rwmutex_rwlock(ethr_rwmutex *);int ethr_rwmutex_rwunlock(ethr_rwmutex *);#endif#endif#ifndef ETHR_TRY_INLINE_FUNCSint ethr_atomic_init(ethr_atomic_t *var, long i);int ethr_atomic_set(ethr_atomic_t *var, long i);int ethr_atomic_read(ethr_atomic_t *var, long *i);int ethr_atomic_inctest(ethr_atomic_t *, long *);int ethr_atomic_dectest(ethr_atomic_t *, long *);int ethr_atomic_inc(ethr_atomic_t *);int ethr_atomic_dec(ethr_atomic_t *);int ethr_atomic_addtest(ethr_atomic_t *, long, long *);int ethr_atomic_add(ethr_atomic_t *, long);int ethr_atomic_and_old(ethr_atomic_t *, long, long *);int ethr_atomic_or_old(ethr_atomic_t *, long, long *);int ethr_atomic_xchg(ethr_atomic_t *, long, long *);#if defined(ETHR_PTHREADS)int ethr_spinlock_init(ethr_spinlock_t *);int ethr_spinlock_destroy(ethr_spinlock_t *);int ethr_spin_unlock(ethr_spinlock_t *);int ethr_spin_lock(ethr_spinlock_t *);int ethr_rwlock_init(ethr_rwlock_t *);int ethr_rwlock_destroy(ethr_rwlock_t *);int ethr_read_unlock(ethr_rwlock_t *);int ethr_read_lock(ethr_rwlock_t *);int ethr_write_unlock(ethr_rwlock_t *);int ethr_write_lock(ethr_rwlock_t *);#endif#endifint ethr_time_now(ethr_timeval *);int ethr_tsd_key_create(ethr_tsd_key *);int ethr_tsd_key_delete(ethr_tsd_key);int ethr_tsd_set(ethr_tsd_key, void *);void *ethr_tsd_get(ethr_tsd_key);#ifdef ETHR_HAVE_ETHR_SIG_FUNCS#include <signal.h>int ethr_sigmask(int how, const sigset_t *set, sigset_t *oset);int ethr_sigwait(const sigset_t *set, int *sig);#endif#ifdef ETHR_XCHKint ethr_xchk_have_locked_mutexes(void);#endif#endif /* #ifndef ETHREAD_H__ */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -