Esempio n. 1
0
File: usart.c Progetto: art103/ar-t6
/**
 * @brief  Initialize the USART1 serial port
 * @param  None
 * @retval None
 */
void usart_init() {
	/* Clock configuration -------------------------------------------------------*/
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA,
			ENABLE);

	/* Configure the GPIO ports( USART1 Transmit and Receive Lines) */
	/* Configure the USART1_Tx as Alternate function Push-Pull */
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	/* Configure the USART1_Rx as input floating */
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	/* USART1 configuration ------------------------------------------------------*/
	/* USART1 configured as follow:
	 - BaudRate = 115200 baud
	 - Word Length = 8 Bits
	 - One Stop Bit
	 - No parity
	 - Hardware flow control disabled (RTS and CTS signals)
	 - Receive and transmit enabled
	 */
	USART_InitTypeDef USART_InitStructure;
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl =
			USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

	/* Configure the USART1 */
	USART_Init(USART1, &USART_InitStructure);

	/* Enable USART1 interrupt */
	NVIC_InitTypeDef nvic;
	nvic.NVIC_IRQChannel = USART1_IRQn;
	nvic.NVIC_IRQChannelPreemptionPriority = 15;
	nvic.NVIC_IRQChannelSubPriority = 1;
	nvic.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&nvic);

	Queue_init(&txbuf);
	Queue_init(&rxbuf);

	/* Enable the USART1 */
	USART_Cmd(USART1, ENABLE);
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

	// print invitation
	usart_puts("FS-T6\r\n");
}
Esempio n. 2
0
void Serial_begin(uint32_t speed, uint32_t config)
{
    uint8_t ucsra;
    uint32_t ubrr;

    ucsra = config;

    if((speed & SERIAL_CUSTOM_UBRR_MASK) == 0UL)
    {
        ubrr = (F_CPU - 4U * speed) / (8U * speed);

        if(ubrr > 0x0FFFU)
        {
            ubrr = (F_CPU - 8U * speed) / (16U * speed);
            ucsra &= ~(1U << U2X0);
        }
        else
        {
            ucsra |= (1U << U2X0);
        }
    }
    else
    {
        ubrr = speed & ~SERIAL_CUSTOM_UBRR_MASK;

        if((speed & SERIAL_CUSTOM_UBRR_DIV16) == 0UL)
        {
            ucsra |= (1U << U2X0);
        }
        else if((speed & SERIAL_CUSTOM_UBRR_DIV8) == 0UL)
        {
            ucsra &= ~(1U << U2X0);
        }
        else
        {
            ASSERT(0); /* Invalid custom speed option. */
        }
    }

    PRR &= ~(1U << PRUSART0); /* Enable UART clock. */

    UCSR0B = 0; /* Disable TX and RX. */

    Queue_init(&RxBuff, &RxBuff_data, sizeof(RxBuff_data), 1);
    Queue_init(&TxBuff, &TxBuff_data, sizeof(TxBuff_data), 1);

    /* Set speed and other configurations. */
    UBRR0 = ubrr;
    UCSR0A = ucsra;
    UCSR0B = config >> 8U;
    UCSR0C = config >> 16U;
}
/*
 *  ======== CERuntime_init ========
 */
Void CERuntime_init(Void)
{

    GT_init();



    /* allow user to over-ride via CE_TRACE. */
    GT_set(Global_getenv("CE_TRACE"));
    Global_init();

    Sem_init();
    SemMP_init();


    Memory_init();
    Queue_init();
    Comm_init();
    Thread_init();
    Processor_init();
    LockMP_init();  /* Must be called before DMAN3_init() */
    Algorithm_init();
    XdmUtils_init();
    Lock_init();

    Engine_init();
    Server_init();

}
Esempio n. 4
0
void bfs(BFSVertex* vertices, int nelem, int s) {
	Queue q;
	for (int u = 0; u < nelem; u++) {
		if (vertices[u].super.n != s) {
			vertices[u].super.color = WHITE;
			vertices[u].d = 1E10;
			vertices[u].super.parent = -1;
		}
	}
	vertices[s].super.color = GRAY;
	vertices[s].d = 0;
	vertices[s].super.parent = -1;
	Queue_init(&q);
	Queue_create_queue(&q, nelem);
	Queue_enqueue(&q, s);
	while (!Queue_is_empty(&q)) {
		int u = Queue_dequeue(&q);
		for (Adj* adj_v = vertices[u].super.first; adj_v; adj_v = adj_v->next) {
			if (vertices[adj_v->n].super.color == WHITE) {
				vertices[adj_v->n].super.color = GRAY;
				vertices[adj_v->n].d = vertices[u].d + 1;
				vertices[adj_v->n].super.parent = u;
				Queue_enqueue(&q, adj_v->n);
			}
		}
		vertices[u].super.color = BLACK;
	}
}
Esempio n. 5
0
int main()
{
	Queue queue = Queue_init();

	bool isEnqueued = Enqueue(queue, 15);

	QElementType* value = (QElementType*)malloc(sizeof(int));
	bool isDequeued = Dequeue(queue, value);

	for(int i = 0; i < MAXSIZE; i++)
	{
		isEnqueued = Enqueue(queue, i);

		if(isEnqueued == true){
			printf("%d Ok.\n", i);
		}
		else
		{
			printf("%d Full.\n",i);
		}
	}

	printf("%d\n", queue->front);

	for(int i = 0; i < MAXSIZE; i++)
	{
		isDequeued = Dequeue(queue, value);
 		if(isDequeued == true)
 		{
 			printf("%d\n", *value);
 		}
	}

	return 0;
}
Esempio n. 6
0
/*
 *  ======== CERuntime_init ========
 */
Void CERuntime_init(Void)
{
    extern Void IPC_generatedInit();

    GT_init();


    /* if CE_DEBUG is set, turn on tracing and DSP auto trace collection */
    if (Global_getenv("CE_DEBUG") != NULL) {
        extern Bool   Engine_alwaysCollectDspTrace;
        extern String Engine_ceDebugDspTraceMask;

        Engine_alwaysCollectDspTrace = TRUE;



        if (Global_getenv("CE_DEBUG")[0] == '1') {
            GT_set("*+67,CE-3,GT_time=0,GT_prefix=1235");
            Engine_ceDebugDspTraceMask = "*+67,GT_prefix=1235,GT_time=3";
        }
        else if (Global_getenv("CE_DEBUG")[0] == '2') {
            GT_set(
                "*+01234567,CE-3,ti.sdo.ce.osal.SemMP=67,OG=467,OM=4567,OC=67,GT_time=0,GT_prefix=1235");
            Engine_ceDebugDspTraceMask =
                "*+01234567,CR=67,ti.sdo.fc.dman3-2,ti.sdo.fc.dskt2-2,GT_prefix=1235,GT_time=3";
        } else {
            GT_set("*+01234567,CE-3,GT_time=0,GT_prefix=12345");
            Engine_ceDebugDspTraceMask = "*+01234567,GT_prefix=12345,GT_time=3";
        }
    }

    if (Global_getenv("CE_CHECK") != NULL) {
        extern Bool VISA_checked;

        /*
         * Currently just change _this_ processor's value... perhaps we should
         * enable remote processors as well?
         */
        if (Global_getenv("CE_CHECK")[0] == '1') {
            VISA_checked = TRUE;
            /* turn on all GT_7CLASS trace (errors) */
            GT_set("*+7");
        } else if (Global_getenv("CE_CHECK")[0] == '0') {
            VISA_checked = FALSE;
        } else {
            /* leave it whatever it was... maybe we should drop a warning? */
        }
    }

    /* allow user to over-ride via CE_TRACE. */
    GT_set(Global_getenv("CE_TRACE"));
    IPC_generatedInit();
    Global_init();

    Sem_init();
    SemMP_init();


    Memory_init();
    Queue_init();
    Comm_init();
    Thread_init();
    Processor_init();
    LockMP_init();  /* Must be called before DMAN3_init() */
    Algorithm_init();
    XdmUtils_init();
    Lock_init();

    Engine_init();
    Server_init();

}
void track_server_code (){
	RegisterAs("TRACKSERVER");
	// Server data
	sw_info sw_infos [NUM_SWITCHES];
	int sensor_info [NUM_SENSORS];		// time when the sensor is hit
	int broken_sensors[NUM_SENSORS] = {0};
	// Server's track data
	track_node track[TRACK_MAX];
	init_track(track);
	init_broken_sensors(broken_sensors);
	track_node *src, *dst;
	// Received stuff
	track_receive rcv_buff;
	int sender_tid;
	int request_type;
	int sensor_index = 0;
	//Reply buffs
	int reply_dist = 0;
	int reply_index = 0;
	struct track_loc reply_loc;
	char reply_buff;
	int reply_buff_loc_dist [2];
	
	struct train_server_request tsr;
	int train_server_id;
	while((train_server_id = WhoIs("TRAINSERVER")) == -1);
    int track_to_train_courier_tid = Create(PR_COURIER, track_to_train_courier_code);
    int track_to_train_courier_ready = 0;
    Queue train_serverQ;
    struct train_server_request allocated_buff[TRACK_TO_TRAIN_QUEUE_SIZE];
    Queue_init (&train_serverQ, allocated_buff, TRACK_TO_TRAIN_QUEUE_SIZE, sizeof(struct train_server_request));
    //things for shortest path request
    track_node* path[TRACK_MAX] = {0};
    int dist[TRACK_MAX] = {0};
    int shortest_dist = 0;
    routing_command commands[TRACK_MAX];
    init_routing_commands(commands);
    // switch_command sw_commands[NUM_MAX_SWITCH_ROUTE_COMMANDS];
    // memset(sw_commands, 0, NUM_MAX_SWITCH_ROUTE_COMMANDS * sizeof(switch_command));

    //things for changing switches when train goes
    //within a specified route
    Queue switch_queues[NUM_TRAINS];
    Queue *Q;
    switch_command switch_queue_buffers[NUM_TRAINS][NUM_MAX_SWITCH_ROUTE_COMMANDS];
    memset(switch_queue_buffers, 0, NUM_TRAINS * NUM_MAX_SWITCH_ROUTE_COMMANDS * sizeof(switch_command));
    int trnum;
    for (trnum = 0; trnum < NUM_TRAINS; trnum++) {
        Queue_init(&switch_queues[trnum], switch_queue_buffers[trnum], NUM_MAX_SWITCH_ROUTE_COMMANDS, sizeof(switch_command));
    }
    //


    //Reservation data structures
    track_reservation track_reservations[TRACK_MAX];
    train_reservation_info tr_reservation_infos[NUM_TRAINS];
    memset(track_reservations, 0, TRACK_MAX * sizeof(track_reservation));
    memset(tr_reservation_infos, -1, NUM_TRAINS * sizeof(train_reservation_info));

    //
    Queue isReservableQueue;
    isReservableQueueItem isReservableQueueItems[NUM_TRAINS];
    Queue_init(&isReservableQueue, isReservableQueueItems, NUM_TRAINS, sizeof(isReservableQueueItem));
    isReservableQueueItem irqi;

    int reserve_ret = -1;
	int ret;
	while(1){
		Receive(&sender_tid, &rcv_buff, sizeof(track_receive));
		request_type = rcv_buff.type;
		switch (request_type){
			case TRACK_TYPE_COURIER_RTS:
				track_to_train_courier_ready = 1;
				break;
			case TRACK_TYPE_SENSOR_INFO:
				sensor_index = (rcv_buff.data.track_data.data2 - 'A')*16+rcv_buff.data.track_data.data1-1;
				//if (sensor_index == 56) {HardDebug("D9 is hit. Hit time:%d. Rcv Time:%d", rcv_buff.data.track_data.time, Time());}
				sensor_info[sensor_index] = rcv_buff.data.track_data.time;
				Reply(sender_tid, &reply_buff, sizeof(char));
				tsr.type = TRAIN_TYPE_SENSOR_UPDATE;
				memcpy(&tsr.data, &rcv_buff.data.track_data, sizeof(track_info));
				if (!broken_sensors[sensor_index]) {
					//Send(train_server_id, &tsr, sizeof(struct train_server_request), &reply_buff, sizeof(char));
					ret = Queue_push(&train_serverQ, &tsr);
					Warning(ret == 0, "TRACKSERVER: Queue_push failed");
				}
				break;
			case TRACK_TYPE_SWITCH_INFO:
				(sw_infos[(int)rcv_buff.data.track_data.data1]).swdir = rcv_buff.data.track_data.data2;
				(sw_infos[(int)rcv_buff.data.track_data.data1]).time = rcv_buff.data.track_data.time;
				Reply(sender_tid, &reply_buff, sizeof(char));
				tsr.type = TRAIN_TYPE_SWITCH_UPDATE;
				memcpy(&tsr.data, &rcv_buff.data.track_data, sizeof(track_info));
				//Send(train_server_id, &tsr, sizeof(struct train_server_request), &reply_buff, sizeof(char));
				ret = Queue_push(&train_serverQ, &tsr);
				Warning(ret == 0, "TRACKSERVER: Queue_push failed");
				break;
			case TRACK_TYPE_DIST:
				if((int)rcv_buff.data.track_data.data1 >= TRACK_MAX || (int)rcv_buff.data.track_data.data2 >= TRACK_MAX) {
					reply_dist = 0;
				}
				else {
					src = &track[(int)rcv_buff.data.track_data.data1];
					dst = &track[(int)rcv_buff.data.track_data.data2];
					reply_dist = get_dist_between_nodes(src, dst, sw_infos);
				}
				Reply(sender_tid, &reply_dist, sizeof(int));
				break;
			case TRACK_TYPE_NEXT_LOC:
				//get current loc_index and find next node
				__get_next_node((int)rcv_buff.data.track_data.data1, track, sw_infos, &reply_loc.index, reply_loc.name);
				Reply(sender_tid, &reply_loc, sizeof(struct track_loc));
				break;
			case TRACK_TYPE_NEXT_SENSOR:
				reply_index = get_next_sensor_index((int)rcv_buff.data.track_request_data.data1, track, sw_infos, broken_sensors, &tsr.data.spd.spd_val);
				Assert(reply_index == tsr.data.spd.spd_val.next_expected_sensor_index, "reply_index != result->next_expected_sensor_index");
				//Debug("TRACKSERVER: Got next expected sensor %d, after that %d", tsr.data.spd.spd_val.next_expected_sensor_index, tsr.data.spd.spd_val.sensor_after_expected_sensor);
				Reply(sender_tid, &reply_index, sizeof(int));
				tsr.type = TRAIN_TYPE_SENSOR_PREDICTION;
				tsr.data.spd.trnum = (int)rcv_buff.data.track_request_data.data2;
				if (rcv_buff.data.track_request_data.data3) {
					//Send(train_server_id, &tsr, sizeof(train_server_request), &reply_buff, sizeof(char));
					ret = Queue_push(&train_serverQ, &tsr);
					Warning(ret == 0, "TRACKSERVER: Queue_push failed");
				}
				break;
			case TRACK_TYPE_LOC_NAME:
				Reply(sender_tid, (char*)track[(int)rcv_buff.data.track_data.data1].name, 6 * sizeof(char));
				break;
			case TRACK_TYPE_SENSOR_REVERSE:
				reply_index = get_reverse_sensor((int)rcv_buff.data.track_request_data.data1, track, sw_infos, broken_sensors, &tsr.data.spd.spd_val);
				Reply(sender_tid, &reply_index, sizeof(int));
				tsr.data.spd.trnum = (int)rcv_buff.data.track_request_data.data2;
				if (rcv_buff.data.track_request_data.data3) {
					//Send(train_server_id, &tsr, sizeof(train_server_request), &reply_buff, sizeof(char));
					ret = Queue_push(&train_serverQ, &tsr);
					Warning(ret == 0, "TRACKSERVER: Queue_push failed");
				}
				break;

			case TRACK_TYPE_UPDATE_LOC_AND_DISTANCE:
				
				translate_sensor_loc(rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data2, track, sw_infos, reply_buff_loc_dist);
				Reply(sender_tid, reply_buff_loc_dist, 2*sizeof(int));
				break;

			case TRACK_TYPE_FIND_ROUTE:
				Warning(((0 <= rcv_buff.data.track_request_data.data2 && rcv_buff.data.track_request_data.data2 < TRACK_MAX) &&
						 (0 <= rcv_buff.data.track_request_data.data3 && rcv_buff.data.track_request_data.data3 < TRACK_MAX)),
						 "TRACKSERVER: TRACK_TYPE_FIND_ROUTE - Source(%d) or destination(%d) is invalid", rcv_buff.data.track_request_data.data2, rcv_buff.data.track_request_data.data3);
				//Debug("FINDING ROUTE:trnum:%d ~ src:%s ~ dest:%s ~ offset:%d",rcv_buff.data.track_request_data.data1, track[rcv_buff.data.track_request_data.data2].name, track[rcv_buff.data.track_request_data.data3].name, rcv_buff.data.track_request_data.data4);
                shortest_dist = shortest_path((int)rcv_buff.data.track_request_data.data2, (int)rcv_buff.data.track_request_data.data3, track, path, dist, track_reservations);
                if (shortest_dist == __INFINITY)
                {
                	shortest_path((int)rcv_buff.data.track_request_data.data2, (int)rcv_buff.data.track_request_data.data3, track, path, dist, 0);
                }
                generate_commands(path, track, commands);
                print_commands(commands);
                
                pull_out_switch_orders(commands, tsr.data.path_commands.sw_commands, &switch_queues[rcv_buff.data.track_request_data.data1]);
                pull_out_reverse_orders(commands, tsr.data.path_commands.rv_commands);
                tsr.type = TRAIN_TYPE_ROUTE_SWITCH_ORDERS;
                Reply(sender_tid, &tsr, sizeof(train_server_request));
                break;

            case TRACK_TYPE_RESERVATION_INIT:
            	reserve_ret = reserve_inital_piece (track_reservations, 
													track, 
													tr_reservation_infos, 
													sw_infos,
													&switch_queues[rcv_buff.data.track_request_data.data1],
													rcv_buff.data.track_request_data.data1, 
													rcv_buff.data.track_request_data.data2, 
													rcv_buff.data.track_request_data.data3,
													rcv_buff.data.track_request_data.data4);
            	Debug("TRACKSERVER: Received reservation initialization request for train %d", rcv_buff.data.track_request_data.data1);
            	tsr.type = TRAIN_TYPE_RESERVATION_SUCCESS;
            	Reply(sender_tid, &tsr, sizeof(train_server_request));
            	break;
            case TRACK_TYPE_RESERVATION:
            	Q = &switch_queues[rcv_buff.data.track_request_data.data1];
            	reserve_ret = reserve_track(track_reservations, track, tr_reservation_infos, sw_infos, Q, rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data2);
            	ret = release_track(track_reservations, track, tr_reservation_infos, sw_infos, rcv_buff.data.track_request_data.data1, rcv_buff.data.track_request_data.data3);
            	Assert (ret != -1, "TRACKSERVER: release failed: ret:%d", ret);
            	tsr.type = (reserve_ret == -1) ? TRAIN_TYPE_RESERVATION_FAILURE : TRAIN_TYPE_RESERVATION_SUCCESS;
            	Reply(sender_tid, &tsr, sizeof(train_server_request));
            	if (/*rcv_buff.data.track_request_data.data2 < 0 ||*/ rcv_buff.data.track_request_data.data3 > 0) {
            		check_isReservableRequests(&isReservableQueue, &train_serverQ, track, sw_infos, track_reservations, tr_reservation_infos, train_server_id);
            	}
            	
            	print_total_reserved_dist(track_reservations, tr_reservation_infos, rcv_buff.data.track_request_data.data1);
             	break;
			case TRACK_TYPE_RESERVATION_REVERSE:
				reserve_ret = reverse_reservation  (track, 
													tr_reservation_infos, 
													sw_infos, 
													rcv_buff.data.track_request_data.data1);
				tsr.type = TRAIN_TYPE_RESERVATION_SUCCESS;
            	Reply(sender_tid, &tsr, sizeof(train_server_request));
				break;
			case TRACK_TYPE_RESERVATION_SENSOR_UPDATE:
				reserve_ret = reservation_sensor_update(track_reservations,
														track,
														tr_reservation_infos,
														sw_infos,
														&switch_queues[rcv_buff.data.track_request_data.data1],
														rcv_buff.data.track_request_data.data1,
														rcv_buff.data.track_request_data.data2,
														rcv_buff.data.track_request_data.data3,
														rcv_buff.data.track_request_data.data4,
														rcv_buff.data.track_request_data.data5);
				tsr.type = (reserve_ret == -1) ? TRAIN_TYPE_RESERVATION_FAILURE : TRAIN_TYPE_RESERVATION_SUCCESS;
            	Reply(sender_tid, &tsr, sizeof(train_server_request));
            	//check_isReservableRequests(&isReservableQueue, &train_serverQ, track, sw_infos, track_reservations, tr_reservation_infos, train_server_id);
				break;
			case TRACK_TYPE_RESERVATION_STOP_UPDATE:
				reserve_ret = reservation_stop_update ( track_reservations,
														track,
														tr_reservation_infos,
														sw_infos,
														&switch_queues[rcv_buff.data.track_request_data.data1],
														rcv_buff.data.track_request_data.data1,
														rcv_buff.data.track_request_data.data2,
														rcv_buff.data.track_request_data.data3,
														rcv_buff.data.track_request_data.data4,
														rcv_buff.data.track_request_data.data5);
				tsr.type = /*(reserve_ret == -1) ? TRAIN_TYPE_RESERVATION_FAILURE :*/ TRAIN_TYPE_RESERVATION_SUCCESS;
            	Reply(sender_tid, &tsr, sizeof(train_server_request));
				break;
			case TRACK_TYPE_IS_RESERVABLE:
				tsr.type = TRAIN_TYPE_REQ_IN_PROGRESS;
				Reply(sender_tid, &tsr, sizeof(train_server_request));
				irqi.trnum = rcv_buff.data.track_request_data.data1;
				irqi.mm = rcv_buff.data.track_request_data.data2;
				ret = Queue_push(&isReservableQueue, &irqi);
				Warning(ret == 0, "TRACKSERVER: Queue_push failed");
				break;
            case TRACK_TYPE_PRINT_RESERVATIONS:
            	Reply(sender_tid, &reply_buff, sizeof(char));
            	print_reservations(track_reservations, track, tr_reservation_infos);
            	break;

			default:
				Reply(sender_tid, &reply_buff, sizeof(char));
				Warning(0,"Unknown receive in track_server_code: type=%d from %d", request_type, sender_tid);
				break;
		}
		if (track_to_train_courier_ready && !Queue_pop(&train_serverQ, &tsr)){
			Reply(track_to_train_courier_tid, &tsr, sizeof(train_server_request));
			track_to_train_courier_ready = 0;
		}
	}
	Exit();
	Warning(0,"Unexpected return from Exit() at track_server_code\n\r");
}
Esempio n. 8
0
int main(int argc, char* argv[])
{
	pthread_t consumer_pool[10];
	pthread_t producer_pool[10];
	EPacket_t producerArgs[10];
	DPacket_t consumerArgs[10];
	char* dataArray[10] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};
	
	char* returnValuePool[10];
	//void (*enqptr)(void* ) = &enq_helper;
	//void (*deqptr)(void* ) = &deq_helper;
	Queue_t kiutqueue;
	char* text = "meow";
	char* text2 = "nuu";
	Queue_init(&kiutqueue);
	int queue = 2; //TODO: PASS IN AS ARG
	
	// Initialization of consumer and producer data packets -------------------
	int i;
	for(i = 0;i < CONSUMERS; i++)
	{
		consumerArgs[i].q = &kiutqueue;
		consumerArgs[i].returnvalue = &returnValuePool[i];
	}
	
	int j;
	for(j = 0;j < PRODUCERS; j++)
	{
		producerArgs[j].q = &kiutqueue;
		producerArgs[j].x = dataArray[j];
		producerArgs[j].queue_size = queue;
	}
	
	// ---------------------------------------------------------------------------
	
	int k;
	for(k = 0; k < PRODUCERS; k++)
	{
		pthread_create(&producer_pool[k], NULL, enq_helper, ((void*)(&producerArgs[k])));			
	}
	
	int l;
	for(l = 0; l< CONSUMERS; l++)
	{
		pthread_create(&consumer_pool[l], NULL, deq_helper, ((void*)(&consumerArgs[l])));	
	}
	
	printf("main getting mainmutex lock\n");
	pthread_mutex_lock(&mainMutex);
	printf("main got mainmutex lock\n");
	
	printf("main shall sleep now\n");
	pthread_cond_wait(&noWorkInSystem,&mainMutex); 
	while (workInSystem != 0 && kiutqueue.size != 0) {
		printf("main sleeping while waiting for 0 workinsystem\n");
		pthread_cond_wait(&noWorkInSystem,&mainMutex); //sleep while workers not done
	}
	printf("main woke up or never slept, is unlocking\n");
	printf("%d workInSystem\n", workInSystem);
	pthread_mutex_unlock(&mainMutex);
	// Signalling for this condition has yet to be done Buggle, please look into it.	
	printf("main released mainmutex lock\n");
	
	int m;
	for(m = 0; m < PRODUCERS; m++)
	{
		pthread_join(producer_pool[m], NULL);			
	}
	
	int n;
	for(n = 0; n < CONSUMERS; n++)
	{
		pthread_join(consumer_pool[n], NULL);			
	}
	
	printf("done\n");
	return 0;
}
Esempio n. 9
0
void Queue_test()
{
	queue_t testQueue;
	Queue_init(&testQueue, 5, sizeof(int));

	assert( Queue_isEmpty(&testQueue) == true );
	assert( Queue_isFull(&testQueue) == false );

	int a = 34;
	int b = 35;
	int c = 36;
	int d = 37;
	int e = 38;

	assert( Queue_push( &testQueue, (uint8_t*)&a ) == true);
	assert( Queue_push( &testQueue, (uint8_t*)&b ) == true);
	assert( Queue_push( &testQueue, (uint8_t*)&c ) == true);

	assert( Queue_isEmpty(&testQueue) == false );
	assert( Queue_isFull(&testQueue) == false );

	assert( Queue_push( &testQueue, (uint8_t*)&d ) == true);
	assert( Queue_push( &testQueue, (uint8_t*)&e ) == true);
	assert( Queue_push( &testQueue, (uint8_t*)&e ) == false);

	assert( Queue_isEmpty(&testQueue) == false );
	assert( Queue_isFull(&testQueue) == true );

	int read;

	assert( Queue_pop( &testQueue, (uint8_t*)&read ) == true);
	assert(read == 34);

	assert( Queue_pop( &testQueue, (uint8_t*)&read ) == true);
	assert(read == 35);

	assert( Queue_pop( &testQueue, (uint8_t*)&read ) == true);
	assert(read == 36);

	assert( Queue_pop( &testQueue, (uint8_t*)&read ) == true);
	assert(read == 37);

	assert( Queue_pop( &testQueue, (uint8_t*)&read ) == true);
	assert(read == 38);

	assert( Queue_pop( &testQueue, (uint8_t*)&read ) == false);
	assert(read == 38);

	assert( Queue_isEmpty(&testQueue) == true );
	assert( Queue_isFull(&testQueue) == false );


	assert( Queue_push( &testQueue, (uint8_t*)&a ) == true);
	assert( Queue_push( &testQueue, (uint8_t*)&b ) == true);
	assert( Queue_push( &testQueue, (uint8_t*)&c ) == true);

	Queue_clear(&testQueue);

	assert( Queue_isEmpty(&testQueue) == true );
	assert( Queue_isFull(&testQueue) == false );
}