Queue* Q_teardown(Queue* Q) { QNode* node; while ((node = Q_pop(Q))) free(node); return Q; }
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); }
int sthread_mutex_unlock(sthread_mutex_t *mutex) { //fighting for the right to modify Q while(test_and_set(&mutex->M) == 1){} //I am the only one checking mutex //can modify Q struct queue *pQ = &mutex->Q; if(Q_first(pQ)->count == 1) { Q_pop(pQ); if(Q_empty(pQ) == 0) { //Queue is not empty //there are other threads sleeping //int the Q sthread_wake(Q_first(pQ)->ptr_thread); } else { //I am the last one int the Q //Do nothing } return 0; } else if( Q_first(pQ)->count > 1) { //I have acquired the lock owned by me //more than once need to unlock Q_first(pQ)->count--; } else { // printf("Bad!! count is 0 or less\n"); return -1; } }