void *main_thread(void *arg) { CircularQueue *entryQueue = create_all_numbers_queue(_max_number); CircularQueue *exitQueue; queue_init(&exitQueue, _max_number); pthread_t filter_tid; pthread_create(&filter_tid, NULL, filter_thread, exitQueue); unsigned int first = queue_get(entryQueue); if (place_number_into_shared_memory(first)) { printf("Error while accessing the shared memory region.\n"); exit(EXIT_FAILURE); } #if DEBUG printf("Placed %d on the shared memory region.\n", first); #endif unsigned int number = 0; while (( number = queue_get(entryQueue) )) // Get each number from the entry queue up until "0" if (number % first) // Up so far, the number is not prime. queue_put(exitQueue, number); queue_put(exitQueue, 0); queue_destroy(entryQueue); return NULL; }
void unsorted_mode() { queue_t *q = queue_create(); char *t1 = malloc(1); char *t2 = malloc(2); char *t3 = malloc(4); char *t4 = malloc(8); test *s1 = malloc(sizeof(test)); s1->test = t1; test *s2 = malloc(sizeof(test)); s2->test = t2; test *s3 = malloc(sizeof(test)); s3->test = t3; test *s4 = malloc(sizeof(test)); s4->test = t4; queue_put(q, s1); queue_put(q, s2); queue_put(q, s3); queue_put(q, s4); test *t; queue_get(q, (void **)&t); free_test(t); queue_get(q, (void **)&t); free_test(t); queue_destroy_complete(q, (void *)free_test); }
void sorted2_mode() { queue_t *q = queue_create_limited_sorted(3, 1, (void *)cmp_int_ptr); int t1 = 1; queue_put(q, &t1); int t2 = 15; queue_put(q, &t2); int t3 = 3; queue_put(q, &t3); int t4 = 27; queue_put(q, &t4); int t5 = 9; queue_put(q, &t5); int *i; queue_get(q, (void **)&i); printf("first element was %d\n", *i); queue_get(q, (void **)&i); printf("second element was %d\n", *i); queue_get(q, (void **)&i); printf("third element was %d\n", *i); queue_get(q, (void **)&i); printf("fourth element was %p\n", i); queue_get(q, (void **)&i); printf("fifth element was %p\n", i); queue_destroy_complete(q, NULL); }
void sorted_mode() { queue_t *q = queue_create_sorted(1, (void *)cmp_int_ptr); int *t1 = malloc(sizeof(int)); int *t2 = malloc(sizeof(int)); int *t3 = malloc(sizeof(int)); int *t4 = malloc(sizeof(int)); *t1 = 10; *t2 = 12; *t3 = 1; *t4 = 1; queue_put(q, t1); queue_put(q, t2); queue_put(q, t3); queue_put(q, t4); int *t; queue_get(q, (void **)&t); printf("first int %i\n", *t); free(t); queue_get(q, (void **)&t); printf("second int %i\n", *t); free(t); queue_get(q, (void **)&t); printf("third int %i\n", *t); free(t); queue_get(q, (void **)&t); printf("fourth int %i\n", *t); free(t); queue_destroy_complete(q, NULL); }
void *filterThreadFunc(void *arg) { CircularQueue* q = arg; QueueElem elem = queue_get(q); QueueElem first = elem; addPrimesToList(first); if(first > (int) sqrt(N)){ elem = queue_get(q); while(elem != 0) { addPrimesToList(elem); elem = queue_get(q); } sem_post(&canTerminate); } else { pthread_t filterThreadId; CircularQueue* temp; queue_init(&temp, queueSize); if(pthread_create(&filterThreadId, NULL, filterThreadFunc, (void*) temp)) { printf("Thread error."); } do { elem = queue_get(q); if(elem % first != 0) queue_put(temp, elem); } while (elem != 0); queue_put(temp, 0); } queue_destroy(q); return NULL; }
void* Filter(void* q){ QueueElem primo= queue_get(q); QueueElem temp= queue_get(q); //printf(" ler %d \n",temp); if(primo>ceil(sqrt(N))){ while(temp!=0){ // printf(" a ler finais : %d \n",temp); if(temp!=0){ pthread_mutex_lock(&mutex2); primes[ind]=temp; ind++; pthread_mutex_unlock(&mutex2); } temp=queue_get(q); } pthread_mutex_lock(&mutex2); primes[ind]=primo; ind++; pthread_mutex_unlock(&mutex2); queue_destroy(q); sem_post(&semaforo); } else{ CircularQueue* q2; queue_init(&q2,10); pthread_t ThreadPrimo; pthread_create(&ThreadPrimo,NULL,Filter,q2); // printf("thread created \n"); pthread_mutex_lock(&mutex1); number_threads++; pthread_mutex_unlock(&mutex1); while(temp!=0){ if(temp%primo!=0){ queue_put(q2,temp); // printf(" por %d \n",temp); } temp=queue_get(q); // printf(" ler %d \n",temp); } queue_put(q2,0); queue_destroy(q); pthread_mutex_lock(&mutex2); primes[ind]=primo; ind++; pthread_mutex_unlock(&mutex2); } pthread_mutex_lock(&mutex1); number_threads--; pthread_mutex_unlock(&mutex1); return NULL; }
void *filter_thread(void *arg) { CircularQueue *entryQueue = (CircularQueue *) arg; CircularQueue *exitQueue; queue_init(&exitQueue, _max_number); unsigned int first = queue_get(entryQueue); if (place_number_into_shared_memory(first)) { printf("Error while accessing the shared memory region.\n"); exit(EXIT_FAILURE); } #if DEBUG printf("Placed %d on the shared memory region.\n", first); #endif unsigned int number = 0; if (first > sqrt(_max_number)) { #if DEBUG printf("Finished, the rest are primes. (%d and up)\n", first); #endif while (( number = queue_get(entryQueue) )) // Get each number from the entry queue up until "0" place_number_into_shared_memory(number); // Signal the EOP semaphore, we are done! sem_t *semaphore = sem_open(SEM1_NAME, 0, 0600, NULL); if (semaphore == SEM_FAILED) { printf("Error while getting the End of Program semaphore.\n"); exit(EXIT_FAILURE); } sem_post(semaphore); return NULL; } pthread_t filter_tid; pthread_create(&filter_tid, NULL, filter_thread, exitQueue); while (( number = queue_get(entryQueue) )) // Get each number from the entry queue up until "0" if (number % first) // So far, the number is prime. queue_put(exitQueue, number); queue_put(exitQueue, 0); queue_destroy(entryQueue); return NULL; }
static void playlist_delete_internal(struct playlist *playlist, unsigned song, const struct song **queued_p) { unsigned songOrder; assert(song < queue_length(&playlist->queue)); songOrder = queue_position_to_order(&playlist->queue, song); if (playlist->playing && playlist->current == (int)songOrder) { bool paused = pc_get_state() == PLAYER_STATE_PAUSE; /* the current song is going to be deleted: stop the player */ pc_stop(); playlist->playing = false; /* see which song is going to be played instead */ playlist->current = queue_next_order(&playlist->queue, playlist->current); if (playlist->current == (int)songOrder) playlist->current = -1; if (playlist->current >= 0 && !paused) /* play the song after the deleted one */ playlist_play_order(playlist, playlist->current); else /* no songs left to play, stop playback completely */ playlist_stop(playlist); *queued_p = NULL; } else if (playlist->current == (int)songOrder) /* there's a "current song" but we're not playing currently - clear "current" */ playlist->current = -1; /* now do it: remove the song */ if (!song_in_database(queue_get(&playlist->queue, song))) pc_song_deleted(queue_get(&playlist->queue, song)); queue_delete(&playlist->queue, song); /* update the "current" and "queued" variables */ if (playlist->current > (int)songOrder) { playlist->current--; } }
void *doSum(void *arg) { int sum = 0; Queue *queue = (Queue*)arg; Task *task = (Task*)queue_get(queue); while(task) { sum += task->value; free(task); task = (Task*)queue_get(queue); } pthread_exit((void*)(intptr_t)sum); }
static int i2s_stm32_read(struct device *dev, void **mem_block, size_t *size) { struct i2s_stm32_data *const dev_data = DEV_DATA(dev); int ret; if (dev_data->rx.state == I2S_STATE_NOT_READY) { LOG_DBG("invalid state"); return -EIO; } if (dev_data->rx.state != I2S_STATE_ERROR) { ret = k_sem_take(&dev_data->rx.sem, dev_data->rx.cfg.timeout); if (ret < 0) { return ret; } } /* Get data from the beginning of RX queue */ ret = queue_get(&dev_data->rx.mem_block_queue, mem_block, size); if (ret < 0) { return -EIO; } return 0; }
void join_threads(void) { cnode *curnode; printf("joining threads...\n"); while (numthreads) { pthread_mutex_lock(&cq.control.mutex); /* below, we sleep until there really is a new cleanup node. This takes care of any false wakeups... even if we break out of pthread_cond_wait(), we don't make any assumptions that the condition we were waiting for is true. */ while (cq.cleanup.head==NULL) { pthread_cond_wait(&cq.control.cond,&cq.control.mutex); } /* at this point, we hold the mutex and there is an item in the list that we need to process. First, we remove the node from the queue. Then, we call pthread_join() on the tid stored in the node. When pthread_join() returns, we have cleaned up after a thread. Only then do we free() the node, decrement the number of additional threads we need to wait for and repeat the entire process, if necessary */ curnode = (cnode *) queue_get(&cq.cleanup); pthread_mutex_unlock(&cq.control.mutex); pthread_join(curnode->tid,NULL); printf("joined with thread %d\n",curnode->threadnum); free(curnode); numthreads--; } }
static int get_ports(ClientData clientData, Tcl_Interp *interp, int argc, char *argv[]) { char msg[255]; channel_t *chan; int i, n; memset(msg, 0, 255); n = queue_length(priv_c); Tcl_SetResult(interp,NULL,TCL_STATIC); for(i = 0; i < n; i++) { chan = (channel_t*)queue_get(priv_c, i, Q_KEEP); sprintf(msg, "%d", chan->port); Tcl_AppendElement(interp,msg); } UNUSED(clientData); UNUSED(argc); UNUSED(argv); return TCL_OK; }
void *write_thread(void *_ts) { struct ts *ts = _ts; struct packet *packet; mode_t umask_val = umask(0); dir_perm = (0777 & ~umask_val) | (S_IWUSR | S_IXUSR); set_thread_name("tsdump-write"); while ((packet = queue_get(ts->packet_queue))) { if (!packet->data_len) continue; p_dbg1(" - Got packet %d, size: %u, file_time:%lu packet_time:%lu depth:%d\n", packet->num, packet->data_len, ALIGN_DOWN(packet->ts.tv_sec, ts->rotate_secs), packet->ts.tv_sec, ts->packet_queue->items); handle_files(ts, packet); if (ts->output_fd > -1) { p_dbg2(" - Writing into fd:%d size:%d file:%s\n", ts->output_fd, packet->data_len, ts->output_filename); ssize_t written = write(ts->output_fd, packet->data, packet->data_len); if (written != packet->data_len) { p_err("Can not write data (fd:%d written %zd of %d file:%s)", ts->output_fd, written, packet->data_len, ts->output_filename); } } free_packet(packet); } close_output_file(ts, NO_UNLINK); return NULL; }
void *threadfunc(void *myarg) { wnode *mywork; cnode *mynode; mynode=(cnode *) myarg; pthread_mutex_lock(&wq.control.mutex); while (wq.control.active) { while (wq.work.head==NULL && wq.control.active) { pthread_cond_wait(&wq.control.cond, &wq.control.mutex); } if (!wq.control.active) break; //we got something! mywork=(wnode *) queue_get(&wq.work); pthread_mutex_unlock(&wq.control.mutex); //perform processing... printf("Thread number %d processing job %d\n",mynode->threadnum,mywork->jobnum); free(mywork); pthread_mutex_lock(&wq.control.mutex); } pthread_mutex_unlock(&wq.control.mutex); pthread_mutex_lock(&cq.control.mutex); queue_put(&cq.cleanup,(node *) mynode); pthread_mutex_unlock(&cq.control.mutex); pthread_cond_signal(&cq.control.cond); printf("thread %d shutting down...\n",mynode->threadnum); return NULL; }
enum playlist_result spl_save_queue(const char *name_utf8, const struct queue *queue) { char *path_fs; FILE *file; if (map_spl_path() == NULL) return PLAYLIST_RESULT_DISABLED; if (!spl_valid_name(name_utf8)) return PLAYLIST_RESULT_BAD_NAME; path_fs = map_spl_utf8_to_fs(name_utf8); if (path_fs == NULL) return PLAYLIST_RESULT_BAD_NAME; if (g_file_test(path_fs, G_FILE_TEST_EXISTS)) { g_free(path_fs); return PLAYLIST_RESULT_LIST_EXISTS; } file = fopen(path_fs, "w"); g_free(path_fs); if (file == NULL) return PLAYLIST_RESULT_ERRNO; for (unsigned i = 0; i < queue_length(queue); i++) playlist_print_song(file, queue_get(queue, i)); fclose(file); idle_add(IDLE_STORED_PLAYLIST); return PLAYLIST_RESULT_SUCCESS; }
void SWI0_IRQHandler(void) { uint32_t err_code; while (!queue_is_empty(&m_evt_queue)) { radio_evt_t evt; err_code = queue_get(&m_evt_queue, &evt.type); ASSUME_SUCCESS(err_code); switch (evt.type) { case PACKET_RECEIVED: err_code = rx_queue_get(&evt.packet); ASSUME_SUCCESS(err_code); break; default: break; } (*m_evt_handler)(&evt); } }
/* worker thread | glock: UNUSED */ void* write_dqueue_worker(void *arg) { struct timeval tv; uint32_t sec,usec,cnt; uint8_t *id; (void)arg; for (;;) { queue_get(dqueue,&sec,&usec,&id,&cnt); gettimeofday(&tv,NULL); if ((uint32_t)(tv.tv_usec) < usec) { tv.tv_sec--; tv.tv_usec += 1000000; } if ((uint32_t)(tv.tv_sec) < sec) { // time went backward !!! sleep(1); } else if ((uint32_t)(tv.tv_sec) == sec) { usleep(1000000-(tv.tv_usec-usec)); } cnt--; if (cnt>0) { gettimeofday(&tv,NULL); queue_put(dqueue,tv.tv_sec,tv.tv_usec,(uint8_t*)id,cnt); } else { queue_put(jqueue,0,0,id,0); } } return NULL; }
/** audio tx isr (to be called from dispatcher) * - get chunk from tx queue * - if valid, release old pending chunk to buffer pool * - configure DMA * - if not valid, configure DMA to replay same chunk again * Parameters: * @param pThis pointer to own object * * @return None */ void audioTx_isr(void *pThisArg) { // create local casted pThis to avoid casting on every single access audioTx_t *pThis = (audioTx_t*) pThisArg; chunk_t *pchunk = NULL; // validate that TX DMA IRQ was triggered if ( *pDMA4_IRQ_STATUS & 0x1 ) { //printf("[TXISR]\n"); /* We need to remove the data from the queue and create space for more data * (The data was read previously by the DMA) 1. First, attempt to get the new chunk, and check if it's available: */ if (PASS == queue_get(&pThis->queue, (void **)&pchunk) ) { /* 2. If so, release old chunk on success back to buffer pool */ bufferPool_release(pThis->pBuffP, pThis->pPending); /* 3. Register the new chunk as pending */ pThis->pPending = pchunk; } else { //printf("[Audio TX]: TX Queue Empty! \r\n"); } *pDMA4_IRQ_STATUS |= DMA_DONE; // Clear the interrupt // config DMA either with new chunk (if there was one), or with old chunk on empty Q audioTx_dmaConfig(pThis->pPending); } }
static void *waitq_thread(void *arg) { Conn *start = NULL; Exec *e = arg; for (;;) { Conn *c = queue_get(e->waitq); if (c->io->stop) { start = NULL; conn_free(c); } else if (sys_iready(c->io, 0)) { start = NULL; queue_put(e->runq, c); } else if (sys_millis() - c->time > KEEP_ALIVE_MS) { start = NULL; conn_free(c); } else { if (start == c) /* we are in the wait-loop */ queue_wait(e->waitq, 10); /* wait for new connections */ if (start == NULL) start = c; queue_put(e->waitq, c); } } return NULL; }
void *connection_writer(void *arg) { msg_node *mn; message *msg; conn_node *c; c=(conn_node *) arg; #ifndef NDEBUG print( DEBUG, "started (fd=%d, tid=%lu)", c->fd, pthread_self() ); #endif pthread_mutex_lock(&(c->outcoming.control.mutex)); while(c->outcoming.control.active || c->outcoming.list.head) { while(!(c->outcoming.list.head) && c->outcoming.control.active) { pthread_cond_wait(&(c->outcoming.control.cond), &(c->outcoming.control.mutex)); } mn = (msg_node *) queue_get(&(c->outcoming.list)); if(!mn) continue; pthread_mutex_unlock(&(c->outcoming.control.mutex)); pthread_mutex_lock(&(c->control.mutex)); while(c->freeze) { pthread_cond_wait(&(c->control.cond), &(c->control.mutex)); } pthread_mutex_unlock(&(c->control.mutex)); msg = mn->msg; if(send_message(c->fd, msg)) { print( ERROR, "cannot send the following message" ); dump_message(msg); } free_message(msg); free(mn); pthread_mutex_lock(&(c->outcoming.control.mutex)); } pthread_mutex_unlock(&(c->outcoming.control.mutex)); pthread_mutex_lock(&(c->control.mutex)); c->writer_done=1; pthread_mutex_unlock(&(c->control.mutex)); pthread_cond_broadcast(&(c->control.cond)); send_me_to_graveyard(); return 0; }
int __libnet_internal__serial_read(struct port *port, unsigned char *buf, int size) { unsigned char *p = buf; while (!queue_empty(port->recv) && (size--)) queue_get(port->recv, *p++); return p - buf; }
/** * unload all handlers by calling dlclose and removing them from list. */ void unload_handlers() { handler *h; while((h=(handler *) queue_get(&(handlers)))) { if(dlclose(h->dl_handle)) print( ERROR, "dlclose on \"%s\": %s", h->name, dlerror() ); } }
/* * Send a list of the queue to the client. */ void command_listq(sp_session *session, const struct command * const command) { unsigned i = 0; if(queue_print_cur_first) { i = queue_get_pos(); } while(queue_get(i) != NULL && i < NUM_SEARCH_RESULTS) { char buf[API_MESSAGE_LEN]; num_pre(buf, API_MESSAGE_LEN, i, track_to_str, queue_get(i)); sock_send_str(command->sockfd, buf); sock_send_str(command->sockfd, "\n"); ++i; } }
static void* mevent_start_base_entry(void *arg) { int rv; struct timespec ts; struct queue_entry *q; struct event_entry *e = (struct event_entry*)arg; int *mypos = _tls_get_mypos(); *mypos = e->cur_thread; mtc_dbg("I'm %s %dnd thread", e->name, *mypos); for (;;) { /* Condition waits are specified with absolute timeouts, see * pthread_cond_timedwait()'s SUSv3 specification for more * information. We need to calculate it each time. * We sleep for 1 sec. There's no real need for it to be too * fast (it's only used so that stop detection doesn't take * long), but we don't want it to be too slow either. */ mutil_utc_time(&ts); ts.tv_sec += 1; rv = 0; queue_lock(e->op_queue[*mypos]); while (queue_isempty(e->op_queue[*mypos]) && rv == 0) { rv = queue_timedwait(e->op_queue[*mypos], &ts); } if (rv != 0 && rv != ETIMEDOUT) { errlog("Error in queue_timedwait()"); /* When the timedwait fails the lock is released, so * we need to properly annotate this case. */ __release(e->op_queue[*mypos]->lock); continue; } q = queue_get(e->op_queue[*mypos]); queue_unlock(e->op_queue[*mypos]); if (q == NULL) { if (e->loop_should_stop) { break; } else { continue; } } mtc_dbg("%s %d process", e->name, *mypos); e->process_driver(e, q, *mypos); /* Free the entry that was allocated when tipc queued the * operation. This also frees it's components. */ queue_entry_free(q); } return NULL; }
void UART0_task(INT8U my_id, INT8U my_state, INT8U my_event, INT8U my_data) { while(uart0_rx_queue.length > 0) { INT8U contents = queue_get(&uart0_rx_queue); put_queue(Q_INPUT, contents, 0); } }
void playlist_delete_song(struct playlist *playlist, const struct song *song) { for (int i = queue_length(&playlist->queue) - 1; i >= 0; --i) if (song == queue_get(&playlist->queue, i)) playlist_delete(playlist, i); pc_song_deleted(song); }
int DLL_EXPORT check(queue_t * to_check){ int sum = 0; for(int i = 0 ; i < queue_size(to_check); i++){ sum+=queue_get(to_check,i); } printf("SUM AT CHECK IS %i\n", sum); return sum > 100; }
void event_loop(EventQueue* queue) { Event* event; for (;;) { event = (Event*)queue_get(queue); if (!event) break; (event->callback)(event); } }
static void async_cb(uv_async_t *handle) { Loop *l = handle->loop->data; uv_mutex_lock(&l->mutex); while (!queue_empty(l->thread_events)) { Event ev = queue_get(l->thread_events); queue_put_event(l->fast_events, ev); } uv_mutex_unlock(&l->mutex); }
/*! * \brief * Returns the key to the caller. * If we have bt_wait flag set, it waits for the user choice. * * \param wait Wait for key flag * \retval key pressed or -1(EOF) if none. */ keys_t btn_getkey (uint8_t wait) { keys_t ret=BTN_NULL; // wait for user's action while (wait && queue_is_empty (&btn_q)) ; queue_get (&btn_q, (void*)&ret); return ret; }