コード例 #1
0
ファイル: mypthread.c プロジェクト: meilank/CS416-hw0
void mypthread_exit(void *retval) 
{

	allThreads[threadIndex].status = EXITED;
	allThreads[threadIndex].retval = retval;
	free(allThreads[threadIndex].context.uc_stack.ss_sp);

	if(allThreads[threadIndex].retadd)
	{
		*(allThreads[threadIndex].retadd) = retval;
	}

	if (allThreads[threadIndex].beingJoinedOnBy > -1)
	{
		int joiningThreadIndex = allThreads[threadIndex].beingJoinedOnBy;
		allThreads[joiningThreadIndex].status = READY;
	}

	int next = getReady();
	if (next < 0)
	{
		int waiting = getWaiting();
		if (waiting < 0)
		{
			printf("No waiting or exiting threads found. Exiting.\n");
			exit(1);
		}
		next = allThreads[waiting].index;
	}
	threadIndex = next;

	allThreads[threadIndex].status = RUNNING;
	setcontext(&(allThreads[threadIndex].context));
	
}
コード例 #2
0
ファイル: mypthread.c プロジェクト: meilank/CS416-hw0
int mypthread_yield(void) 
{
	if (!mainSetup)
	{
		int unused = getUnused();
		if (getcontext(&(allThreads[unused].context)) != 0)
		{
			printf("Error getting context.\n");
			exit(0);
		}

		allThreads[unused].context.uc_stack.ss_sp = malloc(STACKSIZE);
		allThreads[unused].context.uc_stack.ss_size = STACKSIZE;
		allThreads[unused].status = READY;
		allThreads[threadIndex].status = RUNNING;

		mainSetup = 1;

		swapcontext(&(allThreads[unused].context), &(allThreads[threadIndex].context));
		return 0;
	}
	allThreads[threadIndex].status = READY;

	int next = getReady();

	if (next < 0)
	{
		int waiting = getWaiting();
		
		if (waiting < 0)
		{
			printf("No waiting threads found.\n");
			exit(0);
		}
		
		next = allThreads[waiting].index;
	}

	int old = threadIndex;

	threadIndex = next;

	allThreads[threadIndex].status = RUNNING;

	swapcontext(&(allThreads[old].context), &(allThreads[threadIndex].context));
	
	return 0;
}
コード例 #3
0
ファイル: type_mongos.cpp プロジェクト: i80and/mongo
BSONObj MongosType::toBSON() const {
    BSONObjBuilder builder;

    if (_name)
        builder.append(name.name(), getName());
    if (_ping)
        builder.append(ping.name(), getPing());
    if (_uptime)
        builder.append(uptime.name(), getUptime());
    if (_waiting)
        builder.append(waiting.name(), getWaiting());
    if (_mongoVersion)
        builder.append(mongoVersion.name(), getMongoVersion());
    if (_configVersion)
        builder.append(configVersion.name(), getConfigVersion());

    return builder.obj();
}
コード例 #4
0
ファイル: mypthread.c プロジェクト: meilank/CS416-hw0
int mypthread_join(mypthread_t thread, void **retval) 
{
	if (!mainSetup)
	{
		int unused = getUnused();
		if (getcontext(&(allThreads[unused].context)) != 0)
		{
			printf("Error getting context.\n");
			exit(0);
		}
		allThreads[unused].context.uc_stack.ss_sp = malloc(STACKSIZE);
		allThreads[unused].context.uc_stack.ss_size = STACKSIZE;

		allThreads[thread.index].beingJoinedOnBy = allThreads[unused].index;

		threadIndex = thread.index;

		allThreads[threadIndex].status = RUNNING;
		allThreads[unused].status = WAITING;

		if (retval)
		{	
			printf("retval detected in join call, setting return address in thread struct\n");
			allThreads[threadIndex].retadd = retval;
			printf("retadd set to %p\n", allThreads[threadIndex].retadd);
		}

		mainSetup = 1;

		swapcontext(&(allThreads[unused].context), &(allThreads[threadIndex].context));

		return 0;
	}
	if (threadIndex == thread.index)
	{
		printf("A thread cannot join on itself.\n");
		return -1;
	}
	if (allThreads[thread.index].beingJoinedOnBy > -1)
	{
		printf("Thread is already being joined on.\n");
		return -1;
	}
	if (retval)
		{	
			printf("retval detected in join call (not in main setup), setting return address in thread struct\n");
			allThreads[thread.index].retadd = retval;
			printf("retadd set to %p\n", allThreads[threadIndex].retadd);
		}
	if (allThreads[threadIndex].status == EXITED)
	{
		allThreads[threadIndex].status = UNUSED;
		free(allThreads[threadIndex].context.uc_stack.ss_sp);
		allThreads[threadIndex].beingJoinedOnBy = -1;
		count--;
		return 0;
	}

	allThreads[threadIndex].status = WAITING;
	allThreads[thread.index].beingJoinedOnBy = threadIndex;

	int next = getReady();

	if (next < 0)
	{
		int waiting = getWaiting();
		
		if (waiting < 0)
		{
			printf("No waiting threads found.\n");
			exit(0);
		}	
		next = allThreads[waiting].index;
	}

	int old = threadIndex;
	threadIndex = next;
	allThreads[threadIndex].status = RUNNING;
	swapcontext(&(allThreads[old].context), &(allThreads[threadIndex].context));
	return 0;
}