Redesigned how cancellation unblocks a thread from internal
[kopensolaris-gnu/glibc.git] / linuxthreads / join.c
1 /* Linuxthreads - a simple clone()-based implementation of Posix        */
2 /* threads for Linux.                                                   */
3 /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr)              */
4 /*                                                                      */
5 /* This program is free software; you can redistribute it and/or        */
6 /* modify it under the terms of the GNU Library General Public License  */
7 /* as published by the Free Software Foundation; either version 2       */
8 /* of the License, or (at your option) any later version.               */
9 /*                                                                      */
10 /* This program is distributed in the hope that it will be useful,      */
11 /* but WITHOUT ANY WARRANTY; without even the implied warranty of       */
12 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        */
13 /* GNU Library General Public License for more details.                 */
14
15 /* Thread termination and joining */
16
17 #include <errno.h>
18 #include <sched.h>
19 #include <unistd.h>
20 #include "pthread.h"
21 #include "internals.h"
22 #include "spinlock.h"
23 #include "restart.h"
24
25 void pthread_exit(void * retval)
26 {
27   pthread_descr self = thread_self();
28   pthread_descr joining;
29   struct pthread_request request;
30
31   /* Reset the cancellation flag to avoid looping if the cleanup handlers
32      contain cancellation points */
33   THREAD_SETMEM(self, p_canceled, 0);
34   /* Call cleanup functions and destroy the thread-specific data */
35   __pthread_perform_cleanup();
36   __pthread_destroy_specifics();
37   /* Store return value */
38   __pthread_lock(THREAD_GETMEM(self, p_lock), self);
39   THREAD_SETMEM(self, p_retval, retval);
40   /* Say that we've terminated */
41   THREAD_SETMEM(self, p_terminated, 1);
42   /* See whether we have to signal the death.  */
43   if (THREAD_GETMEM(self, p_report_events))
44     {
45       /* See whether TD_DEATH is in any of the mask.  */
46       int idx = __td_eventword (TD_DEATH);
47       uint32_t mask = __td_eventmask (TD_DEATH);
48
49       if ((mask & (__pthread_threads_events.event_bits[idx]
50                    | THREAD_GETMEM(self,
51                                    p_eventbuf.eventmask).event_bits[idx]))
52           != 0)
53         {
54           /* Yep, we have to signal the death.  */
55           THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
56           THREAD_SETMEM(self, p_eventbuf.eventdata, self);
57           __pthread_last_event = self;
58
59           /* Now call the function to signal the event.  */
60           __linuxthreads_death_event();
61         }
62     }
63   /* See if someone is joining on us */
64   joining = THREAD_GETMEM(self, p_joining);
65   __pthread_unlock(THREAD_GETMEM(self, p_lock));
66   /* Restart joining thread if any */
67   if (joining != NULL) restart(joining);
68   /* If this is the initial thread, block until all threads have terminated.
69      If another thread calls exit, we'll be terminated from our signal
70      handler. */
71   if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
72     request.req_thread = self;
73     request.req_kind = REQ_MAIN_THREAD_EXIT;
74     __libc_write(__pthread_manager_request, (char *)&request, sizeof(request));
75     suspend(self);
76   }
77   /* Exit the process (but don't flush stdio streams, and don't run
78      atexit functions). */
79   _exit(0);
80 }
81
82 /* Function called by pthread_cancel to remove the thread from
83    waiting on a condition variable queue. */
84
85 static int join_extricate_func(void *obj, pthread_descr th)
86 {
87   volatile pthread_descr self = thread_self();
88   pthread_handle handle = obj;
89   pthread_descr jo;
90   int did_remove = 0;
91
92   __pthread_lock(&handle->h_lock, self);
93   jo = handle->h_descr;
94   did_remove = jo->p_joining != NULL;
95   jo->p_joining = NULL;
96   __pthread_unlock(&handle->h_lock);
97
98   return did_remove;
99 }
100
101 int pthread_join(pthread_t thread_id, void ** thread_return)
102 {
103   volatile pthread_descr self = thread_self();
104   struct pthread_request request;
105   pthread_handle handle = thread_handle(thread_id);
106   pthread_descr th;
107   pthread_extricate_if extr;
108   int already_canceled = 0;
109
110   /* Set up extrication interface */
111   extr.pu_object = handle;
112   extr.pu_extricate_func = join_extricate_func;
113
114   __pthread_lock(&handle->h_lock, self);
115   if (invalid_handle(handle, thread_id)) {
116     __pthread_unlock(&handle->h_lock);
117     return ESRCH;
118   }
119   th = handle->h_descr;
120   if (th == self) {
121     __pthread_unlock(&handle->h_lock);
122     return EDEADLK;
123   }
124   /* If detached or already joined, error */
125   if (th->p_detached || th->p_joining != NULL) {
126     __pthread_unlock(&handle->h_lock);
127     return EINVAL;
128   }
129   /* If not terminated yet, suspend ourselves. */
130   if (! th->p_terminated) {
131     /* Register extrication interface */
132     __pthread_set_own_extricate_if(self, &extr); 
133     if (!(THREAD_GETMEM(self, p_canceled)
134         && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
135       th->p_joining = self;
136     else
137       already_canceled = 1;
138     __pthread_unlock(&handle->h_lock);
139
140     if (already_canceled) {
141       __pthread_set_own_extricate_if(self, 0); 
142       pthread_exit(PTHREAD_CANCELED);
143     }
144
145     suspend(self);
146     /* Deregister extrication interface */
147     __pthread_set_own_extricate_if(self, 0); 
148
149     /* This is a cancellation point */
150     if (THREAD_GETMEM(self, p_woken_by_cancel)
151         && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
152       THREAD_SETMEM(self, p_woken_by_cancel, 0);
153       pthread_exit(PTHREAD_CANCELED);
154     }
155     __pthread_lock(&handle->h_lock, self);
156   }
157   /* Get return value */
158   if (thread_return != NULL) *thread_return = th->p_retval;
159   __pthread_unlock(&handle->h_lock);
160   /* Send notification to thread manager */
161   if (__pthread_manager_request >= 0) {
162     request.req_thread = self;
163     request.req_kind = REQ_FREE;
164     request.req_args.free.thread_id = thread_id;
165     __libc_write(__pthread_manager_request,
166                  (char *) &request, sizeof(request));
167   }
168   return 0;
169 }
170
171 int pthread_detach(pthread_t thread_id)
172 {
173   int terminated;
174   struct pthread_request request;
175   pthread_handle handle = thread_handle(thread_id);
176   pthread_descr th;
177
178   __pthread_lock(&handle->h_lock, NULL);
179   if (invalid_handle(handle, thread_id)) {
180     __pthread_unlock(&handle->h_lock);
181     return ESRCH;
182   }
183   th = handle->h_descr;
184   /* If already detached, error */
185   if (th->p_detached) {
186     __pthread_unlock(&handle->h_lock);
187     return EINVAL;
188   }
189   /* If already joining, don't do anything. */
190   if (th->p_joining != NULL) {
191     __pthread_unlock(&handle->h_lock);
192     return 0;
193   }
194   /* Mark as detached */
195   th->p_detached = 1;
196   terminated = th->p_terminated;
197   __pthread_unlock(&handle->h_lock);
198   /* If already terminated, notify thread manager to reclaim resources */
199   if (terminated && __pthread_manager_request >= 0) {
200     request.req_thread = thread_self();
201     request.req_kind = REQ_FREE;
202     request.req_args.free.thread_id = thread_id;
203     __libc_write(__pthread_manager_request,
204                  (char *) &request, sizeof(request));
205   }
206   return 0;
207 }