/* * Initialization */ void init_flattener(flattener_t *flat, term_manager_t *mngr) { flat->terms = term_manager_get_terms(mngr); flat->manager = mngr; init_int_queue(&flat->queue, 0); init_int_hset(&flat->cache, 128); init_ivector(&flat->resu, 64); }
/* * Same thing for the internal queue */ static int_queue_t *intern_tbl_get_queue(intern_tbl_t *tbl) { int_queue_t *tmp; tmp = tbl->queue; if (tmp == NULL) { tmp = (int_queue_t *) safe_malloc(sizeof(int_queue_t)); init_int_queue(tmp, 100); tbl->queue = tmp; } return tmp; }
void uart1_receiver_server() { bwdebug( DBG_SYS, UART1_RECEIVER_DEBUG_AREA, "UART1_RECEIVER_SERVER: enters" ); //Register the server RegisterAs("uart1_receiver"); //Create the notifier int notifier_tid = Create(UART1_RECEIVER_NOTIFIER_PRIORITY, &uart1_receiver_notifier); bwdebug( DBG_SYS, UART1_RECEIVER_DEBUG_AREA, "UART1_RECEIVER_SERVER: uart1_receiver_server_notifier created. [tid: %d priority: %d]", notifier_tid, UART1_RECEIVER_NOTIFIER_PRIORITY ); //Request & Reply UART_request request; UART_reply reply; //Buffers Char_queue cqueue; init_char_queue( &cqueue ); Int_queue iqueue; init_int_queue( &iqueue ); FOREVER { //Utility functions for communication int sender_tid = -1; int target_tid = -1; //Receive request from: // system function (Getc) // notifier (uart1_receiver_notifier) //bwprintf( COM2, "A\n" ); bwdebug( DBG_SYS, UART1_RECEIVER_DEBUG_AREA, "UART1_RECEIVER_SERVER: Waiting for request" ); Receive(&sender_tid, (char *) &request, sizeof(request)); switch(request.type){ case UART1_RECEIVE_REQUEST: //Enqueue the system function tid to reply later bwdebug( DBG_SYS, UART1_RECEIVER_DEBUG_AREA, "UART1_RECEIVER_SERVER: Getc request from [sender_tid: %d]", sender_tid ); //bwprintf( COM2, "B\n" ); enqueue_int_queue( sender_tid, &iqueue ); break; case UART1_RECEIVE_NOTIFIER_REQUEST: //Reply to unblock the notifier bwdebug( DBG_SYS, UART1_RECEIVER_DEBUG_AREA, "UART1_RECEIVER_SERVER: message received from notifier" ); //bwprintf( COM2, "C\n" ); Reply(sender_tid, (char *) 0, 0); //Enqueue received character //if ( iqueue.size > 0 ){ //todo_debug( 0x7, 1 ); char_queue_push( request.ch, &cqueue ); //todo_debug( 0x8, 1 ); //} break; default: // Invalid Request bwdebug( DBG_SYS, UART1_RECEIVER_DEBUG_AREA, "UART1_RECEIVER_SERVER: invalid request from [sender_tid: %d]", sender_tid ); //bwprintf( COM2, "D\n" ); reply.type = INVALID_REQUEST; reply.ch = 0; Reply(sender_tid, (char *) &reply, sizeof(reply)); break; } //If there are system functions to receive //and characters to be sent while(iqueue.size > 0 && cqueue.size > 0) { //Prepare the reply to the system function target_tid = dequeue_int_queue( &iqueue ); reply.type = UART1_RECEIVE_REPLY; reply.ch = char_queue_pop( &cqueue ); //Perform the reply bwdebug( DBG_SYS, UART1_RECEIVER_DEBUG_AREA, "UART1_RECIEVER_SERVER: replying [to: %d with: %d]", target_tid, reply.ch ); //bwprintf( COM2, "E\n" ); Reply(target_tid, (char *) &reply, sizeof(reply)); } } }