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