void test_queue3(queue_t q, queue_pfcompare_t pf){ queue_append(q, &c); queue_append(q, &c); queue_append(q, &c); queue_reverse(q); queue_sort(q, pf); queue_link_t cur = q->head; int k = 0; while(k < 3){ if(*(int*)cur->e != 2){ puts("test_queue3 failed @ line 227"); } k++; cur = cur->next; } queue_element_t e = NULL; queue_remove(q, &e); queue_remove(q, &e); queue_remove(q, &e); if(queue_size(q) != 0) puts("test_queue3 failed @ line 238"); if(q->head != NULL) puts("test_queue3 failes @ line 240"); }
/** * If TGDB is ready to process another command, then this command will be * sent to the debugger. However, if TGDB is not ready to process another command, * then the command will be queued and run when TGDB is ready. * * \param tgdb * The TGDB context to use. * * \param command * The command to run or queue. * * \return * 0 on success or -1 on error */ int Ctgdb::Run_or_queue_command (struct tgdb_command *command) { bool can_issue; can_issue = Can_issue_command(); if (can_issue) { if (Deliver_command(command) == -1) return -1; } else { /* Make sure to put the command into the correct queue. */ switch (command->command_choice) { case TGDB_COMMAND_FRONT_END: case TGDB_COMMAND_TGDB_CLIENT: case TGDB_CUSTOME_COMMAND_FRONT_END: queue_append (gdb_input_queue, command); break; case TGDB_COMMAND_TGDB_CLIENT_PRIORITY: queue_append (oob_input_queue, command); break; case TGDB_COMMAND_CONSOLE: Logger_write_pos( __FILE__, __LINE__, "unimplemented command"); return -1; break; default: Logger_write_pos( __FILE__, __LINE__, "unimplemented command"); return -1; } } return 0; }
int append_size_test(){ queue *q = queue_create(); int x = 0, y = 1, z = 2; queue_append(q, &x); queue_append(q, &y); queue_append(q, &z); assert(queue_size(q) == 3); queue_destroy(q,false); return 0; }
int remove_size_test(){ queue *q = queue_create(); int x = 0, y = 1, z = 2; queue_append(q, &x); queue_append(q, &y); queue_append(q, &z); assert(queue_size(q) == 3); queue_element *ret_val; queue_remove(q, &ret_val); assert(queue_size(q) == 2); queue_destroy(q,false); return 0; }
/* * Add a thread to the queue based on its state. */ static void queue_add(Thread *threadp) { assert(threadp != NULL); switch(threadp->state) { case ThreadReady: queue_append(&ready_queue, threadp); break; case ThreadBlocked: queue_append(&blocked_queue, threadp); break; default: fprintf(stderr, "Thread state has been corrupted: %d\n", threadp->state); exit(1); } }
// Cria uma nova tarefa. Retorna um ID> 0 ou erro. int task_create (task_t *task, // descritor da nova tarefa void (*start_func)(void *), // funcao corpo da tarefa void *arg){ // argumentos para a tarefa task->next = NULL; task->prev = NULL; task->tid = id++; task->flag=FALSE; ucontext_t context; getcontext(&context); char *stack; stack = malloc(STACKSIZE); if(stack){ context.uc_stack.ss_sp = stack; context.uc_stack.ss_size = STACKSIZE; context.uc_stack.ss_flags = 0; context.uc_link = 0; }else{ perror("Erro na pilha"); return (-1); } // Cria contexto makecontext(&context, (void*) (*start_func), 1, arg); task->context = context; if (task != &dispatcher) queue_append((queue_t **) &ready,(queue_t*) task); #ifdef DEBUG printf("task_create: criou tarefa %d\n",task->tid); #endif }
void port_request_socket_update(port_state_t* port_state, sock_state_t* sock_state) { if (queue_enqueued(sock_state_to_queue_node(sock_state))) return; queue_append(&port_state->sock_update_queue, sock_state_to_queue_node(sock_state)); }
void minimsg_network_handler(network_interrupt_arg_t* arg) { interrupt_level_t old_level = set_interrupt_level(DISABLED); //disable interrupt //Get header and destination port mini_header_t receivedHeader; memcpy(&receivedHeader, arg->buffer, sizeof(mini_header_t)); int destPort = (int)unpack_unsigned_short(receivedHeader.destination_port); assert(destPort >= UNBOUNDED_PORT_START && destPort <= UNBOUNDED_PORT_END); // sanity checking //if the unbounded port has not been initialized, throw away the packet if (g_unboundedPortPtrs[destPort] == NULL) { set_interrupt_level(old_level); //restore interrupt level return; } //queue the packet and V the semaphore assert(g_unboundedPortPtrs[destPort]->port_type == 'u' && g_unboundedPortPtrs[destPort]->unbound_port.datagrams_ready != NULL && g_unboundedPortPtrs[destPort]->unbound_port.incoming_data != NULL); int appendSuccess = queue_append(g_unboundedPortPtrs[destPort]->unbound_port.incoming_data, (void*)arg); AbortOnCondition(appendSuccess == -1, "Queue_append failed in minimsg_network_handler()"); semaphore_V(g_unboundedPortPtrs[destPort]->unbound_port.datagrams_ready); set_interrupt_level(old_level); //restore interrupt level }
void port_add_deleted_socket(port_state_t* port_state, sock_state_t* sock_state) { if (queue_enqueued(sock_state_to_queue_node(sock_state))) return; queue_append(&port_state->sock_deleted_queue, sock_state_to_queue_node(sock_state)); }
int main (int argc, char *argv[]) { pthread_t thread [NUM_THREADS] ; long i, status ; for (i=0; i<N; i++){ elemento[i].item = random() % 100; elemento[i].prev = NULL ; elemento[i].next = NULL ; } queue_integer= NULL; for (i=0;i<10;i++){ queue_append((queue_t **)&queue_integer,(queue_t *)&elemento[i]); } for (i=0; i<NUM_THREADS; i++){ printf ("Main: criando thread %02ld\n", i); status = pthread_create(&thread[i], NULL, threadBody, (void *) i); if (status){ perror ("pthread_create") ; exit (1) ; } } pthread_exit (NULL) ; }
cmd_t * cmd_set(char *setname_p, cmd_kind_t kind, char *fcnname_p) { cmd_t *new_p; set_t *set_p; set_p = set_find(setname_p); if (!set_p) { semantic_err(gettext("no set named \"$%s\""), setname_p); return (NULL); } if (kind == CMD_CONNECT && !fcn_find(fcnname_p)) { semantic_err(gettext("no function named \"&%s\""), fcnname_p); return (NULL); } new_p = new(cmd_t); queue_init(&new_p->qn); #ifdef LATEBINDSETS new_p->isnamed = B_TRUE; new_p->expr.setname_p = setname_p; #else new_p->isnamed = B_FALSE; new_p->expr.expr_p = expr_dup(set_p->exprlist_p); #endif new_p->isnew = B_TRUE; new_p->kind = kind; new_p->fcnname_p = fcnname_p; (void) queue_append(&g_cmdlist, &new_p->qn); return (new_p); } /* end cmd_set */
/* * Appends an void* to the multilevel queue at the specified level. * Return 0 (success) or -1 (failure). */ int multilevel_queue_enqueue(multilevel_queue_t queue, int level, void* item) { if (NULL == queue || level < 0 || level >= queue->lvl) return -1; return queue_append(queue->q[level], item); }
void task_yield() { // Adiciona a tarefa em execução na fila de prontas queue_append((queue_t**) &ready, (queue_t*) exec); // Retorna o controle para a tarefa dispatcher task_switch(&dispatcher); }
//Call after setting disk.h's 'disk_size' superblock_t minifile_initialize(superblock_t sblock){ int i; inode_t root_inode; inode_t first_inode; block_t first_block; if(DEBUG) printf("minifile_initialize starting...\n"); //CREATE SUPERBLOCK sblock = (superblock_t) malloc(sizeof(struct superblock)); sblock->data.disk_size = disk_size; sblock->data.magic_number = 19540119; //SET UP INODES AND BLOCKS sblock->data.inode_queue_FREE = queue_new(); sblock->data.inode_queue_ACTIVE = queue_new(); sblock->data.block_queue_FREE = queue_new(); sblock->data.block_queue_ACTIVE = queue_new(); if(DEBUG) printf("inode nad block queues created. Their lengths are: %d %d %d %d\n", queue_length(sblock->data.inode_queue_FREE),queue_length(sblock->data.inode_queue_ACTIVE), queue_length(sblock->data.block_queue_FREE),queue_length(sblock->data.block_queue_ACTIVE)); root_inode = create_inode(0); first_inode = create_inode(1); queue_append(sblock->data.inode_queue_ACTIVE,(any_t)root_inode); queue_append(sblock->data.inode_queue_FREE,(any_t)first_inode); for(i=2;i<(0.1*disk_size);i++){ queue_append(sblock->data.inode_queue_FREE,(any_t)create_inode(i)); } first_block = (block_t) malloc(sizeof(struct block)); queue_append(sblock->data.block_queue_FREE,(any_t)first_block); for(i=1;i<(0.9*disk_size-1);i++){ queue_append(sblock->data.block_queue_FREE,(any_t)create_block(i)); } if(DEBUG) printf("inode and block queues created. Their lengths are: %d %d %d %d\n", queue_length(sblock->data.inode_queue_FREE),queue_length(sblock->data.inode_queue_ACTIVE), queue_length(sblock->data.block_queue_FREE),queue_length(sblock->data.block_queue_ACTIVE)); //FINISH SETTING UP SUPERBLOCK sblock->data.root_inode = root_inode; //RETURN SUPERBLOCK return sblock; }
static void ev_irc_cb(EV_P_ ev_io *w, int revents) { irc_bot *b = w->data; size_t msg_space = 512; size_t offset = 0, msg_len = 0, buff_len = 0; char *msg, *msg_start, *msg_end, *buff_end; char buff[RECV_BUFF_SIZE]; if (revents & EV_READ) { msg = malloc(sizeof(char) * msg_space); buff_end = buff + RECV_BUFF_SIZE; while ((buff_len = recv(b->sockfd, &buff, RECV_BUFF_SIZE, 0)) > 0) { // find first msg msg_start = buff; msg_end = strstr(msg_start, "\r\n"); while (msg_end != NULL) { // calculate msg length msg_len = (msg_end - msg_start + offset) / sizeof(char); // if there's not enough space allocated for this msg, realloc if (msg_space < msg_len + 1) { while (msg_space < msg_len + 1) msg_space = msg_space << 1; msg = realloc(msg, sizeof(char) * msg_space); } // copy the msg, add nul-byte strncpy(msg + offset, msg_start, msg_len); msg[msg_len] = '\0'; // reset offset if (offset > 0) offset = 0; // do something with message printf("msg: %s\n", msg); queue_append(b->irc_in_queue, msg); // find the next message msg_start = msg_end + 2; msg_end = strstr(msg_start, "\r\n"); // if we have a partial message if (msg_end == NULL && (buff + buff_len * sizeof(char) + 1) == buff_end) { msg_len = (buff_end - msg_start) / sizeof(char); strncpy(msg, msg_start, msg_len); offset = msg_len - 1; } } } free(msg); } }
void minithread_stop() { current_thread->status = BLOCKED; semaphore_P(blocked_q_lock); queue_append(blocked_q,current_thread); semaphore_V(blocked_q_lock); scheduler(); }
int append_apply_test(){ queue* q = queue_create(); int x = 0; int y = 1; int z = 2; queue_append(q, &x); queue_append(q, &y); queue_append(q, &z); queue_append(q, &x); printf("Queue size is %zu\n", queue_size(q)); int index = 0; queue_apply(q, show_one, &index); queue_destroy(q,false); return 0; }
/** * Have TGDB append a command to it's list of commands it needs to run. * * \param tgdb * An instance of the tgdb library to operate on. * * \param request * The requested command to have TGDB process. * * \return * 0 on success or -1 on error */ int Ctgdb::Queue_append(tgdb_request_ptr request) { if (!request) return -1; queue_append(gdb_client_request_queue, request); return 0; }
// The employee unpack function which unpacks phones to hand out to the customer. int employee_unpack() { phone_t new_phone = (phone_t) malloc(sizeof(struct phone)); new_phone -> serial_no = assign_serial_no(); printf("Stocked: %d", new_phone->serial_no); queue_append(phoneQueue, new_phone); semaphore_V(phoneSem); return 0; }
int main (int argc, char const *argv[]) { UNUSED_ARG(argc); UNUSED_ARG(argv); //Create queue lpqueue * q = queue_init(10); assert(q != NULL); //Add 10 elements (fill the queue) bool success; unsigned int i; printf("\nEnqueue... "); for(i = 0; i < 10; i++) { printf("%u, ", i); success = queue_append(q, strings[i], strlen(strings[i]), true); assert(success == true); } //Should be full success = queue_append(q, strings[1], strlen(strings[1]), true); assert(success == false); //Pop 10 elements void * data; size_t size; printf("\nDequeue... "); for(i = 0; i < 10; i++) { printf("%u, ", i); success = queue_pop(q, &data, &size); assert(success == true); //Check that they are ok assert(size = strlen(strings[i])); assert(memcmp(data, strings[i], size) == 0); free(data); } //Should now be empty success = queue_pop(q, &data, &size); assert(success == VALUES_QUEUE_EMPTY); printf("\nTEST SUCCESSFUL!\n"); return 0; }
/* * semaphore_P(semaphore_t sem) * Wait on the semaphore. */ void semaphore_P(semaphore_t sem) { while(atomic_test_and_set(&(sem->mutex))); if (--sem->limit < 0) { queue_append(sem->waiting, minithread_self()); sem->mutex = 0; minithread_stop(); } else { sem->mutex = 0; } }
int minithread_exit(minithread_t completed) { current_thread->status = DEAD; semaphore_P(dead_q_lock); queue_append(dead_q, current_thread); semaphore_V(dead_q_lock); semaphore_V(dead_sem); scheduler(); while(1); return 0; }
void test_queue5(queue_t q, queue_pfcompare_t pf){ queue_append(q, &a); queue_reverse(q); queue_sort(q, pf); queue_element_t e = NULL; queue_remove(q, &e); if(queue_size(q) != 0) puts("test_queue5 failed @ line 289"); if(q->head != NULL) puts("test_queue5 failes @ line 291"); }
/** * Network handler function which gets called whenever packet * arrives. Handler disables interrupts for duration of function. * Puts packet onto pkt_q to be processed later by process_packets * thread. */ void network_handler(network_interrupt_arg_t* pkt){ interrupt_level_t l; l = set_interrupt_level(DISABLED); if (queue_append(pkt_q, pkt)){ //queue was not initialized set_interrupt_level(l); return; } set_interrupt_level(l); semaphore_V(pkt_available_sem); //wake up packet processor return; }
/* Final proc of all newly created minithreads. Will never terminate and threads add * themselves to the cleanup_queue and then context switch to the reaper thread to perform * all the necesary cleanup steps to dispose of the thread properly and safely. */ int minithread_cleanup(int* id) { minithread_t* mini = minithread_self(); mini->status = ZOMBIE; interrupt_level_t old_level = set_interrupt_level(DISABLED); queue_append(schedule_data->cleanup_queue, mini); set_interrupt_level(old_level); // notify reaper thread and let it run semaphore_V(reaper_sema); minithread_stop(); return 0; }
void test_queue1(queue_t q){ queue_append(q, &a); queue_append(q, &b); queue_append(q, &c); queue_link_t cur = q->head; int k = 0; while(k < 3){ if(*(int*)cur->e != k){ puts("test_queue1 failes @ line 179"); } k++; cur = cur->next; } queue_element_t e = NULL; queue_remove(q, &e); queue_remove(q, &e); queue_remove(q, &e); if(queue_size(q) != 0) puts("test_queue1 failed @ line 189"); if(q->head != NULL) puts("test_queue1 failes @ line 191"); }
void test_queue2(queue_t q){ queue_append(q, &a); queue_append(q, &d); queue_append(q, &c); queue_link_t cur = q->head; int k = 0; while(k < 3){ if(k == 0 && *(int*)cur->e != 0) puts("test_queue2 failed @ line 201"); if(k == 1 && *(int*)cur->e != -4) puts("test_queue2 failed @ line 202"); if(k == 2 && *(int*)cur->e != 2) puts("test_queue2 failed @ line 203"); k++; cur = cur->next; } queue_element_t e = NULL; queue_remove(q, &e); queue_remove(q, &e); queue_remove(q, &e); if(queue_size(q) != 0) puts("test_queue2 failed @ line 212"); if(q->head != NULL) puts("test_queue2 failes @ line 214"); }
/* * semaphore_P(semaphore_t sem) * P on the sempahore. */ void semaphore_P(semaphore_t sem) { /* while (1 == atomic_test_and_set(&(sem->lock))) ; */ if (0 > --(sem->count)) { queue_append(sem->wait, minithread_self()); /* atomic_clear(&(sem->lock)); */ minithread_stop(); } /* else { atomic_clear(&(sem->lock)); } */ }
int reverse_test(){ queue* q = queue_create(); int x = 0; int y = 1; int z = 2; queue_append(q, &x); queue_append(q, &y); queue_append(q, &z); assert(queue_size(q) == 3); int *ret_val; queue_reverse(q); queue_remove(q, (queue_element **)&ret_val); assert(*ret_val == z); queue_remove(q, (queue_element **)&ret_val); assert(*ret_val == y); queue_remove(q, (queue_element **)&ret_val); assert(*ret_val == x); queue_destroy(q,false); return 0; }
void test_queue4(queue_t q, queue_pfcompare_t pf){ queue_append(q, &b); queue_append(q, &c); queue_append(q, &d); queue_append(q, &a); queue_reverse(q); queue_link_t cur = q->head; int k = 0; while(k < 4){ if(k == 0 && *(int*)cur->e != 0) puts("test_queue3 failed @ line 252"); if(k == 1 && *(int*)cur->e != -4) puts("test_queue3 failed @ line 253"); if(k == 2 && *(int*)cur->e != 2) puts("test_queue3 failed @ line 254"); if(k == 3 && *(int*)cur->e != 1) puts("test_queue3 failed @ line 255"); k++; cur = cur->next; } cur = q->head; queue_sort(q, pf); k = 0; while(k < 4){ if(k == 0 && *(int*)cur->e != -4) puts("test_queue2 failed @ line 263"); if(k == 1 && *(int*)cur->e != 0) puts("test_queue2 failed @ line 264"); if(k == 2 && *(int*)cur->e != 1) puts("test_queue2 failed @ line 265"); if(k == 3 && *(int*)cur->e != 2) puts("test_queue2 failed @ line 266"); k++; cur = cur->next; } queue_element_t e = NULL; queue_remove(q, &e); queue_remove(q, &e); queue_remove(q, &e); queue_remove(q, &e); if(queue_size(q) != 0) puts("test_queue4 failed @ line 277"); if(q->head != NULL) puts("test_queue4 failes @ line 279"); }