Exemplo n.º 1
0
static void
g_thread_set_priority_posix_impl (gpointer thread, GThreadPriority priority)
{
#ifdef HAVE_PRIORITIES
# ifdef G_THREADS_IMPL_POSIX
  struct sched_param sched;
  int policy;
  posix_check_for_error (pthread_getschedparam (*(pthread_t*)thread, 
						&policy, &sched));
  sched.sched_priority = g_thread_map_priority (priority);
  posix_check_for_error (pthread_setschedparam (*(pthread_t*)thread, 
						policy, &sched));
# else /* G_THREADS_IMPL_DCE */
  posix_check_for_error (pthread_setprio (*(pthread_t*)thread, 
					  g_thread_map_priority (priority)));
# endif
#endif /* HAVE_PRIORITIES */
}
int
thread_set_priority (thread_t *self, int prio)
{
  int old_prio = self->thr_priority;

  if (prio < 0 && prio >= MAX_PRIORITY)
    return old_prio;

#if defined (PRI_RR_MIN) && !defined(__osf__)
  switch (prio)
    {
    case LOW_PRIORITY:
      prio = PRI_RR_MIN;
      break;
    case NORMAL_PRIORITY:
      prio = (PRI_RR_MIN + PRI_RR_MAX) / 2;
      break;
    case HIGH_PRIORITY:
      prio = PRI_RR_MAX;
      break;
    default:
      return old_prio;
    }

  /*
   *  Cannot set priority on main thread, because it does not have a handle
   */
  if (self != _main_thread &&
      pthread_setprio (*(pthread_t *) self->thr_handle, prio))
    {
	prio = old_prio;
    }
#endif

  self->thr_priority = prio;

  return old_prio;
}
Exemplo n.º 3
0
int main(int argc, char *argv[]) {
	char *line, word[12], cmdChar, *ip;
	int n;
	pthread_t driverTid;
#if _POSIX_VERSION >= 199506
	struct sched_param param;
#else  /* _POSIX_VERSION >= 199506 */
	pthread_attr_t attr;
#endif /* _POSIX_VERSION >= 199506 */

	if(processPresent("excl_Track")) {
	    printf("Another Track type process is running\n");
	    exit(1);
	}
	setpriority(PRIO_PROCESS, (0), (TRACKUSERPRIO));
	readline_initialize_everything();
	read_history(HIST_FILE);
	tsshm = OpenShm(TSSHMNAME, TSSHMSZ);
	tsshm->azCmd = OFF_CMD;
	tsshm->elCmd = OFF_CMD;
	tsshm->az = tsshm->encAz;
	tsshm->el = tsshm->encEl;
	tsshm->azVel = 0;
	tsshm->elVel = 0;
	tsshm->msecCmd = tsshm->msec;

	/* Start the driver thread */
#if _POSIX_VERSION >= 199506
	if(pthread_create(&driverTid, NULL, driver, (void *)0) < 0) {
	    perror("Failed to create driver");
	    exit(1);
	}
	param.sched_priority = TRACKPRIO;
	pthread_setschedparam(driverTid, SCHED_DEFAULT, &param);
#else  /* _POSIX_VERSION >= 199506 */
	pthread_attr_create(&attr);
	if(pthread_create(&driverTid, attr, driver, (void *)0) < 0) {
	    perror("Failed to create driver");
	    exit(1);
	}
	pthread_setprio(driverTid, TRACKPRIO);
#endif /* _POSIX_VERSION >= 199506 */

	/* This main loop communicates with the user */
	for(;;) {
	    line = readline("trk: ");
	    if(*line) {			/* Readline removes the '\n' */
		add_history(line);

		/* At this point the main part of the program should run.  */
	        cmdChar = *line;
		/* Skip the remainder of the first word */
	        for(ip = 1 + line; ! isspace(*ip); ip++) ;
		switch(cmdChar) {
		case 'p':
		    n = 1;
		    printf("Fault = %d az cmd %d, state %d    "
			    "el cmd %d, state %d\n", tsshm->fault,
			    tsshm->azCmd, tsshm->azState,
			    tsshm->elCmd, tsshm->elState);
		    printf("az %9.4f, azVel %9.4f  curAz %9.4f, encAz %9.4f\n",
			    tsshm->az/MSEC_PER_DEG, tsshm->azVel/MSEC_PER_DEG,
			    (double)tsshm->cmdAz/MSEC_PER_DEG,
			    (double)tsshm->encAz/MSEC_PER_DEG);
		    printf("el %9.4f, elVel %9.4f  curEl %9.4f, encEl %9.4f\n",
			    tsshm->el/MSEC_PER_DEG, tsshm->elVel/MSEC_PER_DEG,
			    (double)tsshm->cmdEl/MSEC_PER_DEG,
			    (double)tsshm->encEl/MSEC_PER_DEG);
		    printf("day %d, msec %d, msecCmd %d   "
			    "msecAccept %d\n", tsshm->day,
			    tsshm->msec, tsshm->msecCmd, tsshm->msecAccept);
		    printf("MSEC_PER_DEG = %9.4f\n", (double)n*MSEC_PER_DEG);
		    printf("m3Cmd = %d = %s  m3State = %d = %s\n", tsshm->m3Cmd,
			(tsshm->m3Cmd == CLOSE_M3_CMD)? "Closed":
			(tsshm->m3Cmd == OPEN_M3_CMD)? "open":"Unknown",
			tsshm->m3State,
			(tsshm->m3State == CLOSED_ST)? "Closed":
			(tsshm->m3State == OPEN_ST)? "open":"Unknown");
		    printf("lastMsec = %d, servoKnownDead = %d\n", lastMsec,
			servoKnownDead);
		    break;
		case 'a':
		    if(line[1] == 'a') {
			n = sscanf(ip, "%lf %lf", &newP, &newV);

			switch(n) {
			case 1:
			    newV = 0;
			case 2:		/* This is supposed to fall through */
			    if(line[2] == 's') {
				newP = tsshm->az/MSEC_PER_DEG + newP/3600;
				newV = tsshm->azVel/MSEC_PER_DEG + newV/3600;
			    } else {
				newP = tsshm->az/MSEC_PER_DEG + newP;
				newV = tsshm->azVel/MSEC_PER_DEG + newV;
			    }
			    newAz = 1;	/* Signal driver new az pos, vel. */
			    break;
			default:
			    printf("??\n");
			    break;
			}
			break;
		    } else if(line[1] == 'h') {
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->sendEncToPalm = AZ_TO_PALM;
			    } else if(strcmp("off", word) == 0) {
				tsshm->sendEncToPalm = NOTHING_TO_PALM;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    }
		    n = sscanf(ip, "%lf %lf", &newP, &newV);
		    switch(n) {
		    case 1:
			newV = 0;
		    case 2:		/* This is supposed to fall through */
			newAz = 1;	/* Signal driver new az pos, vel. */
			break;
		    case 0:
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->msecCmd = tsshm->msec;
				usleep(10000);
				tsshm->azCmd = ON_CMD;
			    } else if(strcmp("off", word) == 0) {
				tsshm->azCmd = OFF_CMD;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    default:
			printf("??\n");
			break;
		    }
		    break;
		case 'e':
		    if(line[1] == 'a') {
			n = sscanf(ip, "%lf %lf", &newP, &newV);

			switch(n) {
			case 1:
			    newV = 0;
			case 2:		/* This is supposed to fall through */
			    if(line[2] == 's') {
				newP = tsshm->el/MSEC_PER_DEG + newP/3600;
				newV = tsshm->elVel/MSEC_PER_DEG + newV/3600;
			    } else {
				newP = tsshm->el/MSEC_PER_DEG + newP;
				newV = tsshm->elVel/MSEC_PER_DEG + newV;
			    }
			    newEl = 1;	/* Signal driver new az pos, vel. */
			    break;
			default:
			    printf("??\n");
			    break;
			}
			break;
		    } else if(line[1] == 'h') {
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->sendEncToPalm = EL_TO_PALM;
			    } else if(strcmp("off", word) == 0) {
				tsshm->sendEncToPalm = NOTHING_TO_PALM;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    }
		    n = sscanf(ip, "%lf %lf", &newP, &newV);
		    switch(n) {
		    case 1:
			newV = 0;
		    case 2:		/* This is supposed to fall through */
			newEl = 1;	/* Signal driver */
			break;
		    case 0:
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("on", word) == 0) {
				tsshm->msecCmd = tsshm->msec;
				usleep(10000);
				tsshm->elCmd = ON_CMD;
			    } else if(strcmp("off", word) == 0) {
				tsshm->msecCmd = tsshm->msec;
				tsshm->elCmd = OFF_CMD;
			    } else {
				printf("??\n");
			    }
			}
			break;
		    default:
			printf("??\n");
			break;
		    }
		    break;
		case 'm':
			if(sscanf(ip, "%s", word)) {
			    if(strcmp("open", word) == 0)
				tsshm->m3Cmd = OPEN_M3_CMD;
			    else if(strcmp("closed", word) == 0)
				tsshm->m3Cmd = CLOSE_M3_CMD;
			    else {
				printf("??\n");
			    }
			}
		    break;
		case 'q':
		    tsshm->azCmd = OFF_CMD;
		    tsshm->elCmd = OFF_CMD;
		    write_history(HIST_FILE);
		    return(0);
		default:
		    printf("??\n");
		    break;
		}

		/* end of the main loop */
	    }
	    free(line);
	}
	fprintf(stderr, "!!! Control should not get here !!!\n");
/*	shm_unlink(TSSHMNAME); */
	return(0);
}
Exemplo n.º 4
0
/****************************************************************//**
Creates a new thread of execution. The execution starts from
the function given. The start function takes a void* parameter
and returns an ulint.
@return	handle to the thread */
UNIV_INTERN
os_thread_t
os_thread_create(
/*=============*/
#ifndef __WIN__
	os_posix_f_t		start_f,
#else
	ulint (*start_f)(void*),		/*!< in: pointer to function
						from which to start */
#endif
	void*			arg,		/*!< in: argument to start
						function */
	os_thread_id_t*		thread_id)	/*!< out: id of the created
						thread, or NULL */
{
#ifdef __WIN__
	os_thread_t	thread;
	DWORD		win_thread_id;

	os_mutex_enter(os_sync_mutex);
	os_thread_count++;
	os_mutex_exit(os_sync_mutex);

	thread = CreateThread(NULL,	/* no security attributes */
			      0,	/* default size stack */
			      (LPTHREAD_START_ROUTINE)start_f,
			      arg,
			      0,	/* thread runs immediately */
			      &win_thread_id);

	if (srv_set_thread_priorities) {

		/* Set created thread priority the same as a normal query,
		we try to prevent starvation of threads by assigning same
		priority QUERY_PRIOR to all */

		ut_a(SetThreadPriority(thread, srv_query_thread_priority));
	}

	if (thread_id) {
		*thread_id = win_thread_id;
	}

	return(thread);
#else
	int		ret;
	os_thread_t	pthread;
	pthread_attr_t	attr;

#ifndef UNIV_HPUX10
	pthread_attr_init(&attr);
#endif

#ifdef UNIV_AIX
	/* We must make sure a thread stack is at least 32 kB, otherwise
	InnoDB might crash; we do not know if the default stack size on
	AIX is always big enough. An empirical test on AIX-4.3 suggested
	the size was 96 kB, though. */

	ret = pthread_attr_setstacksize(&attr,
					(size_t)(PTHREAD_STACK_MIN
						 + 32 * 1024));
	if (ret) {
		srv_panic(ret,
			"InnoDB: Error: pthread_attr_setstacksize"
			" returned %d\n", ret);
	}
#endif
#ifdef __NETWARE__
	ret = pthread_attr_setstacksize(&attr,
					(size_t) NW_THD_STACKSIZE);
	if (ret) {
		srv_panic(ret,
			"InnoDB: Error: pthread_attr_setstacksize"
			" returned %d\n", ret);
	}
#endif
	os_mutex_enter(os_sync_mutex);
	os_thread_count++;
	os_mutex_exit(os_sync_mutex);

#ifdef UNIV_HPUX10
	ret = pthread_create(&pthread, pthread_attr_default, start_f, arg);
#else
	ret = pthread_create(&pthread, &attr, start_f, arg);
#endif
	if (ret) {
		srv_panic(ret,
			"InnoDB: Error: pthread_create returned %d\n", ret);
	}

#ifndef UNIV_HPUX10
	pthread_attr_destroy(&attr);
#endif
	if (srv_set_thread_priorities) {

#ifdef HAVE_PTHREAD_SETPRIO 
		pthread_setprio(pthread, srv_query_thread_priority);
#endif
	}

	if (thread_id) {
		*thread_id = pthread;
	}

	return(pthread);
#endif
}