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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
|
%%Page: (1) 1
%%BeginPageSetup
/pagesave save def
%%EndPageSetup
iso1dict begin
(Test Files) 35.25 water
gsave
llx lly 12 add translate
/v 0 store
/x0 x v get 4.703931 add sx cw mul add store
/y0 y v get bfs th add sub store
x0 y0 moveto
(# Mutual exclusion -- for use with module sched) c n
() p n
(# A mutex has two pieces of state -- a 'locked' bit and a queue.) c n
(# When the mutex is not locked, the queue is empty.) N
(# Otherwise, the queue contains 0 or more \(function, argument\) pairs) N
(# representing functions \(or methods\) waiting to acquire the lock.) N
(# When the mutex is unlocked while the queue is not empty,) N
(# the first queue entry is removed and its function\(argument\) pair called,) N
(# implying it now has the lock.) N
(#) N
(# Of course, no multi-threading is implied -- hence the funny interface) N
(# for lock, where a function is called once the lock is aquired.) N
(#) N
(class) K
( mutex:) p n
() S 8 T () S
(#) c n
() p 8 T () S
(# Create a new mutex -- initially unlocked) c n
() p 8 T () S
(#) c n
() p 8 T () S
(def) K
( __init__\(self\):) p n
() S 8 T () S 16 T (self.locked = 0) N
() S 8 T () S 16 T (self.queue = []) N
() S 8 T () S
(#) c n
() p 8 T () S
(# Test the locked bit of the mutex) c n
() p 8 T () S
(#) c n
() p 8 T () S
(def) K
( test\(self\):) p n
() S 8 T () S 16 T () S
(return) K
( self.locked) p n
() S 8 T () S
(#) c n
() p 8 T () S
(# Atomic test-and-set -- grab the lock if it is not set,) c n
() p 8 T () S
(# return true if it succeeded) c n
() p 8 T () S
(#) c n
() p 8 T () S
(def) K
( testandset\(self\):) p n
() S 8 T () S 16 T () S
(if) K
( ) p
(not) K
( self.locked:) p n
() S 8 T () S 16 T () S 24 T (self.locked = 1) N
() S 8 T () S 16 T () S 24 T () S
(return) K
( 1) p n
() S 8 T () S 16 T () S
(else) K
(:) p n
() S 8 T () S 16 T () S 24 T () S
(return) K
( 0) p n
() S 8 T () S
(#) c n
() p 8 T () S
(# Lock a mutex, call the function with supplied argument) c n
() p 8 T () S
(# when it is acquired.) c n
() p 8 T () S
(# If the mutex is already locked, place function and argument) c n
() p 8 T () S
(# in the queue.) c n
() p 8 T () S
(#) c n
() p 8 T () S
(def) K
( lock\(self, function, argument\):) p n
() S 8 T () S 16 T (""") S
(Take it.) str
(""") p n
() S 8 T () S 16 T () S
(if) K
( self.testandset\(\):) p n
() S 8 T () S 16 T () S 24 T (function\(argument\)) N
() S 8 T () S 16 T () S
(else) K
(:) p n
() S 8 T () S 16 T () S 24 T (self.queue.append\(\(function, argument\)\)) N
() S 8 T () S
(#) c n
() p 8 T () S
(# Unlock a mutex. If the queue is not empty, call the next) c n
() p 8 T () S
(# function with its argument.) c n
() p 8 T () S
(#) c n
() p 8 T () S
(def) K
( unlock\(self\):) p n
() S 8 T () S 16 T (""") S
(Release.) str
(""") p n
() S 8 T () S 16 T () S
(if) K
( self.queue:) p n
() S 8 T () S 16 T () S 24 T (function, argument = self.queue[0]) N
() S 8 T () S 16 T () S 24 T () S
(del) K
( self.queue[0]) p n
() S 8 T () S 16 T () S 24 T (function\(argument\)) N
() S 8 T () S 16 T () S
(else) K
(:) p n
() S 8 T () S 16 T () S 24 T (self.locked = 0) N
() S 8 T () S
(#) c n
(mutex.py) (right) (1) title
border
grestore
(The room for the header) rhead
(Printed by Somebody from a machine) (Right) (1) footer
end % of iso1dict
pagesave restore
showpage
%%Trailer
end
%%EOF
|