Browse Source

升级workerman主程序

walkor 11 năm trước cách đây
mục cha
commit
5e3e9d9354
1 tập tin đã thay đổi với 51 bổ sung0 xóa
  1. 51 0
      man/Core/Lib/Mutex.php

+ 51 - 0
man/Core/Lib/Mutex.php

@@ -0,0 +1,51 @@
+<?php
+namespace Man\Core\Lib;
+/**
+ * 锁
+ */
+class Mutex
+{
+    /**
+     * 共享内存key
+     * @var int
+     */
+    const SEM_KEY = 0x5655656;
+    
+    /**
+     * 信号量
+     * @var resource
+     */
+    private static $semFd = null;
+    
+    /**
+     * 获取写锁
+     * @return true
+     */
+    public static function get()
+    {
+        self::getSemFd() && sem_acquire(self::getSemFd());
+        return true;
+    }
+    
+    /**
+     * 释放写锁
+     * @return true
+     */
+    public static function release()
+    {
+        self::getSemFd() && sem_release(self::getSemFd());
+        return true;
+    }
+    
+    /**
+     * 获得SemFd
+     */
+    protected static function getSemFd()
+    {
+        if(!self::$semFd && extension_loaded('sysvsem'))
+        {
+            self::$semFd = sem_get(self::SEM_KEY);
+        }
+        return self::$semFd;
+    }
+}