blob: 113135bef9d0f4de463f419667e9125875aae1f4 [file] [log] [blame]
Guido van Rossumcc544171994-04-14 20:28:41 +00001# Very rudimentary test of thread module
2
3# Create a bunch of threads, let each do some work, wait until all are done
4
Barry Warsaw5b456641997-01-13 22:54:27 +00005from test_support import verbose
Guido van Rossumcc544171994-04-14 20:28:41 +00006import whrandom
7import thread
8import time
9
10mutex = thread.allocate_lock()
Guido van Rossumd3b68421994-05-23 12:17:36 +000011whmutex = thread.allocate_lock() # for calls to whrandom
Guido van Rossumcc544171994-04-14 20:28:41 +000012running = 0
13done = thread.allocate_lock()
14done.acquire()
15
Guido van Rossumd3b68421994-05-23 12:17:36 +000016numtasks = 10
17
Guido van Rossumcc544171994-04-14 20:28:41 +000018def task(ident):
19 global running
Guido van Rossumd3b68421994-05-23 12:17:36 +000020 whmutex.acquire()
21 delay = whrandom.random() * numtasks
22 whmutex.release()
Barry Warsawaf0a1a61996-12-16 23:42:35 +000023 if verbose:
Guido van Rossumc864ad61997-07-18 23:50:22 +000024 print 'task', ident, 'will run for', round(delay, 1), 'sec'
Guido van Rossumcc544171994-04-14 20:28:41 +000025 time.sleep(delay)
Barry Warsawaf0a1a61996-12-16 23:42:35 +000026 if verbose:
27 print 'task', ident, 'done'
Guido van Rossumcc544171994-04-14 20:28:41 +000028 mutex.acquire()
29 running = running - 1
30 if running == 0:
31 done.release()
32 mutex.release()
33
34next_ident = 0
35def newtask():
36 global next_ident, running
37 mutex.acquire()
38 next_ident = next_ident + 1
Barry Warsawaf0a1a61996-12-16 23:42:35 +000039 if verbose:
40 print 'creating task', next_ident
Guido van Rossumcc544171994-04-14 20:28:41 +000041 thread.start_new_thread(task, (next_ident,))
42 running = running + 1
43 mutex.release()
44
Guido van Rossumd3b68421994-05-23 12:17:36 +000045for i in range(numtasks):
Guido van Rossumcc544171994-04-14 20:28:41 +000046 newtask()
47
48print 'waiting for all tasks to complete'
49done.acquire()
50print 'all tasks done'
Guido van Rossumd3b68421994-05-23 12:17:36 +000051
52class barrier:
53 def __init__(self, n):
54 self.n = n
55 self.waiting = 0
56 self.checkin = thread.allocate_lock()
57 self.checkout = thread.allocate_lock()
58 self.checkout.acquire()
59
60 def enter(self):
61 checkin, checkout = self.checkin, self.checkout
62
63 checkin.acquire()
64 self.waiting = self.waiting + 1
65 if self.waiting == self.n:
66 self.waiting = self.n - 1
67 checkout.release()
68 return
69 checkin.release()
70
71 checkout.acquire()
72 self.waiting = self.waiting - 1
73 if self.waiting == 0:
74 checkin.release()
75 return
76 checkout.release()
77
78numtrips = 3
79def task2(ident):
80 global running
81 for i in range(numtrips):
82 if ident == 0:
83 # give it a good chance to enter the next
84 # barrier before the others are all out
85 # of the current one
86 delay = 0.001
87 else:
88 whmutex.acquire()
89 delay = whrandom.random() * numtasks
90 whmutex.release()
Barry Warsawaf0a1a61996-12-16 23:42:35 +000091 if verbose:
Guido van Rossumc864ad61997-07-18 23:50:22 +000092 print 'task', ident, 'will run for', round(delay, 1), 'sec'
Guido van Rossumd3b68421994-05-23 12:17:36 +000093 time.sleep(delay)
Barry Warsawaf0a1a61996-12-16 23:42:35 +000094 if verbose:
95 print 'task', ident, 'entering barrier', i
Guido van Rossumd3b68421994-05-23 12:17:36 +000096 bar.enter()
Barry Warsawaf0a1a61996-12-16 23:42:35 +000097 if verbose:
98 print 'task', ident, 'leaving barrier', i
Guido van Rossumd3b68421994-05-23 12:17:36 +000099 mutex.acquire()
100 running = running - 1
101 if running == 0:
102 done.release()
103 mutex.release()
104
105print '\n*** Barrier Test ***'
106if done.acquire(0):
107 raise ValueError, "'done' should have remained acquired"
108bar = barrier(numtasks)
109running = numtasks
110for i in range(numtasks):
111 thread.start_new_thread(task2, (i,))
112done.acquire()
113print 'all tasks done'