int myrg_rnext(MYRG_INFO *info, uchar *buf, int inx) { int err; MI_INFO *mi; if (!info->current_table) return (HA_ERR_KEY_NOT_FOUND); /* at first, do rnext for the table found before */ if ((err=mi_rnext(info->current_table->table,NULL,inx))) { if (err == HA_ERR_END_OF_FILE) { queue_remove(&(info->by_key),0); if (!info->by_key.elements) return HA_ERR_END_OF_FILE; } else return err; } else { /* Found here, adding to queue */ queue_top(&(info->by_key))=(uchar *)(info->current_table); queue_replaced(&(info->by_key)); } /* now, mymerge's read_next is as simple as one queue_top */ mi=(info->current_table=(MYRG_TABLE *)queue_top(&(info->by_key)))->table; return _myrg_mi_read_record(mi,buf); }
int myrg_rkey(MYRG_INFO *info,uchar *buf,int inx, const uchar *key, key_part_map keypart_map, enum ha_rkey_function search_flag) { uchar *key_buff; uint pack_key_length; uint16 last_used_keyseg; MYRG_TABLE *table; MI_INFO *mi; int err; DBUG_ENTER("myrg_rkey"); LINT_INIT(key_buff); LINT_INIT(pack_key_length); LINT_INIT(last_used_keyseg); if (_myrg_init_queue(info,inx,search_flag)) DBUG_RETURN(my_errno); for (table=info->open_tables ; table != info->end_table ; table++) { mi=table->table; if (table == info->open_tables) { err=mi_rkey(mi, 0, inx, key, keypart_map, search_flag); /* Get the saved packed key and packed key length. */ key_buff=(uchar*) mi->lastkey+mi->s->base.max_key_length; pack_key_length=mi->pack_key_length; last_used_keyseg= mi->last_used_keyseg; } else { mi->once_flags|= USE_PACKED_KEYS; mi->last_used_keyseg= last_used_keyseg; err=mi_rkey(mi, 0, inx, key_buff, pack_key_length, search_flag); } info->last_used_table=table+1; if (err) { if (err == HA_ERR_KEY_NOT_FOUND) continue; DBUG_PRINT("exit", ("err: %d", err)); DBUG_RETURN(err); } /* adding to queue */ queue_insert(&(info->by_key),(uchar *)table); } DBUG_PRINT("info", ("tables with matches: %u", info->by_key.elements)); if (!info->by_key.elements) DBUG_RETURN(HA_ERR_KEY_NOT_FOUND); mi=(info->current_table=(MYRG_TABLE *)queue_top(&(info->by_key)))->table; mi->once_flags|= RRND_PRESERVE_LASTINX; DBUG_PRINT("info", ("using table no: %d", (int) (info->current_table - info->open_tables + 1))); DBUG_DUMP("result key", (uchar*) mi->lastkey, mi->lastkey_length); DBUG_RETURN(_myrg_mi_read_record(mi,buf)); }
int myrg_rlast(MYRG_INFO *info, uchar *buf, int inx) { MYRG_TABLE *table; MI_INFO *mi; int err; if (_myrg_init_queue(info,inx, HA_READ_KEY_OR_PREV)) return my_errno; for (table=info->open_tables ; table < info->end_table ; table++) { if ((err=mi_rlast(table->table,NULL,inx))) { if (err == HA_ERR_END_OF_FILE) continue; return err; } /* adding to queue */ queue_insert(&(info->by_key),(uchar *)table); } /* We have done a read in all tables */ info->last_used_table=table; if (!info->by_key.elements) return HA_ERR_END_OF_FILE; mi=(info->current_table=(MYRG_TABLE *)queue_top(&(info->by_key)))->table; return _myrg_mi_read_record(mi,buf); }
/* Release the event handle returned by framework_event_query. * In the ASYNC model we must remove the event handle from the FIFO request * queue. */ void framework_release_handle(naf_handle handle) { queue_entry* entry; if (!handle) { return; } if (queue_empty()) { /* Why do the application try to release a handle on an empty queue? */ NABTO_LOG_FATAL(("SW error: Calling framework_release_handle on an empty queue")); return; } /* Find the entry containing the handle */ entry = queue_find_entry(handle); /* The given handle must belong to the queue */ UNABTO_ASSERT(entry); entry->state = APPREQ_FREE; LOG_APPREQ_WHERE("framework_release_handle", entry); /* Remove top entry from FIFO queue - and remove all consecutive * entries that have expired/finished in the mean time. */ while (!queue_empty()) { if (queue_top()->state != APPREQ_FREE) break; queue_pop(); } LOG_APPREQ_QUEUE(); }
void queueToString(queue *q,char *a){ char x; int i=0; while(1){ if(isQueueEmpty(q)==1){ break; } x=queue_top(q); dequeue(q); a[i]=x; i++; } a[i]='\0'; }
void *consumer(void *arg){ while(1){ sem_wait(&full); pthread_mutex_lock(&mutex); int num = queue_top(&Q); printf("pop a num %d\n", num); queue_pop(&Q); pthread_mutex_unlock(&mutex); sem_post(&empty); sleep(3); } }
void linked_queue(void) { Queue* queue = queue_init(); int i; fprintf(stdout, "\n**************LinkedQueue**************\n"); for (i = 0; i < 10; ++i) queue_push(queue, i * i + 343); while (!queue_empty(queue)) { fprintf(stdout, "LinkedQueue element value is : %d\n", queue_top(queue)); queue_pop(queue); } queue_destroy(&queue); }
void *consumer(void *arg) { while(1) { pthread_mutex_lock(&mutex); while(queue_is_empty(&Q)) { printf("wait producer\n"); pthread_cond_wait(&full, &mutex); } int data = queue_top(&Q); queue_pop(&Q); printf("consumer a data: %d\n", data); pthread_cond_signal(&empty); pthread_mutex_unlock(&mutex); } }
BOOL thread_pool_get_task_from_queue(pool_t *pool, task_t *task) { pthread_mutex_lock(&pool->mutex_); while(queue_is_empty(&pool->queue_) == TRUE && pool->is_started_ == TRUE) pthread_cond_wait(&pool->cond_, &pool->mutex_); if(pool->is_started_ == FALSE) { pthread_mutex_unlock(&pool->mutex_); return FALSE; } *task = queue_top(&pool->queue_); queue_pop(&pool->queue_); pthread_mutex_unlock(&pool->mutex_); return TRUE; }
/** Search for the first running or first pending requrest in the requesst * queue. Returns the first running requrest if found. Otherwise the first * pending request is returned. If neither is found NULL is returned. */ queue_entry* find_any_request_in_queue(void) { #if NABTO_APPREQ_QUEUE_SIZE > 1 queue_entry* first_pending; queue_entry* first_running; queue_entry* entry; //At least one record must be present in the queue (for loop to be //finite). //queue may be full. queue_first_used == queue_next_free means full. UNABTO_ASSERT(!queue_empty()); first_running = NULL; first_pending = NULL; entry = queue_first_used; do { if (!first_pending && entry->state == APPREQ_WAITING) { first_pending = entry; if (first_running) break; } if (!first_running && entry->state == APPREQ_IN_APP) { first_running = entry; if (first_pending) break; } queue_inc(entry); } while (entry != queue_next_free); if (first_running) return first_running; if (first_pending) return first_pending; return NULL; #else //NABTO_APPREQ_QUEUE_SIZE == 1 UNABTO_ASSERT(!queue_empty()); return queue_top(); #endif }
void commit_comes(int sessionfd, const char* msg, int RW){ long pageaddr = *(long*)msg; block_signal(SIGALRM); // printf(" invalidation responce at %lx\n", pageaddr); q_dta_t* the_queue = &req_queues[addr_to_pagenum (dsm_heaptop, pageaddr, dsm_pagesize)]; if (RW == QUEUE_WRITE){ memcpy((void*)pageaddr, msg + sizeof(long), dsm_pagesize); assert(the_queue->update_pending); if (the_queue->listlen){ if (the_queue->num_parallel_readers){ for (int i = 0;i < the_queue->num_parallel_readers;i++){ int posval = the_queue->fd_queue[(i + the_queue->currhead) % (2*NumNode)]; dsm_send(0,dsm_pagesize,SERVER_PAGE,(char*)pageaddr,ABS(posval)); } }else{ dsm_send(0, dsm_pagesize, SERVER_PAGE, (char*)pageaddr, ABS(queue_top(the_queue))); } } } the_queue->update_pending = 0; unblock_signal(SIGALRM); }
void tic_PIT() { horloge ++; while ((!queue_empty(&sleeping)) && horloge >= (queue_top(&sleeping, sleeping_t, chain))->clock) { sleeping_t *sleep = queue_out(&sleeping, sleeping_t, chain); process_tab[sleep->pid].state = WAITING; queue_add(&process_tab[sleep->pid], &process_queue, process_t, chain, prio); mem_free(sleep, sizeof(sleeping_t)); // ordonnance si prioritaire ? y penser. } if(cmpt==p){ cmpt=0; ordonnance(); } else cmpt++; // TODO table des processus en attente d'un t à parcourir }
/* * This function executes in signal handler context */ static void timerhandler(int whatever){ // printf("timer tick\n"); if (clock_state == TIMER_STOPPED) return; long invalbuf[NUM_PAGES]; int numinval = 0; int posval; clock_stop(); for (int i = 0; i < NumNode; ++i){ numinval = 0; for (int curr = activehead, last = curr;curr != -1; last = curr,curr = req_queues[curr].next_active){ if (curr == activehead){ last = curr; } if (curr_owners[curr] != i){ continue; } q_dta_t* the_queue = &(req_queues[curr]); if (the_queue->update_pending){ continue; } if (!the_queue->num_writers) continue; if (popped[curr]) continue; //better be true assert(the_queue->num_writers); qaddr = pagenum_to_addr(dsm_heaptop, curr, dsm_pagesize); int sessionfd = pop_queue(the_queue); popped[curr] = 1; if (!READ_REQ(sessionfd)){ the_queue->num_writers--; //was writing; need to wait for update if (the_queue->listlen && !the_queue->num_writers){ for (int i = 0;i < the_queue->listlen;i++){ the_queue->num_parallel_readers++; } } the_queue->update_pending = 1; }else{ if (the_queue->num_parallel_readers > 0){ //read requests granted before any writers came along //always at front of queue the_queue->num_parallel_readers --; } if (the_queue->listlen && !the_queue->num_parallel_readers){ int nextsessionfd = queue_top(the_queue); dsm_send(0, dsm_pagesize, SERVER_PAGE,(char*)pagenum_to_addr(dsm_heaptop,curr,dsm_pagesize) , ABS(nextsessionfd)); } } if ((!the_queue->listlen) || ABS(queue_top(the_queue)) != nid_to_sessionfd[i]){ invalbuf[numinval++] = curr * dsm_pagesize + dsm_heaptop; } if (the_queue->listlen){ //transfer ownership int tmp; posval = queue_top(the_queue); curr_owners[curr] = (short)(long)hash_get((void*)(long)ABS(posval),sessionfd_to_nid,&tmp); assert(tmp); }else{ curr_owners[curr] = -1; } if (!the_queue->num_writers){ if (the_queue->listlen){ the_queue->q_state = QUEUE_READERS; }else{ the_queue->q_state = QUEUE_EMPTY; } //take off the active list if (curr == last){ //delete from head activehead = req_queues[curr].next_active; }else{ req_queues[last].next_active = req_queues[curr].next_active; curr = last; } } } //TODO: send invalidation message to client if (numinval) { //printf("about to send %d inv to %d\n",numinval, nid_to_sessionfd[i]); dsm_send(0, sizeof(long)*numinval,SERVER_INVALIDATE, (char*)invalbuf, nid_to_sessionfd[i]); } } if (activehead != -1){ clock_start(); } our_memset (popped, 0, sizeof(char)*NUM_PAGES); //printf("END timer tick\n"); }