I'm working on a new project which should be implemented by Swoole extension.
Here is the docs for swoole locks: https://www.swoole.co.uk/docs/modules/swoole-lock
Here is supported lock types:
SWOOLE_FILELOCK: file lock
SWOOLE_RWLOCK: read write lock
SWOOLE_SEM: Linux semaphore
SWOOLE_MUTEX: Mutex
SWOOLE_SPINLOCK: spin lock
Here is my questions:
Why only SWOOLE_SPINLOCK works and all other locks returns false when trying to acquire the lock?
How to lock for read or for write, and how to release read or write locks in SWOOLE_RWLOCK mode? The docs only said about acquiring read locks (which as I said in #1 it always returns false).
Execution Results:
SWOOLE_RWLOCK:
$lock_1 = new swoole_lock(1);
$lock_2 = new swoole_lock(1);
var_dump($lock_1->lock_read());
// result: bool(false)
var_dump($lock_2->lock());
// result: bool(false)
SWOOLE_MUTEX:
$lock = new swoole_lock(3);
var_dump($lock->lock());
// result: bool(true)
// It's funny that this lock was not working when I asked the question and now it's working!
SWOOLE_SEM:
$lock = new swoole_lock(4);
var_dump($lock->lock());
// Result: Assertion failed: (key != 0), function swSem_create, file /Users/***USER***/Desktop/pecl/swoole-src/src/lock/Semaphore.c, line 27. Abort trap: 6
SWOOLE_SEM:
$lock = new swoole_lock(5);
var_dump($lock->lock());
// result: bool(true)
I havn't checked the SWOOLE_FILELOCK mode as It tries to lock a file on disk, which is not an option for this project.
Also, It seems that all these 5 constants are not defined, so I used their corresponding integer values in above samples.
I'm using latest version of PHP 7.2.4 & Swoole 2.1.1 on macOS Sierra. Here is phpinfo():
swoole
swoole support => enabled
Version => 2.1.1
Author => tianfeng.han[email: mikan.tenny@gmail.com]
coroutine => enabled
kqueue => enabled
rwlock => enabled
async redis client => enabled
async http/websocket client => enabled
pcre => enabled
zlib => enabled
mysqlnd => enabled
phpinfo (php -i) configure command (php compiled from source):
Configure Command => './configure' '--prefix=/usr/local/php' '--with-bz2' '--with-zlib' '--enable-zip' '--disable-cgi' '--enable-soap' '--with-openssl=/usr/local/Cellar/openssl/1.0.2o_1' '--with-libedit=/usr/local/Cellar/libedit/20170329-3.1' '--with-curl' '--enable-ftp' '--enable-mysqlnd' '--with-mysqli=mysqlnd' '--with-pdo-mysql=mysqlnd' '--enable-sockets' '--enable-pcntl' '--with-pspell' '--with-gd' '--enable-exif' '--with-jpeg-dir=/usr/local/Cellar/jpeg/9c' '--with-png-dir=/usr/local/Cellar/libpng/1.6.34' '--with-vpx-dir=/usr/local/Cellar/libvpx/1.6.1' '--with-freetype-dir=/usr/local/Cellar/freetype/2.9' '--with-zlib-dir=/usr/local/Cellar/zlib/1.2.11' '--with-xsl' '--enable-bcmath' '--enable-mbstring' '--enable-calendar' '--enable-simplexml' '--enable-json' '--enable-hash' '--enable-session' '--enable-xml' '--enable-wddx' '--enable-opcache' '--with-pcre-regex' '--with-config-file-path=/Users/***USER***/localhost/Server/php-configs' '--with-config-file-scan-dir=/Users/***USER***/localhost/Server/php-configs/extensions' '--enable-cli' '--enable-maintainer-zts' '--with-tsrm-pthreads' '--enable-debug' '--enable-fpm' '--with-fpm-user=www-data' '--with-fpm-group=www-data' '--with-imap-ssl' '--with-pear' '--with-xmlrpc' '--with-ds' '--with-igbinary' '--with-imagick' '--with-memcached' '--with-mustache' '--with-swoole'
swoole configure command (swoole compiled from source):
./configure --enable-debug --enable-openssl=/usr/local/Cellar/openssl/1.0.2o_1/include --enable-async-redis --enable-mysqlnd