/* Process any events whose time has come. */ void event_process(void) { struct event *the_event; long new_time; while ((long) pulse >= queue_key(event_q)) { if (!(the_event = (struct event *) queue_head(event_q))) { log("SYSERR: Attempt to get a NULL event"); return; } /* ** Set the_event->q_el to NULL so that any functions called beneath ** event_process can tell if they're being called beneath the actual ** event function. */ the_event->q_el = NULL; /* call event func, reenqueue event if retval > 0 */ if ((new_time = (the_event->func)(the_event->event_obj)) > 0) the_event->q_el = queue_enq(event_q, the_event, new_time + pulse); else { free(the_event); } } }
int main() { char buf[MSG_SIZE]; int ch, e; queue q; printf("\n 1 - Enque"); printf("\n 2 - Deque"); printf("\n 3 - Front element"); printf("\n 4 - Empty"); printf("\n 5 - Exit"); printf("\n 6 - Display"); printf("\n 7 - Queue size"); queue_create(&q); while (1) { printf("\n Enter choice : "); scanf("%d", &ch); switch (ch) { case 1: printf("Enter data : "); scanf("%s", buf); queue_enq(&q, buf); break; case 2: e = queue_deq(&q, buf); puts(buf); if (e == 0) printf("Front element : %s", buf); else printf("\n No front element in Queue as queue is empty"); break; case 3: e = queue_peek(&q, buf); puts(buf); if (e == 0) printf("Front element : %s", buf); else printf("\n No front element in Queue as queue is empty"); break; case 4: queue_empty(&q); break; case 5: exit(0); case 6: queue_display(&q); break; case 7: queue_size(&q); break; default: printf("Wrong choice, Please enter correct choice "); break; } } return 0; }
int pktout_enqueue(queue_entry_t *queue, odp_buffer_hdr_t *buf_hdr) { /* * Set port number directly in a descriptor. * TODO: Remove it when PA will be used. */ ti_em_cppi_set_psflags(&buf_hdr->desc, queue->s.out_port_id); return queue_enq(queue, buf_hdr); }
/* creates an event and returns it */ struct event *event_create(EVENTFUNC(*func), void *event_obj, long when) { struct event *new_event; if (when < 1) /* make sure its in the future */ when = 1; CREATE(new_event, struct event, 1); new_event->func = func; new_event->event_obj = event_obj; new_event->q_el = queue_enq(event_q, new_event, when + pulse); return new_event; }
int main(int argc, char *argv[]) { pthread_t producers[PRODUCER_THREADS]; pthread_t consumers[CONSUMER_THREADS]; int i; int ids[PRODUCER_THREADS + CONSUMER_THREADS]; (void)argc; (void)argv; srandomdev(); printf("Init queue...\n"); q = queue_init(); queue_limit(q, 5); printf("Done, queue allocated at %p, starting workers\n", (void *)q); for (i=0; i < PRODUCER_THREADS; i++) { ids[i] = i; AZ(pthread_create(&producers[i], NULL, produce, &ids[i])); } for (i = 0; i < CONSUMER_THREADS; i++) { ids[i] = i; AZ(pthread_create(&consumers[i], NULL, consume, &ids[i])); } for (i=0;i < PRODUCER_THREADS;i++) if (producers[i]) pthread_join(producers[i], NULL); for (i=0;i < CONSUMER_THREADS;i++) if (consumers[i]) queue_enq(q, NULL); for (i=0;i < CONSUMER_THREADS;i++) if (consumers[i]) pthread_join(consumers[i], NULL); printf("Ok, still alive. Now see if destroy works...\n"); queue_destroy(q); printf("Still here. Time to go.\n"); return 0; }
void * produce(void *args) { int i; char *str; int *bla = args; int id = *bla; printf("Producer %d launched, stand by...\n", id); for(i=0;i<PRODUCER_ITERS;i++) { str = malloc(256); sprintf(str, "Iteration %d", i); printf("Producer %d making '%s'\n", id, str); fflush(stdout); queue_enq(q, str); #ifndef NOSLEEP usleep(random() % 10000); #endif } printf("Producer %d is done, bye!\n", id); return 0; }
int main (int argc, char ** argv) { queue_t queue; size_t i, j, size; stack_t stack_in, stack_out; /* Create a new queue */ queue = queue_init (); if (queue == NULL) { printf ("Failed to initialize the initial queue\n"); return 99; } /* Test empty queue */ test_empty (queue); /* Test a few inserts */ size = 10; for (i = 0; i < size; i++) if (queue_enq (queue, (void*)i) == QUEUE_MALLOC_FAILED) { printf ("Insert: Malloc failed\n"); return 99; } if (size != queue_size (queue)) { printf ("Few Inserts: Size Failed\n"); return EXIT_FAILURE; } for (i = 0; i < size; i++) if (queue_peek (queue) != (void*)i || queue_deq (queue) != (void*)i) { printf ("Few Inserts: Integrity Failed\n"); return EXIT_FAILURE; } /* Test Clear */ queue_clear (queue); test_empty (queue); /* Test Many Inserts */ size = 10000; stack_in = stack_init (); stack_out = stack_init (); if (stack_in == NULL || stack_out == NULL) { printf ("Failed to create stacks\n"); return 99; } for (i = 0; i < size; i++) { /* Queue the elements */ stack_push (stack_in, (void*)i); queue_enq (queue, (void*)i); /* Queue and dequeue many elements */ for (j = 0; j < 100; j++) { stack_push (stack_in, (void*)i); queue_enq (queue, (void*)i); /* Check the queue */ if (queue_peek (queue) != peek (stack_in, stack_out) || queue_deq (queue) != deq (stack_in, stack_out)) { printf ("Many Inserts: Integrity Failed\n"); return EXIT_FAILURE; } } } if (size != queue_size (queue)) { printf ("Many Inserts: Size Failure\n"); return EXIT_FAILURE; } for (i = 0; i < size; i++) if (queue_peek (queue) != peek (stack_in, stack_out) || queue_deq (queue) != deq (stack_in, stack_out)) { printf ("Many Inserts: Integrity Failed\n"); return EXIT_FAILURE; } stack_destroy (stack_in); stack_destroy (stack_out); /* Destroy the queue */ queue_destroy (queue); return EXIT_SUCCESS; }
int pktin_enqueue(queue_entry_t *qentry, odp_buffer_hdr_t *buf_hdr) { /* Use default action */ return queue_enq(qentry, buf_hdr); }