4

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

ここに私の質問があります:

  1. ロックを取得しようとすると、SWOLE_SPINLOCK のみが機能し、他のすべてのロックが false を返すのはなぜですか?

  2. 読み取りまたは書き込みのためにロックする方法、および SWOLE_RWLOCK モードで読み取りまたは書き込みロックを解放する方法は? ドキュメントは、読み取りロックの取得についてのみ述べています(#1で述べたように、常にfalseを返します)。


実行結果:

SWOLE_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)

SWOLE_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!

SWOLE_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

SWOLE_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) configure コマンド (ソースからコンパイルされた 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 configure コマンド (ソースからコンパイルされた swoole):

./configure --enable-debug --enable-openssl=/usr/local/Cellar/openssl/1.0.2o_1/include --enable-async-redis --enable-mysqlnd
4

0 に答える 0