@@ -27,6 +27,7 @@ static struct PyModuleDef thread_module;
27
27
struct module_thread {
28
28
PyThreadState * tstate ;
29
29
int daemonic ;
30
+ int running ;
30
31
PyThread_type_lock lifetime_mutex ;
31
32
int lifetime_mutex_held ;
32
33
struct module_thread * prev ;
@@ -83,13 +84,24 @@ static void
83
84
module_threads_fini (struct module_threads * threads )
84
85
{
85
86
// Wait for all the threads to finalize.
86
- PyThread_acquire_lock (threads -> mutex , WAIT_LOCK );
87
- while (threads -> head != NULL ) {
88
- PyThread_release_lock (threads -> mutex );
89
- // XXX Sleep?
87
+ int done = 0 ;
88
+ while (!done ) {
89
+ done = 1 ;
90
90
PyThread_acquire_lock (threads -> mutex , WAIT_LOCK );
91
+ struct module_thread * mt = threads -> head ;
92
+ while (mt != NULL ) {
93
+ if (mt -> running ) {
94
+ assert (mt -> daemonic );
95
+ // It was killed with PyThread_exit_thread().
96
+ }
97
+ else {
98
+ done = 0 ;
99
+ break ;
100
+ }
101
+ mt = mt -> next ;
102
+ }
103
+ PyThread_release_lock (threads -> mutex );
91
104
}
92
- PyThread_release_lock (threads -> mutex );
93
105
94
106
PyThread_free_lock (threads -> mutex );
95
107
}
@@ -147,6 +159,7 @@ add_module_thread(struct module_threads *threads,
147
159
}
148
160
mt -> tstate = tstate ;
149
161
mt -> daemonic = daemonic ;
162
+ mt -> running = 0 ;
150
163
mt -> prev = NULL ;
151
164
mt -> next = NULL ;
152
165
@@ -192,13 +205,17 @@ module_thread_starting(struct module_thread *mt)
192
205
// when add_module_thread() was called.
193
206
PyThread_acquire_lock (mt -> lifetime_mutex , WAIT_LOCK );
194
207
mt -> lifetime_mutex_held = 1 ;
208
+
209
+ mt -> running = 1 ;
195
210
}
196
211
197
212
static void
198
213
module_thread_finished (struct module_thread * mt )
199
214
{
200
215
mt -> tstate -> interp -> threads .count -- ;
201
216
217
+ mt -> running = 0 ;
218
+
202
219
// Notify other threads that this one is done.
203
220
// XXX Do it explicitly here rather than via tstate.on_delete().
204
221
}
0 commit comments