Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 1 | # 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 Warsaw | 5b45664 | 1997-01-13 22:54:27 +0000 | [diff] [blame] | 5 | from test_support import verbose |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 6 | import whrandom |
| 7 | import thread |
| 8 | import time |
| 9 | |
| 10 | mutex = thread.allocate_lock() |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 11 | whmutex = thread.allocate_lock() # for calls to whrandom |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 12 | running = 0 |
| 13 | done = thread.allocate_lock() |
| 14 | done.acquire() |
| 15 | |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 16 | numtasks = 10 |
| 17 | |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 18 | def task(ident): |
| 19 | global running |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 20 | whmutex.acquire() |
| 21 | delay = whrandom.random() * numtasks |
| 22 | whmutex.release() |
Barry Warsaw | af0a1a6 | 1996-12-16 23:42:35 +0000 | [diff] [blame] | 23 | if verbose: |
Guido van Rossum | c864ad6 | 1997-07-18 23:50:22 +0000 | [diff] [blame] | 24 | print 'task', ident, 'will run for', round(delay, 1), 'sec' |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 25 | time.sleep(delay) |
Barry Warsaw | af0a1a6 | 1996-12-16 23:42:35 +0000 | [diff] [blame] | 26 | if verbose: |
| 27 | print 'task', ident, 'done' |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 28 | mutex.acquire() |
| 29 | running = running - 1 |
| 30 | if running == 0: |
| 31 | done.release() |
| 32 | mutex.release() |
| 33 | |
| 34 | next_ident = 0 |
| 35 | def newtask(): |
| 36 | global next_ident, running |
| 37 | mutex.acquire() |
| 38 | next_ident = next_ident + 1 |
Barry Warsaw | af0a1a6 | 1996-12-16 23:42:35 +0000 | [diff] [blame] | 39 | if verbose: |
| 40 | print 'creating task', next_ident |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 41 | thread.start_new_thread(task, (next_ident,)) |
| 42 | running = running + 1 |
| 43 | mutex.release() |
| 44 | |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 45 | for i in range(numtasks): |
Guido van Rossum | cc54417 | 1994-04-14 20:28:41 +0000 | [diff] [blame] | 46 | newtask() |
| 47 | |
| 48 | print 'waiting for all tasks to complete' |
| 49 | done.acquire() |
| 50 | print 'all tasks done' |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 51 | |
| 52 | class 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 | |
| 78 | numtrips = 3 |
| 79 | def 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 Warsaw | af0a1a6 | 1996-12-16 23:42:35 +0000 | [diff] [blame] | 91 | if verbose: |
Guido van Rossum | c864ad6 | 1997-07-18 23:50:22 +0000 | [diff] [blame] | 92 | print 'task', ident, 'will run for', round(delay, 1), 'sec' |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 93 | time.sleep(delay) |
Barry Warsaw | af0a1a6 | 1996-12-16 23:42:35 +0000 | [diff] [blame] | 94 | if verbose: |
| 95 | print 'task', ident, 'entering barrier', i |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 96 | bar.enter() |
Barry Warsaw | af0a1a6 | 1996-12-16 23:42:35 +0000 | [diff] [blame] | 97 | if verbose: |
| 98 | print 'task', ident, 'leaving barrier', i |
Guido van Rossum | d3b6842 | 1994-05-23 12:17:36 +0000 | [diff] [blame] | 99 | mutex.acquire() |
| 100 | running = running - 1 |
| 101 | if running == 0: |
| 102 | done.release() |
| 103 | mutex.release() |
| 104 | |
| 105 | print '\n*** Barrier Test ***' |
| 106 | if done.acquire(0): |
| 107 | raise ValueError, "'done' should have remained acquired" |
| 108 | bar = barrier(numtasks) |
| 109 | running = numtasks |
| 110 | for i in range(numtasks): |
| 111 | thread.start_new_thread(task2, (i,)) |
| 112 | done.acquire() |
| 113 | print 'all tasks done' |