1 /*
2   +----------------------------------------------------------------------+
3   | Swoole                                                               |
4   +----------------------------------------------------------------------+
5   | This source file is subject to version 2.0 of the Apache license,    |
6   | that is bundled with this package in the file LICENSE, and is        |
7   | available through the world-wide-web at the following url:           |
8   | http://www.apache.org/licenses/LICENSE-2.0.html                      |
9   | If you did not receive a copy of the Apache2.0 license and are unable|
10   | to obtain it through the world-wide-web, please send a note to       |
11   | license@swoole.com so we can mail you a copy immediately.            |
12   +----------------------------------------------------------------------+
13   | Author: Tianfeng Han  <mikan.tenny@gmail.com>                        |
14   |         Twosee  <twose@qq.com>                                       |
15   +----------------------------------------------------------------------+
16 */
17 
18 #pragma once
19 
20 #include "swoole.h"
21 #include "swoole_memory.h"
22 
23 #include <system_error>
24 
25 namespace swoole {
26 
27 class Lock {
28   public:
29     enum Type {
30         NONE,
31         RW_LOCK = 1,
32         FILE_LOCK = 2,
33         MUTEX = 3,
34         SEM = 4,
35         SPIN_LOCK = 5,
36         ATOMIC_LOCK = 6,
37     };
get_type()38     Type get_type() {
39         return type_;
40     }
~Lock()41     virtual ~Lock(){};
42     virtual int lock_rd() = 0;
43     virtual int lock() = 0;
44     virtual int unlock() = 0;
45     virtual int trylock_rd() = 0;
46     virtual int trylock() = 0;
47 
48   protected:
Lock()49     Lock() {
50         type_ = NONE;
51         shared_ = false;
52     }
53     enum Type type_;
54     bool shared_;
55 };
56 
57 struct MutexImpl;
58 
59 class Mutex : public Lock {
60     MutexImpl *impl;
61 
62   public:
63     enum Flag {
64         PROCESS_SHARED = 1,
65         ROBUST = 2,
66     };
67 
68     Mutex(int flags);
69     ~Mutex();
70     int lock_rd() override;
71     int lock() override;
72     int unlock() override;
73     int trylock_rd() override;
74     int trylock() override;
75     int lock_wait(int timeout_msec);
76 };
77 
78 #ifdef HAVE_RWLOCK
79 struct RWLockImpl;
80 
81 class RWLock : public Lock {
82     RWLockImpl *impl;
83 
84   public:
85     RWLock(int use_in_process);
86     ~RWLock();
87     int lock_rd() override;
88     int lock() override;
89     int unlock() override;
90     int trylock_rd() override;
91     int trylock() override;
92 };
93 #endif
94 
95 #ifdef HAVE_SPINLOCK
96 class SpinLock : public Lock {
97     pthread_spinlock_t *impl;
98 
99   public:
100     SpinLock(int use_in_process);
101     ~SpinLock();
102     int lock_rd() override;
103     int lock() override;
104     int unlock() override;
105     int trylock_rd() override;
106     int trylock() override;
107 };
108 #endif
109 }  // namespace swoole
110