-
Notifications
You must be signed in to change notification settings - Fork 0
/
sem.py
30 lines (27 loc) · 830 Bytes
/
sem.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
class Sem:
def __init__(self):
self.semaphores = {}
def sem_acquire(self, name):
if name in self.semaphores:
semaphore = self.semaphores[name]
if semaphore["count"] > 0:
semaphore["count"] -= 1
return True
else:
return False
else:
return False
def sem_release(self, name):
if name in self.semaphores:
semaphore = self.semaphores[name]
semaphore["count"] += 1
return True
else:
return False
def sem_create(self, name, initial_count):
if name in self.semaphores:
return False
else:
semaphore = {"count": initial_count}
self.semaphores[name] = semaphore
return True