-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule.js
114 lines (90 loc) · 2.82 KB
/
module.js
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
111
112
113
114
const mutexAddon = require("./build/Release/mutex");
const mutexNameMaxLength = 31;
const mutexLocked = 35;
function createMutex(name, fileMode) {
if (name.length > mutexNameMaxLength) {
throw `mutex name length cannot be greater than ${mutexNameMaxLength}`;
}
const handle = Buffer.alloc(mutexAddon.sizeof_MutexHandle);
const res = mutexAddon.CreateMutex(name, fileMode, handle);
if (res !== 0) {
throw `could not create mutex for object ${name}: ${res}`;
}
return handle;
}
function openMutex(name) {
if (name.length > mutexNameMaxLength) {
throw `mutex name length cannot be greater than ${mutexNameMaxLength}`;
}
const handle = Buffer.alloc(mutexAddon.sizeof_MutexHandle);
const res = mutexAddon.OpenMutex(name, handle);
if (res !== 0) {
throw `could not open mutex for object ${name}: ${res}`;
}
return handle;
}
function tryLockMutex(handle) {
const res = mutexAddon.TryLockMutex(handle);
if (res === mutexLocked) {
throw `mutex is already locked`;
} else if (res !== 0) {
throw `could not wait for mutex: ${res}`;
}
}
function waitMutexAsync(handle, waitMs, callback) {
setImmediate(() => _waitMutexAsync(handle, waitMs, callback));
}
function _waitMutexAsync(handle, remainingTimeout, callback) {
const res = mutexAddon.TryLockMutex(handle);
if (res === 0) {
callback();
}
if (res === mutexLocked) {
if (remainingTimeout <= 0) {
callback(`mutex timeout expired`);
} else {
setTimeout(
() => _waitMutexAsync(handle, remainingTimeout - 20, callback),
20
);
}
} else if (res !== 0) {
callback(`could not wait for mutex: ${res}`);
}
}
function releaseMutex(handle) {
const res = mutexAddon.ReleaseMutex(handle);
if (res !== 0) {
throw `could not release mutex: ${res}`;
}
}
function closeMutex(handle) {
mutexAddon.CloseMutex(handle);
}
module.exports = {
createMutex,
openMutex,
tryLockMutex,
waitMutexAsync,
releaseMutex,
closeMutex,
mutexFileMode: {
S_IRWXU: 0000700 /* [XSI] RWX mask for owner */,
S_IRUSR: 0000400 /* [XSI] R for owner */,
S_IWUSR: 0000200 /* [XSI] W for owner */,
S_IXUSR: 0000100 /* [XSI] X for owner */,
/* Read, write, execute/search by group */
S_IRWXG: 0000070 /* [XSI] RWX mask for group */,
S_IRGRP: 0000040 /* [XSI] R for group */,
S_IWGRP: 0000020 /* [XSI] W for group */,
S_IXGRP: 0000010 /* [XSI] X for group */,
/* Read, write, execute/search by others */
S_IRWXO: 0000007 /* [XSI] RWX mask for other */,
S_IROTH: 0000004 /* [XSI] R for other */,
S_IWOTH: 0000002 /* [XSI] W for other */,
S_IXOTH: 0000001 /* [XSI] X for other */,
S_ISUID: 0004000 /* [XSI] set user id on execution */,
S_ISGID: 0002000 /* [XSI] set group id on execution */,
S_ISVTX: 0001000 /* [XSI] directory restrcted delete */,
},
};