mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
add pthread lock
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
#ifndef SKYNET_RWLOCK_H
|
||||
#define SKYNET_RWLOCK_H
|
||||
|
||||
#ifndef USE_PTHREAD_LOCK
|
||||
|
||||
struct rwlock {
|
||||
int write;
|
||||
int read;
|
||||
@@ -45,4 +47,42 @@ rwlock_runlock(struct rwlock *lock) {
|
||||
__sync_sub_and_fetch(&lock->read,1);
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
// only for some platform doesn't have __sync_*
|
||||
// todo: check the result of pthread api
|
||||
|
||||
struct rwlock {
|
||||
pthread_rwlock_t lock;
|
||||
};
|
||||
|
||||
static inline void
|
||||
rwlock_init(struct rwlock *lock) {
|
||||
pthread_rwlock_init(&lock->lock, NULL);
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_rlock(struct rwlock *lock) {
|
||||
pthread_rwlock_rdlock(&lock->lock);
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_wlock(struct rwlock *lock) {
|
||||
pthread_rwlock_wrlock(&lock->lock);
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_wunlock(struct rwlock *lock) {
|
||||
pthread_rwlock_unlock(&lock->lock);
|
||||
}
|
||||
|
||||
static inline void
|
||||
rwlock_runlock(struct rwlock *lock) {
|
||||
pthread_rwlock_unlock(&lock->lock);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user