int main() { pthread_attr_t attr; int rc; /* Initialize attr */ rc = pthread_attr_init(&attr); if( rc != 0) { perror(ERROR_PREFIX "pthread_attr_init"); exit(PTS_UNRESOLVED); } /* Get the default stack_addr and stack_size value */ rc = pthread_attr_getstack(&attr, &stack_addr, &stack_size); if( rc != 0) { perror(ERROR_PREFIX "pthread_attr_getstack"); exit(PTS_UNRESOLVED); } /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ stack_size = PTHREAD_STACK_MIN; if (posix_memalign (&stack_addr, sysconf(_SC_PAGE_SIZE), stack_size) != 0) { perror (ERROR_PREFIX "out of memory while " "allocating the stack memory"); exit(PTS_UNRESOLVED); } stack_addr = stack_addr + OFFSET; /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ rc = pthread_attr_setstack(&attr, stack_addr, stack_size); if (rc != EINVAL ) { printf("The function didn't fail when stackaddr " "lacks proper alignment\n"); } stack_addr = stack_addr + OFFSET; stack_size = PTHREAD_STACK_MIN + OFFSET; /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ rc = pthread_attr_setstack(&attr, stack_addr, stack_size); if (rc != EINVAL ) { printf("The function didn't fail when (stackaddr + stacksize) " "lacks proper alignment\n"); } rc = pthread_attr_destroy(&attr); if(rc != 0) { perror(ERROR_PREFIX "pthread_attr_destroy"); exit(PTS_UNRESOLVED); } printf("Test PASSED\n"); return PTS_PASS; }
main() { int max = 50, max1 = 100, max2 = 200, i; FILE *fd; pthread_attr_t attr; pthread_t *th1, *th2; void *st1, *st2; size_t sz; int policy; struct timeval tp; pthread_mutex_init(&m, NULL); pthread_attr_init(&attr); st1 = (void *) malloc(MYSTACKSIZE); pthread_attr_setstacksize(&attr, MYSTACKSIZE); pthread_attr_setstack(&attr, st1, MYSTACKSIZE); pthread_attr_getstacksize(&attr, &sz); pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE); pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS); pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED); th1 = (pthread_t *) malloc(sizeof(pthread_t)); // if (pthread_create(th1, &attr, odd, (void *)&max1)) { if (pthread_create(th1, &attr, odd, &max1)) { perror("error creating the first thread"); exit(1); } printf("created the first thread\n"); st2 = (void *)malloc(MYSTACKSIZE); pthread_attr_setstacksize(&attr, MYSTACKSIZE); pthread_attr_setstack(&attr, st2, MYSTACKSIZE); th2 = (pthread_t *) malloc(sizeof(pthread_t)); // if (pthread_create(th2, &attr, even, (void *)&max2)) { if (pthread_create(th2, &attr, even, &max2)) { perror("error creating the second thread"); exit(1); } printf("created the second thread\n"); fd = fopen("whole_num", "w"); pthread_mutex_lock(&m); for (i = 0; i < 10000; i++) printf("main\n"); pthread_mutex_unlock(&m); for (i = 1; i < max; i++) fprintf(fd, "%d\n", i); pthread_join(*th1, 0); pthread_join(*th2, 0); }
void start_background_threads(pthread_attr_t *attra, void **stackspts) { pthread_attr_t attr; int r; r=pthread_attr_init(&attr); assert(r==0); void *sp; #ifdef DEBUG r=posix_memalign(&sp, sysconf(_SC_PAGESIZE), glovars.stack_size); assert(r==0); stackspts[glovars.mysql_threads+2+0]=sp; r=pthread_attr_setstack(&attr, sp, glovars.stack_size); assert(r==0); r=pthread_create(&thread_dbg_logger, &attr, debug_logger , NULL); assert(r==0); #endif if (glovars.mysql_query_cache_enabled==TRUE) { PROXY_TRACE(); fdb_hashes_new(&QC,glovars.mysql_query_cache_partitions, glovars.mysql_query_cache_default_timeout, glovars.mysql_query_cache_size); // pthread_t qct; r=posix_memalign(&sp, sysconf(_SC_PAGESIZE), glovars.stack_size); assert(r==0); stackspts[glovars.mysql_threads+2+1]=sp; r=pthread_attr_setstack(&attr, sp, glovars.stack_size); assert(r==0); r=pthread_create(&thread_qct, &attr, purgeHash_thread, &QC); assert(r==0); } // Added by chan //printf("=> create new qr_hash\n"); qr_hashes_new(&QR_HASH_T); //printf("=> end\n"); r=posix_memalign(&sp, sysconf(_SC_PAGESIZE), glovars.stack_size); assert(r==0); stackspts[glovars.mysql_threads+2+2]=sp; r=pthread_attr_setstack(&attr, sp, glovars.stack_size); assert(r==0); r=pthread_create(&thread_qr, &attr, qr_report_thread, &QR_HASH_T); assert(r==0); // Added by chan end. // pthread_t cppt; r=posix_memalign(&sp, sysconf(_SC_PAGESIZE), glovars.stack_size); assert(r==0); stackspts[glovars.mysql_threads+2+3]=sp; r=pthread_attr_setstack(&attr, sp, glovars.stack_size); assert(r==0); r=pthread_create(&thread_cppt, &attr, mysql_connpool_purge_thread , NULL); assert(r==0); }
int main(int argc, char **argv) { int i; pthread_t cthread; pthread_attr_t stack_attr; if (argc != 2) exit(255); data[0] = atoi(argv[1]); pthread_attr_init(&stack_attr); for (i = 0; i < THREADS; i++) { pthread_mutex_init(mutex + i, NULL); pthread_mutex_lock(mutex + i); #if defined(__MINGW32__) || defined(__MINGW64__) pthread_attr_setstackaddr(&stack_attr, &stacks[i]); pthread_attr_setstacksize(&stack_attr, sizeof(struct stack)); #else pthread_attr_setstack(&stack_attr, &stacks[i], sizeof(struct stack)); #endif pthread_create(&cthread, &stack_attr, thread, (void*)(uintptr_t)i); } pthread_mutex_unlock(mutex + 0); pthread_join(cthread, NULL); }
int main (void) { pthread_t thread; int rv; pthread_attr_t attr; size_t stacksize, guardsize; char *stackaddr; pthread_attr_init(&attr); /* 起始化執行緒屬性物件*/ /* 申請用作執行緒堆疊的空間 */ stackaddr = (void *)thread_stack_alloc(PTHREAD_STACK_MIN); /* 設定執行緒屬性物件中的堆疊位址和大小 */ rv = pthread_attr_setstack(&attr, stackaddr, PTHREAD_STACK_MIN); check_error(rv, "pthread_setstack()"); /* 檢視堆疊屬性值 */ pthread_attr_getstack(&attr, (void *)&stackaddr, &stacksize); pthread_attr_getguardsize(&attr, &guardsize); printf("stack attributes: stackaddr=%d, stacksize=%d, guardsize=%d\n", stackaddr, stacksize, guardsize); /* 建立執行緒 */ rv = pthread_create(&thread, &attr, (void *(*)())Hello, (void *)NULL); check_error(rv, "pthread_create()"); pthread_attr_destroy(&attr); /* 及時銷毀執行緒屬性物件,避免再次使用 */ check_error(rv, "pthread_attr_destroy()"); /* ... 後繼程式程式碼 ... */ pthread_exit(NULL); }
int main() { pthread_t thread; void *arg; size_t stacksize = 1024 * 1024; void *stackaddr = mmap(0, stacksize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); assert(stackaddr != MAP_FAILED); pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstack(&attr, stackaddr, stacksize); while (1) { arg = malloc(10); int res = pthread_create(&thread, &attr, start_routine, arg); if (res != 0) { fprintf(stderr, "error creating thread: %s\n", strerror(res)); return -1; } /* thread will free arg, and pass back to us a different arg */ res = pthread_join(thread, &arg); if (res != 0) { fprintf(stderr, "pthread_join() failed: %s\n", strerror(res)); return -1; } free(arg); } }
void AsyncFuncImpl::start() { struct rlimit rlim; m_node = Util::next_numa_node(); // Allocate the thread-stack pthread_attr_init(&m_attr); if (getrlimit(RLIMIT_STACK, &rlim) != 0 || rlim.rlim_cur == RLIM_INFINITY || rlim.rlim_cur < m_stackSizeMinimum) { rlim.rlim_cur = m_stackSizeMinimum; } // On Success use the allocated memory for the thread's stack if (posix_memalign(&m_threadStack, Util::s_pageSize, rlim.rlim_cur) == 0) { size_t guardsize; if (pthread_attr_getguardsize(&m_attr, &guardsize) == 0 && guardsize) { mprotect(m_threadStack, guardsize, PROT_NONE); } madvise(m_threadStack, rlim.rlim_cur, MADV_DONTNEED); Util::numa_bind_to(m_threadStack, rlim.rlim_cur, m_node); pthread_attr_setstack(&m_attr, m_threadStack, rlim.rlim_cur); } pthread_create(&m_threadId, &m_attr, ThreadFunc, (void*)this); assert(m_threadId); }
/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped * in memory. Beware: all writes to libc globals from this function will * apply to linker-private copies and will not be visible from libc later on. * * Note: this function creates a pthread_internal_t for the initial thread and * stores the pointer in TLS, but does not add it to pthread's thread list. This * has to be done later from libc itself (see __libc_init_common). * * This function also stores a pointer to the kernel argument block in a TLS slot to be * picked up by the libc constructor. */ void __libc_init_tls(KernelArgumentBlock& args) { __libc_auxv = args.auxv; static void* tls[BIONIC_TLS_SLOTS]; static pthread_internal_t main_thread; main_thread.tls = tls; // Tell the kernel to clear our tid field when we exit, so we're like any other pthread. // As a side-effect, this tells us our pid (which is the same as the main thread's tid). main_thread.tid = __set_tid_address(&main_thread.tid); main_thread.set_cached_pid(main_thread.tid); // Work out the extent of the main thread's stack. uintptr_t stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; size_t stack_size = get_main_thread_stack_size(); void* stack_bottom = reinterpret_cast<void*>(stack_top - stack_size); // We don't want to free the main thread's stack even when the main thread exits // because things like environment variables with global scope live on it. pthread_attr_init(&main_thread.attr); pthread_attr_setstack(&main_thread.attr, stack_bottom, stack_size); main_thread.attr.flags = PTHREAD_ATTR_FLAG_USER_ALLOCATED_STACK | PTHREAD_ATTR_FLAG_MAIN_THREAD; __init_thread(&main_thread, false); __init_tls(&main_thread); __set_tls(main_thread.tls); tls[TLS_SLOT_BIONIC_PREINIT] = &args; __init_alternate_signal_stack(&main_thread); }
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 main(int argc, char **argv) { int i; pthread_t cthread; pthread_attr_t stack_attr; if (argc != 2) exit(255); data[0] = atoi(argv[1]); pthread_attr_init(&stack_attr); for (i = 0; i < THREADS; i++) { pthread_mutex_init(mutex + i, NULL); pthread_mutex_lock(mutex + i); pthread_attr_setstack(&stack_attr, &stacks[i], sizeof(struct stack)); pthread_create(&cthread, &stack_attr, thread, (void*)i); } pthread_mutex_unlock(mutex + 0); pthread_join(cthread, NULL); return 0; }
void __libc_init_common(uintptr_t *elfdata) { int argc = *elfdata; char** argv = (char**)(elfdata + 1); char** envp = argv + argc + 1; pthread_attr_t thread_attr; static pthread_internal_t thread; static void* tls_area[BIONIC_TLS_SLOTS]; /* setup pthread runtime and maint thread descriptor */ unsigned stacktop = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; unsigned stacksize = 128 * 1024; unsigned stackbottom = stacktop - stacksize; pthread_attr_init(&thread_attr); pthread_attr_setstack(&thread_attr, (void*)stackbottom, stacksize); _init_thread(&thread, gettid(), &thread_attr, (void*)stackbottom); __init_tls(tls_area, &thread); /* clear errno - requires TLS area */ errno = 0; /* set program name */ __progname = argv[0] ? argv[0] : "<unknown>"; /* setup environment pointer */ environ = envp; /* setup system properties - requires environment */ __system_properties_init(); }
/* static pthread_t thread_fork(int ssize,const char *name,IFUNCP func,...) */ static int thread_fork(int ssize,const char *name,IFUNCP func,...) { pthread_t thread; int tid; ThreadArgs *ta; int err; int ai; void *sadr = 0; size_t ssiz = 0; VARGS(7,func); ta = (ThreadArgs*)malloc(sizeof(ThreadArgs)); ta->a_name = name; ta->a_argv[0] = (char*)func; for( ai = 0; ai < 7; ai++ ) ta->a_argv[1+ai] = va[ai]; if( ssize == 0 ) ssize = STKSIZE; ta->a_saddr = 0; ta->a_ssize = ssize; pthread_attr_init(&ta->a_attr); /* pthread_attr_getstacksize(&ta->a_attr,&ssiz); pthread_attr_getstackaddr(&ta->a_attr,&sadr); */ pthread_attr_setstacksize(&ta->a_attr,ssize); #if 0 if( 1 ){ ta->a_saddr = (char*)calloc(ssize/8,8); pthread_attr_setstack(&ta->a_attr,ta->a_saddr,ta->a_ssize); } #endif err = pthread_create(&thread,&ta->a_attr,(void*(*)(void*))thread1,ta); if( err ) porting_dbg("#### pthread_create() failed: %d",err); else{ /* pthread_attr_getstacksize(&attr,&ssiz); pthread_attr_getstackaddr(&attr,&sadr); fprintf(stderr,"#### thread %X: ssize=%d saddr=%X\n",thread,ssiz,sadr); */ } if( main_tid == 0 ){ main_tid = pthread_self(); main_tid = (pthread_t)toTid((void*)main_tid,1); } tid = toTid((void*)thread,2); if( lTHREADLOG() ){ syslog_ERROR("thread_fork() = %X %llX\n",tid,(FileSize)thread); } return tid; /* return toTid((void*)thread,1); */ /* return thread; */ }
/* Create a thread stack via mmap() if one is not specified in the user attributes. Parameters: attr_out - (output) The final attributes caller should use. user_attr - User provided attributes; defer to these. size - If non-0, force new stack to this size. */ static void setupThreadStack(pthread_attr_t *attr_out, const pthread_attr_t *user_attr, size_t size) { size_t stack_size; void *stack_addr; int userStack = 0; // If the user's attributes have specified a stack size, use that. if (user_attr != NULL) { pthread_attr_getstack(user_attr, &stack_addr, &stack_size); if (stack_size != 0) { userStack = 1; } if (stack_addr != NULL) { // User provided stack, Copy the user's attributes: JASSERT(stack_size != 0); *attr_out = *user_attr; return; } } #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,27) int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK; #else int mmap_flags = MAP_PRIVATE | MAP_ANONYMOUS; #endif size_t mmap_size; if (userStack) { mmap_size = stack_size; } else if (size == 0) { /* It's possible the we got killed in the middle of pthread_create. If that * happens, this assert is likely to trigger on REPLAY, as the correct * stack size is recorded only after the _real_pthread_create returns. JASSERT(!SYNC_IS_REPLAY); */ // Also figure out the default stack size for NPTL threads using the // architecture-specific limits defined in nptl/sysdeps/ARCH/pthreaddef.h struct rlimit rl; JASSERT(0 == getrlimit(RLIMIT_STACK, &rl)); #ifdef __x86_64__ size_t arch_default_stack_size = 32*1024*1024; #else size_t arch_default_stack_size = 2*1024*1024; #endif mmap_size = (rl.rlim_cur == RLIM_INFINITY) ? arch_default_stack_size : rl.rlim_cur; } else { mmap_size = size; } // mmap() wrapper handles forcing it to the same place on replay. void *s = mmap(NULL, mmap_size, PROT_READ | PROT_WRITE, mmap_flags, -1, 0); if (s == MAP_FAILED) { JTRACE ( "Failed to map thread stack." ) ( mmap_size ) ( strerror(errno) ) (global_log.currentEntryIndex()); JASSERT ( false ); } pthread_attr_setstack(attr_out, s, mmap_size); }
int main() { pthread_attr_t attr; void *stack_addr; size_t stack_size; size_t ssize; void *saddr; int rc; /* Initialize attr */ rc = pthread_attr_init(&attr); if (rc != 0) { perror(ERROR_PREFIX "pthread_attr_init"); exit(PTS_UNRESOLVED); } /* Get the default stack_addr and stack_size value */ rc = pthread_attr_getstack(&attr, &stack_addr, &stack_size); if (rc != 0) { perror(ERROR_PREFIX "pthread_attr_getstack"); exit(PTS_UNRESOLVED); } printf("stack_addr = %p, stack_size = %zu\n", stack_addr, stack_size); stack_size = PTHREAD_STACK_MIN; if (posix_memalign (&stack_addr, sysconf(_SC_PAGE_SIZE), stack_size) != 0) { perror (ERROR_PREFIX "out of memory while " "allocating the stack memory"); exit(PTS_UNRESOLVED); } printf("stack_addr = %p, stack_size = %zu\n", stack_addr, stack_size); rc = pthread_attr_setstack(&attr, stack_addr, stack_size); if (rc != 0) { perror(ERROR_PREFIX "pthread_attr_setstack"); exit(PTS_UNRESOLVED); } rc = pthread_attr_getstack(&attr, &saddr, &ssize); if (rc != 0) { perror(ERROR_PREFIX "pthread_attr_getstack"); exit(PTS_UNRESOLVED); } printf("saddr = %p, ssize = %zu\n", saddr, ssize); rc = pthread_attr_destroy(&attr); if (rc != 0) { perror(ERROR_PREFIX "pthread_attr_destroy"); exit(PTS_UNRESOLVED); } printf("Test PASSED\n"); return PTS_PASS; }
int main (int argc, char *argv[]) { pthread_t thr; pthread_attr_t attr; pthread_attr_t *attrp; /* NULL or &attr */ int s; attrp = NULL; /* If a command-line argument was supplied, use it to set the stack-size attribute and set a few other thread attributes, and set attrp pointing to thread attributes object */ if (argc > 1) { int stack_size; void *sp; attrp = &attr; s = pthread_attr_init (&attr); if (s != 0) handle_error_en (s, "pthread_attr_init"); s = pthread_attr_setdetachstate (&attr, PTHREAD_CREATE_DETACHED); if (s != 0) handle_error_en (s, "pthread_attr_setdetachstate"); s = pthread_attr_setinheritsched (&attr, PTHREAD_EXPLICIT_SCHED); if (s != 0) handle_error_en (s, "pthread_attr_setinheritsched"); stack_size = strtoul (argv[1], NULL, 0); s = posix_memalign (&sp, sysconf (_SC_PAGESIZE), stack_size); if (s != 0) handle_error_en (s, "posix_memalign"); printf ("posix_memalign() allocated at %p\n", sp); s = pthread_attr_setstack (&attr, sp, stack_size); if (s != 0) handle_error_en (s, "pthread_attr_setstack"); } s = pthread_create (&thr, attrp, &thread_start, NULL); if (s != 0) handle_error_en (s, "pthread_create"); if (attrp != NULL) { s = pthread_attr_destroy (attrp); if (s != 0) handle_error_en (s, "pthread_attr_destroy"); } pause (); /* Terminates when other thread calls exit() */ }
xhn::thread::thread() : m_is_completed(false) , m_is_finished(false) , m_is_running(false) , m_is_stopped(false) , m_is_errored(false) { pthread_attr_t thread_attr; size_t stack_size; int status; status = pthread_attr_init (&thread_attr); EAssert(!status, "Create attr"); status = pthread_attr_setdetachstate (&thread_attr, PTHREAD_CREATE_DETACHED); EAssert(!status, "Set detach"); #ifdef _POSIX_THREAD_ATTR_STACKSIZE status = pthread_attr_getstacksize (&thread_attr, &stack_size); EAssert (!status, "Get stack size"); #if BIT_WIDTH == 32 printf ("Default stack size is %u; minimum is %u\n", stack_size, PTHREAD_STACK_MIN); #else printf ("Default stack size is %lu; minimum is %u\n", stack_size, PTHREAD_STACK_MIN); #endif vptr base = (void *) malloc(1024 * 1024); status = pthread_attr_setstack(&thread_attr, base, 1024 * 1024); EAssert (!status, "Set stack"); status = pthread_attr_getstacksize (&thread_attr, &stack_size); EAssert (!status, "Get stack size"); #if BIT_WIDTH == 32 printf ("Default stack size is %u; minimum is %u\n", stack_size, PTHREAD_STACK_MIN); #else printf ("Default stack size is %lu; minimum is %u\n", stack_size, PTHREAD_STACK_MIN); #endif m_stack_range.m_begin_addr = base; m_stack_range.m_size = 1024 * 1024; #endif #if BIT_WIDTH == 32 printf("%x\n", (ref_ptr)this); #else printf("%llx\n", (ref_ptr)this); #endif pthread_mutex_init(&m_mutex, NULL); pthread_cond_init(&m_cond, NULL); SpinLock::Instance inst = s_thread_stack_range_map_lock.Lock(); ref_ptr end_addr = (ref_ptr)m_stack_range.m_begin_addr + m_stack_range.m_size; s_thread_stack_range_map.insert(m_stack_range.m_begin_addr, (vptr)end_addr, this); printf("begin create thread\n"); pthread_create(&m_pt, &thread_attr, thread_proc, (void *) this); printf("end create thread\n"); }
static int do_test (void) { mtrace (); void *stack; int res = posix_memalign (&stack, getpagesize (), 4 * PTHREAD_STACK_MIN); if (res) { printf ("malloc failed %s\n", strerror (res)); return 1; } pthread_attr_t attr; pthread_attr_init (&attr); int result = 0; res = pthread_attr_setstack (&attr, stack, 4 * PTHREAD_STACK_MIN); if (res) { printf ("pthread_attr_setstack failed %d\n", res); result = 1; } for (int i = 0; i < 16; ++i) { /* Create the thread. */ pthread_t th; res = pthread_create (&th, &attr, tf, NULL); if (res) { printf ("pthread_create failed %d\n", res); result = 1; } else { res = pthread_join (th, NULL); if (res) { printf ("pthread_join failed %d\n", res); result = 1; } } } pthread_attr_destroy (&attr); if (seen != 16) { printf ("seen %d != 16\n", seen); result = 1; } free (stack); return result; }
void chpl_task_callMain(void (*chpl_main)(void)) { // since we want to run all work in a task with a comm-friendly stack, // run main in a pthread that we will wait for. size_t stack_size; void* stack; pthread_attr_t attr; pthread_t thread; int rc; stack = NULL; chpl_init_heap_stack(); rc = pthread_attr_init(&attr); if( rc != 0 ) { chpl_internal_error("pthread_attr_init main failed"); } stack_size = chpl_thread_getCallStackSize(); if(chpl_alloc_stack_in_heap){ stack = chpl_alloc_pthread_stack(stack_size); if(stack == NULL) chpl_internal_error("chpl_alloc_pthread_stack main failed"); rc = pthread_attr_setstack(&attr, stack, stack_size); if( rc != 0 ) { chpl_internal_error("pthread_attr_setstack main failed"); } } else { rc = pthread_attr_setstacksize(&attr, stack_size); if( rc != 0 ) { chpl_internal_error("pthread_attr_setstacksize main failed"); } } rc = pthread_create(&thread, &attr, do_callMain, chpl_main); if( rc != 0 ) { chpl_internal_error("pthread_create main failed"); } rc = pthread_join(thread, NULL); if( rc != 0 ) { chpl_internal_error("pthread_join main failed"); } if(chpl_alloc_stack_in_heap){ chpl_free_pthread_stack(stack); } pthread_attr_destroy(&attr); }
static void shell_init(void) { struct sched_param sched_param; pthread_attr_t attr; pthread_t tid; pthread_attr_init(&attr); pthread_attr_setstack(&attr, stack, sizeof(stack)); sched_param.sched_priority = sched_get_priority_max(SCHED_RR); pthread_attr_setschedparam(&attr, &sched_param); pthread_create(&tid, &attr, shell, NULL); pthread_setname_np(tid, "shell"); pthread_detach(tid); pthread_attr_destroy(&attr); }
void c_posixThread::start(const char *caller){ // Get new Thread if(0 == threadId){ //If the stackSize is not 0 we have to set it if(stackSize_l){ pthread_attr_t stAttr; if(pthread_attr_init(&stAttr)){ FZRTE_ERROR("Error could not get the default thread attributes! %s\n", strerror(errno)); return; } #ifdef __CYGWIN__ if (pthread_attr_setstacksize (&stAttr, stackSize_l)){ FZRTE_ERROR("Error could not set the stacksize for the thread! %s\n", strerror(errno)); return; } #else //if (pthread_attr_setstacksize(&stAttr, stackSize_l)) { if(pthread_attr_setstack(&stAttr, m_pacStack, stackSize_l)){ FZRTE_ERROR("Error could not set the stacksize for the thread! %s\n", strerror(errno)); return; } #endif if(pthread_create(&threadId, &stAttr, threadFunction, this)){ FZRTE_ERROR("Error could not create the thread! %s\n", strerror(errno)); return; } if(pthread_attr_destroy(&stAttr)){ FZRTE_ERROR("Error could not free the thread attributes! %s\n", strerror(errno)); return; } } else{ if(pthread_create(&threadId, NULL, threadFunction, this)){ FZRTE_ERROR("Error could not create the thread! %s\n", strerror(errno)); return; } else{ FZRTE_INFO("%s : PosixThread : Created New Thread [0x%x]",caller, threadId); } } } //wait till the thread is up and running do{ //pthread_yield(); sleep(1); }while(!isAlive()); }
/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped * in memory. Beware: all writes to libc globals from this function will * apply to linker-private copies and will not be visible from libc later on. * * Note: this function creates a pthread_internal_t for the initial thread and * stores the pointer in TLS, but does not add it to pthread's gThreadList. This * has to be done later from libc itself (see __libc_init_common). * * This function also stores the elf_data argument in a specific TLS slot to be later * picked up by the libc constructor. */ void __libc_init_tls(unsigned** elf_data) { unsigned stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; unsigned stack_size = 128 * 1024; unsigned stack_bottom = stack_top - stack_size; pthread_attr_t thread_attr; pthread_attr_init(&thread_attr); pthread_attr_setstack(&thread_attr, (void*) stack_bottom, stack_size); static pthread_internal_t thread; _init_thread(&thread, gettid(), &thread_attr, (void*) stack_bottom, false); static void* tls_area[BIONIC_TLS_SLOTS]; __init_tls(tls_area, &thread); tls_area[TLS_SLOT_BIONIC_PREINIT] = elf_data; }
int main() { pthread_attr_t attr; int rc; /* Initialize attr */ rc = pthread_attr_init(&attr); if( rc != 0) { perror(ERROR_PREFIX "pthread_attr_init"); exit(PTS_UNRESOLVED); } /* Get the default stack_addr and stack_size value */ rc = pthread_attr_getstack(&attr, &stack_addr, &stack_size); if( rc != 0) { perror(ERROR_PREFIX "pthread_attr_getstack"); exit(PTS_UNRESOLVED); } /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ stack_size = STACKSIZE; if (posix_memalign (&stack_addr, sysconf(_SC_PAGE_SIZE), stack_size) != 0) { perror (ERROR_PREFIX "out of memory while " "allocating the stack memory"); exit(PTS_UNRESOLVED); } /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ rc = pthread_attr_setstack(&attr, stack_addr, stack_size); if (rc != EINVAL ) { perror(ERROR_PREFIX "Got the wrong return value"); exit(PTS_FAIL); } rc = pthread_attr_destroy(&attr); if(rc != 0) { perror(ERROR_PREFIX "pthread_attr_destroy"); exit(PTS_UNRESOLVED); } printf("Test PASSED\n"); return PTS_PASS; }
static inline int setup_thread_stack(stack_t *s,pthread_attr_t *attr){ if(pthread_attr_init(attr)){ return -1; } if((s->ss_sp = get_stack(&s->ss_size)) == NULL){ pthread_attr_destroy(attr); return -1; } if(pthread_attr_setstack(attr,s->ss_sp,s->ss_size)){ dealloc(s->ss_sp,s->ss_size); pthread_attr_destroy(attr); return -1; } s->ss_flags = 0; return 0; }
/* Init TLS for the initial thread. Called by the linker _before_ libc is mapped * in memory. Beware: all writes to libc globals from this function will * apply to linker-private copies and will not be visible from libc later on. * * Note: this function creates a pthread_internal_t for the initial thread and * stores the pointer in TLS, but does not add it to pthread's gThreadList. This * has to be done later from libc itself (see __libc_init_common). * * This function also stores a pointer to the kernel argument block in a TLS slot to be * picked up by the libc constructor. */ void __libc_init_tls(KernelArgumentBlock& args) { __libc_auxv = args.auxv; uintptr_t stack_top = (__get_sp() & ~(PAGE_SIZE - 1)) + PAGE_SIZE; size_t stack_size = get_main_thread_stack_size(); uintptr_t stack_bottom = stack_top - stack_size; static void* tls[BIONIC_TLS_SLOTS]; static pthread_internal_t thread; thread.tid = gettid(); thread.tls = tls; pthread_attr_init(&thread.attr); pthread_attr_setstack(&thread.attr, (void*) stack_bottom, stack_size); _init_thread(&thread, false); __init_tls(&thread); tls[TLS_SLOT_BIONIC_PREINIT] = &args; }
TEST(pthread, pthread_join__race) { // http://b/11693195 --- pthread_join could return before the thread had actually exited. // If the joiner unmapped the thread's stack, that could lead to SIGSEGV in the thread. for (size_t i = 0; i < 1024; ++i) { size_t stack_size = 64*1024; void* stack = mmap(NULL, stack_size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0); pthread_attr_t a; pthread_attr_init(&a); pthread_attr_setstack(&a, stack, stack_size); pthread_t t; ASSERT_EQ(0, pthread_create(&t, &a, IdFn, NULL)); ASSERT_EQ(0, pthread_join(t, NULL)); ASSERT_EQ(0, munmap(stack, stack_size)); } }
void coro_create (coro_context *ctx, coro_func coro, void *arg, void *sptr, size_t ssize) { static coro_context nctx; static int once; if (!once) { once = 1; pthread_mutex_lock (&coro_mutex); pthread_cond_init (&nctx.cv, 0); null_tid = pthread_self (); } pthread_cond_init (&ctx->cv, 0); if (coro) { pthread_attr_t attr; struct coro_init_args args; args.func = coro; args.arg = arg; args.self = ctx; args.main = &nctx; pthread_attr_init (&attr); #if __UCLIBC__ /* exists, but is borked */ /*pthread_attr_setstacksize (&attr, (size_t)ssize);*/ #elif __CYGWIN__ /* POSIX, not here */ pthread_attr_setstacksize (&attr, (size_t)ssize); #else pthread_attr_setstack (&attr, sptr, (size_t)ssize); #endif pthread_attr_setscope (&attr, PTHREAD_SCOPE_PROCESS); pthread_create (&ctx->id, &attr, coro_init, &args); coro_transfer (args.main, args.self); } else ctx->id = null_tid; }
int main(void) { pthread_t tid; pthread_attr_t attr; void *stack_addr; int stack_size; int ret; while (1) { stack_addr = malloc(PAGE_SIZE * 8); // 1. 指针不能位运算,因此转换成unsigned long // 2. & 0x7 检查地址是否对齐: 32位系统,2^12 >> 0x1000 if ((unsigned long)stack_addr & 0x7 != 0) { printf("地址不是对齐的\n"); free(stack_addr); } else break; } printf("stack_addr: %p\n", stack_addr); pthread_attr_init(&attr); pthread_attr_setstack(&attr, stack_addr, PAGE_SIZE * 8); pthread_attr_getstack(&attr, &stack_addr, &stack_size); printf("stack_addr: %p, stack_size: %#x\n", stack_addr, stack_size); pthread_create(&tid, &attr, thr_rtn, NULL); ret = pthread_join(tid, NULL); if (ret != 0) { printf("thread has detached\n"); perror("pthread_join"); } free(stack_addr); pthread_attr_destroy(&attr); return 0; }
void AsyncFuncImpl::start() { struct rlimit rlim; // Allocate the thread-stack pthread_attr_init(&m_attr); if (getrlimit(RLIMIT_STACK, &rlim) != 0 || rlim.rlim_cur == RLIM_INFINITY || rlim.rlim_cur < m_stackSizeMinimum) { rlim.rlim_cur = m_stackSizeMinimum; } // On Success use the allocated memory for the thread's stack if (posix_memalign(&m_threadStack, Util::s_pageSize, rlim.rlim_cur) == 0) { pthread_attr_setstack(&m_attr, m_threadStack, rlim.rlim_cur); } pthread_create(&m_threadId, &m_attr, ThreadFunc, (void*)this); assert(m_threadId); }
int main( int arg, char ** argv) { int k; int i=0; int b; globle = 0; char *p; pthread_t thread; pthread_attr_t attr; size_t stackSize; p = (char *)malloc(4*1024*1024); pthread_attr_init(&attr); pthread_attr_setstack(&attr, p, 10*1024*1024); pthread_create(&thread, &attr, thread_one, &i); sleep(30); // pause(); return 0; }
int main(int argc, char** args) { if (argc >= 2) token = atoi(args[1]); else token = 1000; pthread_attr_t stack_attr; pthread_attr_init(&stack_attr); for (uint i = 0; i < NUM_THREADS; i++) { pthread_mutex_init( &(mutex[i]), 0); pthread_mutex_lock( &(mutex[i]) ); pthread_attr_setstack( &stack_attr, &(stacks[i]), STACK_SIZE ); pthread_create( &(threadid[i]), &stack_attr, &thread_func, reinterpret_cast<void*>(i) ); } pthread_mutex_unlock( &(mutex[0]) ); pthread_join( threadid[0], 0 ); return 1; }