Пример #1
0
void* q_thread(void *qt)
{
	ASSERT(qt);
	Q_t *q = qt;
	void *func;
	void *arg;
	while(0 == q->is_close) {
	        pthread_mutex_lock(&q->mutex);
        	if (OK == q_isempty(q)) {
			pthread_cond_wait(&q->has_data_cond, &q->mutex);
		}
		/* possible close signal has_data cond */
		if (0 != q->is_close) {
                	pthread_mutex_unlock(&q->mutex);
			break;
		}
		q_remove(q, &func, &arg);
                pthread_mutex_unlock(&q->mutex);
		tp_add(q->pool, func, arg); //unlock first then tp add, let q_add go 
	}
	tp_drop(q->pool);
	q->pool = NULL;
	pthread_mutex_lock(&q->mutex);
	pthread_cond_signal(&q->drop_cond);
	pthread_mutex_unlock(&q->mutex);
	pthread_exit(NULL);
	return NULL;
}
Пример #2
0
/*
 * Application entry point.
 */
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
{
	char *string_command;
	double *outArray;
	mwSize M; //mwSize is a platform independent alternative to int
	mwSize i;
	
	if(nrhs!=1) {
		mexErrMsgIdAndTxt("myex:nrhs:invalidN", "Invalid input: One command string required.\n\nValid commands are:\n   myex('connect')\n   myex('getdata')\n   myex('disconnect')");
	}

	string_command = mxArrayToString(prhs[0]);
    //mexPrintf(">> %s\n", string_command);
	switch (string_command[0]) {
	case 'c':
		Connect();
		break;
	case 'g':
		// retrieve (any) data from the internal buffer - return as plhs[0]
		M = q_nelements();
		i = M; // start at the end of the output array, because we're actually using a FILO data structure for the internal buffer (stack)
		plhs[0] = mxCreateDoubleMatrix(q_nelements(),12,mxREAL);
		outArray = mxGetPr(plhs[0]);
		// iterate through internal buffer
		while (!q_isempty())
		{
			// get next item from the internal buffer
			struct node *topelement = q_topelement();
			i--;
			// add gaze data to output row
			outArray[i] = topelement->X_px;
			outArray[i+M] = topelement->Y_px;  // N.B. C indexing is like the tranpose of MATLAB variable: http://uk.mathworks.com/matlabcentral/newsreader/view_thread/249954
			outArray[i+2*M] = topelement->Timestamp;
			// add eye position data to output row
			outArray[i+3*M] = topelement->HasLeftEyePosition;
			outArray[i+4*M] = topelement->HasRightEyePosition;
			outArray[i+5*M] = topelement->LeftEyeX_mm;
			outArray[i+6*M] = topelement->LeftEyeY_mm;
			outArray[i+7*M] = topelement->LeftEyeZ_mm;
			outArray[i+8*M] = topelement->RightEyeX_mm;
			outArray[i+9*M] = topelement->RightEyeY_mm;
			outArray[i+10*M] = topelement->RightEyeZ_mm;
			outArray[i+11*M] = topelement->EyePosTimestamp;
			// remove this item from the internal buffer
			q_pop();
		}
		break;
	case 'd':
		Disconnect();		
		break;		
	default:
		mexErrMsgIdAndTxt("myex:nrhs:unrecognised", "Invalid input: Unrecognised command.\n\nValid commands are:\n   myex('connect')\n   myex('getdata')\n   myex('disconnect')");
		break;
	}

    mxFree(string_command);	
	return;
}
Пример #3
0
ui_command_t *cmd_readcommand(queue_t *head)
{
    char *ptr;
    int insquote = FALSE;
    int indquote = FALSE;
    ui_command_t *cmd;
    int term = CMD_TERM_EOL;
    ui_token_t *t;

    cmd_eat_leading_white(head);

    if (q_isempty(head)) return NULL;

    cmd = (ui_command_t *) KMALLOC(sizeof(ui_command_t),0);
    q_init(&(cmd->head));

    while ((t = (ui_token_t *) q_deqnext(head))) {

	ptr = &(t->token);

	if (!insquote && !indquote) {
	    if ((*ptr == ';') || (*ptr == '\n')) {
		term = CMD_TERM_SEMI;
		break;
		}	
	    if ((*ptr == '&') && (*(ptr+1) == '&')) {
		term = CMD_TERM_AND;
		break;
		}
	    if ((*ptr == '|') && (*(ptr+1) == '|')) {
		term = CMD_TERM_OR;
		break;
		}
	    }

	if (*ptr == '\'') {
	    insquote = !insquote;
	    }

	if (!insquote) {
	    if (*ptr == '"') {
		indquote = !indquote;
		}
	    }

	q_enqueue(&(cmd->head),&(t->qb));
		
	}

    cmd->term = term;

    /* If we got out by finding a command separator, eat the separator */
    if (term != CMD_TERM_EOL) {
	KFREE(t);
	}

    return cmd;
}
Пример #4
0
int *maxSlidingWindow(int *nums, int numsSize, int k, int *returnSize) {
    if (k <= 0) {
        *returnSize = 0;
        return NULL;
    }
    if (k > numsSize) {
        k = numsSize;
    }
    *returnSize = numsSize + 1 - k;
    int *r = malloc(sizeof(int) * (*returnSize));
    if (k == 1) {
        memcpy(r, nums, sizeof(int) * (*returnSize));
        return r;
    }

    Queue *q = q_create(k);
    int i;
    for (i = 0; i != k; i++) {
        while (!q_isempty(q) && nums[i] >= nums[q_peekback(q)]) {
            q_popback(q);
        }
        q_pushback(q, i);
    }
    for (; i != numsSize; i++) {
        r[i - k] = nums[q_peekfront(q)];

        while (!q_isempty(q) && q_peekfront(q) <= i - k) {
            q_popfront(q);
        }
        while (!q_isempty(q) && nums[i] >= nums[q_peekback(q)]) {
            q_popback(q);
        }
        q_pushback(q, i);
    }
    r[i - k] = nums[q_peekfront(q)];
    q_destory(q);

    return r;
}
Пример #5
0
static void cmd_eat_leading_white(queue_t *head)
{
    ui_token_t *t;

    while (!q_isempty(head)) {
	t = (ui_token_t *) q_getfirst(head);
	if (is_white_space(t)) {
	    q_dequeue(&(t->qb));
	    KFREE(t);
	    }
	else break;
	}
}
Пример #6
0
static void console_save(unsigned char *buffer,int length)
{
    msgqueue_t *msg;

    /*
     * Get a pointer to the last message in the queue.  If 
     * it's full, preprare to allocate a new one
     */

    msg = (msgqueue_t *) console_msgq.q_prev;
    if (q_isempty(&(console_msgq)) || (msg->len == MSGQUEUESIZE)) {
	msg = NULL;
	}

    /*
     * Stuff characters into message chunks till we're done
     */

    while (length) {

	/*
	 * New chunk 
	 */
	if (msg == NULL) {

	    msg = (msgqueue_t *) KMALLOC(sizeof(msgqueue_t),0);
	    if (msg == NULL) return;
	    msg->len = 0;
	    q_enqueue(&console_msgq,(queue_t *) msg);

	    /*
	     * Remove chunks to prevent chewing too much memory
	     */

	    while (q_count(&console_msgq) > MSGQUEUEMAX) {
		msgqueue_t *dropmsg;
		dropmsg = (msgqueue_t *) q_deqnext(&console_msgq);
		if (dropmsg) KFREE(dropmsg);
		}
	    }

	/*
	 * Save text.  If we run off the end of the buffer, prepare
	 * to allocate a new one
	 */
	msg->data[msg->len++] = *buffer++;
	length--;
	if (msg->len == MSGQUEUESIZE) msg = NULL;
	}
}
Пример #7
0
void q_drop(void *qt)
{
	ASSERT(qt);

	Q_t* q = (Q_t*)qt;
	pthread_mutex_lock(&q->mutex);

	q->is_close = 1;
	/* let q thread not wait */
      	if (OK == q_isempty(q))
		pthread_cond_signal(&q->has_data_cond);	
	pthread_cond_wait(&q->drop_cond, &q->mutex);

	pthread_join(q->tid, NULL);

	pthread_mutex_unlock(&q->mutex);

	pthread_mutex_destroy(&q->mutex);
	pthread_cond_destroy(&q->has_data_cond);
	pthread_cond_destroy(&q->drop_cond);
	free(q->head);
	q->head = NULL;
	free(q);
}
/* if future does not have value set */
syscall future_set(future *f, int *i) {
	
	pid32 p_id;
	intmask	mask;
	mask=disable();
	
	if (f->state == FUTURE_EMPTY){
		
		f->value = i;
		f->state = FUTURE_VALID; 
		
		if(f->flag == FUTURE_EXCLUSIVE || f->flag == FUTURE_SHARED){
					
			restore(mask);
			return OK; 
		}else {
			p_id = f_enqueue(currpid, f->set_queue);
			if(p_id == (pid32) SYSERR){
				restore(mask);
				return SYSERR;
			}	
			suspend(currpid);
			f->value = i;	
			restore(mask);
			return OK; 
		}
	} 	

	if(f->state == FUTURE_WAITING){
		if(f->flag == FUTURE_EXCLUSIVE){
			f->value = i;			
			f->state=FUTURE_VALID;
			resume(f->pid);
			restore(mask);
			return OK;
		}else if(f->flag == FUTURE_SHARED){
			f->value = i;			
			f->state=FUTURE_VALID;
			
			while (!q_isempty(f->get_queue))
				resume(f_dequeue(f->get_queue));
			restore(mask);
			return OK;
		}
		else if(f->flag == FUTURE_QUEUE){
			if(!q_isempty(f->get_queue)){
				f->value = i;			
				
				resume(f_dequeue(f->get_queue));
			}else{
				p_id = f_enqueue(currpid, f->set_queue);
				if(p_id == (pid32) SYSERR){
					restore(mask);
					return SYSERR;
				}	
				suspend(currpid);
				f->value = i;	
			}
			
			restore(mask);
			return OK;
		}
	}
	restore(mask);
	return SYSERR;
}