(otherlibs): Added.
[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 <stdlib.h>
20 #include <unistd.h>
21 #include "pthread.h"
22 #include "internals.h"
23 #include "spinlock.h"
24 #include "restart.h"
25
26 void pthread_exit(void * retval)
27 {
28   __pthread_do_exit (retval, CURRENT_STACK_FRAME);
29 }
30
31 void __pthread_do_exit(void *retval, char *currentframe)
32 {
33   pthread_descr self = thread_self();
34   pthread_descr joining;
35   struct pthread_request request;
36
37   /* Reset the cancellation flag to avoid looping if the cleanup handlers
38      contain cancellation points */
39   THREAD_SETMEM(self, p_canceled, 0);
40   /* Call cleanup functions and destroy the thread-specific data */
41   __pthread_perform_cleanup(currentframe);
42   __pthread_destroy_specifics();
43   /* Store return value */
44   __pthread_lock(THREAD_GETMEM(self, p_lock), self);
45   THREAD_SETMEM(self, p_retval, retval);
46   /* See whether we have to signal the death.  */
47   if (THREAD_GETMEM(self, p_report_events))
48     {
49       /* See whether TD_DEATH is in any of the mask.  */
50       int idx = __td_eventword (TD_DEATH);
51       uint32_t mask = __td_eventmask (TD_DEATH);
52
53       if ((mask & (__pthread_threads_events.event_bits[idx]
54                    | THREAD_GETMEM_NC(self,
55                                       p_eventbuf.eventmask.event_bits[idx])))
56           != 0)
57         {
58           /* Yep, we have to signal the death.  */
59           THREAD_SETMEM(self, p_eventbuf.eventnum, TD_DEATH);
60           THREAD_SETMEM(self, p_eventbuf.eventdata, self);
61           __pthread_last_event = self;
62
63           /* Now call the function to signal the event.  */
64           __linuxthreads_death_event();
65         }
66     }
67   /* Say that we've terminated */
68   THREAD_SETMEM(self, p_terminated, 1);
69   /* See if someone is joining on us */
70   joining = THREAD_GETMEM(self, p_joining);
71   __pthread_unlock(THREAD_GETMEM(self, p_lock));
72   /* Restart joining thread if any */
73   if (joining != NULL) restart(joining);
74   /* If this is the initial thread, block until all threads have terminated.
75      If another thread calls exit, we'll be terminated from our signal
76      handler. */
77   if (self == __pthread_main_thread && __pthread_manager_request >= 0) {
78     request.req_thread = self;
79     request.req_kind = REQ_MAIN_THREAD_EXIT;
80     __libc_write(__pthread_manager_request, (char *)&request, sizeof(request));
81     suspend(self);
82     /* Main thread flushes stdio streams and runs atexit functions.
83        It also calls a handler within LinuxThreads which sends a process exit
84        request to the thread manager. */
85     exit(0);
86   }
87   /* Threads other than the main one  terminate without flushing stdio streams
88      or running atexit functions. */
89   _exit(0);
90 }
91
92 /* Function called by pthread_cancel to remove the thread from
93    waiting on a condition variable queue. */
94
95 static int join_extricate_func(void *obj, pthread_descr th)
96 {
97   volatile pthread_descr self = thread_self();
98   pthread_handle handle = obj;
99   pthread_descr jo;
100   int did_remove = 0;
101
102   __pthread_lock(&handle->h_lock, self);
103   jo = handle->h_descr;
104   did_remove = jo->p_joining != NULL;
105   jo->p_joining = NULL;
106   __pthread_unlock(&handle->h_lock);
107
108   return did_remove;
109 }
110
111 int pthread_join(pthread_t thread_id, void ** thread_return)
112 {
113   volatile pthread_descr self = thread_self();
114   struct pthread_request request;
115   pthread_handle handle = thread_handle(thread_id);
116   pthread_descr th;
117   pthread_extricate_if extr;
118   int already_canceled = 0;
119
120   /* Set up extrication interface */
121   extr.pu_object = handle;
122   extr.pu_extricate_func = join_extricate_func;
123
124   __pthread_lock(&handle->h_lock, self);
125   if (nonexisting_handle(handle, thread_id)) {
126     __pthread_unlock(&handle->h_lock);
127     return ESRCH;
128   }
129   th = handle->h_descr;
130   if (th == self) {
131     __pthread_unlock(&handle->h_lock);
132     return EDEADLK;
133   }
134   /* If detached or already joined, error */
135   if (th->p_detached || th->p_joining != NULL) {
136     __pthread_unlock(&handle->h_lock);
137     return EINVAL;
138   }
139   /* If not terminated yet, suspend ourselves. */
140   if (! th->p_terminated) {
141     /* Register extrication interface */
142     __pthread_set_own_extricate_if(self, &extr);
143     if (!(THREAD_GETMEM(self, p_canceled)
144         && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
145       th->p_joining = self;
146     else
147       already_canceled = 1;
148     __pthread_unlock(&handle->h_lock);
149
150     if (already_canceled) {
151       __pthread_set_own_extricate_if(self, 0);
152       __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
153     }
154
155     suspend(self);
156     /* Deregister extrication interface */
157     __pthread_set_own_extricate_if(self, 0);
158
159     /* This is a cancellation point */
160     if (THREAD_GETMEM(self, p_woken_by_cancel)
161         && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
162       THREAD_SETMEM(self, p_woken_by_cancel, 0);
163       __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
164     }
165     __pthread_lock(&handle->h_lock, self);
166   }
167   /* Get return value */
168   if (thread_return != NULL) *thread_return = th->p_retval;
169   __pthread_unlock(&handle->h_lock);
170   /* Send notification to thread manager */
171   if (__pthread_manager_request >= 0) {
172     request.req_thread = self;
173     request.req_kind = REQ_FREE;
174     request.req_args.free.thread_id = thread_id;
175     __libc_write(__pthread_manager_request,
176                  (char *) &request, sizeof(request));
177   }
178   return 0;
179 }
180
181 int pthread_detach(pthread_t thread_id)
182 {
183   int terminated;
184   struct pthread_request request;
185   pthread_handle handle = thread_handle(thread_id);
186   pthread_descr th;
187
188   __pthread_lock(&handle->h_lock, NULL);
189   if (nonexisting_handle(handle, thread_id)) {
190     __pthread_unlock(&handle->h_lock);
191     return ESRCH;
192   }
193   th = handle->h_descr;
194   /* If already detached, error */
195   if (th->p_detached) {
196     __pthread_unlock(&handle->h_lock);
197     return EINVAL;
198   }
199   /* If already joining, don't do anything. */
200   if (th->p_joining != NULL) {
201     __pthread_unlock(&handle->h_lock);
202     return 0;
203   }
204   /* Mark as detached */
205   th->p_detached = 1;
206   terminated = th->p_terminated;
207   __pthread_unlock(&handle->h_lock);
208   /* If already terminated, notify thread manager to reclaim resources */
209   if (terminated && __pthread_manager_request >= 0) {
210     request.req_thread = thread_self();
211     request.req_kind = REQ_FREE;
212     request.req_args.free.thread_id = thread_id;
213     __libc_write(__pthread_manager_request,
214                  (char *) &request, sizeof(request));
215   }
216   return 0;
217 }