node *create_node(ticket *t) { node *m ; m= (node*)malloc (sizeof(node)); m->next = NULL; m->value = create_ticket(t->type,t->server_id,t->hop_count); return m; }
void enqueue(pthread_mutex_t *condition_mutex,queue *q,node *n) { pthread_mutex_lock( condition_mutex ); q->last->next = create_node(create_ticket(n->value->type,n->value->server_id,n->value->hop_count)); q->last= q->last->next; q->last->next = NULL; q->size ++; pthread_mutex_unlock(condition_mutex ); }
WELCOME_PACKET create_queue() {//Create a new queue in the queue manager returning its ticket //TODO:refactor WELCOME_PACKET outcome; QUEUE_TICKET ticket = 0; outcome.result = set_result(SUCCESS,""); PRIORITY_QUEUE* pQueue = NULL; int indexFound = find_open_slot(); if(indexFound == -1) outcome.result = set_result(QUEUE_CANNOT_BE_CREATED, "Exceeded maximum number of queues"); if(outcome.result.code == SUCCESS) //Using outcome's error flag to avoid nested if-else {//Create a ticket ticket = create_ticket(indexFound); if(ticket == 0) outcome.result = set_result(QUEUE_CANNOT_BE_CREATED, "Ticket creation failed"); } if(outcome.result.code == SUCCESS) {//Malloc new queue pQueue = (PRIORITY_QUEUE*) malloc(sizeof(PRIORITY_QUEUE)); if(pQueue == NULL) outcome.result = set_result(OUT_OF_MEMORY, "Failed to allocate memory"); } if(outcome.result.code == SUCCESS) {//Initialize new queue pQueue->ticket = ticket; pQueue->size = 0; pQueue->head = NULL; pQueue->tail = NULL; queue_guard.queues[indexFound] = pQueue; //Give queue manager the new queue queue_guard.size++; //Increment queue manager's size } outcome.ticket = ticket; return outcome; }