You've already forked qlg.tsgz.moe
Init Repo
This commit is contained in:
49
vendor/swoole/src/lock/AtomicLock.c
vendored
Executable file
49
vendor/swoole/src/lock/AtomicLock.c
vendored
Executable file
@ -0,0 +1,49 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| Swoole |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.0 of the Apache license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
| If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
| to obtain it through the world-wide-web, please send a note to |
|
||||
| license@swoole.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "swoole.h"
|
||||
|
||||
static int swAtomicLock_lock(swLock *lock);
|
||||
static int swAtomicLock_unlock(swLock *lock);
|
||||
static int swAtomicLock_trylock(swLock *lock);
|
||||
|
||||
int swAtomicLock_create(swLock *lock, int spin)
|
||||
{
|
||||
bzero(lock, sizeof(swLock));
|
||||
lock->type = SW_ATOMLOCK;
|
||||
lock->object.atomlock.spin = spin;
|
||||
lock->lock = swAtomicLock_lock;
|
||||
lock->unlock = swAtomicLock_unlock;
|
||||
lock->trylock = swAtomicLock_trylock;
|
||||
return SW_OK;
|
||||
}
|
||||
|
||||
static int swAtomicLock_lock(swLock *lock)
|
||||
{
|
||||
sw_spinlock(&lock->object.atomlock.lock_t);
|
||||
return SW_OK;
|
||||
}
|
||||
|
||||
static int swAtomicLock_unlock(swLock *lock)
|
||||
{
|
||||
return lock->object.atomlock.lock_t = 0;
|
||||
}
|
||||
|
||||
static int swAtomicLock_trylock(swLock *lock)
|
||||
{
|
||||
sw_atomic_t *atomic = &lock->object.atomlock.lock_t;
|
||||
return (*(atomic) == 0 && sw_atomic_cmp_set(atomic, 0, 1));
|
||||
}
|
89
vendor/swoole/src/lock/Cond.c
vendored
Executable file
89
vendor/swoole/src/lock/Cond.c
vendored
Executable file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| Swoole |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.0 of the Apache license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
| If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
| to obtain it through the world-wide-web, please send a note to |
|
||||
| license@swoole.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "swoole.h"
|
||||
|
||||
static int swCond_notify(swCond *cond);
|
||||
static int swCond_broadcast(swCond *cond);
|
||||
static int swCond_timewait(swCond *cond, long sec, long nsec);
|
||||
static int swCond_wait(swCond *cond);
|
||||
static int swCond_lock(swCond *cond);
|
||||
static int swCond_unlock(swCond *cond);
|
||||
static void swCond_free(swCond *cond);
|
||||
|
||||
int swCond_create(swCond *cond)
|
||||
{
|
||||
if (pthread_cond_init(&cond->_cond, NULL) < 0)
|
||||
{
|
||||
swWarn("pthread_cond_init fail. Error: %s [%d]", strerror(errno), errno);
|
||||
return SW_ERR;
|
||||
}
|
||||
if (swMutex_create(&cond->_lock, 0) < 0)
|
||||
{
|
||||
return SW_ERR;
|
||||
}
|
||||
|
||||
cond->notify = swCond_notify;
|
||||
cond->broadcast = swCond_broadcast;
|
||||
cond->timewait = swCond_timewait;
|
||||
cond->wait = swCond_wait;
|
||||
cond->lock = swCond_lock;
|
||||
cond->unlock = swCond_unlock;
|
||||
cond->free = swCond_free;
|
||||
|
||||
return SW_OK;
|
||||
}
|
||||
|
||||
static int swCond_notify(swCond *cond)
|
||||
{
|
||||
return pthread_cond_signal(&cond->_cond);
|
||||
}
|
||||
|
||||
static int swCond_broadcast(swCond *cond)
|
||||
{
|
||||
return pthread_cond_broadcast(&cond->_cond);
|
||||
}
|
||||
|
||||
static int swCond_timewait(swCond *cond, long sec, long nsec)
|
||||
{
|
||||
struct timespec timeo;
|
||||
|
||||
timeo.tv_sec = sec;
|
||||
timeo.tv_nsec = nsec;
|
||||
|
||||
return pthread_cond_timedwait(&cond->_cond, &cond->_lock.object.mutex._lock, &timeo);
|
||||
}
|
||||
|
||||
static int swCond_wait(swCond *cond)
|
||||
{
|
||||
return pthread_cond_wait(&cond->_cond, &cond->_lock.object.mutex._lock);
|
||||
}
|
||||
|
||||
static int swCond_lock(swCond *cond)
|
||||
{
|
||||
return cond->_lock.lock(&cond->_lock);
|
||||
}
|
||||
|
||||
static int swCond_unlock(swCond *cond)
|
||||
{
|
||||
return cond->_lock.unlock(&cond->_lock);
|
||||
}
|
||||
|
||||
static void swCond_free(swCond *cond)
|
||||
{
|
||||
pthread_cond_destroy(&cond->_cond);
|
||||
cond->_lock.free(&cond->_lock);
|
||||
}
|
73
vendor/swoole/src/lock/FileLock.c
vendored
Executable file
73
vendor/swoole/src/lock/FileLock.c
vendored
Executable file
@ -0,0 +1,73 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| Swoole |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.0 of the Apache license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
| If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
| to obtain it through the world-wide-web, please send a note to |
|
||||
| license@swoole.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "swoole.h"
|
||||
|
||||
static int swFileLock_lock_rd(swLock *lock);
|
||||
static int swFileLock_lock_rw(swLock *lock);
|
||||
static int swFileLock_unlock(swLock *lock);
|
||||
static int swFileLock_trylock_rw(swLock *lock);
|
||||
static int swFileLock_trylock_rd(swLock *lock);
|
||||
static int swFileLock_free(swLock *lock);
|
||||
|
||||
int swFileLock_create(swLock *lock, int fd)
|
||||
{
|
||||
bzero(lock, sizeof(swLock));
|
||||
lock->type = SW_FILELOCK;
|
||||
lock->object.filelock.fd = fd;
|
||||
lock->lock_rd = swFileLock_lock_rd;
|
||||
lock->lock = swFileLock_lock_rw;
|
||||
lock->trylock_rd = swFileLock_trylock_rd;
|
||||
lock->trylock = swFileLock_trylock_rw;
|
||||
lock->unlock = swFileLock_unlock;
|
||||
lock->free = swFileLock_free;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int swFileLock_lock_rd(swLock *lock)
|
||||
{
|
||||
lock->object.filelock.lock_t.l_type = F_RDLCK;
|
||||
return fcntl(lock->object.filelock.fd, F_SETLKW, &lock->object.filelock);
|
||||
}
|
||||
|
||||
static int swFileLock_lock_rw(swLock *lock)
|
||||
{
|
||||
lock->object.filelock.lock_t.l_type = F_WRLCK;
|
||||
return fcntl(lock->object.filelock.fd, F_SETLKW, &lock->object.filelock);
|
||||
}
|
||||
|
||||
static int swFileLock_unlock(swLock *lock)
|
||||
{
|
||||
lock->object.filelock.lock_t.l_type = F_UNLCK;
|
||||
return fcntl(lock->object.filelock.fd, F_SETLKW, &lock->object.filelock);
|
||||
}
|
||||
|
||||
static int swFileLock_trylock_rw(swLock *lock)
|
||||
{
|
||||
lock->object.filelock.lock_t.l_type = F_WRLCK;
|
||||
return fcntl(lock->object.filelock.fd, F_SETLK, &lock->object.filelock);
|
||||
}
|
||||
|
||||
static int swFileLock_trylock_rd(swLock *lock)
|
||||
{
|
||||
lock->object.filelock.lock_t.l_type = F_RDLCK;
|
||||
return fcntl(lock->object.filelock.fd, F_SETLK, &lock->object.filelock);
|
||||
}
|
||||
|
||||
static int swFileLock_free(swLock *lock)
|
||||
{
|
||||
return close(lock->object.filelock.fd);
|
||||
}
|
100
vendor/swoole/src/lock/Mutex.c
vendored
Executable file
100
vendor/swoole/src/lock/Mutex.c
vendored
Executable file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| Swoole |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.0 of the Apache license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
| If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
| to obtain it through the world-wide-web, please send a note to |
|
||||
| license@swoole.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "swoole.h"
|
||||
|
||||
static int swMutex_lock(swLock *lock);
|
||||
static int swMutex_unlock(swLock *lock);
|
||||
static int swMutex_trylock(swLock *lock);
|
||||
static int swMutex_free(swLock *lock);
|
||||
|
||||
int swMutex_create(swLock *lock, int use_in_process)
|
||||
{
|
||||
int ret;
|
||||
bzero(lock, sizeof(swLock));
|
||||
lock->type = SW_MUTEX;
|
||||
pthread_mutexattr_init(&lock->object.mutex.attr);
|
||||
if (use_in_process == 1)
|
||||
{
|
||||
pthread_mutexattr_setpshared(&lock->object.mutex.attr, PTHREAD_PROCESS_SHARED);
|
||||
}
|
||||
if ((ret = pthread_mutex_init(&lock->object.mutex._lock, &lock->object.mutex.attr)) < 0)
|
||||
{
|
||||
return SW_ERR;
|
||||
}
|
||||
lock->lock = swMutex_lock;
|
||||
lock->unlock = swMutex_unlock;
|
||||
lock->trylock = swMutex_trylock;
|
||||
lock->free = swMutex_free;
|
||||
return SW_OK;
|
||||
}
|
||||
|
||||
static int swMutex_lock(swLock *lock)
|
||||
{
|
||||
return pthread_mutex_lock(&lock->object.mutex._lock);
|
||||
}
|
||||
|
||||
static int swMutex_unlock(swLock *lock)
|
||||
{
|
||||
return pthread_mutex_unlock(&lock->object.mutex._lock);
|
||||
}
|
||||
|
||||
static int swMutex_trylock(swLock *lock)
|
||||
{
|
||||
return pthread_mutex_trylock(&lock->object.mutex._lock);
|
||||
}
|
||||
|
||||
#ifdef HAVE_MUTEX_TIMEDLOCK
|
||||
int swMutex_lockwait(swLock *lock, int timeout_msec)
|
||||
{
|
||||
struct timespec timeo;
|
||||
timeo.tv_sec = timeout_msec / 1000;
|
||||
timeo.tv_nsec = (timeout_msec - timeo.tv_sec * 1000) * 1000 * 1000;
|
||||
return pthread_mutex_timedlock(&lock->object.mutex._lock, &timeo);
|
||||
}
|
||||
#else
|
||||
int swMutex_lockwait(swLock *lock, int timeout_msec)
|
||||
{
|
||||
int sub = 1;
|
||||
int sleep_ms = 1000;
|
||||
|
||||
if (timeout_msec > 100)
|
||||
{
|
||||
sub = 10;
|
||||
sleep_ms = 10000;
|
||||
}
|
||||
|
||||
while( timeout_msec > 0)
|
||||
{
|
||||
if (pthread_mutex_trylock(&lock->object.mutex._lock) == 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
usleep(sleep_ms);
|
||||
timeout_msec -= sub;
|
||||
}
|
||||
}
|
||||
return ETIMEDOUT;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int swMutex_free(swLock *lock)
|
||||
{
|
||||
pthread_mutexattr_destroy(&lock->object.mutex.attr);
|
||||
return pthread_mutex_destroy(&lock->object.mutex._lock);
|
||||
}
|
81
vendor/swoole/src/lock/RWLock.c
vendored
Executable file
81
vendor/swoole/src/lock/RWLock.c
vendored
Executable file
@ -0,0 +1,81 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| Swoole |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.0 of the Apache license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
| If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
| to obtain it through the world-wide-web, please send a note to |
|
||||
| license@swoole.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "swoole.h"
|
||||
|
||||
#ifdef HAVE_RWLOCK
|
||||
|
||||
static int swRWLock_lock_rd(swLock *lock);
|
||||
static int swRWLock_lock_rw(swLock *lock);
|
||||
static int swRWLock_unlock(swLock *lock);
|
||||
static int swRWLock_trylock_rw(swLock *lock);
|
||||
static int swRWLock_trylock_rd(swLock *lock);
|
||||
static int swRWLock_free(swLock *lock);
|
||||
|
||||
int swRWLock_create(swLock *lock, int use_in_process)
|
||||
{
|
||||
int ret;
|
||||
bzero(lock, sizeof(swLock));
|
||||
lock->type = SW_RWLOCK;
|
||||
pthread_rwlockattr_init(&lock->object.rwlock.attr);
|
||||
if (use_in_process == 1)
|
||||
{
|
||||
pthread_rwlockattr_setpshared(&lock->object.rwlock.attr, PTHREAD_PROCESS_SHARED);
|
||||
}
|
||||
if ((ret = pthread_rwlock_init(&lock->object.rwlock._lock, &lock->object.rwlock.attr)) < 0)
|
||||
{
|
||||
return SW_ERR;
|
||||
}
|
||||
lock->lock_rd = swRWLock_lock_rd;
|
||||
lock->lock = swRWLock_lock_rw;
|
||||
lock->unlock = swRWLock_unlock;
|
||||
lock->trylock = swRWLock_trylock_rw;
|
||||
lock->trylock_rd = swRWLock_trylock_rd;
|
||||
lock->free = swRWLock_free;
|
||||
return SW_OK;
|
||||
}
|
||||
|
||||
static int swRWLock_lock_rd(swLock *lock)
|
||||
{
|
||||
return pthread_rwlock_rdlock(&lock->object.rwlock._lock);
|
||||
}
|
||||
|
||||
static int swRWLock_lock_rw(swLock *lock)
|
||||
{
|
||||
return pthread_rwlock_wrlock(&lock->object.rwlock._lock);
|
||||
}
|
||||
|
||||
static int swRWLock_unlock(swLock *lock)
|
||||
{
|
||||
return pthread_rwlock_unlock(&lock->object.rwlock._lock);
|
||||
}
|
||||
|
||||
static int swRWLock_trylock_rd(swLock *lock)
|
||||
{
|
||||
return pthread_rwlock_tryrdlock(&lock->object.rwlock._lock);
|
||||
}
|
||||
|
||||
static int swRWLock_trylock_rw(swLock *lock)
|
||||
{
|
||||
return pthread_rwlock_trywrlock(&lock->object.rwlock._lock);
|
||||
}
|
||||
|
||||
static int swRWLock_free(swLock *lock)
|
||||
{
|
||||
return pthread_rwlock_destroy(&lock->object.rwlock._lock);
|
||||
}
|
||||
|
||||
#endif
|
68
vendor/swoole/src/lock/Semaphore.c
vendored
Executable file
68
vendor/swoole/src/lock/Semaphore.c
vendored
Executable file
@ -0,0 +1,68 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| Swoole |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.0 of the Apache license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
| If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
| to obtain it through the world-wide-web, please send a note to |
|
||||
| license@swoole.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "swoole.h"
|
||||
#include <sys/sem.h>
|
||||
|
||||
static int swSem_lock(swLock *lock);
|
||||
static int swSem_unlock(swLock *lock);
|
||||
static int swSem_free(swLock *lock);
|
||||
|
||||
int swSem_create(swLock *lock, key_t key)
|
||||
{
|
||||
int ret;
|
||||
lock->type = SW_SEM;
|
||||
if ((ret = semget(key, 1, IPC_CREAT | 0666)) < 0)
|
||||
{
|
||||
return SW_ERR;
|
||||
}
|
||||
|
||||
if (semctl(ret, 0, SETVAL, 1) == -1)
|
||||
{
|
||||
swWarn("semctl(SETVAL) failed");
|
||||
return SW_ERR;
|
||||
}
|
||||
lock->object.sem.semid = ret;
|
||||
|
||||
lock->lock = swSem_lock;
|
||||
lock->unlock = swSem_unlock;
|
||||
lock->free = swSem_free;
|
||||
|
||||
return SW_OK;
|
||||
}
|
||||
|
||||
static int swSem_unlock(swLock *lock)
|
||||
{
|
||||
struct sembuf sem;
|
||||
sem.sem_flg = SEM_UNDO;
|
||||
sem.sem_num = 0;
|
||||
sem.sem_op = 1;
|
||||
return semop(lock->object.sem.semid, &sem, 1);
|
||||
}
|
||||
|
||||
static int swSem_lock(swLock *lock)
|
||||
{
|
||||
struct sembuf sem;
|
||||
sem.sem_flg = SEM_UNDO;
|
||||
sem.sem_num = 0;
|
||||
sem.sem_op = -1;
|
||||
return semop(lock->object.sem.semid, &sem, 1);
|
||||
}
|
||||
|
||||
static int swSem_free(swLock *lock)
|
||||
{
|
||||
return semctl(lock->object.sem.semid, 0, IPC_RMID);
|
||||
}
|
62
vendor/swoole/src/lock/SpinLock.c
vendored
Executable file
62
vendor/swoole/src/lock/SpinLock.c
vendored
Executable file
@ -0,0 +1,62 @@
|
||||
/*
|
||||
+----------------------------------------------------------------------+
|
||||
| Swoole |
|
||||
+----------------------------------------------------------------------+
|
||||
| This source file is subject to version 2.0 of the Apache license, |
|
||||
| that is bundled with this package in the file LICENSE, and is |
|
||||
| available through the world-wide-web at the following url: |
|
||||
| http://www.apache.org/licenses/LICENSE-2.0.html |
|
||||
| If you did not receive a copy of the Apache2.0 license and are unable|
|
||||
| to obtain it through the world-wide-web, please send a note to |
|
||||
| license@swoole.com so we can mail you a copy immediately. |
|
||||
+----------------------------------------------------------------------+
|
||||
| Author: Tianfeng Han <mikan.tenny@gmail.com> |
|
||||
+----------------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
#include "swoole.h"
|
||||
|
||||
#ifdef HAVE_SPINLOCK
|
||||
|
||||
static int swSpinLock_lock(swLock *lock);
|
||||
static int swSpinLock_unlock(swLock *lock);
|
||||
static int swSpinLock_trylock(swLock *lock);
|
||||
static int swSpinLock_free(swLock *lock);
|
||||
|
||||
int swSpinLock_create(swLock *lock, int use_in_process)
|
||||
{
|
||||
int ret;
|
||||
bzero(lock, sizeof(swLock));
|
||||
lock->type = SW_SPINLOCK;
|
||||
if ((ret = pthread_spin_init(&lock->object.spinlock.lock_t, use_in_process)) < 0)
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
lock->lock = swSpinLock_lock;
|
||||
lock->unlock = swSpinLock_unlock;
|
||||
lock->trylock = swSpinLock_trylock;
|
||||
lock->free = swSpinLock_free;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int swSpinLock_lock(swLock *lock)
|
||||
{
|
||||
return pthread_spin_lock(&lock->object.spinlock.lock_t);
|
||||
}
|
||||
|
||||
static int swSpinLock_unlock(swLock *lock)
|
||||
{
|
||||
return pthread_spin_unlock(&lock->object.spinlock.lock_t);
|
||||
}
|
||||
|
||||
static int swSpinLock_trylock(swLock *lock)
|
||||
{
|
||||
return pthread_spin_trylock(&lock->object.spinlock.lock_t);
|
||||
}
|
||||
|
||||
static int swSpinLock_free(swLock *lock)
|
||||
{
|
||||
return pthread_spin_destroy(&lock->object.spinlock.lock_t);
|
||||
}
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user