forked from swoole/swoole-src
-
Notifications
You must be signed in to change notification settings - Fork 0
/
swoole_lock.c
173 lines (157 loc) · 5.43 KB
/
swoole_lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
+----------------------------------------------------------------------+
| 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 |
| [email protected] so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Tianfeng Han <[email protected]> |
+----------------------------------------------------------------------+
*/
#include "php_swoole.h"
static PHP_METHOD(swoole_lock, __construct);
static PHP_METHOD(swoole_lock, __destruct);
static PHP_METHOD(swoole_lock, lock);
static PHP_METHOD(swoole_lock, trylock);
static PHP_METHOD(swoole_lock, lock_read);
static PHP_METHOD(swoole_lock, trylock_read);
static PHP_METHOD(swoole_lock, unlock);
static zend_class_entry swoole_lock_ce;
zend_class_entry *swoole_lock_class_entry_ptr;
static const zend_function_entry swoole_lock_methods[] =
{
PHP_ME(swoole_lock, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(swoole_lock, __destruct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_DTOR)
PHP_ME(swoole_lock, lock, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_lock, trylock, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_lock, lock_read, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_lock, trylock_read, NULL, ZEND_ACC_PUBLIC)
PHP_ME(swoole_lock, unlock, NULL, ZEND_ACC_PUBLIC)
PHP_FE_END
};
void swoole_lock_init(int module_number TSRMLS_DC)
{
SWOOLE_INIT_CLASS_ENTRY(swoole_lock_ce, "swoole_lock", "Swoole\\Lock", swoole_lock_methods);
swoole_lock_class_entry_ptr = zend_register_internal_class(&swoole_lock_ce TSRMLS_CC);
REGISTER_LONG_CONSTANT("SWOOLE_FILELOCK", SW_FILELOCK, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_MUTEX", SW_MUTEX, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("SWOOLE_SEM", SW_SEM, CONST_CS | CONST_PERSISTENT);
#ifdef HAVE_RWLOCK
REGISTER_LONG_CONSTANT("SWOOLE_RWLOCK", SW_RWLOCK, CONST_CS | CONST_PERSISTENT);
#endif
#ifdef HAVE_SPINLOCK
REGISTER_LONG_CONSTANT("SWOOLE_SPINLOCK", SW_SPINLOCK, CONST_CS | CONST_PERSISTENT);
#endif
}
PHP_METHOD(swoole_lock, __construct)
{
long type = SW_MUTEX;
char *filelock;
zend_size_t filelock_len = 0;
int ret;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ls", &type, &filelock, &filelock_len) == FAILURE)
{
RETURN_FALSE;
}
swLock *lock = SwooleG.memory_pool->alloc(SwooleG.memory_pool, sizeof(swLock));
if (lock == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "alloc failed.");
RETURN_FALSE;
}
switch(type)
{
#ifdef HAVE_RWLOCK
case SW_RWLOCK:
ret = swRWLock_create(lock, 1);
break;
#endif
case SW_FILELOCK:
if (filelock_len <= 0)
{
php_error_docref(NULL TSRMLS_CC, E_ERROR, "filelock require lock file name.");
RETURN_FALSE;
}
int fd;
if ((fd = open(filelock, O_RDWR | O_CREAT, 0666)) < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "open file[%s] failed. Error: %s [%d]", filelock, strerror(errno), errno);
RETURN_FALSE;
}
ret = swFileLock_create(lock, fd);
break;
case SW_SEM:
ret = swSem_create(lock, IPC_PRIVATE);
break;
#ifdef HAVE_SPINLOCK
case SW_SPINLOCK:
ret = swSpinLock_create(lock, 1);
break;
#endif
case SW_MUTEX:
default:
ret = swMutex_create(lock, 1);
break;
}
if (ret < 0)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "create lock failed");
RETURN_FALSE;
}
swoole_set_object(getThis(), lock);
RETURN_TRUE;
}
PHP_METHOD(swoole_lock, __destruct)
{
swLock *lock = swoole_get_object(getThis());
if (lock)
{
lock->free(lock);
swoole_set_object(getThis(), NULL);
}
}
PHP_METHOD(swoole_lock, lock)
{
swLock *lock = swoole_get_object(getThis());
SW_LOCK_CHECK_RETURN(lock->lock(lock));
}
PHP_METHOD(swoole_lock, unlock)
{
swLock *lock = swoole_get_object(getThis());
SW_LOCK_CHECK_RETURN(lock->unlock(lock));
}
PHP_METHOD(swoole_lock, trylock)
{
swLock *lock = swoole_get_object(getThis());
if (lock->trylock == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "lock[type=%d] can not trylock", lock->type);
RETURN_FALSE;
}
SW_LOCK_CHECK_RETURN(lock->trylock(lock));
}
PHP_METHOD(swoole_lock, trylock_read)
{
swLock *lock = swoole_get_object(getThis());
if (lock->trylock_rd == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "lock[type=%d] can not trylock_read", lock->type);
RETURN_FALSE;
}
SW_LOCK_CHECK_RETURN(lock->trylock(lock));
}
PHP_METHOD(swoole_lock, lock_read)
{
swLock *lock = swoole_get_object(getThis());
if (lock->lock_rd == NULL)
{
php_error_docref(NULL TSRMLS_CC, E_WARNING, "lock[type=%d] can not lock_read", lock->type);
RETURN_FALSE;
}
SW_LOCK_CHECK_RETURN(lock->trylock(lock));
}