//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void ThreadSetupLock(void) { pthread_mutexattr_t mattrib; Log_Print("pthread multi-threading\n"); if (!my_mutex) { my_mutex = GetMemory(sizeof(*my_mutex)); if (pthread_mutexattr_create (&mattrib) == -1) Error ("pthread_mutex_attr_create failed"); if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1) Error ("pthread_mutexattr_setkind_np failed"); if (pthread_mutex_init (my_mutex, mattrib) == -1) Error ("pthread_mutex_init failed"); } if (pthread_attr_create (&attrib) == -1) Error ("pthread_attr_create failed"); if (pthread_attr_setstacksize (&attrib, 0x100000) == -1) Error ("pthread_attr_setstacksize failed"); threaded = true; currentnumthreads = 0; currentthreadid = 0; } //end of the function ThreadInitLock
/* =============== RunThreadsOn =============== */ void RunThreadsOn ( threadfunc_t func ) { #ifdef __alpha pthread_t work_threads[256]; pthread_addr_t status; pthread_attr_t attrib; int i; if (numthreads == 1) { func (NULL); return; } if (pthread_attr_create (&attrib) == -1) Error ("pthread_attr_create failed"); if (pthread_attr_setstacksize (&attrib, 0x100000) == -1) Error ("pthread_attr_setstacksize failed"); for (i=0 ; i<numthreads ; i++) { if (pthread_create(&work_threads[i], attrib , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1) Error ("pthread_create failed"); } for (i=0 ; i<numthreads ; i++) { if (pthread_join (work_threads[i], &status) == -1) Error ("pthread_join failed"); } #else func (NULL); #endif }
/* =============== RunThreadsOn =============== */ void RunThreadsOn (threadfunc_t func) { pthread_t work_threads[MAX_THREADS]; pthread_addr_t status; pthread_attr_t attrib; long i; if (numthreads <= 1) { func (NULL); return; } workfunc = func; if (pthread_attr_create (&attrib) == -1) COM_Error ("pthread_attr_create failed"); if (pthread_attr_setstacksize (&attrib, stacksiz) == -1) COM_Error ("pthread_attr_setstacksize failed"); for (i = 0; i < numthreads; i++) { if (pthread_create(&work_threads[i], attrib, ThreadWorkerFunc, (pthread_addr_t)i) == -1) COM_Error ("pthread_create failed"); } for (i = 0; i < numthreads; i++) { if (pthread_join (work_threads[i], &status) == -1) COM_Error ("pthread_join failed"); } }
//=========================================================================== // // Parameter: - // Returns: - // Changes Globals: - //=========================================================================== void RunThreadsOn(int workcnt, qboolean showpacifier, void(*func)(int)) { int i; pthread_t work_threads[MAX_THREADS]; pthread_addr_t status; pthread_attr_t attrib; pthread_mutexattr_t mattrib; int start, end; Log_Print("pthread multi-threading\n"); start = I_FloatTime (); dispatch = 0; workcount = workcnt; oldf = -1; pacifier = showpacifier; threaded = true; if (numthreads < 1 || numthreads > MAX_THREADS) numthreads = 1; if (pacifier) setbuf (stdout, NULL); if (!my_mutex) { my_mutex = GetMemory(sizeof(*my_mutex)); if (pthread_mutexattr_create (&mattrib) == -1) Error ("pthread_mutex_attr_create failed"); if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1) Error ("pthread_mutexattr_setkind_np failed"); if (pthread_mutex_init (my_mutex, mattrib) == -1) Error ("pthread_mutex_init failed"); } if (pthread_attr_create (&attrib) == -1) Error ("pthread_attr_create failed"); if (pthread_attr_setstacksize (&attrib, 0x100000) == -1) Error ("pthread_attr_setstacksize failed"); for (i=0 ; i<numthreads ; i++) { if (pthread_create(&work_threads[i], attrib , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1) Error ("pthread_create failed"); } for (i=0 ; i<numthreads ; i++) { if (pthread_join (work_threads[i], &status) == -1) Error ("pthread_join failed"); } threaded = false; end = I_FloatTime (); if (pacifier) printf (" (%i)\n", end-start); } //end of the function RunThreadsOn
int ldap_pvt_thread_create( ldap_pvt_thread_t * thread, int detach, void *(*start_routine)( void * ), void *arg) { int rtn; pthread_attr_t attr; /* Always create the thread attrs, so we can set stacksize if we need to */ #if HAVE_PTHREADS > 5 pthread_attr_init(&attr); #else pthread_attr_create(&attr); #endif #ifdef LDAP_PVT_THREAD_SET_STACK_SIZE /* this should be tunable */ pthread_attr_setstacksize( &attr, LDAP_PVT_THREAD_STACK_SIZE ); if ( ldap_int_stackguard ) pthread_attr_setguardsize( &attr, LDAP_PVT_THREAD_STACK_SIZE ); #endif #if HAVE_PTHREADS > 5 detach = detach ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE; #if HAVE_PTHREADS == 6 pthread_attr_setdetachstate(&attr, &detach); #else pthread_attr_setdetachstate(&attr, detach); #endif #endif #if HAVE_PTHREADS < 5 rtn = pthread_create( thread, attr, start_routine, arg ); #else rtn = pthread_create( thread, &attr, start_routine, arg ); #endif #if HAVE_PTHREADS > 5 pthread_attr_destroy(&attr); #else pthread_attr_delete(&attr); if( detach ) { pthread_detach( thread ); } #endif #if HAVE_PTHREADS < 7 if ( rtn < 0 ) rtn = errno; #endif LDAP_JITTER(33); return rtn; }
/* ============= _RunThreadsOn ============= */ void _RunThreadsOn (int workcnt, qboolean showpacifier, void(*func)(int)) { int i; pthread_t work_threads[MAX_THREADS]; pthread_addr_t status; pthread_attr_t attrib; pthread_mutexattr_t mattrib; int start, end; start = I_FloatTime (); dispatch = 0; workcount = workcnt; oldf = -1; pacifier = showpacifier; if (pacifier) setbuf (stdout, NULL); if (!my_mutex) { my_mutex = safe_malloc (sizeof(*my_mutex)); if (pthread_mutexattr_create (&mattrib) == -1) Error ("pthread_mutex_attr_create failed"); if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1) Error ("pthread_mutexattr_setkind_np failed"); if (pthread_mutex_init (my_mutex, mattrib) == -1) Error ("pthread_mutex_init failed"); } if (pthread_attr_create (&attrib) == -1) Error ("pthread_attr_create failed"); if (pthread_attr_setstacksize (&attrib, 0x100000) == -1) Error ("pthread_attr_setstacksize failed"); for (i=0 ; i<numthreads ; i++) { if (pthread_create(&work_threads[i], attrib , (pthread_startroutine_t)func, (pthread_addr_t)i) == -1) Error ("pthread_create failed"); } for (i=0 ; i<numthreads ; i++) { if (pthread_join (work_threads[i], &status) == -1) Error ("pthread_join failed"); } end = I_FloatTime (); if (pacifier) Sys_Printf (" (%i)\n", end-start); }
dim_long dim_start_thread(void *(*thread_ast)(void *), dim_long tag) { pthread_t t_id; pthread_attr_t attr; #if defined (LYNXOS) && !defined (__Lynx__) pthread_attr_create(&attr); pthread_create(&t_id, attr, (void *)thread_ast, (void *)tag); #else pthread_attr_init(&attr); pthread_create(&t_id, &attr, thread_ast, (void *)tag); #endif return((dim_long)t_id); }
int32_t SimpleThread::start() { int32_t rc; static pthread_attr_t attr; static UBool attrIsInitialized = FALSE; PosixThreadImplementation *imp = (PosixThreadImplementation*)fImplementation; imp->fRunning = TRUE; imp->fRan = TRUE; #ifdef HPUX_CMA if (attrIsInitialized == FALSE) { rc = pthread_attr_create(&attr); attrIsInitialized = TRUE; } rc = pthread_create(&(imp->fThread),attr,&SimpleThreadProc,(void*)this); #else if (attrIsInitialized == FALSE) { rc = pthread_attr_init(&attr); #if U_PLATFORM == U_PF_OS390 { int detachstate = 0; // jdc30: detach state of zero causes //threads created with this attr to be in //an undetached state. An undetached //thread will keep its resources after //termination. pthread_attr_setdetachstate(&attr, &detachstate); } #else // pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); #endif attrIsInitialized = TRUE; } rc = pthread_create(&(imp->fThread),&attr,&SimpleThreadProc,(void*)this); #endif if (rc != 0) { // some kind of error occured, the thread did not start. imp->fRan = FALSE; imp->fRunning = FALSE; } return rc; }
/* ================== CalcPortalVis ================== */ void CalcPortalVis (void) { int i; // fastvis just uses mightsee for a very loose bound if (fastvis) { for (i=0 ; i<numportals*2 ; i++) { portals[i].visbits = portals[i].mightsee; portals[i].status = stat_done; } return; } leafon = 0; #ifdef __alpha { pthread_t work_threads[MAX_THREADS]; pthread_addr_t status; pthread_attr_t attrib; pthread_mutexattr_t mattrib; int i; my_mutex = malloc (sizeof(*my_mutex)); if (pthread_mutexattr_create (&mattrib) == -1) Error ("pthread_mutex_attr_create failed"); if (pthread_mutexattr_setkind_np (&mattrib, MUTEX_FAST_NP) == -1) Error ("pthread_mutexattr_setkind_np failed"); if (pthread_mutex_init (my_mutex, mattrib) == -1) Error ("pthread_mutex_init failed"); if (pthread_attr_create (&attrib) == -1) Error ("pthread_attr_create failed"); if (pthread_attr_setstacksize (&attrib, 0x100000) == -1) Error ("pthread_attr_setstacksize failed"); for (i=0 ; i<numthreads ; i++) { if (pthread_create(&work_threads[i], attrib , LeafThread, (pthread_addr_t)i) == -1) Error ("pthread_create failed"); } for (i=0 ; i<numthreads ; i++) { if (pthread_join (work_threads[i], &status) == -1) Error ("pthread_join failed"); } if (pthread_mutex_destroy (my_mutex) == -1) Error ("pthread_mutex_destroy failed"); } #else LeafThread (0); #endif if (verbose) { printf ("portalcheck: %i portaltest: %i portalpass: %i\n",c_portalcheck, c_portaltest, c_portalpass); printf ("c_vistest: %i c_mighttest: %i\n",c_vistest, c_mighttest); } }
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, ¶m); #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); }
/* * The main thread must call this function to convert itself into a thread. */ thread_t * thread_initial (unsigned long stack_size) { int rc; thread_t *thr = NULL; if (_main_thread) return _main_thread; /* * Initialize pthread key */ #ifndef OLD_PTHREADS rc = pthread_key_create (&_key_current, NULL); #else rc = pthread_keycreate (&_key_current, NULL); #endif CKRET (rc); /* * Start off with a value of NULL */ rc = pthread_setspecific (_key_current, NULL); CKRET (rc); /* * Initialize default thread/mutex attributes */ #ifndef OLD_PTHREADS /* attribute for thread creation */ rc = pthread_attr_init (&_thread_attr); CKRET (rc); /* attribute for mutex creation */ rc = pthread_mutexattr_init (&_mutex_attr); CKRET (rc); #else rc = pthread_attr_create (&_thread_attr); CKRET (rc); rc = pthread_mutexattr_create (&_mutex_attr); CKRET (rc); #endif #if defined (PTHREAD_PROCESS_PRIVATE) && !defined(oldlinux) && !defined(__FreeBSD__) rc = pthread_mutexattr_setpshared (&_mutex_attr, PTHREAD_PROCESS_PRIVATE); CKRET (rc); #endif #if defined (MUTEX_FAST_NP) && !defined (_AIX) rc = pthread_mutexattr_setkind_np (&_mutex_attr, MUTEX_FAST_NP); CKRET (rc); #endif #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP rc = pthread_mutexattr_settype (&_mutex_attr, PTHREAD_MUTEX_ADAPTIVE_NP); CKRET (rc); #endif /* * Allocate a thread structure */ thr = (thread_t *) dk_alloc (sizeof (thread_t)); memset (thr, 0, sizeof (thread_t)); assert (_main_thread == NULL); _main_thread = thr; _sched_init (); if (stack_size == 0) stack_size = MAIN_STACK_SIZE; #if (SIZEOF_VOID_P == 8) stack_size *= 2; #endif #if defined (__x86_64 ) && defined (SOLARIS) /*GK: the LDAP on that platform requires that */ stack_size *= 2; #endif stack_size = ((stack_size / 8192) + 1) * 8192; thr->thr_stack_size = stack_size; thr->thr_status = RUNNING; thr->thr_cv = _alloc_cv (); thr->thr_sem = semaphore_allocate (0); thr->thr_schedule_sem = semaphore_allocate (0); if (thr->thr_cv == NULL) goto failed; _thread_init_attributes (thr); thread_set_priority (thr, NORMAL_PRIORITY); rc = pthread_setspecific (_key_current, thr); CKRET (rc); return thr; failed: if (thr) { _thread_free_attributes (thr); dk_free (thr, sizeof (thread_t)); } return NULL; }
void dim_init() { pthread_t t_id; void ignore_sigpipe(); extern int dna_init(); /* #ifdef LYNXOS */ pthread_attr_t attr; /* #endif */ if(DIM_Threads_OFF) { dim_no_threads(); return; } if(!DIM_THR_init_done) { /* int prio; */ DIM_THR_init_done = 1; dna_init(); /* thr_getprio(thr_self(),&prio); thr_setprio(thr_self(),prio+3); */ INIT_thread = pthread_self(); MAIN_thread = INIT_thread; #ifndef darwin sem_init(&DIM_INIT_Sema, 0, (unsigned int)INIT_count); /* sem_init(&DIM_WAIT_Sema, 0, WAIT_count); */ #else DIM_INIT_Semap = sem_open("/Dim_INIT_Sem", O_CREAT, S_IRUSR | S_IWUSR, INIT_count); /* DIM_WAIT_Semap = sem_open("/Dim_WAIT_Sem", O_CREAT, S_IRUSR | S_IWUSR, WAIT_count); */ #endif ignore_sigpipe(); #if defined (LYNXOS) && !defined (__Lynx__) pthread_attr_create(&attr); pthread_create(&t_id, attr, dim_dtq_thread, 0); #else /* pthread_create(&t_id, NULL, dim_dtq_thread, 0); */ pthread_attr_init(&attr); pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); pthread_create(&t_id, &attr, dim_dtq_thread, 0); #endif #ifndef darwin sem_wait(&DIM_INIT_Sema); #else sem_wait(DIM_INIT_Semap); #endif #if defined (LYNXOS) && !defined (__Lynx__) pthread_create(&t_id, attr, dim_tcpip_thread, 0); #else pthread_create(&t_id, &attr, dim_tcpip_thread, 0); #endif #ifndef darwin sem_wait(&DIM_INIT_Sema); #else sem_wait(DIM_INIT_Semap); #endif INIT_thread = 0; } }
void dim_init() { static int done = 0; sigset_t set1; pthread_t t_id; int ret; /* #ifdef LYNXOS */ pthread_attr_t attr; /* #endif */ if(!done) { /* int prio; */ done = 1; dna_init(); /* thr_getprio(thr_self(),&prio); thr_setprio(thr_self(),prio+3); */ INIT_thread = pthread_self(); MAIN_thread = INIT_thread; #ifndef darwin sem_init(&DIM_INIT_Sema, 0, INIT_count); sem_init(&DIM_WAIT_Sema, 0, WAIT_count); #else DIM_INIT_Semap = sem_open("/Dim_INIT_Sem", O_CREAT, S_IRUSR | S_IWUSR, INIT_count); DIM_WAIT_Semap = sem_open("/Dim_WAIT_Sem", O_CREAT, S_IRUSR | S_IWUSR, WAIT_count); #endif ignore_sigpipe(); #ifdef LYNXOS pthread_attr_create(&attr); pthread_create(&t_id, attr, dim_dtq_thread, 0); #else /* pthread_create(&t_id, NULL, dim_dtq_thread, 0); */ pthread_attr_init(&attr); pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); pthread_create(&t_id, &attr, dim_dtq_thread, 0); #endif #ifndef darwin ret = sem_wait(&DIM_INIT_Sema); #else ret = sem_wait(DIM_INIT_Semap); #endif #ifdef LYNXOS pthread_create(&t_id, attr, dim_tcpip_thread, 0); #else pthread_create(&t_id, &attr, dim_tcpip_thread, 0); #endif #ifndef darwin ret = sem_wait(&DIM_INIT_Sema); #else ret = sem_wait(DIM_INIT_Semap); #endif INIT_thread = 0; } }