- Timestamp:
- Feb 27, 2010 1:20:56 PM (15 years ago)
- Branches:
- axis_example, compt_changes, info-ops, master, version-3.01, version-3.02
- Children:
- f017447
- Parents:
- 7dc855c
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fedd/federation/synch_store.py
r7dc855c r5e3d889 10 10 deadlock threads waiting for the assignment. 11 11 """ 12 13 # Thrown if a key is assigned more than once 12 14 class CollisionError(Exception): pass 15 16 # Thrown if a non-existant key is deleted 13 17 class BadDeletionError(Exception): pass 14 18 19 # A value in the dict. Each has its own condition (and lock) for threads 20 # to wait on. 15 21 class synch_value: 16 def __init__(self, key, value=None):17 self.key = key22 # The synch_value constructor. 23 def __init__(self, value=None): 18 24 self.value = value 19 25 self.condition = Condition() 20 26 27 # Debugging representation 21 28 def __str__(self): 22 return "key %s value %s cond %s" % \ 23 (self.key, self.value, self.condition) 29 return "value %s cond %s" % (self.value, self.condition) 24 30 31 # The synch_store constructor. Note that the store has its own lock for 32 # serializing additions to the dict. 25 33 def __init__(self): 26 34 self.main_lock = Lock() 27 35 self.values = { } 28 36 37 # Set the value in the dict. If threads are waiting, wake them. 29 38 def set_value(self, key, value): 30 39 self.main_lock.acquire() … … 41 50 raise synch_store.CollisionError("%s already set" % key) 42 51 else: 43 self.values[key] = synch_store.synch_value( key,value)52 self.values[key] = synch_store.synch_value(value) 44 53 self.main_lock.release() 45 54 55 # Ask for the value. If wait is true, wait until the key is set, if it is 56 # not already. If wait is false, poll. 46 57 def get_value(self, key, wait=True): 47 58 self.main_lock.acquire() 48 59 if not self.values.has_key(key): 49 self.values[key] = synch_store.synch_value( key)60 self.values[key] = synch_store.synch_value() 50 61 v = self.values[key] 51 62 v.condition.acquire() … … 59 70 return rv 60 71 72 # Remove the key from the shared store. If the key is not present or has 73 # never been set, fail. Otherwise, wake any waiting threads (there 74 # shouldn't be any) and remove the value and key from the store. 61 75 def del_value(self, key): 62 76 self.main_lock.acquire() … … 75 89 raise synch_store.BadDeletionError("%s does not exist yet" % key) 76 90 91 # A debugging representation 77 92 def __str__(self): 78 93 rv = "" … … 80 95 for k, v in self.values.items(): 81 96 self.values[k].condition.acquire() 82 rv += "%s: %s " % ( k, str(self.values[k]))97 rv += "%s: %s\n" % ( k, str(self.values[k])) 83 98 self.values[k].condition.release() 84 99 self.main_lock.release()
Note: See TracChangeset
for help on using the changeset viewer.