]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/libpthread/src/join.c
update
[l4.git] / l4 / pkg / uclibc / lib / libpthread / src / 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 <stdlib.h>
19 #include <unistd.h>
20 #include "pthread.h"
21 #include "internals.h"
22 #include "spinlock.h"
23 #include "restart.h"
24
25 #include <l4/util/util.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   /* Say that we've terminated */
49   THREAD_SETMEM(self, p_terminated, 1);
50   /* See if someone is joining on us */
51   joining = THREAD_GETMEM(self, p_joining);
52   __pthread_unlock(THREAD_GETMEM(self, p_lock));
53   /* Restart joining thread if any */
54   if (joining != NULL)
55     {
56       restart(joining);
57       l4_sleep_forever();
58     }
59
60   /* If this is the initial thread, block until all threads have terminated.
61      If another thread calls exit, we'll be terminated from our signal
62      handler. */
63   if (self == __pthread_main_thread && __pthread_manager_request >= 0)
64     {
65       request.req_thread = self;
66       request.req_kind = REQ_MAIN_THREAD_EXIT;
67       __pthread_send_manager_rq(&request, 1);
68     /* Main thread flushes stdio streams and runs atexit functions.
69        It also calls a handler within LinuxThreads which sends a process exit
70        request to the thread manager. */
71     exit(0);
72   }
73   /* Threads other than the main one  terminate without flushing stdio streams
74      or running atexit functions. */
75
76   //_exit(0);
77
78   request.req_thread = self;
79   request.req_kind = REQ_THREAD_EXIT;
80   __pthread_send_manager_rq(&request, 1);
81
82   l4_sleep_forever();
83 }
84
85 /* Function called by pthread_cancel to remove the thread from
86    waiting on a condition variable queue. */
87
88 static int join_extricate_func(void *obj, pthread_descr th)
89 {
90   __volatile__ pthread_descr self = thread_self();
91   pthread_handle handle = obj;
92   pthread_descr jo;
93   int did_remove = 0;
94
95   __pthread_lock(handle_to_lock(handle), self);
96   jo = handle_to_descr(handle);
97   did_remove = jo->p_joining != NULL;
98   jo->p_joining = NULL;
99   __pthread_unlock(handle_to_lock(handle));
100
101   return did_remove;
102 }
103
104 int pthread_join(pthread_t thread_id, void ** thread_return)
105 {
106   __volatile__ pthread_descr self = thread_self();
107   struct pthread_request request;
108   pthread_handle handle = thread_handle(thread_id);
109   pthread_descr th;
110   pthread_extricate_if extr;
111   int already_canceled = 0;
112
113   /* Set up extrication interface */
114   extr.pu_object = handle;
115   extr.pu_extricate_func = join_extricate_func;
116
117   __pthread_lock(handle_to_lock(handle), self);
118   if (nonexisting_handle(handle, thread_id)) {
119     __pthread_unlock(handle_to_lock(handle));
120     return ESRCH;
121   }
122   th = handle_to_descr(handle);
123   if (th == self) {
124     __pthread_unlock(handle_to_lock(handle));
125     return EDEADLK;
126   }
127   /* If detached or already joined, error */
128   if (th->p_detached || th->p_joining != NULL) {
129     __pthread_unlock(handle_to_lock(handle));
130     return EINVAL;
131   }
132   /* If not terminated yet, suspend ourselves. */
133   if (! th->p_terminated) {
134     /* Register extrication interface */
135     __pthread_set_own_extricate_if(self, &extr);
136     if (!(THREAD_GETMEM(self, p_canceled)
137         && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE))
138       th->p_joining = self;
139     else
140       already_canceled = 1;
141     __pthread_unlock(handle_to_lock(handle));
142
143     if (already_canceled) {
144       __pthread_set_own_extricate_if(self, 0);
145       __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
146     }
147
148     suspend(self);
149     /* Deregister extrication interface */
150     __pthread_set_own_extricate_if(self, 0);
151
152     /* This is a cancellation point */
153     if (THREAD_GETMEM(self, p_woken_by_cancel)
154         && THREAD_GETMEM(self, p_cancelstate) == PTHREAD_CANCEL_ENABLE) {
155       THREAD_SETMEM(self, p_woken_by_cancel, 0);
156       __pthread_do_exit(PTHREAD_CANCELED, CURRENT_STACK_FRAME);
157     }
158     __pthread_lock(handle_to_lock(handle), self);
159   }
160   /* Get return value */
161   if (thread_return != NULL) *thread_return = th->p_retval;
162   __pthread_unlock(handle_to_lock(handle));
163   /* Send notification to thread manager */
164   if (__pthread_manager_request >= 0) {
165     request.req_thread = self;
166     request.req_kind = REQ_FREE;
167     request.req_args.free.thread_id = thread_id;
168     __pthread_send_manager_rq(&request, 0);
169   }
170   return 0;
171 }
172
173 int pthread_detach(pthread_t thread_id)
174 {
175   int terminated;
176   struct pthread_request request;
177   pthread_handle handle = thread_handle(thread_id);
178   pthread_descr th;
179
180   __pthread_lock(handle_to_lock(handle), NULL);
181   if (nonexisting_handle(handle, thread_id)) {
182     __pthread_unlock(handle_to_lock(handle));
183     return ESRCH;
184   }
185   th = handle_to_descr(handle);
186   /* If already detached, error */
187   if (th->p_detached) {
188     __pthread_unlock(handle_to_lock(handle));
189     return EINVAL;
190   }
191   /* If already joining, don't do anything. */
192   if (th->p_joining != NULL) {
193     __pthread_unlock(handle_to_lock(handle));
194     return 0;
195   }
196   /* Mark as detached */
197   th->p_detached = 1;
198   terminated = th->p_terminated;
199   __pthread_unlock(handle_to_lock(handle));
200   /* If already terminated, notify thread manager to reclaim resources */
201   if (terminated && __pthread_manager_request >= 0) {
202     request.req_thread = thread_self();
203     request.req_kind = REQ_FREE;
204     request.req_args.free.thread_id = thread_id;
205     __pthread_send_manager_rq(&request, 0);
206   }
207   return 0;
208 }