Example #1
0
int pthread_join(pthread_t thread, void** valuePtr)
{
	if (valuePtr)
		printf("valuePtr != NULL\n");
		
	struct pthread_queue entry;
	struct pthread_queue* head;

	/* Check if it is already on the thread list. */
	if (!pCheckThreadList(thread))
		return 0;	
	
	while (1)
	{
		/* Add to the join list. */
		pthread_mutex_lock(&joinMutex);
	
		head = joinList;

		entry.threadId = pGetCurrentThreadId();
		entry.next = NULL;

		printf("waiting for %d\n", thread);

		if (!head)
		{
			joinList = &entry;
		}else{
			while (head->next)
				head = head->next;
			
			head->next = &entry;	
		}
	
		pthread_mutex_unlock(&joinMutex);
		
		SysSuspendThread(pGetCurrentThreadId());
	
		if (!pCheckThreadList(thread))
			break;
	}
	
	pthread_mutex_lock(&joinMutex);
	pthread_mutex_unlock(&joinMutex);
		
	return 0;
}
Example #2
0
int pthread_cond_wait(pthread_cond_t* cond, pthread_mutex_t* mutex)
{
	struct pthread_queue entry;
	struct pthread_queue* last=cond->list;
	int threadId;

	threadId = pGetCurrentThreadId();

	if (mutex->owner != threadId)
		return EINVAL;

	if (cond->signal == PTHREAD_COND_SIGNAL)
	{
		cond->signal = PTHREAD_COND_INITIAL;
		return 0;
	}

	entry.threadId=threadId;
	entry.next=NULL;

	pthread_mutex_lock(&cond->mutex);

	if (!last)
	{
		cond->list=&entry;
	}else{
		while (last->next)
			last=last->next;

		last->next=&entry;
	}
	
	pthread_mutex_unlock(&cond->mutex);

	pthread_mutex_unlock(mutex);

	while (cond->signal != PTHREAD_COND_SIGNAL)
		if (SysSuspendThread(threadId))
			return -1;

	pthread_mutex_lock(mutex);
			
	cond->signal = PTHREAD_COND_INITIAL;

	return 0;
}
Example #3
0
void Thread::Suspend()
{
	if( this->threadId > 0 )
		SysSuspendThread( this->threadId );
}