Beispiel #1
0
//Read input file function
void* ReqThreads(void* Inputfile) 
{
	char* hostname;
	hostname = malloc(SBUFSIZE*sizeof(char)); //use malloc to allocate memory
	FILE* fp = fopen(Inputfile, "r");
	
	//Read File and Process, list of text, url hostnames
	while (fscanf(fp, INPUTFS, hostname) > 0) {
		
		//If queue is full, wait and sleep
		while(queue_is_full(requesterQ)) { //busy wait
			usleep((rand()% 100)+1);
			continue;   
			} 
		
			pthread_mutex_lock(&Q_lock); //lock so only one thread can access queue
			queue_push(requesterQ, hostname); //push the hostname onto the queue
			pthread_mutex_unlock(&Q_lock); //unlock when done pushing an item

			hostname = malloc(SBUFSIZE*sizeof(char));
	}
	free(hostname); //free the space allocated with malloc
	fclose(fp); //close input file when done
	return 0;
}
Beispiel #2
0
void* Producer(void* args){
	producer_args* arg_p = args;
    char hostname[MAX_NAME_LENGTH];
    char strings[MAX_NAME_LENGTH];
    
    /* Read File and Process*/
    while(fscanf(arg_p->file, INPUTFS, hostname) > 0){
	    
	    /* Prepare to write */
	    strcpy(strings,hostname);   
		
		/* Wait for queue is ready */
		while(queue_is_full(arg_p->q)){
			usleep(random(101));
		}
		
		/*Write to queue */
		pthread_mutex_lock(arg_p->consume_mutex);
		pthread_mutex_lock(arg_p->produce_mutex);
		queue_push(arg_p->q, strings);
		pthread_mutex_unlock(arg_p->produce_mutex);
		pthread_mutex_unlock(arg_p->consume_mutex);
	}
	fclose(arg_p->file);
	
	/*minus 1 thread number before it quit */
	pthread_mutex_lock(arg_p->produce_mutex);
	control-=1;
	pthread_mutex_unlock(arg_p->produce_mutex);
	
	return NULL;
}
Beispiel #3
0
int addEventQueueEntry( EVENT_Q_OBJ q, struct EventQueueEntry *pEntry )
/*  EVENT_Q_OBJ q         -  event queue object to receive entry */
/*  EVENT_Q_ENTRY *pEntry -  address of entry to be added */
{
    struct EventQueueEntry	*current_entry;

    if (queue_is_full( q )) {
        return( -1 );
    }

    if (queue_is_empty( q )) {
        current_entry = q->first = ADDR_FIRST_ARRAY_ENTRY( q );
    }
    else {
        if (q->last == ADDR_LAST_ARRAY_ENTRY( q ))
            current_entry = ADDR_FIRST_ARRAY_ENTRY( q );
        else
            current_entry = q->last + 1;	/* NOT sizeof( EventQueueEntry ) !!!! */
    }

    *current_entry = *pEntry;
    q->last = current_entry;

    return( 0 );
}
void* InputThread (void* p) {
    thread_request_arg_t* args = (thread_request_arg_t*) p;

    /* Local Vars */
    FILE* inputfp = NULL;
   
    /* Open Input File */  
    inputfp = fopen(args->fname, "r");
    if(!inputfp){
        char errorstr[SBUFSIZE];
        sprintf(errorstr, "Error Opening Input File: %s", args->fname);
        perror(errorstr);
        return NULL;
    }	
    
    /* Read File and Process*/
    char hostname[SBUFSIZE];
    //On success, the function returns the number of items of the argument list successfully filled
    // int fscanf ( FILE * stream, const char * format, ... )
    while(fscanf(inputfp, INPUTFS, hostname) > 0){

        // wait until queue is available
        while (1) {

            // lock the request queue mutex while queue is not available
            pthread_mutex_lock(args->mutex_queue);
            
            // if queue is full, unlock mutex
            if (queue_is_full(args->request_queue)) {
                pthread_mutex_unlock(args->mutex_queue);
				// sleep for random time between 0 and 100 microseconds
				// use usleep for microseconds
				// will sleep for random amount of microseconds 
				// rand() % 100 between 0 and 99
                usleep(rand() % 100);
			// exit otherwise
            } else { 
				break; 
			}
        }

        // use malloc to free space for hostname
        char* tocopy = malloc(sizeof(hostname));
        strncpy(tocopy, hostname, sizeof(hostname));

        // push hostname to request queue
        queue_push(args->request_queue, tocopy);

        // unlock the request queue mutex
        pthread_mutex_unlock(args->mutex_queue);
    
    }
    
    /* Close Input File */
    fclose(inputfp);

    return NULL;
}
Beispiel #5
0
int queue_enqueue(volatile void *q, const void *elt, int timeout)
{
	volatile struct generic_queue *gq = q;

	if (queue_is_empty(q)) {
		/* Empty queue, just push something into the front */
		gq->head = gq->memory;
		gq->tail = gq->memory;
	} else {
		if (queue_is_full(q)) {
			/* Full queue, if we have a timeout, keep trying to
			 * insert until we're successful, or the timeout
			 * expires. If not, just yield forever */
			if (-1 == timeout) {
				while(queue_is_full(q)) {
					yield(NULL);
				}
			} else {

				const int expiration_time = get_system_time() + timeout;

				while (queue_is_full(q) && (get_system_time() < expiration_time)) {
					yield(NULL);
				}

				/* If the queue is still full, return an error */
				if (queue_is_full(gq)) {
					return -1;
				}
			}

		}

		if (QUEUE_TAIL_WRAP(gq)) {
			gq->tail = gq->memory;
		} else {
			gq->tail = (uint8_t *)gq->tail + gq->item_size;
		}
	}

	memcpy((void*)gq->tail, elt, gq->item_size);
	gq->len++;

	return 0;
}
Beispiel #6
0
uint32_t queue_new(queue_t * queue, uint8_t ** element)
{
    if (queue_is_full(queue))
        return ERROR_NO_MEMORY;

    *element = &queue->elements[queue->tail * queue->element_size];

    return SUCCESS;
}
Beispiel #7
0
/*
 * Enqueueing.
 * item : a pointer to the item inserted to a queue
 */
void enqueue(void *item) {
    if(queue_is_full()) {
        printf("Fatal error: The queue is full.\n");
        exit(-1);
    }

    queue[in_index] = item;
    in_index = (in_index+1) % memory_size;
}
/* Function for Each Thread to Run */
void* open_read_file(char* file_name)
{
    /* Setup Local Vars */
    FILE* inputfp = NULL;
	char* hostname;
	char errorstr[SBUFSIZE]; 
	
	 
	/* Open Input File */
	inputfp = fopen(file_name, "r");
	if(!inputfp)
	{
		sprintf(errorstr, "Error Opening Input File: %s", file_name);
		perror(errorstr);
		//fprintf(stderr, "Error Opening Input File: %s", file_name);
	}
	
	//malloc hostname for the first time for storage
	hostname = (char*)malloc((MAX_NAME_LENGTH+1)*sizeof(char));
	
	/* Read Each File and Process*/
	while(fscanf(inputfp, INPUTFS, hostname) > 0){
		
		/* Write each domain to the queue */
		pthread_mutex_lock(&mutex_for_queue); //lock the queue
		
		//If a thread tries to write to the queue but finds that it is full, it should sleep for a random period of time between 0 and 100 microseconds
		if(queue_is_full(&q))
		{
			pthread_mutex_unlock(&mutex_for_queue); //unlock the queue so stuff can be taken out of it
			/* Sleep for 0 to 100 uSeconds */
			usleep(rand()%100);
			pthread_mutex_lock(&mutex_for_queue); //relock the queue for pushing
		}
		
		
		if(queue_push(&q, hostname) == QUEUE_FAILURE) //push hostname onto the queue
		{
			fprintf(stderr, "ERROR: queue_push failed with value: %s\n", hostname);
		}
		pthread_mutex_unlock(&mutex_for_queue); //unlock the queue
		
		//create a new pointer for the mutex unlock process
		hostname = (char*)malloc((MAX_NAME_LENGTH+1)*sizeof(char));
	 }
	 
	 //after we're done reading the file, close it
	 fclose(inputfp);
    
    free(hostname); //free the local chunk we have leftover
    hostname=NULL;
    
    /* Exit, Returning NULL*/
    pthread_exit(NULL);
    return NULL;
}
Beispiel #9
0
void queue_enqueue(queue_p queue, node_p node) {
    assert(queue != NULL);

    if (queue_is_full(queue)) {
        return;
    }

    queue->arr[queue->tail] = node;
    queue->tail = (queue->tail + 1) % queue->size;
}
Beispiel #10
0
/*!
  * \brief
  *   This function puts a byte to queue.
  * \param  byte to put
  * \return
  *   \arg  0  Full queue
  *   \arg  1  Done
 */
__Os__ int queue_put (queue_t *queue, void *b)
{
   if (queue_is_full (queue) == 1)  //full queue
      return 0;
   memcpy ((void*)&queue->buf[queue->tail*queue->item_size], b, queue->item_size);
   //rotate pointer
   if ( ++queue->tail >= queue->items )
      queue->tail = 0;
   return 1;
}
Beispiel #11
0
_U1RXInterrupt ()
{
	if (U1STAbits.OERR)
		U1STAbits.OERR = 0;
	while (U1STAbits.URXDA)
		if (!queue_is_full (&g_rx_queue))
			queue_write (&g_rx_queue, U1RXREG);

	IFS0bits.U1RXIF = 0;
}
int main()
{
	int i;
	queue q = queue_create();
	assert(queue_is_empty(q));
	queue_enqueue(q, 3);
	assert(queue_dequeue(q) == 3);
	assert(queue_is_empty(q));
	for (i= 0; i < 99; i++) {
		queue_enqueue(q, i);
	}
	assert(queue_is_full(q));
	assert(queue_dequeue(q) == 0);
	queue_enqueue(q, 1);
	queue_enqueue(q, 1);
	assert(queue_is_full(q));
	queue_destroy(&q);
	assert(q == NULL);
	return 0;
}
Beispiel #13
0
BOOL inqueue(queue_t* queue, const void* item) {
	if (queue_is_full(queue) == TRUE)
		return FALSE;

	memcpy((char*) queue->array_addr + (queue->item_size * queue->put_index),
			item, queue->item_size);

	queue->put_index = (queue->put_index + 1) % queue->max_item_count;
	queue->last_op = QUEUE_INQUEUE;
	return TRUE;
}
Beispiel #14
0
void
queue_head_add(struct queue * q, struct queue_item * item)
{
    if (!queue_is_full(q)) {
        if (g_verbose > 0)
            fprintf(stdout, "queuing \"%s\" in position %d\n", item->frame, q->head);
        q->elems[q->head] = item;
        q->head = (q->head + 1) % q->max_size;
        pthread_mutex_lock(&q->mux);
        {
            q->nr_elems++;
            if (queue_is_full(q))
                pthread_cond_wait(&q->not_full, &q->mux);
            pthread_cond_signal(&q->not_empty);
        }
        pthread_mutex_unlock(&q->mux);
    } else {
        fprintf(stdout, "[warning]: element inserted discarted, queue is full\n");
    }
}
Beispiel #15
0
/** audio tx put
 *   copies filled pChunk into the TX queue for transmission
 *    if queue is full, then chunk is dropped 
 * Parameters:
 * @param pThis  pointer to own object
 *
 * @return Zero on success.
 * Negative value on failure.
 */
int audioTx_put(audioTx_t *pThis, chunk_t *pChunk)
{
	chunk_t *pchunk_temp = NULL;

    if ( NULL == pThis || NULL == pChunk ) {
        //printf("[Audio TX]: Failed to put \r\n");
        return FAIL;
    }
    
    // block if queue is full
    //while(queue_is_full(&pThis->queue)) {
    if(queue_is_full(&pThis->queue)) {
        //printf("[Audio TX]: Queue Full \r\n");
        return FAIL;
        //powerMode_change(PWR_ACTIVE);
        //asm("idle;");
    }
    //powerMode_change(PWR_FULL_ON);

    // get free chunk from pool
    if ( PASS == bufferPool_acquire(pThis->pBuffP, &pchunk_temp) ) {
    	// copy chunk into free buffer for queue
    	chunk_copy(pChunk, pchunk_temp);

    	/* If DMA not running ? */
        if ( 0 == pThis->running ) {
        	/* directly put chunk to DMA transfer & enable */
        	pThis->running  = 1;
            pThis->pPending = pchunk_temp;
            audioTx_dmaConfig(pThis->pPending);
            ENABLE_SPORT0_TX();

        } else {
        	/* DMA already running add chunk to queue */
            if ( PASS != queue_put(&pThis->queue, pchunk_temp) ) {
            	// return chunk to pool if queue is full, effectively dropping the chunk
                bufferPool_release(pThis->pBuffP, pchunk_temp);
                return FAIL;
            }
            else
            {
            	return PASS;
            }
        }

    } else {
    	// drop if we don't get free space
    	//printf("[Audio TX]: failed to get buffer \r\n");
    	return FAIL;
    }
    
    return FAIL;
}
Beispiel #16
0
int queue_push(queue* q, void* new_payload){
    
    if(queue_is_full(q)){
	    return QUEUE_FAILURE;
    }

    q->array[q->rear].payload = new_payload;

    q->rear = ((q->rear+1) % q->maxSize);

    return QUEUE_SUCCESS;
}
Beispiel #17
0
void
queue_resize (queue *q, int size)
{
    queue r;
    queue_create(&r, size);

    while (!queue_is_empty(q) && !queue_is_full(&r))
        queue_enqueue(&r, queue_dequeue(q));

    queue_destroy(q);

    *q = r;
}
Beispiel #18
0
void
queue_enqueue (queue *q, pos data)
{
    if (queue_is_full(q))
    {
        printf("ERROR: Full queue. (enqueue %d,%d)\n", data.x, data.y);
        return;
    }

    q->content[q->end] = data;
    q->end = ++q->end % q->size;
    q->length++;
}
Beispiel #19
0
static int
queue_write (volatile struct queue *q, unsigned char c)
{
	if (queue_is_full (q))
		return 0;

	unsigned end = q->start + q->length;
	if (end >= sizeof q->data)
		end -= sizeof q->data;
	q->data[end] = c;
	q->length++;
	return 1;
}
Beispiel #20
0
void
queue_debug(struct queue * q)
{
    int i;

    if (!queue_is_full(q)) {
        for (i = q->tail; i != q->head; i = (i + 1) % q->max_size)
            printf("==> %s\n", q->elems[i]->frame);
    } else {
        printf("%s\n", q->elems[q->tail]->frame);
        for (i = (q->tail + 1) % q->max_size; i != q->head; i = (i + 1) % q->max_size)
            printf("==> %s\n", q->elems[i]->frame);
    }
}
Beispiel #21
0
int uart_putchar(char c)
{
	if( c == '\n')
		uart_putchar('\r');
	if(!queue_is_full(&uart_tx_buf)) {
		queue_enqueue(&uart_tx_buf, &c);
	} else {
		return -ENOMEM;
	}

	usart_enable_tx_interrupt(USART1);

	return 0;
}
Beispiel #22
0
unsigned
queue_append(struct queue *queue, struct song *song)
{
	unsigned id = queue_generate_id(queue);

	assert(!queue_is_full(queue));

	queue->items[queue->length] = (struct queue_item){
		.song = song,
		.id = id,
		.version = queue->version,
	};

	queue->order[queue->length] = queue->length;
	queue->id_to_position[id] = queue->length;

	++queue->length;

	return id;
}

void
queue_swap(struct queue *queue, unsigned position1, unsigned position2)
{
	struct queue_item tmp;
	unsigned id1 = queue->items[position1].id;
	unsigned id2 = queue->items[position2].id;

	tmp = queue->items[position1];
	queue->items[position1] = queue->items[position2];
	queue->items[position2] = tmp;

	queue->items[position1].version = queue->version;
	queue->items[position2].version = queue->version;

	queue->id_to_position[id1] = position2;
	queue->id_to_position[id2] = position1;
}

static void
queue_move_song_to(struct queue *queue, unsigned from, unsigned to)
{
	unsigned from_id = queue->items[from].id;

	queue->items[to] = queue->items[from];
	queue->items[to].version = queue->version;
	queue->id_to_position[from_id] = to;
}
Beispiel #23
0
int queue_enqueue(queue q, void *value)
{
	if (queue_is_full(q)) {
		return -1;
	}

	*q->last++ = value;

	if (q->last == q->end) {
		q->last = q->start;
	}

	q->is_empty = 0;

	return 0;
}
Beispiel #24
0
void queue_push( int data, queue_t *queue ) {
    int done = 0;

    do_lock( &queue->operation_lock );

    while ( queue_is_full( queue ) ) {    
        do_unlock( &queue->operation_lock );
        __asm__("hlt");
        do_lock( &queue->operation_lock );
    }
    queue->data[queue->head] = data;
    queue->head = QUEUE_WRAP( queue->head + 1 );
    done = 1;

    do_unlock( &queue->operation_lock );
}
Beispiel #25
0
void
queue_fill (queue *q)
{
    static int seeded = false;

    if (!seeded)
    {
        srand(time(NULL));
        seeded = true;
    }

    while (!queue_is_full(q))
    {
        pos p;
        p.x = rand() % 100;
        p.y = rand() % 100;
        queue_enqueue(q, p);
    }
}
Beispiel #26
0
// Write a single character to the output queue
static void
serial_putchar (char c)
{
	while (1)
	{
		DISABLE_INTERRUPTS
		if (!queue_is_full (&g_tx_queue))
			break;

		ENABLE_INTERRUPTS
		delay_loop_ms (1);
	}

	// Send straight away if we may
	if (queue_is_empty (&g_tx_queue) && !U1STAbits.UTXBF)
		U1TXREG = c;
	else
		queue_write (&g_tx_queue, c);
	ENABLE_INTERRUPTS
}
Beispiel #27
0
static inline void msc_send_csw(USB_MSC* msc)
{
	ASSERT(!queue_is_full(msc->queue));
	msc->current_buf = queue_allocate_buffer_ms(msc->queue, INFINITE);
	CSW* csw = (CSW*)msc->current_buf;
	csw->bCSWStatus = msc->csw_status;
	csw->dCSWSignature = CBW_MAGIC;
	csw->dCSWTag = msc->cbw.dCBWTag;
	csw->dCSWDataResidue = msc->scsi_transferred;
	usb_write(usbd_get_usb(msc->usbd), EP_IN(msc->ep_num), msc->current_buf, CSW_SIZE);

#if (USB_MSC_DEBUG_FLOW)
	printf("USB_MSC: TX CSW\n\r");
#endif

#if (USB_DEBUG_ERRORS)
	if (csw->bCSWStatus == CSW_STATUS_ERROR)
		printf("MSC phase error\n\r");
#endif
}
Beispiel #28
0
void usart1_isr(void)
{
	char c;
	if (usart_get_flag(USART1, USART_SR_TXE)) {
		if (!queue_is_empty(&uart_tx_buf)) {
			queue_dequeue(&uart_tx_buf, &c);
			usart_send(USART1, (uint16_t)c);
		} else {
			usart_disable_tx_interrupt(USART1);
		}
	}


	if (usart_get_flag(USART1, USART_SR_RXNE)) {
		if (!queue_is_full(&uart_rx_buf)) {
			c = usart_recv(USART1);
			queue_enqueue(&uart_rx_buf, &c);
		}
	}
}
Beispiel #29
0
void queue_push(Queue *queue, Item item){
	if(!queue_is_full(queue)){
    	QNode node = queue_make_node(item);
    	if(NULL != node){
    		Position head = NULL;
    		head = queue->head;
        	if(queue_is_empty(queue)){
            	queue->tail = node;
        	}

        	node->pre = head;
        	node->next = head->next;

        	if(NULL != head->next){
            	head->next->pre = node;
        	}
        	head->next = node;
        	++(queue->size);
    	}
	}
}
Beispiel #30
0
void on_msc_received(EP_CLASS ep, void* param)
{
	USB_MSC* msc = (USB_MSC*)param;
	unsigned int block_size;
	switch (msc->state)
	{
	case MSC_STATE_CBW:
		memcpy(&msc->cbw, msc->current_buf, CBW_SIZE);
		queue_release_buffer(msc->queue, msc->current_buf);
		msc->current_buf = NULL;
		msc->scsi_requested = 0;
		msc->scsi_transferred = 0;
		event_set(msc->event);
		break;
	case MSC_STATE_DATA:
		queue_push(msc->queue, msc->current_buf);
		msc->current_buf = NULL;
		if (msc->scsi_transferred < msc->scsi_requested)
		{
			ASSERT(!queue_is_full(msc->queue));
			msc->current_buf = queue_allocate_buffer_ms(msc->queue, INFINITE);
			block_size = msc->scsi_requested - msc->scsi_transferred;
			if (block_size > msc->block_size)
				block_size = msc->block_size;
			msc->scsi_transferred += block_size;
			usb_read(usbd_get_usb(msc->usbd), EP_OUT(msc->ep_num), msc->current_buf, block_size);
		}
		else
			event_set(msc->event);
		break;
	case MSC_STATE_CSW:
		msc->state = MSC_STATE_CSW_SENT;
		msc_send_csw(msc);
		break;
	default:
		break;
	}
}