我正在开发一个应该由 Swoole 扩展来实现的新项目。
这是 swoole 锁的文档: https ://www.swoole.co.uk/docs/modules/swoole-lock
以下是支持的锁类型:
SWOOLE_FILELOCK: file lock
SWOOLE_RWLOCK: read write lock
SWOOLE_SEM: Linux semaphore
SWOOLE_MUTEX: Mutex
SWOOLE_SPINLOCK: spin lock
这是我的问题:
为什么在尝试获取锁时只有 SWOOLE_SPINLOCK 有效,而所有其他锁都返回 false?
在 SWOOLE_RWLOCK 模式下如何锁定读或写,以及如何释放读或写锁?文档只提到了获取读锁(正如我在 #1 中所说,它总是返回 false)。
执行结果:
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)
我没有检查 SWOOLE_FILELOCK 模式,因为它试图锁定磁盘上的文件,这不是该项目的选项。
另外,这 5 个常量似乎都没有定义,所以我在上面的示例中使用了它们对应的整数值。
我在 macOS Sierra 上使用最新版本的 PHP 7.2.4 和 Swoole 2.1.1。这是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) 配置命令(php从源码编译):
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 配置命令(swoole 源码编译):
./configure --enable-debug --enable-openssl=/usr/local/Cellar/openssl/1.0.2o_1/include --enable-async-redis --enable-mysqlnd