-
Notifications
You must be signed in to change notification settings - Fork 3
/
SemLock.py
35 lines (29 loc) · 923 Bytes
/
SemLock.py
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
from sysv_ipc import *
import Config
class SemLock:
sems = None
@staticmethod
def Init():
semkey = Config.Config.GetInt("PUBLIC_SEMID", 0x54188)
try:
SemLock.sems = [Semaphore(semkey)]
except ExistentialError:
SemLock.sems = [Semaphore(semkey, flags = IPC_CREAT | IPC_EXCL, mode = 0700)];
for i in range(len(sems)):
SemLock.sems[i].release()
@staticmethod
def Lock(lockid, timeout = None):
if (SemLock.sems == None):
SemLock.Init()
if (lockid != 0):
Log.error("Invalid lockid!")
lockid = 0
SemLock.sems[lockid].acquire(timeout)
@staticmethod
def Unlock(lockid):
if (SemLock.sems == None):
SemLock.Init()
if (lockid != 0):
Log.error("Invalid lockid!")
lockid = 0
SemLock.sems[lockid].release()