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