library/src/threads/BP-LinuxThreads.cc
// Attempt to rejoin a single thread
Attempt to rejoin a single thread
Attempt to rejoin a single thread
BP_ERROR_T BP_RejoinThread
(
	P_BP_THREAD_MANAGER thread_manager,
	P_BP_MANAGED_THREAD managed_thread,
	BP_BOOL lock_thread_manager,
	BP_BOOL lock_managed_thread
)
{
	if(!thread_manager)
		return ERR_FAILURE;
	if(!managed_thread)
		return ERR_FAILURE;
	switch(pthread_join(managed_thread->thread_id, NULL))
	{
		case EINVAL:
			printf("\n pthread_join: EINVAL");
			break;
		case ESRCH:
			BP_THREAD_MANAGER_ENTER_CRITICAL_SECTION;
			if(managed_thread->thread_id)
			{
				thread_manager->threads_running_n--;
				if( (thread_manager->threads_reclaimed_total_n+1) == 0)
					thread_manager->threads_reclaimed_total_n_overflow_count++;
				thread_manager->threads_reclaimed_total_n++;
				managed_thread->current_status = BP_THREAD_STATUS_REJOINED;
				managed_thread->thread_id = 0;
			}
			BP_THREAD_MANAGER_LEAVE_CRITICAL_SECTION
			break;
		case EDEADLK:
			printf("\n pthread_join: EDEADLK");
			break;
		default:
			BP_THREAD_MANAGER_ENTER_CRITICAL_SECTION;
			thread_manager->threads_running_n--;
			if( (thread_manager->threads_reclaimed_total_n+1) == 0)
				thread_manager->threads_reclaimed_total_n_overflow_count++;
			thread_manager->threads_reclaimed_total_n++;
			managed_thread->current_status = BP_THREAD_STATUS_REJOINED;
			managed_thread->thread_id = 0;
			BP_THREAD_MANAGER_LEAVE_CRITICAL_SECTION
			break;
	}
	return ERR_SUCCESS;
}
BP_ERROR_T BP_RejoinThread ( P_BP_THREAD_MANAGER thread_manager , P_BP_MANAGED_THREAD managed_thread , BP_BOOL lock_thread_manager , BP_BOOL lock_managed_thread ) { if ( ! thread_manager ) return ERR_FAILURE ; if ( ! managed_thread ) return ERR_FAILURE ; switch ( pthread_join ( managed_thread -> thread_id , NULL ) ) { case EINVAL : printf ( " \n " ) ; break ; case ESRCH : BP_THREAD_MANAGER_ENTER_CRITICAL_SECTION ; if ( managed_thread -> thread_id ) { thread_manager -> threads_running_n -- ; if ( ( thread_manager -> threads_reclaimed_total_n + 1 ) == 0 ) thread_manager -> threads_reclaimed_total_n_overflow_count ++ ; thread_manager -> threads_reclaimed_total_n ++ ; managed_thread -> current_status = BP_THREAD_STATUS_REJOINED ; managed_thread -> thread_id = 0 ; } BP_THREAD_MANAGER_LEAVE_CRITICAL_SECTION break ; case EDEADLK : printf ( " \n " ) ; break ; default : BP_THREAD_MANAGER_ENTER_CRITICAL_SECTION ; thread_manager -> threads_running_n -- ; if ( ( thread_manager -> threads_reclaimed_total_n + 1 ) == 0 ) thread_manager -> threads_reclaimed_total_n_overflow_count ++ ; thread_manager -> threads_reclaimed_total_n ++ ; managed_thread -> current_status = BP_THREAD_STATUS_REJOINED ; managed_thread -> thread_id = 0 ; BP_THREAD_MANAGER_LEAVE_CRITICAL_SECTION break ; } return ERR_SUCCESS ; }
Attempt to rejoin a single thread
Attempt to rejoin a single thread
// ensure we have a thread manager // ensure we have a managed thread // attempt to join the threads // enter critical section // if we got a thread id, it possibly can exit before we get a chance to // examine it.  This forces the removal of a thread that has a valid id // but cannot be found (this fixes a sinister bug) // decrement the number of threads running // increment the overflow count // increment the number of reclaimed threads // set the thread as exited // set thread id // leave critical section // enter critical section // decrement the number of threads running // increment the overflow count // increment the number of reclaimed threads // set the thread as exited // set thread id // leave critical section // break // return indicating success