Nantes Université

Skip to content
Extraits de code Groupes Projets
Valider f1cd0835 rédigé par Mikaël BRIDAY's avatar Mikaël BRIDAY
Parcourir les fichiers

ajout ou2semaphore, avec ou sans timeout.

parent 4e588144
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
*~
## Corrections..
tpBacQ*.py
## Vim...
.*.swp
##python
......
......@@ -21,15 +21,14 @@ class Semaphore():
if the value is > 0, it decrements the semaphore value and returns
otherwise, it blocks until value get > 0
"""
__synchro__.acquire() #get the lock
while self.value <= 0:
with __synchro__:
while self.value <= 0:
if sModel:
sModel.addWaitingThread(self,threading.currentThread().name)
__synchro__.wait()
self.value -=1
if sModel:
sModel.addWaitingThread(self,threading.currentThread().name)
__synchro__.wait()
self.value -=1
if sModel:
sModel.update(self)
__synchro__.release()
sModel.update(self)
def P(self):
""" same as 'acquire'. wrapper for historical reasons """
......@@ -39,44 +38,58 @@ class Semaphore():
""" release the semaphore.
the value is incremented and a waiting threads can be woken up. Non blocking call.
"""
__synchro__.acquire() #get the lock
#notify all so that they are blocked again and we can deduce
#the list of waiting threads.
__synchro__.notify_all()
self.value +=1
if sModel:
sModel.clearWaitingThread(self)
__synchro__.release()
with __synchro__:
#notify all so that they are blocked again and we can deduce
#the list of waiting threads.
__synchro__.notify_all()
self.value +=1
if sModel:
sModel.clearWaitingThread(self)
def V(self):
""" same as 'release'. wrapper for historical reasons """
self.release()
def ou2Semaphore(s1,s2):
def ou2Semaphore(s1,s2,timeout=None):
""" Wait for the first available semaphore between 2. If the 2 are available, s1 is chosen.
returns 1 if s1 is chosen, 2 if s2. This is a blocking call.
"""
if not isinstance(s1,Semaphore) or not isinstance(s2,Semaphore):
raise Exception('Input is not a semaphore in ou2Semaphore')
result = 0
__synchro__.acquire()
#one already available
if s1.value > 0:
result = 1
s1.value -=1
elif s2.value > 0:
result = 2
s2.value -=1
while result == 0: # we will block
__synchro__.wait()
with __synchro__:
#one already available
if s1.value > 0:
result = 1
s1.value -=1
if sModel:
sModel.update(s1)
elif s2.value > 0:
result = 2
s2.value -=1
__synchro__.release()
if sModel:
sModel.update(s2)
while result == 0: # we will block
if sModel:
threadName = threading.currentThread().name
sModel.addWaitingThread(s1,threadName)
sModel.addWaitingThread(s2,threadName)
noTimeout = __synchro__.wait(timeout)
if noTimeout:
if s1.value > 0:
result = 1
s1.value -=1
if sModel:
sModel.update(s1)
sModel.removeWaitingThread(s2,threading.currentThread().name)
elif s2.value > 0:
result = 2
s2.value -=1
if sModel:
sModel.removeWaitingThread(s1,threading.currentThread().name)
sModel.update(s2)
else: #timeout
result = 3 #timeout
return result
......@@ -55,6 +55,14 @@ class SemaphoreModel(QAbstractTableModel):
self.semaphores[idx] = (sem.name,sem.value,self.semaphores[idx][2])
self.dataChanged.emit(self.index(idx,0),self.index(idx,2))
def removeWaitingThread(self,sem,threadName):
with self.mutex:
idx = self.semaphoreNames[sem.name]
self.waitingThreads[sem.name].remove(threadName)
threads = ' - '.join(self.waitingThreads[sem.name])
self.semaphores[idx] = (sem.name,-len(self.waitingThreads[sem.name]),threads)
self.dataChanged.emit(self.index(idx,0),self.index(idx,2))
def addWaitingThread(self,sem,threadName):
with self.mutex:
idx = self.semaphoreNames[sem.name]
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Veuillez vous inscrire ou vous pour commenter