static int demote_cblock(struct smq_policy *mq, struct policy_locker *locker, dm_oblock_t *oblock) { struct entry *demoted = q_peek(&mq->clean, mq->clean.nr_levels, false); if (!demoted) /* * We could get a block from mq->dirty, but that * would add extra latency to the triggering bio as it * waits for the writeback. Better to not promote this * time and hope there's a clean block next time this block * is hit. */ return -ENOSPC; if (locker->fn(locker, demoted->oblock)) /* * We couldn't lock this block. */ return -EBUSY; del(mq, demoted); *oblock = demoted->oblock; free_entry(&mq->cache_alloc, demoted); return 0; }
/* * Pops an entry from a level that is not past a sentinel. */ static struct entry *q_pop_old(struct queue *q, unsigned max_level) { struct entry *e = q_peek(q, max_level, false); if (e) q_del(q, e); return e; }
static struct entry *q_pop(struct queue *q) { struct entry *e = q_peek(q, q->nr_levels, true); if (e) q_del(q, e); return e; }
char * test_peek(void) { int v; mu_assert("init", q_init() == q_success); mu_assert("insert", q_insert(18) == q_success); mu_assert("peek", q_peek(&v) == q_success); mu_assert("value", v == 18); mu_assert("destroy", q_destroy() == q_success); return NULL; }
char * test_peek_two_empty(void) { int v; mu_assert("init", q_init() == q_success); mu_assert("insert", q_insert(18) == q_success); mu_assert("insert", q_insert(42) == q_success); mu_assert("remove", q_remove(&v) == q_success); mu_assert("remove", q_remove(&v) == q_success); mu_assert("peek", q_peek(&v) == q_failure); return NULL; }
char * test_peek_deep(void) { int v; mu_assert("init", q_init() == q_success); for(int i = 0; i < 30; i++) { mu_assert("insert", q_insert(i) == q_success); } for(int i = 0; i < 1000; i++) { mu_assert("peek", q_peek(&v) == q_success); mu_assert("value", v == i); mu_assert("remove", q_remove(&v) == q_success); mu_assert("value", v == i); mu_assert("insert", q_insert(30 + i) == q_success); } mu_assert("destroy", q_destroy() == q_success); return NULL; }
void sequence(chatmessage_t* message, packet_t* newpacket) { message->seqnum = atoi(newpacket->packetbody); remove_elem(UNSEQ_CHAT_MSGS,(void*)message); q_enqueue(HBACK_Q,(void*)message); chatmessage_t* firstmessage = (chatmessage_t*)q_peek(HBACK_Q); pthread_mutex_lock(&seqno_mutex); if(firstmessage->messagetype == JOIN && SEQ_NO == -1) //my first message to display! { SEQ_NO = firstmessage->seqnum; } if(firstmessage->seqnum > SEQ_NO) { printf("SEQUENCE OUT OF SYNC. Skipping Ahead by %d messages\n",firstmessage->seqnum-SEQ_NO); SEQ_NO = firstmessage->seqnum; } if(firstmessage->seqnum <= SEQ_NO) { SEQ_NO = firstmessage->seqnum + 1; client_t* firstclientmatchbyname; if(firstmessage->messagetype == CHAT) { // printf("\E[34m%s\E(B\E[m (sequenced: %d):\t%s\n", firstmessage->sender, firstmessage->seqnum,firstmessage->messagebody); firstclientmatchbyname = find_client_by_uid(firstmessage->senderuid); } else { // printf("\E[34m%s\E(B\E[m joined the chat (sequenced: %d)\n", firstmessage->messagebody, firstmessage->seqnum); firstclientmatchbyname = find_client_by_uid(firstmessage->senderuid); } char* uid = ""; if(firstclientmatchbyname != NULL) { uid = firstclientmatchbyname->uid; remove_elem(UNSEQ_CHAT_MSGS,firstmessage); } if(firstmessage->messagetype == CHAT) print_msg_with_senderids(firstmessage->sender,firstmessage->messagebody, uid); q_dequeue(HBACK_Q); } pthread_mutex_unlock(&seqno_mutex); return; }
char * test_peek_null(void) { mu_assert("init", q_init() == q_success); mu_assert("insert", q_insert(18) == q_success); mu_assert("peek", q_peek(NULL) == q_failure); return NULL; }
char * test_peek_empty(void) { int v; mu_assert("init", q_init() == q_success); mu_assert("peek", q_peek(&v) == q_failure); return NULL; }
void intersection_after_exit(Direction origin, Direction destination) { KASSERT(intersection_lock != NULL); /* replace this default implementation with your own implementation */ (void)origin; /* avoid compiler complaint about unused parameter */ (void)destination; /* avoid compiler complaint about unused parameter */ // KASSERT(intersectionSem != NULL); // V(intersectionSem); // lock_release(intersection_lock); lock_acquire(intersection_lock); if (!q_empty(next_lights)){ // if (num_vehicles_waiting_b > 0){ // num_vehicles_waiting_b--; // if (origin == south){ // num_vehicles_waiting_south--; // } // else if (origin == north){ // num_vehicles_waiting_north--; // } // else if(origin == east){ // num_vehicles_waiting_east--; // } // else if(origin == west){ // num_vehicles_waiting_west--; // } // } // if (origin == south){ // num_vehicles_waiting_south--; // if(num_vehicles_waiting_south == 0){ // q_remhead(next_lights); // } // } // else if (origin == north){ // num_vehicles_waiting_north--; // if(num_vehicles_waiting_north == 0){ // q_remhead(next_lights); // } // } // else if(origin == east){ // num_vehicles_waiting_east--; // if(num_vehicles_waiting_east == 0){ // q_remhead(next_lights); // } // } // else if(origin == west){ // num_vehicles_waiting_west--; // if(num_vehicles_waiting_west == 0){ // q_remhead(next_lights); // } // } q_remhead(next_lights); // if(num_vehicles_waiting_b == 0){ // q_remhead(next_lights); // } if (!q_empty(next_lights)) { // q_remhead(next_lights); // current_light = q_peek(next_lights); current_light = q_peek(next_lights); if (*current_light == north){ //*current_light = east; //num_vehicles_waiting -= cv_north_go->cv_wchan->wc_threads.tl_count; //struct threadlist l = (struct threadlist) cv_north_go->cv_wchan->wc_threads; // num_vehicles_waiting = 0; // is_waiting = true; //lock_release(intersection_lock); // num_vehicles_waiting_b = num_vehicles_waiting_north; cv_broadcast(cv_north_go,intersection_lock); } else if (*current_light == south){ //num_vehicles_waiting -= cv_south_go->cv_wchan->wc_threads.tl_count; //*current_light = west; // num_vehicles_waiting = 0; //lock_release(intersection_lock); // num_vehicles_waiting_b = num_vehicles_waiting_south; cv_broadcast(cv_south_go,intersection_lock); } else if (*current_light == east){ // num_vehicles_waiting -= cv_east_go->cv_wchan->wc_threads.tl_count; // num_vehicles_waiting = 0; //*current_light = south; //lock_release(intersection_lock); // num_vehicles_waiting_b = num_vehicles_waiting_east; cv_broadcast(cv_east_go,intersection_lock); } else if (*current_light == west){ //*current_light = north; // num_vehicles_waiting -= cv_west_go->cv_wchan->wc_threads.tl_count; // num_vehicles_waiting = 0; //lock_release(intersection_lock); // num_vehicles_waiting_b = num_vehicles_waiting_west; cv_broadcast(cv_west_go,intersection_lock); } } } lock_release(intersection_lock); }
void intersection_before_entry(Direction origin, Direction destination) { /* replace this default implementation with your own implementation */ //(void)origin; /* avoid compiler complaint about unused parameter */ (void)destination; /* avoid compiler complaint about unused parameter */ // KASSERT(intersectionSem != NULL); // P(intersectionSem); KASSERT(intersection_lock != NULL); KASSERT(next_lights != NULL); KASSERT(cv_south_go != NULL); KASSERT(cv_north_go != NULL); KASSERT(cv_east_go != NULL); KASSERT(cv_west_go != NULL); lock_acquire(intersection_lock); // bool check = in_queue(origin); // if (!check){ // check = true; // } // if (q_empty(next_lights)){ // first_dir = origin; // current_light = &first_dir; // } // add the cu // if(!in_queue(origin)){ // q_addtail(next_lights,pointer); // } // current_light = q_peek(next_lights); // q_remhead(next_lights); // if (origin == north){ // // if the light is green // if(!in_queue(origin)){ // q_addtail(next_lights,&north_dir); // } // current_light = q_peek(next_lights); // // q_remhead(next_lights); // if (origin == *current_light) { // if (num_vehicles_waiting_north != 0){ // num_vehicles_waiting_north++; // cv_wait(cv_north_go,intersection_lock); // } // } // else{ // num_vehicles_waiting_north++; // cv_wait(cv_north_go,intersection_lock); // } // } // else if (origin == south){ // // if the light is green // if(!in_queue(origin)){ // q_addtail(next_lights,&south_dir); // } // current_light = q_peek(next_lights); // // q_remhead(next_lights); // if (origin == *current_light) { // if (num_vehicles_waiting_south != 0){ // num_vehicles_waiting_south++; // cv_wait(cv_south_go,intersection_lock); // } // } // else{ // num_vehicles_waiting_south++; // cv_wait(cv_south_go,intersection_lock); // } // } // else if (origin == east){ // // if the light is green // if(!in_queue(origin)){ // q_addtail(next_lights,&east_dir); // } // current_light = q_peek(next_lights); // // q_remhead(next_lights); // if (origin == *current_light) { // if (num_vehicles_waiting_east != 0){ // num_vehicles_waiting_east++; // cv_wait(cv_east_go,intersection_lock); // } // } // else{ // num_vehicles_waiting_east++; // cv_wait(cv_east_go,intersection_lock); // } // } // else if (origin == west){ // // if the light is green // if(!in_queue(origin)){ // q_addtail(next_lights,&west_dir); // } // current_light = q_peek(next_lights); // // q_remhead(next_lights); // if (origin == *current_light) { // if (num_vehicles_waiting_west != 0){ // num_vehicles_waiting_west++; // cv_wait(cv_west_go,intersection_lock); // } // }else{ // num_vehicles_waiting_west++; // cv_wait(cv_west_go,intersection_lock); // } // } if (origin == north) { // if the light is green if(!in_queue(origin)){ q_addtail(next_lights,&north_dir); } current_light = q_peek(next_lights); // q_remhead(next_lights); num_vehicles_waiting_north++; if (origin != *current_light) { cv_wait(cv_north_go,intersection_lock); } } else if (origin == south){ // if the light is green if(!in_queue(origin)){ q_addtail(next_lights,&south_dir); } current_light = q_peek(next_lights); // q_remhead(next_lights); num_vehicles_waiting_south++; if (origin != *current_light) { cv_wait(cv_south_go,intersection_lock); } } else if (origin == east){ // if the light is green if(!in_queue(origin)){ q_addtail(next_lights,&east_dir); } current_light = q_peek(next_lights); num_vehicles_waiting_east++; // q_remhead(next_lights); if (origin != *current_light) { cv_wait(cv_east_go,intersection_lock); } } else if (origin == west){ // if the light is green if(!in_queue(origin)){ q_addtail(next_lights,&west_dir); } num_vehicles_waiting_west++; current_light = q_peek(next_lights); // q_remhead(next_lights); if (origin == *current_light) { cv_wait(cv_west_go,intersection_lock); } } lock_release(intersection_lock); }