int main(){ q = alloc_queue(1); create_workers(1, &q); bool ret; fibarg i = (fibarg){35, 0}; spawn(fib, (arg)&i, &ret, &q); waiton(&ret, &q); printf("fib %d = %d \n", i.i, i.r); }
/* LIFO Based queue test */ void testQ() { struct queue *my_queue; my_queue=NULL; my_queue=alloc_queue(my_queue); push_queue(my_queue,5); push_queue(my_queue,10); push_queue(my_queue,15); push_queue(my_queue,20); printf("Popped from queue: %d\n",pop_queue(my_queue)); printf("Popped from queue: %d\n",pop_queue(my_queue)); push_queue(my_queue,35); printf("Popped from queue: %d\n",pop_queue(my_queue)); printf("Popped from queue: %d\n",pop_queue(my_queue)); }
int main() { int N=100, i; int *data; struct work_queue* wq = alloc_queue(1000, "Num queue"); for(i=0;i<N;i++) { push_to_queue(wq,i); } printf("Done pushing %d elements\n", N); printf("Size of queue is %d elements\n", get_queue_size(wq)); i=0; while(data = pop(wq)) { printf("%d\n",*data); i++; } printf("Popped out %d elements\n", i); printf("Size of queue is %d elements\n", get_queue_size(wq)); return 0; }
int OpenComPort( char Port) { /* install int. handler */ unsigned status; int retval = -1; /* allocate input and output queues */ Serial_In_Queue = alloc_queue(SerInBufSize); if ((QUEUE *) 0 == Serial_In_Queue) return retval; Serial_Out_Queue = alloc_queue(SerOutBufSize); if ((QUEUE *) 0 == Serial_Out_Queue) { free(Serial_In_Queue); return retval; } retval = 0; /* Setup Comm base port address and IRQ number */ switch (Port) { case '1': ComBase = 0x3F8; IrqNum = 4; break; case '2': ComBase = 0x2F8; IrqNum = 3; break; case '3': ComBase = 0x3E8; IrqNum = 4; break; case '4': ComBase = 0x2E8; IrqNum = 3; break; default: ComBase = 0x3F8; IrqNum = 4; break; } old_comm_params.int_enable = inp(ComBase + INT_EN); outp(ComBase + INT_EN, 0); /* turn off comm interrupts */ /* save old comm parameters */ old_comm_params.line = inp(ComBase + LINE_CNTRL); old_comm_params.modem = inp(ComBase + MODEM_CNTRL); status = inp(ComBase + LINE_CNTRL); outp(ComBase + LINE_CNTRL, (unsigned char) status | 0x80); old_comm_params.baud_lsb = inp(ComBase + BAUD_LSB); old_comm_params.baud_msb = inp(ComBase + BAUD_MSB); status = inp(ComBase + LINE_CNTRL); outp(ComBase + LINE_CNTRL, (unsigned char) status | 0x7F); status = OUT2 | DTR; /* DTR/OUT2 must be set! */ outp(ComBase + MODEM_CNTRL, (unsigned char) status); /* get serial port address/vector */ oldvector_serial = (void (INTERRUPT FAR *) (void)) getvect(IrqNum + 8); /* set our interrupt handler */ setvect(IrqNum + 8, serial); /* save the PIC */ old_comm_params.int_cntrl = inp(0x21); status = (1 << IrqNum); /* calculate int enable bit */ status = ~status; /* ok enable comm ints */ outp(0x21, (unsigned char) old_comm_params.int_cntrl & (unsigned char) status); atexit(CloseComPort); return retval; }
static void obj_input(struct nl_object *obj, void *arg) { struct nfnl_queue_msg *msg = (struct nfnl_queue_msg *) obj; struct nl_dump_params dp = { .dp_type = NL_DUMP_STATS, .dp_fd = stdout, .dp_dump_msgtype = 1, }; nfnl_queue_msg_set_verdict(msg, NF_ACCEPT); nl_object_dump(obj, &dp); nfnl_queue_msg_send_verdict(nf_sock, msg); } static int event_input(struct nl_msg *msg, void *arg) { if (nl_msg_parse(msg, &obj_input, NULL) < 0) fprintf(stderr, "<<EVENT>> Unknown message type\n"); /* Exit nl_recvmsgs_def() and return to the main select() */ return NL_STOP; } int main(int argc, char *argv[]) { struct nl_sock *rt_sock; struct nl_cache *link_cache; struct nfnl_queue *queue; enum nfnl_queue_copy_mode copy_mode; uint32_t copy_range; int err = 1; int family; nf_sock = nfnl_queue_socket_alloc(); if (nf_sock == NULL) nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket"); nl_socket_disable_seq_check(nf_sock); nl_socket_modify_cb(nf_sock, NL_CB_VALID, NL_CB_CUSTOM, event_input, NULL); if ((argc > 1 && !strcasecmp(argv[1], "-h")) || argc < 3) { printf("Usage: nf-queue family group [ copy_mode ] " "[ copy_range ]\n"); printf("family: [ inet | inet6 | ... ] \n"); printf("group: the --queue-num arg that you gave to iptables\n"); printf("copy_mode: [ none | meta | packet ] \n"); return 2; } nl_cli_connect(nf_sock, NETLINK_NETFILTER); if ((family = nl_str2af(argv[1])) == AF_UNSPEC) nl_cli_fatal(NLE_INVAL, "Unknown family \"%s\"", argv[1]); nfnl_queue_pf_unbind(nf_sock, family); if ((err = nfnl_queue_pf_bind(nf_sock, family)) < 0) nl_cli_fatal(err, "Unable to bind logger: %s", nl_geterror(err)); queue = alloc_queue(); nfnl_queue_set_group(queue, atoi(argv[2])); copy_mode = NFNL_QUEUE_COPY_PACKET; if (argc > 3) { copy_mode = nfnl_queue_str2copy_mode(argv[3]); if (copy_mode < 0) nl_cli_fatal(copy_mode, "Unable to parse copy mode \"%s\": %s", argv[3], nl_geterror(copy_mode)); } nfnl_queue_set_copy_mode(queue, copy_mode); copy_range = 0xFFFF; if (argc > 4) copy_range = atoi(argv[4]); nfnl_queue_set_copy_range(queue, copy_range); if ((err = nfnl_queue_create(nf_sock, queue)) < 0) nl_cli_fatal(err, "Unable to bind queue: %s", nl_geterror(err)); rt_sock = nl_cli_alloc_socket(); nl_cli_connect(rt_sock, NETLINK_ROUTE); link_cache = nl_cli_link_alloc_cache(rt_sock); nl_socket_set_buffer_size(nf_sock, 1024*127, 1024*127); while (1) { fd_set rfds; int nffd, rtfd, maxfd, retval; FD_ZERO(&rfds); maxfd = nffd = nl_socket_get_fd(nf_sock); FD_SET(nffd, &rfds); rtfd = nl_socket_get_fd(rt_sock); FD_SET(rtfd, &rfds); if (maxfd < rtfd) maxfd = rtfd; /* wait for an incoming message on the netlink socket */ retval = select(maxfd+1, &rfds, NULL, NULL, NULL); if (retval) { if (FD_ISSET(nffd, &rfds)) nl_recvmsgs_default(nf_sock); if (FD_ISSET(rtfd, &rfds)) nl_recvmsgs_default(rt_sock); } } return 0; }
void test_alloc_queue() { Queue *queue = alloc_queue(); assert(queue != NULL); }
void ether_init() { struct thread *ethertask; ether_queue = alloc_queue(256); ethertask = create_kernel_thread(ether_dispatcher, NULL, /*PRIORITY_ABOVE_NORMAL*/ PRIORITY_NORMAL, "ethertask"); }
int pPathfind(struct game *g, int checkonly) { int i = 0, x, y; struct queue_t *queue; struct grid_t *grid; printf("Copying grid\n"); grid = copy_grid(g); printf("Allocating queue\n"); queue = alloc_queue(grid); /* printf("Have we crashed yet?\n"); for(i=0;i< (signed int)grid->size;i++) { if ( i % grid->w == 0 ) printf("\n"); switch(grid->ar[i]) { case 0: printf("%3d",grid->path[i]); break; case 1: printf("###"); break; case 2: printf("XXX"); break; case 254: printf("SSS"); break; case 255: printf("EEE"); break; } } printf("\n\n");*/ for(i=0; i < (signed int)grid->size; i++) { if (grid->ar[i] == 255) { push(queue, grid, i, 1); } } while (grid->marked[HEAD(queue).id] != 0) { checkfield(queue, grid); queue->head++; if ( queue->head >= (signed int)grid->size ) break; // printf("Gonna check: %d (%d)\n", HEAD(queue).id, queue->head); } /* for(i=0;i< (signed int)grid->size;i++) { if ( i % grid->w == 0 ) printf("\n"); switch(grid->ar[i]) { case 0: printf("%3d",grid->path[i]); break; case 1: printf("###"); break; case 2: printf("XXX"); break; case 254: printf("SSS"); break; case 255: printf("EEE"); break; } } printf("\n\n");*/ if ( checkonly ) { int ret = 0; // puts("We need to check if any of the entrances are blocked."); for(i=0;i<g->startN;i++) { if ( grid->path[g->start[i][0]+(g->start[i][1]*G_WIDTH)] > 0 ) { continue; } // printf("Start position no: %d, cell %d x %d, is value %d\n", i, g->start[i][0], g->start[i][1]*G_WIDTH, grid->path[g->start[i][0]+(g->start[i][1]*G_WIDTH)]); ret++; } free_queue(queue); free_grid(grid); return ret; } i = 0; for(y=0; y < G_HEIGHT; y++) { for(x=0; x < G_WIDTH; x++) { g->path[y][x] = grid->path[i]; i++; } } free_queue(queue); free_grid(grid); return 0; }
static unsigned decode_nd_tree (wfa_t *wfa, bitfile_t *input) /* * Read 'wfa' prediction tree of given 'input' stream. * * No return value. * * Side effects: * 'wfa->into' is filled with the decoded values */ { lqueue_t *queue; /* queue of states */ int next, state; /* state and its current child */ unsigned total = 0; /* total number of predicted states */ u_word_t sum0, sum1; /* Probability model */ u_word_t code; /* The present input code value */ u_word_t low; /* Start of the current code range */ u_word_t high; /* End of the current code range */ /* * Initialize arithmetic decoder */ code = get_bits (input, 16); low = 0; high = 0xffff; sum0 = 1; sum1 = 11; queue = alloc_queue (sizeof (int)); state = wfa->root_state; queue_append (queue, &state); /* * Traverse the WFA tree in breadth first order (using a queue). */ while (queue_remove (queue, &next)) { unsigned label; if (wfa->level_of_state [next] > wfa->wfainfo->p_max_level + 1) { /* * Nondetermismn is not allowed at levels larger than * 'wfa->wfainfo->p_max_level'. */ for (label = 0; label < MAXLABELS; label++) if (ischild (state = wfa->tree [next][label])) queue_append (queue, &state); /* continue with childs */ } else if (wfa->level_of_state [next] > wfa->wfainfo->p_min_level) { for (label = 0; label < MAXLABELS; label++) if (ischild (state = wfa->tree [next][label])) { unsigned count; /* Current interval count */ unsigned range; /* Current interval range */ count = (((code - low) + 1) * sum1 - 1) / ((high - low) + 1); if (count < sum0) { /* * Decode a '0' symbol * First, the range is expanded to account for the * symbol removal. */ range = (high - low) + 1; high = low + (u_word_t) ((range * sum0) / sum1 - 1 ); RESCALE_INPUT_INTERVAL; /* * Update the frequency counts */ sum0++; sum1++; if (sum1 > 50) /* scale the symbol frequencies */ { sum0 >>= 1; sum1 >>= 1; if (!sum0) sum0 = 1; if (sum0 >= sum1) sum1 = sum0 + 1; } if (wfa->level_of_state [state] > wfa->wfainfo->p_min_level) queue_append (queue, &state); } else { /* * Decode a '1' symbol * First, the range is expanded to account for the * symbol removal. */ range = (high - low) + 1; high = low + (u_word_t) ((range * sum1) / sum1 - 1); low = low + (u_word_t) ((range * sum0) / sum1); RESCALE_INPUT_INTERVAL; /* * Update the frequency counts */ sum1++; if (sum1 > 50) /* scale the symbol frequencies */ { sum0 >>= 1; sum1 >>= 1; if (!sum0) sum0 = 1; if (sum0 >= sum1) sum1 = sum0 + 1; } append_edge (next, 0, -1, label, wfa); total++; }
int main(int argc, char **argv) { int i; int opt; int threads = -1; int cpu; char *needle = NULL; char *dir = NULL; struct kmp_table table; struct queue q; struct produce_arg parg; struct consume_arg carg; pthread_t producer; pthread_t *consumers; while ((opt = getopt(argc, argv, "n:s:d:")) != -1) { switch (opt) { case 'n': threads = atoi(optarg); break; case 's': needle = checked_strdup(optarg); break; case 'd': dir = checked_strdup(optarg); break; case '?': default: usage(argv[0]); return (1); } } if (!needle || strlen(needle) <= 0) { usage(argv[0]); return (1); } if (!dir) { dir = checked_malloc(sizeof (char) * 2); dir[0] = '.'; dir[1] = 0; } if (threads <= 0) { cpu = sysconf(_SC_NPROCESSORS_ONLN); if (cpu <= 1) { threads = 1; } else if (cpu >= 20) { threads = 20; } else { threads = cpu; } } alloc_table(&table, needle); fill_table(&table); alloc_queue(&q, INITIAL_CAPACITY); parg.q = &q; parg.path = dir; parg.consumer_count = threads; carg.q = &q; carg.t = &table; consumers = checked_malloc(sizeof (pthread_t) * threads); checked_thread_create(&producer, NULL, &produce, &parg); for (i = 0; i < threads; i++) { checked_thread_create(consumers + i, NULL, &consume, &carg); } checked_thread_join(producer, NULL); for (i = 0; i < threads; i++) { checked_thread_join(consumers[i], NULL); } free_queue(&q); free_table(&table); free(consumers); free(needle); free(dir); return (0); }