H
H
Hikolai2016-04-27 13:03:37
linux
Hikolai, 2016-04-27 13:03:37

What happens if a thread has already called pthread_join?

There is code for the pthread_join function from glibc.
Please tell me:
1) Line number and what happens if pthread_join has already been called for a thread?

#include <errno.h>
#include <stdlib.h>
#include <atomic.h>
#include "pthreadP.h"
#include <stap-probe.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;
 LIBC_PROBE (pthread_join, 1, threadid);
 
 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 (__glibc_likely (result == 0))
 {
 pd->tid = -1;
 
 if (thread_return != NULL)
*thread_return = pd->result;
 __free_tcb (pd);
 }
 LIBC_PROBE (pthread_join_ret, 3, threadid, result, pd->result);
 return result;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MiiNiPaa, 2016-04-27
@MiiNiPaa

Documentation, motherfucker, do you read it?
man7.org/linux/man-pages/man3/pthread_join.3.html

Joining with a thread that has previously been joined results in undefined behavior.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question