Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
181 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -20,7 +20,7 @@ SYSCFLAGS= | ||
| SYSLDFLAGS= | ||
| SYSLIBS= | ||
|
|
||
| MYCFLAGS= | ||
| MYCFLAGS=-I../threads | ||
| MYLDFLAGS= | ||
| MYLIBS= | ||
| MYOBJS= | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| #ifndef SKYNET_ATOMIC_H | ||
| #define SKYNET_ATOMIC_H | ||
|
|
||
| #define ATOM_CAS(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) | ||
| #define ATOM_CAS_POINTER(ptr, oval, nval) __sync_bool_compare_and_swap(ptr, oval, nval) | ||
| #define ATOM_INC(ptr) __sync_add_and_fetch(ptr, 1) | ||
| #define ATOM_FINC(ptr) __sync_fetch_and_add(ptr, 1) | ||
| #define ATOM_DEC(ptr) __sync_sub_and_fetch(ptr, 1) | ||
| #define ATOM_FDEC(ptr) __sync_fetch_and_sub(ptr, 1) | ||
| #define ATOM_ADD(ptr,n) __sync_add_and_fetch(ptr, n) | ||
| #define ATOM_SUB(ptr,n) __sync_sub_and_fetch(ptr, n) | ||
| #define ATOM_AND(ptr,n) __sync_and_and_fetch(ptr, n) | ||
|
|
||
| #endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| #ifndef SKYNET_RWLOCK_H | ||
| #define SKYNET_RWLOCK_H | ||
|
|
||
| #ifndef USE_PTHREAD_LOCK | ||
|
|
||
| struct rwlock { | ||
| int write; | ||
| int read; | ||
| }; | ||
|
|
||
| static inline void | ||
| rwlock_init(struct rwlock *lock) { | ||
| lock->write = 0; | ||
| lock->read = 0; | ||
| } | ||
|
|
||
| static inline void | ||
| rwlock_rlock(struct rwlock *lock) { | ||
| for (;;) { | ||
| while(lock->write) { | ||
| __sync_synchronize(); | ||
| } | ||
| __sync_add_and_fetch(&lock->read,1); | ||
| if (lock->write) { | ||
| __sync_sub_and_fetch(&lock->read,1); | ||
| } else { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| static inline void | ||
| rwlock_wlock(struct rwlock *lock) { | ||
| while (__sync_lock_test_and_set(&lock->write,1)) {} | ||
| while(lock->read) { | ||
| __sync_synchronize(); | ||
| } | ||
| } | ||
|
|
||
| static inline void | ||
| rwlock_wunlock(struct rwlock *lock) { | ||
| __sync_lock_release(&lock->write); | ||
| } | ||
|
|
||
| static inline void | ||
| 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| #ifndef SKYNET_SPINLOCK_H | ||
| #define SKYNET_SPINLOCK_H | ||
|
|
||
| #define SPIN_INIT(q) spinlock_init(&(q)->lock); | ||
| #define SPIN_LOCK(q) spinlock_lock(&(q)->lock); | ||
| #define SPIN_UNLOCK(q) spinlock_unlock(&(q)->lock); | ||
| #define SPIN_DESTROY(q) spinlock_destroy(&(q)->lock); | ||
|
|
||
| #ifndef USE_PTHREAD_LOCK | ||
|
|
||
| struct spinlock { | ||
| int lock; | ||
| }; | ||
|
|
||
| static inline void | ||
| spinlock_init(struct spinlock *lock) { | ||
| lock->lock = 0; | ||
| } | ||
|
|
||
| static inline void | ||
| spinlock_lock(struct spinlock *lock) { | ||
| while (__sync_lock_test_and_set(&lock->lock,1)) {} | ||
| } | ||
|
|
||
| static inline int | ||
| spinlock_trylock(struct spinlock *lock) { | ||
| return __sync_lock_test_and_set(&lock->lock,1) == 0; | ||
| } | ||
|
|
||
| static inline void | ||
| spinlock_unlock(struct spinlock *lock) { | ||
| __sync_lock_release(&lock->lock); | ||
| } | ||
|
|
||
| static inline void | ||
| spinlock_destroy(struct spinlock *lock) { | ||
| (void) lock; | ||
| } | ||
|
|
||
| #else | ||
|
|
||
| #include <pthread.h> | ||
|
|
||
| // we use mutex instead of spinlock for some reason | ||
| // you can also replace to pthread_spinlock | ||
|
|
||
| struct spinlock { | ||
| pthread_mutex_t lock; | ||
| }; | ||
|
|
||
| static inline void | ||
| spinlock_init(struct spinlock *lock) { | ||
| pthread_mutex_init(&lock->lock, NULL); | ||
| } | ||
|
|
||
| static inline void | ||
| spinlock_lock(struct spinlock *lock) { | ||
| pthread_mutex_lock(&lock->lock); | ||
| } | ||
|
|
||
| static inline int | ||
| spinlock_trylock(struct spinlock *lock) { | ||
| return pthread_mutex_trylock(&lock->lock) == 0; | ||
| } | ||
|
|
||
| static inline void | ||
| spinlock_unlock(struct spinlock *lock) { | ||
| pthread_mutex_unlock(&lock->lock); | ||
| } | ||
|
|
||
| static inline void | ||
| spinlock_destroy(struct spinlock *lock) { | ||
| pthread_mutex_destroy(&lock->lock); | ||
| } | ||
|
|
||
| #endif | ||
|
|
||
| #endif |