void PC_Put(PC *pc, int color) { Lock_Acquire(&pc->lock); assert(color <= pc->maxColor); while(pc->capacity == pc->used){ pc->waitingP++; Cond_Wait(&pc->spaceAvail, &pc->lock); pc->waitingP--; } pc->used++; addItem(color, &(pc->list)); assert(((Item *)List_Last(&(pc->list)))->color == color); if(pc->used == 1){ /* * Went from empty to non-empty. Signal */ if(pc->maxColor == 0){ Cond_Signal(&pc->stuffAvail, &pc->lock); } else{ Cond_Broadcast(&pc->stuffAvail, &pc->lock); } } assert(((Item *)List_Last(&(pc->list)))->color == color); Lock_Release(&pc->lock); }
static void addItem(int color, List_Links *list) { Item *i; i = (Item *)malloc(sizeof(Item)); assert(i); List_Init((List_Links *)i); i->color = color; List_Insert((List_Links *)i, LIST_ATREAR(list)); assert(((Item *)List_Last(list))->color == color); return; }
int main(void) { INIT(); int i, j; Object list = List_Create(); for(j = 0; j < TIMES; j++) { Object front, back; for(i = 0; i < NODES; i++) { if(i & 1) { List_PushBack(list, INT_AS_OBJECT(i)); } else { List_PushFront(list, INT_AS_OBJECT(i)); }; }; front = List_First(list); back = List_Last(list); while(!ListIterator_ThisEnd(front)) { ListIterator_Next(front); ListIterator_Prev(back); }; if(!ListIterator_ThisBegin(back)) { return 1; }; List_Clean(list); }; Object_Release(list); return 0; };
int main(void) { INIT(); long int i; int l = 10, r = 5; Object list = List_Create(); Object list2 = List_Create(); Object temp_list; Object front, back; // list2 = (0 1 2 3 4 5 6 ... 148 149) for(i = 0; i < l + r; i++) { List_PushBack(list2, INT_AS_OBJECT(i)); }; // list = (99998 99996 ... 8 6 4 2 0 1 3 5 7 9 ... 99997 99999 ) for(i = 0; i < NODES; i++) { if(i & 1) { List_PushBack(list, INT_AS_OBJECT(i)); } else { List_PushFront(list, INT_AS_OBJECT(i)); }; }; for(front = List_First(list), i = 0; i < NODES / 2; ListIterator_Next(front), i++) { List_AddAfterPosition(list2, INT_AS_OBJECT(l - 1 + i), ListIterator_ThisData(front)); }; for(back = List_Last(list), i = 0; i < NODES / 2; ListIterator_Prev(back), i++) { List_AddAfterPosition(list2, INT_AS_OBJECT(l + NODES / 2 - 1), ListIterator_ThisData(back)); }; front = List_IteratorFromPosition(list2, INT_AS_OBJECT(l)); TEST("Checking for correctness of IteratorFromPosition after AddList{Before|After}", OBJECT_AS_INT(ListIterator_ThisData(front)) == (NODES & (~1)) - ((!(NODES & 1)) * 2)); back = List_IteratorFromPosition(list2, INT_AS_OBJECT(l + NODES - 1)); TEST("Checking for correctness of IteratorFromPosition after AddList{Before|After}", OBJECT_AS_INT(ListIterator_ThisData(back)) == (NODES & (~1)) - 1 + ((NODES & 1) * 2)); temp_list = List_SublistBetweenIterators(list2, front, back); front = List_First(temp_list); back = List_Last(temp_list); for(i = 1; i < NODES / 2; i++) { if(OBJECT_AS_INT(ListIterator_ThisData(front)) != OBJECT_AS_INT(ListIterator_ThisData(back)) + 1) { DEBUG("Got %li and %li.\n", OBJECT_AS_INT(ListIterator_ThisData(front)), OBJECT_AS_INT(ListIterator_ThisData(back))); return 1; }; ListIterator_Next(front); ListIterator_Prev(back); }; Object_Release(list); Object_Release(list2); Object_Release(temp_list); return 0; };