-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmutex.cc
More file actions
106 lines (80 loc) · 1.92 KB
/
mutex.cc
File metadata and controls
106 lines (80 loc) · 1.92 KB
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
#include "mutex.h"
#include <fcntl.h>
#include <unistd.h>
namespace tinyco {
int FileMtx::InitMtx(void *arg) {
if (!arg) return -1;
std::string lf = static_cast<char *>(arg);
fd_ =
open(lf.c_str(), O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd_ != -1) {
const char *byte_to_write = "*";
for (auto byte_count = 0; byte_count < 10; byte_count++) {
write(fd_, byte_to_write, 1);
}
// trick: no need to remove it
unlink(lf.c_str());
return 0;
}
return -1;
}
int FileMtx::TryLock() {
struct flock fl = {0};
fl.l_type = F_WRLCK;
fl.l_whence = SEEK_SET;
if (fcntl(fd_, F_SETLK, &fl) == -1) {
return -1;
}
return 0;
}
int FileMtx::Unlock() {
struct flock fl = {0};
fl.l_type = F_UNLCK;
fl.l_whence = SEEK_SET;
if (fcntl(fd_, F_SETLK, &fl) == -1) {
return -1;
}
return 0;
}
#if (__amd64__ || __amd64)
inline uint64_t AtomicCompareAndSet(uint64_t *a, uint64_t old, uint64_t n) {
u_char res;
__asm__ volatile(
" cmpxchgq %3, %1; "
" sete %0; "
: "=a"(res)
: "m"(*a), "a"(old), "r"(n)
: "cc", "memory");
return res;
}
#else
inline uint64_t AtomicCompareAndSet(uint64_t *a, uint64_t old, uint64_t n) {
*a = n;
return n;
}
#endif
AtomicMtx::~AtomicMtx() {
if (ptr_) munmap(ptr_, sizeof(*ptr_));
}
int AtomicMtx::InitMtx(void *arg) {
ptr_ = (uint64_t *)mmap(NULL, sizeof(uint64_t), PROT_READ | PROT_WRITE,
MAP_ANON | MAP_SHARED, -1, 0);
*ptr_ = 0;
return 0;
}
int AtomicMtx::TryLock() {
if (!ptr_) return -1;
return (*ptr_ == 0 && AtomicCompareAndSet(ptr_, 0, getpid())) ? 0 : -1;
}
int AtomicMtx::Unlock() {
if (!ptr_) return -1;
return AtomicCompareAndSet(ptr_, getpid(), 0) ? 0 : -1;
}
int AtomicMtx::ForcedUnlockIfNeed(void *check_data) {
if (*ptr_ == *reinterpret_cast<uint64_t *>(check_data)) {
*ptr_ = 0;
return 0;
}
return 0;
}
}