4

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:

  1. Why only SWOOLE_SPINLOCK works and all other locks returns false when trying to acquire the lock?

  2. 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
Davis.S
  • 41
  • 3
  • For 1. can you add a minimal example that fails? Also I'm not sure about that but can you check if you got a _php-zts_ build? F.e. `php -i | grep Thread` should yield `Thread Safety => enabled`. For 2. as far as I see in [swoole source for lock](https://github.com/swoole/swoole-src/blob/master/swoole_lock.c) and the [rwlock implementation](https://github.com/swoole/swoole-src/blob/master/src/lock/RWLock.c) which is based on `pthread_rwlock_*`, it's `$lock->lock_read()` for the read lock and `$lock->lock()` for the write lock, `$lock->unlock()` releases whatever lock is held by the thread. – makadev Apr 07 '18 at 13:10
  • Thank you @makadev for the reply and helping me. Thread Safety is enabled. I updated my question's details with sample codes and their execution results. – Davis.S Apr 07 '18 at 16:47
  • That's weird, the constants are declared in the same file as the methods... did you try `\SWOOLE_RWLOCK`, maybe a namespace issue. Also, did you try the OOP API? Like f.e. `$lock = new \Swoole\Lock(\Swoole\Lock::RWLOCK);`, not that it should be much different.. Another Question is, how did you setup php/swoole? Did you brew it with something like `brew install php72 --with-pear --with-thread-safety` and `pecl install swoole`? – makadev Apr 07 '18 at 18:33
  • @makadev, I tried OOP API just now, it seems that constants are defined but the result is the same and returns false. Also, Recently brew's php formula has changed and totally ruined my environment, so after two days of struggling, finally I gave up and I compiled php from source, and installed some extensions by pecl or by `phpize`/`make`/`make install`, (pecl was not able to install swoole). It seems that everything works fine (except swoole's locks). I added configure commands to the question. Thanks again. – Davis.S Apr 07 '18 at 19:28
  • I just recompiled swoole from source, nothing changed – Davis.S Apr 07 '18 at 19:56
  • I just downgraded swoole to 1.10.3 by using pecl, nothing changed – Davis.S Apr 07 '18 at 20:00
  • I tried to reproduce the Problem on my mac (Sierra). I used `phpbrew` (php from source too) due to new brew formulas not being able to make a zts build... installed php 7.2 with `phpbrew install php-7.2 +default+zts` and after that installed swoole 1.10.3 with `phpbrew ext install swoole`. I too can reproduce that for `\Swoole\Lock::RWLOCK` neither `lock` nor `lock_read` works. `\Swoole\Lock::MUTEX` works thought, I've tested `trylock` / `lock` / `unlock` and could even produce a deadlock. No clue why `pthread_rwlock_*` doesn't work. Maybe it's worth a bugreport. – makadev Apr 07 '18 at 23:27
  • Thank you so much @makadev, I really appreciate that you tried to solve and reproduce my problem. I tried to report the bug in github but it seems that github flagged my newly created account. I'll try again soon. Thank you again. – Davis.S Apr 08 '18 at 00:39

0 Answers0