This is the mail archive of the
libc-alpha@sources.redhat.com
mailing list for the glibc project.
Re-use of user-defined stacks for threads
- To: libc-alpha at sources dot redhat dot com
- Subject: Re-use of user-defined stacks for threads
- From: Wolfram Gloger <wmglo at dent dot med dot uni-muenchen dot de>
- Date: 25 Nov 2000 11:33:41 -0000
Hello,
When providing a user-defined stack, e.g. one allocated with malloc(),
for a thread T, I understand I can re-use this stack immediately after
pthread_join(T) has returned successfully. (This is not explicit in
the spec, but I consider it quite obvious.)
LinuxThreads breaks this, because the actual freeing of the thread
resources within the stack can occur _after_ pthread_join() has
returned. Below is a fix.
Regards,
Wolfram.
2000-11-25 Wolfram Gloger <wg@malloc.de>
* join.c (pthread_join): For a user-defined stack, wait until the
thread resources have actually been freed by the manager thread
before returning.
* manager.c (pthread_free): Wake up the joining thread if the
stack is user-defined.
--- linuxthreads/join.c.orig Wed Jun 21 20:11:00 2000
+++ linuxthreads/join.c Sat Nov 25 11:45:52 2000
@@ -111,6 +111,7 @@
pthread_descr th;
pthread_extricate_if extr;
int already_canceled = 0;
+ int userstack;
/* Set up extrication interface */
extr.pu_object = handle;
@@ -161,6 +162,8 @@
}
/* Get return value */
if (thread_return != NULL) *thread_return = th->p_retval;
+ userstack = th->p_userstack;
+ th->p_joining = userstack ? self : NULL;
__pthread_unlock(&handle->h_lock);
/* Send notification to thread manager */
if (__pthread_manager_request >= 0) {
@@ -169,6 +172,12 @@
request.req_args.free.thread_id = thread_id;
__libc_write(__pthread_manager_request,
(char *) &request, sizeof(request));
+ if (userstack) {
+ /* Must wait for pthread_free() to actually have completed,
+ because the stack can be reused immediately after
+ pthread_join() returns. */
+ suspend(self);
+ }
}
return 0;
}
--- linuxthreads/manager.c.orig Thu Sep 7 20:56:28 2000
+++ linuxthreads/manager.c Sat Nov 25 11:59:25 2000
@@ -715,6 +715,13 @@
munmap (guardaddr, stacksize + guardsize);
#endif
}
+ else
+ {
+ /* pthread_join() may be waiting for the stack to have been actually
+ freed. */
+ if (th->p_joining)
+ restart(th->p_joining);
+ }
}
/* Handle threads that have exited */