Пример #1
0
int sthread_mutex_lock(sthread_mutex_t *mutex)
{
    sthread_t ptr_this_thread = sthread_self();
    //fighting for the right to modify Q
    while(test_and_set(&mutex->M)){}
    //after this I am now the only one
    //to modify the passed in mutex
    //therefore I should go ahead and
    //modify the lock and Q if needed

    //check if I should go into critical
    //section
    struct queue *pQ = &mutex->Q;
    if(Q_empty(pQ))
    {
        //lock available
        Q_push(pQ, ptr_this_thread);
        mutex->M = 0; // I am done with Q; others can come and look / change Q.
        return 0; //this_thread will go into critical section
    }
    else
    {
        //lock not available
        //there are someone or myself before me in the Q
        //if it is myself in criticle section
            //I shoud not wait for myself to wake myself up
            //I will increment counter and keep running in criticle seciton
        //else
            //someone else is before me in the Q 
            //either he is sleeping (he is not the 1st)
            //or he is in the critical section now (he is the 1st and 
            //I am the 2nd in Q)
                //either way I should go to bed and wait for him to wake me up
        if(Q_first(pQ)->ptr_thread == ptr_this_thread)
        {
            Q_first(pQ)->count++; //Trying to acquire the lock own by myself
                                 //take down this attempt and keep myself in
                                 //criticle section
            mutex->M = 0; //I am done with lock; others can now check / modify lock.
        }
        else
        {
            Q_push(pQ, ptr_this_thread);
            mutex->M = 0;
            sthread_suspend();  //Other thread is in criticle section
                        //I should go to bed and sleep 
            return 0;   //the moment I am woken up will
                        //return 0 to caller
        }
        
    }
}
Пример #2
0
void LG_topPrint(LGraph lg)
{
	unsigned *indegree;
	unsigned i,count=0;
	DLL_Node* node_ptr;
	Queue q;
	if(lg.count==0)
		return ;
	indegree=(unsigned *)malloc(sizeof(unsigned)*lg.count);
	for(i=0;i<lg.count;i++)
		indegree[i]=lg.in_ptr[i];

	Q_init(&q);
	for(i=0;i<lg.count;i++)
		if(indegree[i]==0)
			Q_push(&q,i);
	while(!Q_isEmpty(q))
	{
		Q_getFront(q,&i);
		Q_pop(&q);
		printf("%c ",lg.d_ptr[i]);
		++count;
		for(node_ptr=lg.l_ptr[i].head;node_ptr;node_ptr=node_ptr->next)
		{
			i=node_ptr->data;
			if(--indegree[i]==0)
				Q_push(&q,i);
		}
	}
	if(count<lg.count)
		printf("ͼÖÐÓлØ·£¬ÅÅÐòʧ°Ü\n");
	else
		printf("\n");
	Q_clear(&q);
	free(indegree);
	
}
Пример #3
0
int sthread_mutex_trylock(sthread_mutex_t *mutex)
{
    sthread_t ptr_this_thread = sthread_self();
    //fighting for the right to modify Q
    while(test_and_set(&mutex->M)){}
    //after this I am now the only one
    //to modify the passed in mutex
    //therefore I should go ahead and
    //modify the lock and Q if needed

    //check if I should go into critical
    //section
    struct queue *pQ = &mutex->Q;
    if(Q_empty(pQ))
    {
        //lock available
        Q_push(pQ, ptr_this_thread);
        mutex->M = 0; // I am done with Q; others can come and look / change Q.
        //this_thread will go into critical section
        return 0;
    }
    else
    {
        //lock not available
        if(Q_first(pQ)->ptr_thread == ptr_this_thread)
        {
            Q_first(pQ)->count++;//Trying to acquire the lock own by myself
                                 //take down this attempt and keep myself in
                                 //criticle section
            mutex->M = 0; //I am done with lock; others can now check / modify lock.
        }
        //do not block caller
        //return non-zero indication lock not available
        return -1;
    }

}