Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 1 | """Mutual exclusion -- for use with module sched |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 3 | A mutex has two pieces of state -- a 'locked' bit and a queue. |
| 4 | When the mutex is not locked, the queue is empty. |
| 5 | Otherwise, the queue contains 0 or more (function, argument) pairs |
| 6 | representing functions (or methods) waiting to acquire the lock. |
| 7 | When the mutex is unlocked while the queue is not empty, |
| 8 | the first queue entry is removed and its function(argument) pair called, |
| 9 | implying it now has the lock. |
| 10 | |
| 11 | Of course, no multi-threading is implied -- hence the funny interface |
| 12 | for lock, where a function is called once the lock is aquired. |
| 13 | """ |
| 14 | |
Guido van Rossum | ce08448 | 1991-12-26 13:06:29 +0000 | [diff] [blame] | 15 | class mutex: |
Guido van Rossum | 7bc817d | 1993-12-17 15:25:27 +0000 | [diff] [blame] | 16 | def __init__(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 17 | """Create a new mutex -- initially unlocked.""" |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 18 | self.locked = 0 |
| 19 | self.queue = [] |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 20 | |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 21 | def test(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 22 | """Test the locked bit of the mutex.""" |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 23 | return self.locked |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 25 | def testandset(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 26 | """Atomic test-and-set -- grab the lock if it is not set, |
| 27 | return true if it succeeded.""" |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 28 | if not self.locked: |
| 29 | self.locked = 1 |
| 30 | return 1 |
| 31 | else: |
| 32 | return 0 |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 89a7869 | 1992-12-14 12:57:56 +0000 | [diff] [blame] | 34 | def lock(self, function, argument): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 35 | """Lock a mutex, call the function with supplied argument |
| 36 | when it is acquired. If the mutex is already locked, place |
| 37 | function and argument in the queue.""" |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 38 | if self.testandset(): |
| 39 | function(argument) |
| 40 | else: |
Guido van Rossum | b7f48e3 | 1996-10-08 14:06:17 +0000 | [diff] [blame] | 41 | self.queue.append((function, argument)) |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 43 | def unlock(self): |
Guido van Rossum | 54f22ed | 2000-02-04 15:10:34 +0000 | [diff] [blame] | 44 | """Unlock a mutex. If the queue is not empty, call the next |
| 45 | function with its argument.""" |
Guido van Rossum | e2e162e | 1991-04-21 19:32:43 +0000 | [diff] [blame] | 46 | if self.queue: |
| 47 | function, argument = self.queue[0] |
| 48 | del self.queue[0] |
| 49 | function(argument) |
| 50 | else: |
| 51 | self.locked = 0 |