Thread_t thread_create(int prio, int sched, ThreadFunc_t func, void *arg) { #define n_static 256 static struct ThreadArgs args_hack[n_static]; static unsigned hack_idx = 0; struct ThreadArgs *args = &args_hack[hack_idx++%n_static]; #undef n_static pthread_t thr; pthread_attr_t attr; #ifndef __KERNEL__ if (sched == Default && geteuid() == 0) sched = RR; else if (sched == Default) sched = Other; #else if (sched == Default) sched = RR; #endif if (prio < thread_prio_min(sched)) prio = thread_prio_min(sched); if (prio > thread_prio_max(sched)) prio = thread_prio_max(sched); pthread_attr_init(&attr); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); if (sched != Inherit) { struct sched_param sp; sched = scheduler_translate(sched); pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&attr, sched); sp.sched_priority = prio; pthread_attr_setschedparam(&attr, &sp); } else if (sched == Inherit) { pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); } #ifdef RTLINUX pthread_attr_setfp_np(&attr, 1); #endif args->func = func; args->arg = arg; (void) prio; /* TODO: implement priorities.. */ if (pthread_create(&thr, &attr, thread_wrapper, args)) /* TODO handle errors here.. */ return NULL; pthread_attr_destroy(&attr); return (Thread_t)thr; }
/** * Starts the thread. This method will signal to start the thread and * return immediately. Note that the thread might not yet run when this * method returns! The abstract method Main() is the entry point for the * new thread. You have to implement the Main() method in your subclass. * * @see StartThread() */ int Thread::SignalStartThread() { // prepare the thread properties int res = pthread_attr_setinheritsched(&__thread_attr, PTHREAD_EXPLICIT_SCHED); if (res) { std::cerr << "Thread creation failed: Could not inherit thread properties." << std::endl << std::flush; RunningCondition.Set(false); return res; } res = pthread_attr_setdetachstate(&__thread_attr, PTHREAD_CREATE_JOINABLE); if (res) { std::cerr << "Thread creation failed: Could not request a joinable thread." << std::endl << std::flush; RunningCondition.Set(false); return res; } res = pthread_attr_setscope(&__thread_attr, PTHREAD_SCOPE_SYSTEM); if (res) { std::cerr << "Thread creation failed: Could not request system scope for thread scheduling." << std::endl << std::flush; RunningCondition.Set(false); return res; } res = pthread_attr_setstacksize(&__thread_attr, MIN_STACK_SIZE); if (res) { std::cerr << "Thread creation failed: Could not set minimum stack size." << std::endl << std::flush; RunningCondition.Set(false); return res; } // Create and run the thread res = pthread_create(&this->__thread_id, &__thread_attr, __pthread_launcher, this); switch (res) { case 0: // Success break; case EAGAIN: std::cerr << "Thread creation failed: System doesn't allow to create another thread." << std::endl << std::flush; RunningCondition.Set(false); break; case EPERM: std::cerr << "Thread creation failed: You're lacking permisssions to set required scheduling policy and parameters." << std::endl << std::flush; RunningCondition.Set(false); break; default: std::cerr << "Thread creation failed: Unknown cause." << std::endl << std::flush; RunningCondition.Set(false); break; } return res; }
int start_interrupt_source( int intr ) { pthread_attr_t pattr; struct sched_param param; pthread_attr_init( &pattr ); param.sched_priority = 15; pthread_attr_setschedparam( &pattr, ¶m ); pthread_attr_setinheritsched( &pattr, PTHREAD_EXPLICIT_SCHED ); return pthread_create( NULL, NULL, interrupt_thread, (void *)intr ); }
int main() { pthread_t new_th; pthread_attr_t attr; int rc; struct sched_param sp; /* Initialize attr */ rc = pthread_attr_init(&attr); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_init"); exit(PTS_UNRESOLVED); } rc = pthread_attr_setschedpolicy(&attr, policy); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_setschedpolicy"); exit(PTS_UNRESOLVED); } sp.sched_priority = 1; rc = pthread_attr_setschedparam(&attr, &sp); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_setschedparam"); exit(PTS_UNRESOLVED); } int insched = PTHREAD_EXPLICIT_SCHED; rc = pthread_attr_setinheritsched(&attr, insched); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_setinheritsched"); exit(PTS_UNRESOLVED); } rc = pthread_create(&new_th, &attr, thread_func, NULL); if (rc != 0) { printf("Error at pthread_create(): %s\n", strerror(rc)); exit(PTS_UNRESOLVED); } rc = pthread_join(new_th, NULL); if (rc != 0) { printf(ERROR_PREFIX "pthread_join"); exit(PTS_UNRESOLVED); } rc = pthread_attr_destroy(&attr); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_destroy"); exit(PTS_UNRESOLVED); } printf("Test PASSED\n"); return PTS_PASS; }
int start_timer_source( void ) { pthread_attr_t pattr; struct sched_param param; pthread_attr_init( &pattr ); param.sched_priority = 10; pthread_attr_setschedparam( &pattr, ¶m ); pthread_attr_setinheritsched( &pattr, PTHREAD_EXPLICIT_SCHED ); return pthread_create( NULL, &pattr, timer_thread, NULL ); }
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; }
thread_type *thread_create_c(char *name, void *(*start_routine)(void *), void *arg, int detached, int line, char *file) { int ok = 1; thread_type *thread = NULL; thread_start_t *start = NULL; pthread_attr_t attr; thread = (thread_type *)acalloc(1, sizeof(thread_type)); do { start = (thread_start_t *)acalloc(1, sizeof(thread_start_t)); if (pthread_attr_init (&attr) < 0) break; thread->line = line; thread->file = strdup(file); _mutex_lock (&_threadtree_mutex); thread->thread_id = _next_thread_id++; _mutex_unlock (&_threadtree_mutex); thread->name = strdup(name); thread->create_time = time(NULL); start->start_routine = start_routine; start->arg = arg; start->thread = thread; pthread_attr_setstacksize (&attr, 512*1024); pthread_attr_setinheritsched (&attr, PTHREAD_INHERIT_SCHED); if (detached) { pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); thread->detached = 1; } if (pthread_create (&thread->sys_thread, &attr, _start_routine, start) == 0) { pthread_attr_destroy (&attr); return thread; } else pthread_attr_destroy (&attr); } while (0); #ifdef THREAD_DEBUG LOG_ERROR("Could not create new thread %s", name); #endif if (start) free (start); if (thread) free (thread); return NULL; }
int main(int argc, char* argv[]) { //pthread_t threads[4]; printf("Start des Beispiels \n"); //printf("Argumente verfuegbar: ARGC\n", 3*argc); p_rb -> p_in = p_start; p_rb -> p_out = p_start; p_rb -> count = 0; printf("Counter value %d\n", p_rb ->count); pthread_attr_t my_thread_attr; // Thread Attribut struct sched_param my_prio; pthread_attr_init(&my_thread_attr); pthread_attr_setinheritsched(&my_thread_attr, PTHREAD_EXPLICIT_SCHED); // Freigabe der Parameteränd. pthread_attr_setschedpolicy(&my_thread_attr, SCHED_FIFO); my_prio.sched_priority = 10; // Priority ändern pthread_attr_setschedparam(&my_thread_attr, &my_prio); // Threads erstellen pthread_create(&threads[2], NULL, control, (void *)&thread_id[2]); pthread_create(&threads[0], NULL, p_1_w, (void *)thread_id); pthread_create(&threads[1], NULL, p_2_w, (void *)&thread_id[1]); pthread_create(&threads[3], NULL, consumer, (void *)&thread_id[3]); // Controller join und status abfangen pthread_join(threads[2], &status); // Wenn erfolgreich beendet if (status == 0) { printf("Control join erfolgreich!\n"); pthread_cond_signal(&p1_unlock); // signal für das Freigeben, wenn p1 gestoppt ist. pthread_cond_signal(&p2_unlock); // signal für das Freigeben, wenn p2 gestoppt ist. pthread_cond_signal(&c_unlock); // signal für das Freigeben, wenn c gestoppt ist. pthread_cancel(threads[0]); // beenden pthread_cancel(threads[1]); // beenden pthread_cancel(threads[3]); // beenden } //for(i = 0; i<4; i++) { result[0] = pthread_join(threads[0], NULL); // &status result[1] = pthread_join(threads[1], NULL); // &status result[3] = pthread_join(threads[3], NULL); // &status //printf("Exit status: %d\n", *(int *)status); // Speicherzugriffsfehler //result[i] = *(int *)status; //} // vosichthalber vor dem Join ein Signal für den jeweiligen Thread mit condtion schicken. printf("Ende nach Join der Threads\n"); //printf("Control Thread returns: %d\n",result[2]); printf("Producer_1 Thread returns: %d\n",result[0]); printf("Producer_2 Thread returns: %d\n",result[1]); printf("Consumer Thread returns: %d\n",result[3]); return 0; }
static int native_thread_create(rb_thread_t *th) { int err = 0; if (use_cached_thread(th)) { thread_debug("create (use cached thread): %p\n", (void *)th); } else { pthread_attr_t attr; size_t stack_size = 512 * 1024; /* 512KB */ size_t space; #ifdef PTHREAD_STACK_MIN if (stack_size < PTHREAD_STACK_MIN) { stack_size = PTHREAD_STACK_MIN * 2; } #endif space = stack_size/5; if (space > 1024*1024) space = 1024*1024; th->machine_stack_maxsize = stack_size - space; #ifdef __ia64 th->machine_stack_maxsize /= 2; th->machine_register_stack_maxsize = th->machine_stack_maxsize; #endif CHECK_ERR(pthread_attr_init(&attr)); #ifdef PTHREAD_STACK_MIN thread_debug("create - stack size: %lu\n", (unsigned long)stack_size); CHECK_ERR(pthread_attr_setstacksize(&attr, stack_size)); #endif #ifdef HAVE_PTHREAD_ATTR_SETINHERITSCHED CHECK_ERR(pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED)); #endif CHECK_ERR(pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED)); err = pthread_create(&th->thread_id, &attr, thread_start_func_1, th); thread_debug("create: %p (%d)", (void *)th, err); CHECK_ERR(pthread_attr_destroy(&attr)); if (!err) { pthread_cond_init(&th->native_thread_data.sleep_cond, 0); } else { st_delete_wrap(th->vm->living_threads, th->self); th->status = THREAD_KILLED; rb_raise(rb_eThreadError, "can't create Thread (%d)", err); } } return err; }
static int run_iddp(struct smokey_test *t, int argc, char *const argv[]) { struct sched_param svparam = {.sched_priority = 71 }; struct sched_param clparam = {.sched_priority = 70 }; pthread_attr_t svattr, clattr; int s; s = socket(AF_RTIPC, SOCK_DGRAM, IPCPROTO_IDDP); if (s < 0) { if (errno == EAFNOSUPPORT) return -ENOSYS; } else close(s); pthread_attr_init(&svattr); pthread_attr_setdetachstate(&svattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(&svattr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&svattr, SCHED_FIFO); pthread_attr_setschedparam(&svattr, &svparam); errno = pthread_create(&svtid, &svattr, &server, NULL); if (errno) fail("pthread_create"); pthread_attr_init(&clattr); pthread_attr_setdetachstate(&clattr, PTHREAD_CREATE_JOINABLE); pthread_attr_setinheritsched(&clattr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&clattr, SCHED_FIFO); pthread_attr_setschedparam(&clattr, &clparam); errno = pthread_create(&cltid, &clattr, &client, NULL); if (errno) fail("pthread_create"); pthread_join(cltid, NULL); pthread_cancel(svtid); pthread_join(svtid, NULL); return 0; }
//-------------------------------------------------------------------------------------------------- static ThreadObj_t* CreateThread ( const char* name, ///< [in] Name of the thread. le_thread_MainFunc_t mainFunc, ///< [in] The thread's main function. void* context ///< [in] Value to pass to mainFunc when it is called. ) { // Create a new thread object. ThreadObj_t* threadPtr = le_mem_ForceAlloc(ThreadPool); // Copy the name. We will make the names unique by adding the thread ID later so we allow any // string as the name. LE_WARN_IF(le_utf8_Copy(threadPtr->name, name, sizeof(threadPtr->name), NULL) == LE_OVERFLOW, "Thread name '%s' has been truncated to '%s'.", name, threadPtr->name); // Initialize the pthreads attribute structure. LE_ASSERT(pthread_attr_init(&(threadPtr->attr)) == 0); // Make sure when we create the thread it takes it attributes from the attribute object, // as opposed to inheriting them from its parent thread. if (pthread_attr_setinheritsched(&(threadPtr->attr), PTHREAD_EXPLICIT_SCHED) != 0) { LE_CRIT("Could not set scheduling policy inheritance for thread '%s'.", name); } // By default, Legato threads are not joinable (they are detached). if (pthread_attr_setdetachstate(&(threadPtr->attr), PTHREAD_CREATE_DETACHED) != 0) { LE_CRIT("Could not set the detached state for thread '%s'.", name); } threadPtr->isJoinable = false; threadPtr->isStarted = false; threadPtr->mainFunc = mainFunc; threadPtr->context = context; threadPtr->destructorList = LE_SLS_LIST_INIT; threadPtr->threadHandle = 0; memset(&threadPtr->mutexRec, 0, sizeof(threadPtr->mutexRec)); memset(&threadPtr->semaphoreRec, 0, sizeof(threadPtr->semaphoreRec)); memset(&threadPtr->eventRec, 0, sizeof(threadPtr->eventRec)); memset(&threadPtr->timerRec, 0, sizeof(threadPtr->timerRec)); // Create a safe reference for this object. Lock(); threadPtr->safeRef = le_ref_CreateRef(ThreadRefMap, threadPtr); Unlock(); return threadPtr; }
int main(void) { pthread_t tid; int ret; pthread_attr_t attr; int policy, inher; struct sched_param param; policy = SCHED_FIFO; param.sched_priority = 99; pthread_attr_init(&attr); pthread_attr_getinheritsched(&attr, &inher); if (inher == PTHREAD_INHERIT_SCHED) printf("Can't change sched policy!\n"); else if (inher == PTHREAD_EXPLICIT_SCHED) printf("Can change sched policy!\n"); #if 1 pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); ret = pthread_attr_setschedpolicy(&attr, policy); if (ret) { printf("set policy:%s\n", strerror(ret)); exit(1); } ret = pthread_attr_setschedparam(&attr, ¶m); if (ret) { printf("set policy:%s\n", strerror(ret)); exit(1); } #endif ret = pthread_create(&tid, &attr, thread_handler, NULL); if (ret) { printf("pthread_create:%s\n", strerror(ret)); exit(1); } struct timeval priv, next; gettimeofday(&priv, NULL); do_prime(300000007); gettimeofday(&next, NULL); testTime(&priv, &next); pthread_exit(NULL); }
int main(int argc, char *argv[]) { struct hostent *hp; int flags; struct timeval tv; struct stun_state st; pthread_attr_t attr; pthread_t thread; gettimeofday(&tv, 0); srandom(tv.tv_sec + tv.tv_usec); hp=gethostbyname(argv[1]); memcpy(&stunserver.sin_addr, hp->h_addr, sizeof(stunserver.sin_addr)); stunserver.sin_port = htons(3478); st.sock=socket(PF_INET,SOCK_DGRAM,0); flags = fcntl(st.sock, F_GETFL); fcntl(st.sock, F_SETFL, flags | O_NONBLOCK); st.bindaddr.sin_family=AF_INET; st.bindaddr.sin_addr.s_addr=inet_addr(argv[2]); st.bindaddr.sin_port=htons((random() % (65535-1023))+1023); bind(st.sock,(struct sockaddr *)&st.bindaddr,sizeof(struct sockaddr_in)); pthread_attr_init(&attr); pthread_attr_setschedpolicy(&attr, SCHED_RR); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); pthread_create(&thread, &attr, data_thread, &st); stun_send(STUN_BINDREQ,&st,NULL,0,0); while(1) { usleep(20000); gettimeofday(&tv,0); if ((tv.tv_sec*1000000+tv.tv_usec)-(st.laststun.tv_sec*1000000+st.laststun.tv_usec) > atoi(argv[3])*1000) { if ((st.result & STUN_NAT_SYMN) && (st.pcnt == 1)) { stun_send(STUN_BINDREQ,&st,NULL,0,2); } else { if (st.result < STUN_NAT_OPEN) printf("NEW IP:%s:%i Result: %i\n",inet_ntoa(st.bindaddr.sin_addr),ntohs(st.bindaddr.sin_port),st.result); else printf("NEW IP:%s:%i Result: %i\n",inet_ntoa(st.maddr.sin_addr),ntohs(st.maddr.sin_port),st.result); break; } } } pthread_attr_destroy(&attr); shutdown(st.sock,SHUT_RDWR); exit(0); }
void init_priority(pthread_attr_t * attr, int priority) { struct sched_param sched = {0}; // 1-99, 99 is max, above 49 could starve sockets? // according to SOEM sample code sched.sched_priority = priority; // need EXPLICIT_SCHED or the default is // INHERIT_SCHED from parent process assert(pthread_attr_init(attr) == 0); assert(pthread_attr_setinheritsched(attr, PTHREAD_EXPLICIT_SCHED) == 0); assert(pthread_attr_setschedpolicy(attr, SCHED_FIFO) == 0); assert(pthread_attr_setschedparam(attr, &sched) == 0); }
int test( void ) { pthread_attr_t attr; int inheritsched; int result; inheritsched = PTHREAD_INHERIT_SCHED; inheritsched = PTHREAD_EXPLICIT_SCHED; result = pthread_attr_setinheritsched( &attr, inheritsched ); return result; }
int start_syspoll_source( void ) { pthread_attr_t pattr; struct sched_param param; pthread_attr_init( &pattr ); param.sched_priority = 11; pthread_attr_setschedparam( &pattr, ¶m ); pthread_attr_setinheritsched( &pattr, PTHREAD_EXPLICIT_SCHED ); pthread_attr_setstacksize( &pattr, 16*1024 ); return pthread_create( NULL, NULL, syspoll_thread, NULL ); }
int sc_tecreate(void (*entry) (void *), int tid, int prio, int mode, u_long ustacksz, u_long sstacksz __attribute__ ((unused)), char *paddr, u_long psize, int *errp) { struct vrtx_task_iargs iargs; struct sched_param param; pthread_attr_t thattr; int err, policy; pthread_t thid; /* Migrate this thread to the Linux domain since we are about to issue a series of regular kernel syscalls in order to create the new Linux thread, which in turn will be mapped to a VRTX shadow. */ XENOMAI_SYSCALL1(__xn_sys_migrate, XENOMAI_LINUX_DOMAIN); iargs.tid = tid; iargs.prio = prio; iargs.mode = mode; iargs.entry = entry; iargs.param = paddr; __real_sem_init(&iargs.sync, 0, 0); pthread_attr_init(&thattr); ustacksz = xeno_stacksize(ustacksz); pthread_attr_setinheritsched(&thattr, PTHREAD_EXPLICIT_SCHED); policy = vrtx_task_set_posix_priority(prio, ¶m); pthread_attr_setschedparam(&thattr, ¶m); pthread_attr_setschedpolicy(&thattr, policy); pthread_attr_setstacksize(&thattr, ustacksz); pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_DETACHED); err = __real_pthread_create(&thid, &thattr, &vrtx_task_trampoline, &iargs); if (err) { *errp = err; __real_sem_destroy(&iargs.sync); return -1; } while (__real_sem_wait(&iargs.sync) && errno == EINTR) ; __real_sem_destroy(&iargs.sync); return iargs.tid; }
int initRTThreadAttr(pthread_attr_t *p_attributes, unsigned int p_stackSize, int p_priority) { int ret; ret = pthread_attr_init(p_attributes); if (ret != 0) { PRINT_ERR("Failed to init pthread attributes (%d).\n", ret); return ret; } ret = pthread_attr_setinheritsched(p_attributes, PTHREAD_EXPLICIT_SCHED); if(ret != 0) { PRINT_ERR("Failed to set inheritsched of pthread attribute (%d).\n", ret); return ret; } ret = pthread_attr_setstacksize(p_attributes, p_stackSize); if(ret != 0) { PRINT_ERR("Failed to set stacksize of pthread attribute (%d).\n", ret); return ret; } ret = pthread_attr_setdetachstate(p_attributes, PTHREAD_CREATE_JOINABLE); if(ret != 0) { PRINT_ERR("Failed to set detachstate of pthread attribute (%d).\n", ret); return ret; } ret = pthread_attr_setscope(p_attributes, PTHREAD_SCOPE_SYSTEM); if(ret != 0) { PRINT_ERR("Failed to set scope of pthread attribute (%d).\n", ret); return ret; } ret = pthread_attr_setschedpolicy(p_attributes, SCHEDULING_POLICY); if(ret != 0) { PRINT_ERR("Failed to set scheduling policy of pthread attribute (%d).\n", ret); return ret; } struct sched_param schedParam; // only priority is used by pthreads schedParam.sched_priority = p_priority; ret = pthread_attr_setschedparam(p_attributes, &schedParam); if(ret != 0) { PRINT_ERR("Failed to set scheduling priority of pthread attribute (%d).\n", ret); return ret; } return 0; }
void main(void) { int rc, scope; printf("Before adjustments to scheduling policy:\n"); print_scheduler(); #ifdef RUN_RT_THREAD pthread_attr_init(&main_sched_attr); pthread_attr_setinheritsched(&main_sched_attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&main_sched_attr, SCHED_FIFO); rt_max_prio = sched_get_priority_max(SCHED_FIFO); rt_min_prio = sched_get_priority_min(SCHED_FIFO); main_param.sched_priority = rt_max_prio; rc=sched_setscheduler(getpid(), SCHED_FIFO, &main_param); if (rc) { printf("ERROR; sched_setscheduler rc is %d\n", rc); perror("sched_setschduler"); exit(-1); } printf("After adjustments to scheduling policy:\n"); print_scheduler(); main_param.sched_priority = rt_max_prio; pthread_attr_setschedparam(&main_sched_attr, &main_param); rc = pthread_create(&main_thread, &main_sched_attr, delay_test, (void *)0); if (rc) { printf("ERROR; pthread_create() rc is %d\n", rc); perror("pthread_create"); exit(-1); } pthread_join(main_thread, NULL); if(pthread_attr_destroy(&main_sched_attr) != 0) perror("attr destroy"); #else delay_test((void *)0); #endif printf("TEST COMPLETE\n"); }
int pth_attri_set (pth_attri_t *attri, pth_attr_types_t t, void *data) { int *di = (int *)data; size_t *ds = (size_t *)data; if (attri != (pth_attri_t *)NULL) { switch (t) { /* pthread_attr_setdetachstate */ case PTH_ATTR_DETACHED: attri->at |= t; return pthread_attr_setdetachstate (&(attri->attr), PTHREAD_CREATE_JOINABLE); /* pthread_attr_setdetachstate */ case PTH_ATTR_JOINABLE: attri->at |= t; return pthread_attr_setdetachstate (&(attri->attr), PTHREAD_CREATE_DETACHED); #ifndef LINUX /* pthread_attr_setstacksize */ case PTH_ATTR_STACKSZ: attri->at |= t; return pthread_attr_setstacksize (&(attri->attr), *ds); /* pthread_attr_setguardsize */ case PTH_ATTR_GUARDSZ: attri->at |= t; return pthread_attr_setguardsize (&(attri->attr), *ds); #endif /* !LINUX */ /* pthread_attr_setschedparam */ case PTH_ATTR_SCHEDPARAM: attri->at |= t; return pthread_attr_setschedparam (&(attri->attr), (struct sched_param *)data); /* pthread_attr_setinheritsched */ case PTH_ATTR_INSCHEDPARAM: attri->at |= t; return pthread_attr_setinheritsched (&(attri->attr), *di); /* pthread_attr_setschedpolicy */ case PTH_ATTR_SCHEDPOLICY: attri->at |= t; return pthread_attr_setschedpolicy (&(attri->attr), *di); /* pthread_attr_setscope */ case PTH_ATTR_SCOPE: attri->at |= t; return pthread_attr_setscope (&(attri->attr), *di); default: return CAF_ERROR_SUB; } } return CAF_ERROR_SUB; }
osd_thread *osd_thread_create(osd_thread_callback callback, void *cbparam) { osd_thread *thread; pthread_attr_t attr; thread = (osd_thread *)calloc(1, sizeof(osd_thread)); pthread_attr_init(&attr); pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); if ( pthread_create(&thread->thread, &attr, callback, cbparam) != 0 ) { free(thread); return NULL; } return thread; }
static gpointer default_push (GstTaskPool * pool, GstTaskPoolFunction func, gpointer data, GError ** error) { TestRTId *tid; gint res; pthread_attr_t attr; struct sched_param param; //g_message ("pushing Realtime pool %p, %p", pool, func); tid = g_slice_new0 (TestRTId); //g_message ("set policy"); pthread_attr_init (&attr); if (strstr(policyName, "FIFO")){ if ((res = pthread_attr_setschedpolicy (&attr, SCHED_RR)) != 0) g_warning ("setschedpolicy: failure: %p", g_strerror (res)); } else { if ((res = pthread_attr_setschedpolicy (&attr, SCHED_FIFO)) != 0) g_warning ("setschedpolicy: failure: %p", g_strerror (res)); } //g_message ("set prio"); param.sched_priority = spectrum3d.priority; if ((res = pthread_attr_setschedparam (&attr, ¶m)) != 0) g_warning ("setschedparam: failure: %p", g_strerror (res)); //g_message ("set inherit"); if ((res = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED)) != 0) g_warning ("setinheritsched: failure: %p", g_strerror (res)); //g_message ("create thread"); res = pthread_create (&tid->thread, &attr, (void *(*)(void *)) func, data); if (res == 0) { printf("REALTIME mode (%s, p = %d)\n", policyName, spectrum3d.priority); } else if (res != 0) { g_set_error (error, G_THREAD_ERROR, G_THREAD_ERROR_AGAIN, "Error creating thread: %s", g_strerror (res)); g_slice_free (TestRTId, tid); tid = NULL; } return tid; }
int PRUloader::setup_pruss(void) { /* Allocate and initialize memory */ if (prussdrv_init()){ perror("prussdrv_init failed"); return -1; } if (prussdrv_open(PRU_EVTOUT_0)){ perror("prussdrv_open evt0 failed"); return -1; } if (prussdrv_open(PRU_EVTOUT_1)){ printf("prussdrv_open evt1 failed\n"); return -1; } if (prussdrv_pruintc_init(&pruss_intc_initdata)){ perror("prussdrv_pruintc_init failed"); return -1; } /* Start IRQ thread for event handling */ pthread_attr_t pthread_attr; struct sched_param sched_param; pthread_attr_init(&pthread_attr); int evt_priority = sched_get_priority_max(SCHED_FIFO) - 2; pthread_attr_setinheritsched(&pthread_attr, PTHREAD_EXPLICIT_SCHED); pthread_attr_setschedpolicy(&pthread_attr, SCHED_FIFO); sched_param.sched_priority = evt_priority; pthread_attr_setschedparam(&pthread_attr, &sched_param); quit_rx_thread = false; if(pthread_create(&irq_thread_evt0, &pthread_attr, &pruevtout0_thread, this) != 0){ printf("ERR >> Event thread not created\r\n"); return -1; } pthread_attr_destroy(&pthread_attr); // map pruss memory prussdrv_map_extmem(&ddrMemOrigin); // get max size ddr_mem_size = prussdrv_extmem_size(); printf("PRU_DEBUG >> DDR size is 0x%0x bytes\r\nPRU_DEBUG >> DDR virtual address is 0x%0x\r\n", (uint32_t)ddr_mem_size, (uint32_t)ddrMemOrigin); // Get physical address phy_add = prussdrv_get_phys_addr(ddrMemOrigin); printf("PRU_DEBUG >> Start of physical address allocated is 0x%X\r\n", phy_add); memset((char *)ddrMemOrigin, 0, ddr_mem_size); return 1; }
int main() { pthread_attr_t attr; pthread_t thread_id; struct sched_param param; int rc = 0; param.sched_priority = PRIORITY; rc = pthread_setschedparam(pthread_self(), POLICY, ¶m); if (rc != 0) { printf(ERROR_PREFIX "pthread_setschedparam\n"); exit(PTS_UNRESOLVED); } rc = pthread_attr_init(&attr); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_init\n"); exit(PTS_UNRESOLVED); } rc = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED); if (rc != 0) { printf(ERROR_PREFIX "pthread_attr_setinheritsched\n"); printf("Test FAILED\n"); exit(PTS_FAIL); } rc = pthread_create(&thread_id, &attr, thread, NULL); if (rc != 0) { printf(ERROR_PREFIX "pthread_create\n"); exit(PTS_UNRESOLVED); } rc = pthread_join(thread_id, NULL); if (rc != 0) { printf(ERROR_PREFIX "pthread_join\n"); exit(PTS_UNRESOLVED); } if ((priority_correct != 1) || (policy_correct != 1)) { printf("Test FAIL\n"); exit(PTS_FAIL); } printf("Test PASSED\n"); exit(PTS_PASS); }
static void start_fb_threads(QSP_ARG_DECL int n_frame_buffers,int* fd_arr) { int i; pthread_attr_init(&attr1); /* initialize to default values */ pthread_attr_setinheritsched(&attr1,PTHREAD_INHERIT_SCHED); for(i=0;i<n_frame_buffers;i++){ ppi[i].ppi_index=i; ppi[i].ppi_flags = 0; ppi[i].ppi_fd = fd_arr[i]; sprintf(ERROR_STRING,"calling pthread_create for thread %d",i); advise(ERROR_STRING); pthread_create(&fb_thr[i],&attr1,fb_pair_daemon,&ppi[i]); } }
/************************************************* * Function: Pthread_attr_setinheritsched() * Description: 设置线程继承属性包裹函数 * Input: *attr---线程属性结构 * inheritsched--线程继承关系 PTHREAD_INHERIT_SCHED:表示新线程将继承创建线程的调度策略和优先级 PTHREAD_EXPLICIT_SCHED:表示使用在schedpolicy和schedparam属性中显式设置的调度策略和优先级 !!!要使用在schedpolicy和schedparam属性中显式设置的调度信息,必须在设置之前将inheritsched 属性设置为PTHREAD_EXPLICIT_SCHED. * Output: *attr---线程属性结构 * Return: 0/errno *************************************************/ int Pthread_attr_setinheritsched(pthread_attr_t *attr, int inheritsched) { int rval; if(attr==AII_NULL) { return -1; } if((rval = pthread_attr_setinheritsched(attr, inheritsched)) != 0) { debug_info(DEBUG_LEVEL_4,"Pthread_attr_setinheritsched() failed!\n"); } return rval; }
static inline void _start_data_thread(QSP_ARG_DECL Polh_Read_Info *prip) { /* create data-processing threads */ pthread_attr_init(&thread_attr); /* Why do we set it to inherit the parent thread scheduling? */ pthread_attr_setinheritsched(&thread_attr, PTHREAD_INHERIT_SCHED); if( pthread_create(&polh_thread, &thread_attr, transfer_polh_data, prip) != 0 ){ perror("pthread_create"); warn("error creating polhemus transfer thread"); return; } thread_running=1; }
GL_Status_t GL_PthreadAttrSetinheritsched(GL_PthreadAttr_t pAttr, GL_UINT32 inherit) { gsl_pthread_attr *hnd = (gsl_pthread_attr *)pAttr; if (!hnd) { return GL_FAILURE; } if (inherit == GL_PTHREAD_INHERIT_SCHED || inherit == GL_PTHREAD_EXPLICIT_SCHED) { /* modify@machao */ //hnd->thread_attr.inheritsched = inherit; pthread_attr_setinheritsched(&(hnd->thread_attr), inherit); } return GL_SUCCESS; }
//static int g_create_i = 0; int OSA_thrCreate(OSA_ThrHndl *hndl, OSA_ThrEntryFunc entryFunc, Uint32 pri, Uint32 stackSize, void *prm) { int status=OSA_SOK; pthread_attr_t thread_attr; struct sched_param schedprm; // initialize thread attributes structure status = pthread_attr_init(&thread_attr); if(status!=OSA_SOK) { OSA_ERROR("OSA_thrCreate() - Could not initialize thread attributes\n"); return status; } if(stackSize!=OSA_THR_STACK_SIZE_DEFAULT) pthread_attr_setstacksize(&thread_attr, stackSize); status |= pthread_attr_setinheritsched(&thread_attr, PTHREAD_EXPLICIT_SCHED); status |= pthread_attr_setschedpolicy(&thread_attr, SCHED_FIFO); if(pri>OSA_THR_PRI_MAX) pri=OSA_THR_PRI_MAX; else if(pri<OSA_THR_PRI_MIN) pri=OSA_THR_PRI_MIN; schedprm.sched_priority = pri; status |= pthread_attr_setschedparam(&thread_attr, &schedprm); if(status!=OSA_SOK) { OSA_ERROR("OSA_thrCreate() - Could not initialize thread attributes\n"); goto error_exit; } status = pthread_create(&hndl->hndl, &thread_attr, entryFunc, prm); if(status!=OSA_SOK) { OSA_ERROR("OSA_thrCreate() - Could not create thread [%d]\n", status); } // printf("OSA_thrcreate %d thr ,and hndl = %d==func=%p\n",g_create_i++,hndl->hndl,entryFunc); error_exit: pthread_attr_destroy(&thread_attr); return status; }
static int insert_entry(int sev, char *buf, int bufsz) { struct log_entry *lent; pthread_attr_t attrs; lent = malloc(sizeof(*lent)); if (!lent) return -1; lent->sev = sev; lent->message = buf; lent->bufsz = bufsz; pthread_mutex_lock(&log_mutex); if (log_size >= MAX_QUEUE_LENGTH) { free(lent->message); free(lent); ++dropped; lent = (struct log_entry *)(le(_log_entries)->le_prev); lent->sev = LOG_WARNING; snprintf(lent->message, lent->bufsz, "%d message(s) lost due to syslog load\n", dropped + 1); /* Dropped +1 because we overwrote a message to * give the 'dropped' message */ } else { ++log_size; dropped = 0; list_insert(&_log_entries, lent); } if (!thread_id) { pthread_attr_init(&attrs); pthread_attr_setinheritsched(&attrs, PTHREAD_INHERIT_SCHED); if (pthread_create(&thread_id, &attrs, _log_thread, NULL) < 0) thread_id = 0; pthread_mutex_unlock(&log_mutex); } else { pthread_mutex_unlock(&log_mutex); pthread_cond_signal(&log_cond); } return 0; }