123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114 |
- #include <errno.h>
- #include <stdlib.h>
- #include <atomic.h>
- #include "pthreadP.h"
- static void
- cleanup (void *arg)
- {
-
- (void) atomic_compare_and_exchange_bool_acq ((struct pthread **) arg, NULL,
- THREAD_SELF);
- }
- int
- pthread_join (
- pthread_t threadid,
- void **thread_return)
- {
- struct pthread *pd = (struct pthread *) threadid;
-
- if (INVALID_NOT_TERMINATED_TD_P (pd))
-
- return ESRCH;
-
- if (IS_DETACHED (pd))
-
- return EINVAL;
- struct pthread *self = THREAD_SELF;
- int result = 0;
-
- pthread_cleanup_push (cleanup, &pd->joinid);
-
- int oldtype = CANCEL_ASYNC ();
- if ((pd == self
- || (self->joinid == pd
- && (pd->cancelhandling
- & (CANCELING_BITMASK | CANCELED_BITMASK | EXITING_BITMASK
- | TERMINATED_BITMASK)) == 0))
- && !CANCEL_ENABLED_AND_CANCELED (self->cancelhandling))
-
- result = EDEADLK;
-
- else if (__builtin_expect (atomic_compare_and_exchange_bool_acq (&pd->joinid,
- self,
- NULL), 0))
-
- result = EINVAL;
- else
-
- lll_wait_tid (pd->tid);
-
- CANCEL_RESET (oldtype);
-
- pthread_cleanup_pop (0);
- if (__builtin_expect (result == 0, 1))
- {
-
- pd->tid = -1;
-
- if (thread_return != NULL)
- *thread_return = pd->result;
-
- __free_tcb (pd);
- }
- return result;
- }
|