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 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
|
/*
+----------------------------------------------------------------------+
| APCu |
+----------------------------------------------------------------------+
| Copyright (c) 2013 The PHP Group |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP 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.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: Joe Watkins <joe.watkins@live.co.uk> |
+----------------------------------------------------------------------+
*/
#ifndef HAVE_APC_LOCK
#define HAVE_APC_LOCK
#ifndef HAVE_APC_LOCK_H
# include "apc_lock.h"
#endif
/*
APCu never checks the return value of a locking call, it assumes it should not fail
and execution continues regardless, therefore it is pointless to check the return
values of calls to locking functions, lets save ourselves the comparison.
*/
/* {{{ There's very little point in initializing a billion sets of attributes */
#ifndef PHP_WIN32
# ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
static pthread_mutexattr_t apc_lock_attr;
# else
static pthread_rwlockattr_t apc_lock_attr;
# endif
# else
# include <unistd.h>
# include <fcntl.h>
static int apc_fcntl_call(int fd, int cmd, int type, off_t offset, int whence, off_t len) {
int ret;
struct flock lock;
lock.l_type = type;
lock.l_start = offset;
lock.l_whence = whence;
lock.l_len = len;
lock.l_pid = 0;
do {
ret = fcntl(fd, cmd, &lock) ;
} while(ret < 0 && errno == EINTR);
return(ret);
}
# endif
# else
PHP_APCU_API int apc_lock_init(apc_lock_t* lock)
{
lock->state = 0;
}
PHP_APCU_API int apc_lock_try(apc_lock_t* lock)
{
int failed = 1;
asm volatile
(
"xchgl %0, 0(%1)" :
"=r" (failed) : "r" (&lock->state),
"0" (failed)
);
return failed;
}
PHP_APCU_API int apc_lock_get(apc_lock_t* lock)
{
int failed = 1;
do {
failed = apc_lock_try(
lock);
#ifdef APC_LOCK_NICE
usleep(0);
#endif
} while (failed);
return failed;
}
PHP_APCU_API int apc_lock_release(apc_lock_t* lock)
{
int released = 0;
asm volatile (
"xchg %0, 0(%1)" : "=r" (released) : "r" (&lock->state),
"0" (released)
);
return !released;
}
# endif
static zend_bool apc_lock_ready = 0;
#endif /* }}} */
/* {{{ Initialize the global lock attributes */
PHP_APCU_API zend_bool apc_lock_init() {
#ifndef PHP_WIN32
if (apc_lock_ready)
return 1;
/* once per process please */
apc_lock_ready = 1;
#ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
if (pthread_mutexattr_init(&apc_lock_attr) == SUCCESS) {
if (pthread_mutexattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) {
pthread_mutexattr_settype(&apc_lock_attr, PTHREAD_MUTEX_RECURSIVE);
return 1;
}
}
# else
if (pthread_rwlockattr_init(&apc_lock_attr) == SUCCESS) {
if (pthread_rwlockattr_setpshared(&apc_lock_attr, PTHREAD_PROCESS_SHARED) == SUCCESS) {
return 1;
}
}
# endif
# endif
#endif
return 0;
#else
return 1;
#endif
} /* }}} */
/* {{{ Cleanup attributes and statics */
PHP_APCU_API void apc_lock_cleanup() {
#ifndef PHP_WIN32
if (!apc_lock_ready)
return;
/* once per process please */
apc_lock_ready = 0;
#ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
pthread_mutexattr_destroy(&apc_lock_attr);
# else
pthread_rwlockattr_destroy(&apc_lock_attr);
# endif
# endif
#endif
#endif
} /* }}} */
PHP_APCU_API zend_bool apc_lock_create(apc_lock_t *lock) {
#ifndef PHP_WIN32
# ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
{
pthread_mutex_init(lock, &apc_lock_attr);
return 1;
}
# else
{
/* Native */
return (pthread_rwlock_init(lock, &apc_lock_attr)==SUCCESS);
}
# endif
# else
{
/* FCNTL */
char lock_path[] = "/tmp/.apc.XXXXXX";
mktemp(
lock_path);
(*lock) = open(lock_path, O_RDWR|O_CREAT, 0666);
if((*lock) > 0 ) {
unlink(
lock_path);
return 1;
} else {
return 0;
}
}
# endif
#else
{
/* SPIN */
lock->state = 0;
return 1;
}
#endif
#else
lock = (apc_lock_t *)apc_windows_cs_create((apc_windows_cs_rwlock_t *)lock);
return (NULL != lock);
#endif
}
PHP_APCU_API zend_bool apc_lock_rlock(apc_lock_t *lock) {
#ifndef PHP_WIN32
#ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
pthread_mutex_lock(lock);
# else
pthread_rwlock_rdlock(lock);
# endif
# else
{
/* FCNTL */
apc_fcntl_call((*lock), F_SETLKW, F_RDLCK, 0, SEEK_SET, 0);
}
# endif
#else
{
/* SPIN */
apc_lock_get(lock);
}
#endif
#else
apc_windows_cs_rdlock((apc_windows_cs_rwlock_t *)lock);
#endif
return 1;
}
PHP_APCU_API zend_bool apc_lock_wlock(apc_lock_t *lock) {
#ifndef PHP_WIN32
#ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
pthread_mutex_lock(lock);
# else
pthread_rwlock_wrlock(lock);
# endif
# else
{
/* FCNTL */
apc_fcntl_call((*lock), F_SETLKW, F_WRLCK, 0, SEEK_SET, 0);
}
# endif
#else
{
/* SPIN */
apc_lock_get(lock);
}
#endif
#else
apc_windows_cs_lock((apc_windows_cs_rwlock_t *)lock);
#endif
return 1;
}
PHP_APCU_API zend_bool apc_lock_wunlock(apc_lock_t *lock) {
#ifndef PHP_WIN32
#ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
pthread_mutex_unlock(lock);
# else
pthread_rwlock_unlock(lock);
# endif
# else
{
/* FCNTL */
apc_fcntl_call((*lock), F_SETLKW, F_UNLCK, 0, SEEK_SET, 0);
}
# endif
#else
{
/* SPIN */
apc_lock_release(lock);
}
#endif
#else
apc_windows_cs_unlock_wr((apc_windows_cs_rwlock_t *)lock);
#endif
return 1;
}
PHP_APCU_API zend_bool apc_lock_runlock(apc_lock_t *lock) {
#ifndef PHP_WIN32
#ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
pthread_mutex_unlock(lock);
# else
pthread_rwlock_unlock(lock);
# endif
# else
{
/* FCNTL */
apc_fcntl_call((*lock), F_SETLKW, F_UNLCK, 0, SEEK_SET, 0);
}
# endif
#else
{
/* SPIN */
apc_lock_release(lock);
}
#endif
#else
apc_windows_cs_unlock_rd((apc_windows_cs_rwlock_t *)lock);
#endif
return 1;
}
PHP_APCU_API void apc_lock_destroy(apc_lock_t *lock) {
#ifndef PHP_WIN32
#ifndef APC_SPIN_LOCK
# ifndef APC_FCNTL_LOCK
# ifdef APC_LOCK_RECURSIVE
/* nothing */
# else
/* nothing */
# endif
# else
{
/* FCNTL */
close ((*lock));
}
# endif
#endif
#else
apc_windows_cs_destroy((apc_windows_cs_rwlock_t *)lock);
#endif
}
#endif
|