在PHP中使用Swoole锁

时间:2018-04-06 23:18:18

标签: php mutex semaphore spinlock swoole

我正在开发一个应该由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

以下是我的问题:

  1. 为什么只有SWOOLE_SPINLOCK有效,而所有其他锁在尝试获取锁时都返回false?

  2. 如何锁定读取或写入,以及如何在SWOOLE_RWLOCK模式下释放读取或写入锁定?文档只谈到获取读锁(正如我在#1中所说,它总是返回false)。

  3. 执行结果:

    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个常量都没有定义,所以我在上面的示例中使用了相应的整数值。

    我正在使用最新版本的PHP 7.2.4&在macOS Sierra上的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
    

0 个答案:

没有答案