Example #1
0
void *
DiskWriter(void *arg)
{
	writebuf_t	*wbuf = 0;
	static int	gotone;

	while (1) {
		pthread_testcancel();

		pthread_mutex_lock(&writequeue_mutex);
		if (queue_empty(&writequeue)) {
			if (gotone)
				writeridles++;
			do {
#ifdef CONDVARS_WORK
				pthread_cond_wait(&writequeue_cond,
						  &writequeue_mutex);
#else
				pthread_mutex_unlock(&writequeue_mutex);
				fsleep(1000);
				pthread_mutex_lock(&writequeue_mutex);
#endif
				pthread_testcancel();
			} while (queue_empty(&writequeue));
		}
		queue_remove_first(&writequeue, wbuf, writebuf_t *, chain);
		writeinprogress = 1; /* XXX */
		gotone = 1;
		pthread_mutex_unlock(&writequeue_mutex);

		if (wbuf->data == NULL) {
			writezeros(wbuf->offset, wbuf->size);
		} else {
			rdycount++;
			assert(wbuf->size <= OUTSIZE);
			writedata(wbuf->offset, (size_t)wbuf->size, wbuf->data);
		}
		free_writebuf(wbuf);
		writeinprogress = 0; /* XXX, ok as unlocked access */
	}
}
Example #2
0
void SerialString(char *s, int tty)
{
    int i = 0,k = 0,sumerrs = 0;
    static int writerrs = 0;
    

    if(tty == 0)
        tty = ttys[0];
    if(tty >= 0){
        while(s[i] != 0){
            if((k = write(tty,&s[i++],1)) < 0)
                sumerrs++;
            else{
                autoreopen = 1;  // have successfully written
                writerrs = 0;
            }
        }
    }
#ifdef Darwin
    // The line to the PC needs to be reopened every time Spike 2 is restarted
    if(sumerrs && autoreopen && ttys[0] == tty){
        fprintf(stderr,"Error Writing %s to %d\n",s,tty);
        if(writerrs++ < 4){
            ReopenSerial();
            fsleep(0.2); 
        }
        else{
            fprintf(stderr,"Can't Reopen Serial Port %d\n",tty);
        }
    }
#endif
    if(seroutfile != NULL && tty != ttys[1])
        i = fprintf(seroutfile,"%s",s);
    if(netoutfile != NULL && tty != ttys[1])
        i = fprintf(netoutfile,"%s",s);
}
Example #3
0
static readbuf_t *
getblock(struct hashregion *reg)
{
#ifndef NOTHREADS
	readbuf_t	*rbuf = 0;
	static int	gotone;

	if (!nothreads) {
		pthread_mutex_lock(&readqueue_mutex);
		if (queue_empty(&readqueue)) {
			if (gotone)
				hasheridles++;
			do {
#ifdef CONDVARS_WORK
				pthread_cond_wait(&readqueue_cond,
						  &readqueue_mutex);
#else
				pthread_mutex_unlock(&readqueue_mutex);
				fsleep(1000);
				pthread_mutex_lock(&readqueue_mutex);
#endif
				pthread_testcancel();
			} while (queue_empty(&readqueue));
		}
		queue_remove_first(&readqueue, rbuf, readbuf_t *, chain);
		gotone = 1;
		pthread_mutex_unlock(&readqueue_mutex);

		if (rbuf->region.start != reg->region.start &&
		    rbuf->region.size != reg->region.size) {
			fprintf(stderr, "reader/hasher out of sync!\n");
			exit(2);
		}

		return rbuf;
	}
Example #4
0
void ControlsetNewPosition(int pos, int diff)
{
    int delaySec;
    int delayMsec,i,charctr,j;
    char str[32];
	char waitStr[10],c;
	char readBuf[16];
	char inbuf[256];
	float fdiff = (float)(diff)/STEP_SEC;
	struct timeval start,end;
    
    TTY t;
	int portstate;
    
	sprintf(waitStr, "? P%c", CR);
    if(diff > 0) {
        delaySec = diff / STEP_SEC;
        delayMsec = diff % STEP_SEC;
    }
    else {
        delaySec = -diff / STEP_SEC;
        delayMsec = -diff % STEP_SEC;
    }
    
    sprintf(str, "P %d%c", pos, CR);
    write(motorPort, str, strlen(str));     delay(10);
	charctr = 0;
    
	if(ioctl(motorPort,TCGETA,&t) < 0)
	{
		perror("iocntl 1");
	}
	printf("terminal %d %d %d\n",t.c_cc[VMIN],t.c_cc[VTIME],t.c_cflag);
	gettimeofday(&start,NULL);
	while((i = read(motorPort,&c,1)) > 0)
        inbuf[charctr++] = c;
	i = write(motorPort, waitStr, strlen(waitStr));
	delay(10);
    
#if defined(IRIX64) || defined(IRIX)
	if(ioctl(motorPort,FIONREAD,&portstate) < 0)
	{
		perror("iocntl 1");
	}
#endif
    
	while((i = read(motorPort,&c,1)) <= 0)
        ;
	gettimeofday(&end,NULL);
	printf("Took %.4f\n",timediff(&start,&end));
	readBuf[0] = c;
	j = 1;
	while((i = read(motorPort,&c,1)) > 0)
        readBuf[j++] = c;
    
    if(delaySec) sleep(delaySec);
    else if(delayMsec) delay(delayMsec);
	fsleep(fdiff * stepvals[DELAY_SCALE]);
    
    /* deselect the current motor after motion is finished
     * this prevents the motor from getting too hot
     */
    sprintf(str, "/B 7%c", CR);                             /* open output latch */
    write(motorPort, str, strlen(str)); delay(10);
    sprintf(str, "B 1%c", CR);                              /* deselect current motor */
    write(motorPort, str, strlen(str)); delay(10);
    sprintf(str, "B 7%c", CR);                              /* close latch */
    write(motorPort, str, strlen(str)); delay(10);
    
    return;
}
Example #5
0
void SendAllStepProps(int channel)
{
    int i;
    fsleep(0.05);
}
Example #6
0
void stepsetup(void)
{
    char str[256];
    
    /*
     * need the sleeps below or it doesn't all get processed
     */
    
    sprintf(str, "%c", CR); 
    SerialString(str, motorPort); fsleep(0.01);
    sprintf(str, "O 0A0H%c", CR);
    SerialString(str, motorPort); fsleep(0.01);
    
    sprintf(str, "F %c%c\n", FIRST_RATE, CR);
	SerialString(str,motorPort); fsleep(0.01);
    sprintf(str, "R %c%c", MAX_RATE, CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "S %c%c", ACCELERATION, CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 7%c", CR);                     /* deselect motors */
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 1%c", CR);                      /* set B 1 high */
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 3%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 4%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 3%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 3%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 4%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "/B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 3%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 2%c", CR);
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "B 7%c", CR);                      /* close latch */
	SerialString(str,motorPort);fsleep(0.01);
    sprintf(str, "A 0%c", CR);                      /* set current position to 0 */
	SerialString(str,motorPort);
}
Example #7
0
static writebuf_t *
alloc_writebuf(off_t offset, off_t size, int allocbuf, int dowait)
{
	writebuf_t *wbuf;
	buffer_t *buf = NULL;
	size_t bufsize;

	pthread_mutex_lock(&writebuf_mutex);
	wbuf = malloc(sizeof(*wbuf));
	if (wbuf == NULL) {
		fprintf(stderr, "could not alloc writebuf header\n");
		exit(1);
	}
	bufsize = allocbuf ? size : 0;
	if (bufsize) {
		do {
			if (maxwritebufmem &&
			    curwritebufmem + bufsize > maxwritebufmem)
				buf = NULL;
			else
				buf = malloc(sizeof(buffer_t) + bufsize);

			if (buf == NULL) {
				if (!dowait) {
					free(wbuf);
					pthread_mutex_unlock(&writebuf_mutex);
					return NULL;
				}

				decompblocks++;
				writebufwanted = 1;
				/*
				 * Once again it appears that linuxthreads
				 * condition variables don't work well.
				 * We seem to sleep longer than necessary.
				 */
				do {
#ifdef CONDVARS_WORK
					pthread_cond_wait(&writebuf_cond,
							  &writebuf_mutex);
#else
					pthread_mutex_unlock(&writebuf_mutex);
					fsleep(1000);
					pthread_mutex_lock(&writebuf_mutex);
#endif
					pthread_testcancel();
				} while (writebufwanted);
			}
		} while (buf == NULL);
		buf->refs = 1;
		buf->size = bufsize;
	}
	curwritebufs++;
	curwritebufmem += bufsize;
	if (curwritebufs > maxbufsalloced)
		maxbufsalloced = curwritebufs;
	if (curwritebufmem > maxmemalloced)
		maxmemalloced = curwritebufmem;
	pthread_mutex_unlock(&writebuf_mutex);

	queue_init(&wbuf->chain);
	wbuf->offset = offset;
	wbuf->size = size;
	wbuf->buf = buf;
	wbuf->data = buf ? buf->data : NULL;

	return wbuf;
}
Example #8
0
static readbuf_t *
alloc_readbuf(uint32_t start, uint32_t size, int dowait)
{
	readbuf_t *rbuf;
	size_t bufsize;

	pthread_mutex_lock(&readbuf_mutex);
	bufsize = sectobytes(size);
	if (size > HASHBLK_SIZE) {
		fprintf(stderr, "%s: hash region too big (%d bytes)\n",
			devfile, size);
		exit(1);
	}

	do {
		if (maxreadbufmem && curreadbufmem + bufsize > maxreadbufmem)
			rbuf = NULL;
		else
			rbuf = malloc(sizeof(*rbuf) + bufsize);

		if (rbuf == NULL) {
			if (!dowait) {
				pthread_mutex_unlock(&readbuf_mutex);
				return NULL;
			}

			readeridles++;
			readbufwanted = 1;
			/*
			 * Once again it appears that linuxthreads
			 * condition variables don't work well.
			 * We seem to sleep longer than necessary.
			 */
			do {
#ifdef CONDVARS_WORK
				pthread_cond_wait(&readbuf_cond,
						  &readbuf_mutex);
#else
				pthread_mutex_unlock(&readbuf_mutex);
				fsleep(1000);
				pthread_mutex_lock(&readbuf_mutex);
#endif
				pthread_testcancel();
			} while (readbufwanted);
		}
	} while (rbuf == NULL);

	curreadbufs++;
	curreadbufmem += bufsize;
	if (curreadbufs > maxbufsalloced)
		maxbufsalloced = curreadbufs;
	if (curreadbufmem > maxmemalloced)
		maxmemalloced = curreadbufmem;
	pthread_mutex_unlock(&readbuf_mutex);

	queue_init(&rbuf->chain);
	rbuf->region.start = start;
	rbuf->region.size = size;

	return rbuf;
}