//---------------------------------------------------------------------- // Name: UDPConnection // Desc: //---------------------------------------------------------------------- UDPConnection::UDPConnection() { pthread_mutex_init( &messageQueueMutex, NULL ); pthread_mutex_init( &listenerThreadMutex, NULL ); // Setting up sharing of resources within Threads pthread_t messageQueue = pthread_self(); // Setting thread ID pthread_attr_t ThreadAttr; // Initialising thread attribute int policy = 0; // Initialising policy variable int max_prio = 0; // Initialising priority variable pthread_attr_init(&ThreadAttr); // Initialising thread ID via address to ThreadAttr variable pthread_attr_getschedpolicy(&ThreadAttr, &policy); // Getting scheduling priority to share resource max_prio = sched_get_priority_max(policy); // Assigning priority value //pthread_setschedprio(messageQueue, max_prio); // Setting the scheduled priority to messageQueue pthread_t listenerThread = pthread_self(); // Setting thread ID pthread_attr_t OtherThAttr; // Initialising thread attribute int policy2 = 0; // Initialising policy variable int max_prio2 = 0; // Initialising priority variable pthread_attr_init(&OtherThAttr); // Initialising thread ID via address to OtherThAttr variable pthread_attr_getschedpolicy(&OtherThAttr, &policy2); // Getting scheduling priority to share resource max_prio2 = sched_get_priority_max(policy2); // Assigning priority value //pthread_setschedprio(listenerThread, max_prio2); // Setting the scheduled priority to listenerThread stopListening = false; //listener = this; }
int CKFWThread::start( ) { int lReturnCode = 0; pthread_attr_t lThreadAttribute; int lThreadPolicy; struct sched_param lThreadScheduleParameters; int lError = 0; if ( (lError = pthread_attr_init( &lThreadAttribute ) ) != 0 ) { throw CKErrNoException( __FILE__, __LINE__, lError ); } if ( (lError = pthread_attr_getschedpolicy( &lThreadAttribute, &lThreadPolicy )) != 0 ) { throw CKErrNoException( __FILE__, __LINE__, lError ); } if ( (lError = pthread_attr_getschedparam( &lThreadAttribute, &lThreadScheduleParameters)) != 0) { throw CKErrNoException( __FILE__, __LINE__, lError ); } pthread_attr_setschedpolicy( &lThreadAttribute, mPolicy ); pthread_attr_getschedpolicy( &lThreadAttribute, &mPolicy ); if ( mPriority < 0.0 ) mPriority = 0.0; if ( mPriority > 1.0 ) mPriority = 1.0; int lPriorMax = sched_get_priority_max( mPolicy ); int lPriorMin = sched_get_priority_min( mPolicy ); lThreadScheduleParameters.sched_priority = lPriorMin + (int)floor( ( lPriorMax - lPriorMin ) * mPriority ); pthread_attr_setschedparam( &lThreadAttribute, &lThreadScheduleParameters ); if ( ( lError = pthread_attr_setscope(&lThreadAttribute, mScope) ) != 0 ) { throw CKErrNoException( __FILE__, __LINE__, lError ); } if ( (lError = pthread_create( &mThread, &lThreadAttribute, CKFWThread::threadFunction, this) ) == 0 ) { if ( mIsDetachable ) { pthread_detach( mThread ); } pthread_attr_destroy( &lThreadAttribute ); } else { throw CKErrNoException( __FILE__, __LINE__, lError ); } return lReturnCode; }
/////////////////////////////////////////////////////////////////////////////// /// \brief starts the processing thread and sets priority to max /// void InputHandler :: start () { pthread_attr_t attr ; pthread_attr_init ( &attr ) ; int policy = 0 ; int max_prio_for_policy = 0 ; pthread_attr_getschedpolicy ( &attr , &policy ) ; max_prio_for_policy = sched_get_priority_max ( policy ) ; glob_t globbuf ; glob ( "/dev/input/event*" , GLOB_TILDE , NULL , &globbuf ) ; printf ( "number of events found %d\n" , ( int ) globbuf . gl_pathc ) ; for ( unsigned int loop = 0 ; loop < globbuf . gl_pathc ; ++loop ) { if ( isValidInputEventFile ( globbuf . gl_pathv [ loop ] ) ) { std :: string *filename = new std :: string ( globbuf . gl_pathv [ loop ] ) ; rt_printf ( "InputHandler starting thread: %s\n" , filename -> c_str () ) ; pthread_create ( &thread , NULL , ( void* (*) ( void*) ) ( thread_func ) , filename ) ; pthread_setschedprio ( thread , max_prio_for_policy ) ; pthread_attr_destroy ( &attr ) ; } } }
TCGvoid TCGClient::AddThreadPriority() { #ifdef ANDROID #if 0 /* Add thread priority */ pthread_attr_t attr; TCGbool policy; TCGbool rs; struct sched_param param; TCGbool maxThreadPriority; rs = pthread_attr_init(&attr); pthread_attr_getschedpolicy(&attr, &policy); if (policy != SCHED_RR) { pthread_attr_setschedpolicy(&attr, SCHED_RR); } maxThreadPriority = sched_get_priority_max(SCHED_RR); rs = pthread_attr_getschedparam(&attr, ¶m); policy = param.sched_priority; if (policy < maxThreadPriority) { param.sched_priority = maxThreadPriority; pthread_attr_setschedparam(&attr, ¶m); rs = pthread_attr_getschedparam(&attr, ¶m); } #endif #endif }
void clone_attributes(pthread_attr_t *new_attr, pthread_attr_t *old_attr) { struct sched_param param; void *addr; size_t size; int value; (void) pthread_attr_init(new_attr); if (old_attr != NULL) { (void) pthread_attr_getstack(old_attr, &addr, &size); /* don't allow a non-NULL thread stack address */ (void) pthread_attr_setstack(new_attr, NULL, size); (void) pthread_attr_getscope(old_attr, &value); (void) pthread_attr_setscope(new_attr, value); (void) pthread_attr_getinheritsched(old_attr, &value); (void) pthread_attr_setinheritsched(new_attr, value); (void) pthread_attr_getschedpolicy(old_attr, &value); (void) pthread_attr_setschedpolicy(new_attr, value); (void) pthread_attr_getschedparam(old_attr, ¶m); (void) pthread_attr_setschedparam(new_attr, ¶m); (void) pthread_attr_getguardsize(old_attr, &size); (void) pthread_attr_setguardsize(new_attr, size); } /* make all pool threads be detached threads */ (void) pthread_attr_setdetachstate(new_attr, PTHREAD_CREATE_DETACHED); }
int verify_policy(pthread_attr_t *attr, int policytype) { int rc; int policy; rc = pthread_attr_getschedpolicy(attr, &policy); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_getschedpolicy\n"); exit(PTS_UNRESOLVED); } switch(policytype) { case SCHED_FIFO: if (policy != FIFOPOLICY) { printf(ERROR_PREFIX "got wrong policy param\n"); exit(PTS_FAIL); } break; case SCHED_RR: if (policy != RRPOLICY) { printf(ERROR_PREFIX "got wrong policy param\n"); exit(PTS_FAIL); } break; case SCHED_OTHER: if (policy != OTHERPOLICY) { printf(ERROR_PREFIX "got wrong policy param\n"); exit(PTS_FAIL); } break; } return 0; }
main(int argc, char *argv[]) { int i, policy; pthread_t tid[NUM_THREADS]; /* the thread identifier */ pthread_attr_t attr; /* set of attributes for the thread */ /* get the default attributes */ pthread_attr_init(&attr); /* get the current scheduling policy */ if (pthread_attr_getschedpolicy(&attr,&policy) != 0) fprintf(stderr, "Unable to get policy.\n"); else { if (policy == SCHED_OTHER) printf("SCHED_OTHER\n"); else if (policy == SCHED_RR) printf("SCHED_OTHER\n"); else if (policy == SCHED_FIFO) printf("SCHED_FIFO\n"); } /* set the scheduling policy - FIFO, RT, or OTHER */ if (pthread_attr_setschedpolicy(&attr, SCHED_OTHER) != 0) printf("unable to set scheduling policy to SCHED_OTHER \n"); /* create the threads */ for (i = 0; i < NUM_THREADS; i++) pthread_create(&tid[i],&attr,runner,NULL); /** * Now join on each thread */ for (i = 0; i < NUM_THREADS; i++) pthread_join(tid[i], NULL); }
int main(void) { unsigned int ind; int rc; int policy; for (ind = 0; ind < NUM_THREADS; ind++) { printf("[%u] thread create\n", ind); ids[ind] = ind; rc = pthread_create(&threads[ind], 0, hello, &ids[ind]); if (rc) { printf("[%u] error: return %d\n", ind, rc); return 1; } } rc = pthread_attr_init(&attr); if (rc != 0) { printf("error: attr_init: %s\n", strerror(rc)); return 1; } rc = pthread_attr_getschedpolicy(&attr, &policy); if (rc != 0) { printf("error: getschedpolicy: %s\n", strerror(rc)); return 1; } pthread_exit(0); return 0; }
void NONS_Thread::call(NONS_ThreadedFunctionPointer function,void *data,bool give_highest_priority){ if (this->called) return; threadStruct *ts=new threadStruct; ts->f=function; ts->d=data; #if NONS_SYS_WINDOWS this->thread=CreateThread(0,0,(LPTHREAD_START_ROUTINE)runningThread,ts,0,0); if (give_highest_priority) SetThreadPriority(this->thread,THREAD_PRIORITY_HIGHEST); #elif NONS_SYS_UNIX pthread_attr_t attr, *pattr=0; if (give_highest_priority){ pattr=&attr; pthread_attr_init(pattr); sched_param params; pthread_attr_getschedparam(pattr,¶ms); int policy; pthread_attr_getschedpolicy(pattr,&policy); params.sched_priority=sched_get_priority_max(policy); pthread_attr_setschedparam(pattr,¶ms); } pthread_create(&this->thread,pattr,runningThread,ts); if (give_highest_priority) pthread_attr_destroy(pattr); #elif NONS_SYS_PSP this->thread=SDL_CreateThread(runningThread,ts); #endif this->called=1; }
static int get_thread_policy(pthread_attr_t *attr) { int policy; int rs = pthread_attr_getschedpolicy(attr,&policy); assert(rs==0); switch(policy) { case SCHED_FIFO: printf("policy=SCHED_FIFO\n"); break; case SCHED_RR: printf("policy=SCHED_RR\n"); break; case SCHED_OTHER: printf("policy=SCHED_OTHER\n"); break; default: printf("policy=UNKNOWN\n"); break; } return policy; }
void pixie_cpu_raise_priority(void) { #if defined WIN32 DWORD_PTR result; result = SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); if (result == 0) { fprintf(stderr, "set_priority: returned error win32:%u\n", (unsigned)GetLastError()); } #elif defined(__linux__) && defined(__GNUC__) pthread_t thread = pthread_self(); pthread_attr_t thAttr; int policy = 0; int max_prio_for_policy = 0; pthread_attr_init(&thAttr); pthread_attr_getschedpolicy(&thAttr, &policy); max_prio_for_policy = sched_get_priority_max(policy); pthread_setschedprio(thread, max_prio_for_policy); pthread_attr_destroy(&thAttr); return; #endif }
static void display_pthread_attr (pthread_attr_t *attr, char *prefix) { int s, i; size_t v; void *stkaddr; struct sched_param sp; s = pthread_attr_getdetachstate (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getdetachstate"); printf ("%sDetach state = %s\n", prefix, (i == PTHREAD_CREATE_DETACHED) ? "PTHREAD_CREATE_DETACHED" : (i == PTHREAD_CREATE_JOINABLE) ? "PTHREAD_CREATE_JOINABLE" : "???"); s = pthread_attr_getscope (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getscope"); printf ("%sScope = %s\n", prefix, (i == PTHREAD_SCOPE_SYSTEM) ? "PTHREAD_SCOPE_SYSTEM" : (i == PTHREAD_SCOPE_PROCESS) ? "PTHREAD_SCOPE_PROCESS" : "???"); s = pthread_attr_getinheritsched (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getinheritsched"); printf ("%sInherit scheduler = %s\n", prefix, (i == PTHREAD_INHERIT_SCHED) ? "PTHREAD_INHERIT_SCHED" : (i == PTHREAD_EXPLICIT_SCHED) ? "PTHREAD_EXPLICIT_SCHED" : "???"); s = pthread_attr_getschedpolicy (attr, &i); if (s != 0) handle_error_en (s, "pthread_attr_getschedpolicy"); printf ("%sScheduling policy = %s\n", prefix, (i == SCHED_OTHER) ? "SCHED_OTHER" : (i == SCHED_FIFO) ? "SCHED_FIFO" : (i == SCHED_RR) ? "SCHED_RR" : "???"); s = pthread_attr_getschedparam (attr, &sp); if (s != 0) handle_error_en (s, "pthread_attr_getschedparam"); printf ("%sScheduling priority = %d\n", prefix, sp.sched_priority); s = pthread_attr_getguardsize (attr, &v); if (s != 0) handle_error_en (s, "pthread_attr_getguardsize"); printf ("%sGuard size = %ld bytes\n", prefix, v); s = pthread_attr_getstack (attr, &stkaddr, &v); if (s != 0) handle_error_en (s, "pthread_attr_getstack"); printf ("%sStack address = %p\n", prefix, stkaddr); printf ("%sStack size = 0x%zx bytes\n", prefix, v); }
void test( void ) { pthread_attr_t attr; int policy; int result; result = pthread_attr_getschedpolicy( &attr, &policy ); }
int startThread(Thread * thrd) { struct sched_param schedp; pthread_condattr_t condattr; int retc, policy, inherit; printf("Start thread priority %d\n", thrd->priority); if (pthread_attr_init(&(thrd->attr)) != 0) { printf("Attr init failed"); exit(2); } thrd->flags = 0; memset(&schedp, 0, sizeof(schedp)); schedp.sched_priority = thrd->priority; policy = thrd->policy; if (pthread_attr_setschedpolicy(&(thrd->attr), policy) != 0) { printf("Can't set policy %d\n", policy); } if (pthread_attr_getschedpolicy(&(thrd->attr), &policy) != 0) { printf("Can't get policy\n"); } else { printf("Policy in attribs is %d\n", policy); } if (pthread_attr_setschedparam(&(thrd->attr), &schedp) != 0) { printf("Can't set params"); } if (pthread_attr_getschedparam(&(thrd->attr), &schedp) != 0) { printf("Can't get params"); } else { printf("Priority in attribs is %d\n", schedp.sched_priority); } if (pthread_attr_setinheritsched(&(thrd->attr), PTHREAD_EXPLICIT_SCHED) != 0) { printf("Can't set inheritsched\n"); } if (pthread_attr_getinheritsched(&(thrd->attr), &inherit) != 0) { printf("Can't get inheritsched\n"); } else { printf("inherit sched in attribs is %d\n", inherit); } if ((retc = pthread_mutex_init(&(thrd->mutex), NULL)) != 0) { printf("Failed to init mutex: %d\n", retc); } if (pthread_condattr_init(&condattr) != 0) { printf("Failed to init condattr\n"); } if (pthread_cond_init(&(thrd->cond), &condattr) != 0) { printf("Failed to init cond\n"); } retc = pthread_create(&(thrd->pthread), &(thrd->attr), thrd->func, thrd); printf("Create returns %d\n\n", retc); return retc; }
CThread::CThread(IRunnable *runner) : _runner(runner), _mutex(), _cond(), _tid(0), _tattr(), _tpoli(0), _tparm(), _running(false), _finished(false), _canceled(false) { int e; if ((e = pthread_attr_init(&_tattr)) != 0) throw XThread(e); if ((e = pthread_attr_getschedpolicy(&_tattr, &_tpoli)) != 0) throw XThread(e); if ((e = pthread_attr_getschedparam(&_tattr, &_tparm)) != 0) throw XThread(e); }
/* Read schedpolicy attribute */ void read_schedpolicy_attribute(pthread_attr_t *attr) { size_t policy; pthread_attr_getschedpolicy(attr,&policy); printf("\n Scheduling Policy Attribute"); if(policy == SCHED_OTHER ) printf("\n\tOther Scheduling Policy\n"); else if(policy == SCHED_RR ) printf("\n\tRound Robin Scheduling Policy\n"); else if(policy == SCHED_FIFO ) printf("\n\tFIFO Scheduling Policy\n"); }
int pthread_attr_init_ex(pthread_attr_ex_t *attr_ex) { struct sched_param param; int policy; /* Start with defaulting all fields to null. */ memset(attr_ex, 0, sizeof(*attr_ex)); /* Merge in the default standard attribute set. */ pthread_attr_init(&attr_ex->std); pthread_attr_getschedpolicy(&attr_ex->std, &policy); attr_ex->nonstd.sched_policy = policy; pthread_attr_getschedparam(&attr_ex->std, ¶m); attr_ex->nonstd.sched_param.sched_priority = param.sched_priority; return 0; }
/************************************************* * Function: Pthread_attr_getschedpolicy() * Description: 获取线程调度策略属性包裹函数 * Input: *attr---线程属性结构 * Output: *policy---线程调度策略属性 * Return: 0/errno *************************************************/ int Pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) { int rval; if(attr==AII_NULL || policy==AII_NULL) { return -1; } if((rval = pthread_attr_getschedpolicy(attr, policy)) != 0) { debug_info(DEBUG_LEVEL_4,"Pthread_attr_getschedpolicy() failed!\n"); } return rval; }
void* fetchworkercmh(void* arg) { pthread_attr_t thAttr; int policy = 0; pthread_attr_init(&thAttr); pthread_attr_getschedpolicy(&thAttr, &policy); pthread_setschedprio(pthread_self(), sched_get_priority_min(policy)); FetchWorker* fw = (FetchWorker*)arg; while(!fw->exit) { FetchQueue* mfsitem = NULL; PX_LOCK(&fw->mt); while(fw->head == NULL) { pthread_cond_wait(&fw->cv, &fw->mt); if(fw->exit) { PX_UNLOCK(&fw->mt); break; } } mfsitem = fw->head; if (mfsitem != NULL) { fw->head = fw->head->next; PX_UNLOCK(&fw->mt); /*---------process write back queue---------*/ ReadCMHToCache(mfsitem->cmh, mfsitem->filesize); free(mfsitem); } else { fw->tail = NULL; PX_UNLOCK(&fw->mt); } continue; } pthread_exit(NULL); }
intptr_t thread_start_low_priority (void (*fn)(void *ctx), void *ctx) { #if defined(__linux__) && !defined(ANDROID) pthread_t tid; pthread_attr_t attr; int s = pthread_attr_init (&attr); if (s != 0) { fprintf (stderr, "pthread_attr_init failed: %s\n", strerror (s)); return 0; } #if !STATICLINK && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4 int policy; s = pthread_attr_getschedpolicy (&attr, &policy); if (s != 0) { fprintf (stderr, "pthread_attr_getschedpolicy failed: %s\n", strerror (s)); return 0; } int minprio = sched_get_priority_min (policy); #endif s = pthread_create (&tid, &attr, (void *(*)(void *))fn, (void*)ctx); if (s != 0) { fprintf (stderr, "pthread_create failed: %s\n", strerror (s)); return 0; } #if !STATICLINK && __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 4 s = pthread_setschedprio (tid, minprio); if (s != 0) { fprintf (stderr, "pthread_setschedprio failed: %s\n", strerror (s)); pthread_cancel (tid); return 0; } #endif s = pthread_attr_destroy (&attr); if (s != 0) { fprintf (stderr, "pthread_attr_destroy failed: %s\n", strerror (s)); pthread_cancel (tid); return 0; } return tid; #else return thread_start (fn, ctx); #endif }
int main(void) /****************************************************************/ { pthread_t thread[10]; pthread_attr_t thread_attr; struct sched_param thread_param; int thread_policy; int th_id; int th_status; int rr_min_priority, rr_max_priority; if(pthread_attr_init(&thread_attr) != 0) { printf("Thread attr init error\n"); return ERROR; } pthread_attr_getschedpolicy(&thread_attr, &thread_policy); pthread_attr_getschedparam(&thread_attr, &thread_param); // printf("Default policy is %s, priority is %d\n", (thread_policy == SCHED_FIFO ? "FIFO" : (thread_policy == SCHED_RR ? "RR" : (thread_policy == SCHED_OTHER ? "OTHER" : "unknown"))), thread_param.sched_priority); // rr_min_priority = sched_get_priority_min(SCHED_RR); rr_max_priority = sched_get_priority_max(SCHED_RR); printf("[Round Robin] Min : %d, Max : %d\n", rr_min_priority, rr_max_priority); // // th_id = pthread_create(&thread[0], NULL, (void*(*)(void*)) plc_main, (void*) &thread[0]); printf("Plc_Main thread created\n"); sleep(3); // // pthread_join(thread[0], (void **)th_status); // return 1; }
void *child_thread(void *arg) { int policy; int max_priority,min_priority; struct sched_param param; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setinheritsched(&attr,PTHREAD_EXPLICIT_SCHED); pthread_attr_getinheritsched(&attr,&policy); if(policy == PTHREAD_EXPLICIT_SCHED) { printf("inheritsched:PTHREAD_EXPLICIT_SCHED\n"); } if(policy == PTHREAD_INHERIT_SCHED) { printf("Inheritsched:PTHREAD_INHERIT_SCHED\n"); } pthread_attr_setschedpolicy(&attr,SCHED_RR); pthread_attr_getschedpolicy(&attr,&policy); if(policy ==SCHED_FIFO) { printf("schedpolicy: SCHED_FIFO\n"); } if(policy == SCHED_RR) printf("schedpolicy: SCHED_RR\n"); if(policy == SCHED_OTHER) printf("schedpolicy: SCHED_OTHER\n"); sched_get_priority_max(max_priority); sched_get_priority_min(min_priority); printf("max priority: %u\n",max_priority); printf("min priority: %u\n",min_priority); param.sched_priority = max_priority; pthread_attr_setschedparam(&attr,¶m); printf("sched_priority:%u\n",param.sched_priority); pthread_attr_destroy(&attr); }
void Initthread_DataFream() { DataLoop=true; DataLoopDF=true; delay.tv_nsec=10000000;//1000000000 delay.tv_sec=0; pthread_attr_t attr_DataFream; pthread_attr_init(&attr_DataFream); pthread_attr_setscope(&attr_DataFream, PTHREAD_SCOPE_PROCESS); pthread_attr_setdetachstate(&attr_DataFream, PTHREAD_CREATE_DETACHED); int policy; int rs = pthread_attr_getschedpolicy( &attr_DataFream, &policy ); int priority = sched_get_priority_max( policy ); struct sched_param param; pthread_attr_getschedparam(&attr_DataFream,¶m); param.sched_priority=priority*2/3; pthread_attr_setschedparam(&attr_DataFream,¶m); pthread_t threadID_DataFream; pthread_mutex_init(&mutex_DataFream, NULL); pthread_create( &threadID_DataFream, &attr_DataFream, DataFream, NULL); }
INTERNAL_QUAL int rtos_task_create(RTOS_TASK* task, int priority, const char * name, int sched_type, size_t stack_size, void * (*start_routine)(void *), ThreadInterface* obj) { int rv; // return value rtos_task_check_priority( &sched_type, &priority ); // Save priority internally, since the pthread_attr* calls are broken ! // we will pick it up later in rtos_task_set_scheduler(). task->priority = priority; // Set name if ( strlen(name) == 0 ) name = "Thread"; task->name = strcpy( (char*)malloc( (strlen(name) + 1) * sizeof(char)), name); if ( (rv = pthread_attr_init(&(task->attr))) != 0 ){ return rv; } // Set scheduler type (_before_ assigning priorities!) if ( (rv = pthread_attr_setschedpolicy(&(task->attr), sched_type)) != 0){ return rv; } pthread_attr_getschedpolicy(&(task->attr), &rv ); assert( rv == sched_type ); struct sched_param sp; sp.sched_priority=priority; // Set priority if ( (rv = pthread_attr_setschedparam(&(task->attr), &sp)) != 0 ){ return rv; } rv = pthread_create(&(task->thread), &(task->attr), start_routine, obj); log(Debug) <<"Created Posix thread "<< task->thread <<endlog(); return rv; }
void thread::start(int priority) { if (atomic::bool_compare_and_swap(&__running, false, true)) { wait(); int e; pthread_attr_t priority_thread_attr; pthread_attr_t* thread_attr = NULL; if (priority != priority_default) { // Currently this means priority_min int policy, min_priority; struct sched_param param; e = pthread_attr_init(&priority_thread_attr); e = e || pthread_attr_getschedpolicy(&priority_thread_attr, &policy); if (e == 0) { min_priority = sched_get_priority_min(policy); if (min_priority == -1) e = errno; } e = e || pthread_attr_getschedparam(&priority_thread_attr, ¶m); if (e == 0) { param.sched_priority = min_priority; } e = e || pthread_attr_setschedparam(&priority_thread_attr, ¶m); if (e != 0) { throw exc(std::string(_("System function failed: ")) + "pthread_attr_*(): " + std::strerror(e), e); } thread_attr = &priority_thread_attr; } e = pthread_create(&__thread_id, thread_attr, __run, this); if (e != 0) { throw exc(std::string(_("System function failed: ")) + "pthread_create(): " + std::strerror(e), e); } __joinable = true; } }
int pth_attri_get (pth_attri_t *attri, pth_attr_types_t t, void *data) { if (attri != (pth_attri_t *)NULL) { switch (t) { /* pthread_attr_getdetachstate */ case PTH_ATTR_DETACHED: return pthread_attr_getdetachstate (&(attri->attr), (int *)data); /* pthread_attr_getdetachstate */ case PTH_ATTR_JOINABLE: return pthread_attr_getdetachstate (&(attri->attr), (int *)data); #ifndef LINUX /* pthread_attr_getstacksize */ case PTH_ATTR_STACKSZ: return pthread_attr_getstacksize (&(attri->attr), (size_t *)data); /* pthread_attr_getguardsize */ case PTH_ATTR_GUARDSZ: return pthread_attr_getguardsize (&(attri->attr), (size_t *)data); #endif /* !LINUX */ /* pthread_attr_getschedparam */ case PTH_ATTR_SCHEDPARAM: return pthread_attr_getschedparam (&(attri->attr), (struct sched_param *)data); /* pthread_attr_getinheritsched */ case PTH_ATTR_INSCHEDPARAM: return pthread_attr_getinheritsched (&(attri->attr), (int *)data); /* pthread_attr_getschedpolicy */ case PTH_ATTR_SCHEDPOLICY: return pthread_attr_getschedpolicy (&(attri->attr), (int *)data); /* pthread_attr_getscope */ case PTH_ATTR_SCOPE: return pthread_attr_getscope (&(attri->attr), (int *)data); default: return CAF_ERROR_SUB; } } return CAF_ERROR_SUB; }
IdleList::IdleList(mdaLooplex *effect, IdleList *next) : effect(effect), next(next), remove(false) { if(effect && !timer) //start timer { #if WIN32 timer = SetTimer(NULL, 0, IDLE_MSEC, TimerCallback); #elif __linux__ timer = 1; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 16 * 1024); int policy; if (pthread_attr_getschedpolicy(&attr, &policy) == 0) { struct sched_param param; param.sched_priority = sched_get_priority_min(policy); pthread_attr_setschedpolicy(&attr, SCHED_OTHER); pthread_attr_setschedparam(&attr, ¶m); } pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, 0); if (pthread_create(&thread, &attr, &ThreadCallback, 0) != 0) { thread = 0; timer = 0; fprintf(stderr, "Error: mdaLooplex.cpp (line %d)\n", __LINE__); } pthread_attr_destroy(&attr); #else //OSX double ms = kEventDurationMillisecond * (double)IDLE_MSEC; InstallEventLoopTimer(GetCurrentEventLoop(), ms, ms, NewEventLoopTimerUPP(TimerCallback), 0, &timer); #endif } }
int32_t system_thread_create(system_thread_t *thread, const system_thread_attr_t *attr, void*(*start_routine)(void *), void *param) { int32_t status = ERR_SUCCESS; int32_t sched_policy; int32_t priority_max, priority_min; struct sched_param sched_param; pthread_attr_t pthread_attr; if (attr) { status = pthread_attr_init(&pthread_attr); /* Set priority */ if (attr->priority && !status) { status = pthread_attr_getschedpolicy(&pthread_attr, &sched_policy); if (!status) { switch (attr->priority) { case MaxThreadPriority: sched_param.sched_priority = sched_get_priority_max( sched_policy); break; case HighThreadPriority: priority_max = sched_get_priority_max(sched_policy); priority_min = sched_get_priority_min(sched_policy); sched_param.sched_priority = (priority_min + 3 * priority_max) / 4; break; case NormalThreadPriority: priority_max = sched_get_priority_max(sched_policy); priority_min = sched_get_priority_min(sched_policy); sched_param.sched_priority = (priority_min + priority_max) / 2; break; case LowThreadPriority: priority_max = sched_get_priority_max(sched_policy); priority_min = sched_get_priority_min(sched_policy); sched_param.sched_priority = (priority_max + 3 * priority_min) / 4; break; case MinThreadPriority: sched_param.sched_priority = sched_get_priority_min( sched_policy); break; default: status = ERR_FAILURE; break; } } if (!status) status = pthread_attr_setschedparam(&pthread_attr, &sched_param); } /* Set stack address */ if (attr->stack_loc && !status) status = pthread_attr_setstackaddr(&pthread_attr, attr->stack_loc); /* Set stack size */ if (attr->stack_size && !status) status = pthread_attr_setstacksize(&pthread_attr, attr->stack_size); if (!status) status = pthread_create((pthread_t *) thread, &pthread_attr, start_routine, param); pthread_attr_destroy(&pthread_attr); } else { status = pthread_create((pthread_t *) thread, NULL, start_routine, param); } if (status) return ERR_FAILURE; return ERR_SUCCESS; }
static void* fetchworker(void* arg) { pthread_attr_t thAttr; int policy = 0; pthread_attr_init(&thAttr); pthread_attr_getschedpolicy(&thAttr, &policy); pthread_setschedprio(pthread_self(), sched_get_priority_min(policy)); PX_ASSERT(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL)==0); FetchWorker* fw = (FetchWorker*)arg; // PathEntry* pe = NULL; while(!fw->exit) { FetchQueue* mfsitem; PX_LOCK(&fw->mt); while(fw->head == NULL) { pthread_cond_wait(&fw->cv, &fw->mt); if(fw->exit) { PX_UNLOCK(&fw->mt); break; } } mfsitem = fw->head; if (mfsitem != NULL) { fw->head = fw->head->next; PX_UNLOCK(&fw->mt); /*---------process write back queue---------*/ ReadFileToCache(mfsitem->rscpath, mfsitem->filesize); // *(mfsitem->flag) = '1' ; free(mfsitem); } else { fw->tail = NULL; PX_UNLOCK(&fw->mt); } continue; #ifdef USEFETCHCONF pe = readfetchconf(); while (pe != NULL) { if (pe->pt == PATHDIR_T) { fetchDir(pe->path); } else if (pe->pt == PATHFILE_T) { ReadFileToCache(pe->path); } PathEntry* freepe = pe; pe = pe->next; free(freepe); } printf("done fetchworker!\n"); sleep(FETCHWORKERSLEEPTIME); #endif } pthread_exit(NULL); }
/*! Begins execution of the thread by calling run(), which should be reimplemented in a QThread subclass to contain your code. The operating system will schedule the thread according to the \a priority parameter. If the thread is already running, this function does nothing. \sa run(), terminate() */ void QThread::start(Priority priority) { Q_D(QThread); QMutexLocker locker(&d->mutex); if (d->running) return; d->running = true; d->finished = false; d->terminated = false; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); d->priority = priority; #if defined(Q_OS_DARWIN) || !defined(Q_OS_OPENBSD) && defined(_POSIX_THREAD_PRIORITY_SCHEDULING) && (_POSIX_THREAD_PRIORITY_SCHEDULING-0 >= 0) switch (priority) { case InheritPriority: { pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); break; } default: { int sched_policy; if (pthread_attr_getschedpolicy(&attr, &sched_policy) != 0) { // failed to get the scheduling policy, don't bother // setting the priority qWarning("QThread::start: Cannot determine default scheduler policy"); break; } int prio_min = sched_get_priority_min(sched_policy); int prio_max = sched_get_priority_max(sched_policy); if (prio_min == -1 || prio_max == -1) { // failed to get the scheduling parameters, don't // bother setting the priority qWarning("QThread::start: Cannot determine scheduler priority range"); break; } int prio; switch (priority) { case IdlePriority: prio = prio_min; break; case TimeCriticalPriority: prio = prio_max; break; default: // crudely scale our priority enum values to the prio_min/prio_max prio = (((prio_max - prio_min) / TimeCriticalPriority) * priority) + prio_min; prio = qMax(prio_min, qMin(prio_max, prio)); break; } sched_param sp; sp.sched_priority = prio; if (pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED) != 0 || pthread_attr_setschedpolicy(&attr, sched_policy) != 0 || pthread_attr_setschedparam(&attr, &sp) != 0) { // could not set scheduling hints, fallback to inheriting them pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); } break; } } #endif // _POSIX_THREAD_PRIORITY_SCHEDULING if (d->stackSize > 0) { #if defined(_POSIX_THREAD_ATTR_STACKSIZE) && (_POSIX_THREAD_ATTR_STACKSIZE-0 > 0) int code = pthread_attr_setstacksize(&attr, d->stackSize); #else int code = ENOSYS; // stack size not supported, automatically fail #endif // _POSIX_THREAD_ATTR_STACKSIZE if (code) { qWarning("QThread::start: Thread stack size error: %s", qPrintable(qt_error_string(code))); // we failed to set the stacksize, and as the documentation states, // the thread will fail to run... d->running = false; d->finished = false; return; } } int code = pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this); if (code == EPERM) { // caller does not have permission to set the scheduling // parameters/policy pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); code = pthread_create(&d->thread_id, &attr, QThreadPrivate::start, this); } pthread_attr_destroy(&attr); if (code) { qWarning("QThread::start: Thread creation error: %s", qPrintable(qt_error_string(code))); d->running = false; d->finished = false; d->thread_id = 0; } }