1 /* 2 * Copyright (c) 2004, Intel Corporation. All rights reserved. 3 * Created by: crystal.xiong REMOVE-THIS AT intel DOT com 4 * This file is licensed under the GPL license. For the full content 5 * of this license, see the COPYING file at the top level of this 6 * source tree. 7 8 * Test pthread_attr_setstack() 9 * 10 * Steps: 11 * 1. Initialize pthread_attr_t object (attr) 12 * 2. set the stackaddr and stacksize to attr 13 * 3. create a thread with the attr 14 */ 15 16 #include <pthread.h> 17 #include <limits.h> 18 #include <stdio.h> 19 #include <string.h> 20 #include <stdlib.h> 21 #include <sys/param.h> 22 #include <errno.h> 23 #include <unistd.h> 24 #include "posixtest.h" 25 26 #define TEST "1-1" 27 #define FUNCTION "pthread_attr_setstack" 28 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": " 29 30 #define STACKADDROFFSET 0x8000000 31 32 static void *stack_addr; 33 size_t stack_size; 34 35 void *thread_func() 36 { 37 pthread_exit(0); 38 return NULL; 39 } 40 41 int main(void) 42 { 43 pthread_t new_th; 44 pthread_attr_t attr; 45 size_t ssize; 46 void *saddr; 47 int rc; 48 49 /* Initialize attr */ 50 rc = pthread_attr_init(&attr); 51 if (rc != 0) { 52 perror(ERROR_PREFIX "pthread_attr_init"); 53 exit(PTS_UNRESOLVED); 54 } 55 56 /* Get the default stack_addr and stack_size value */ 57 rc = pthread_attr_getstack(&attr, &stack_addr, &stack_size); 58 if (rc != 0) { 59 perror(ERROR_PREFIX "pthread_attr_getstack"); 60 exit(PTS_UNRESOLVED); 61 } 62 /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ 63 64 stack_size = PTHREAD_STACK_MIN; 65 66 if (posix_memalign(&stack_addr, sysconf(_SC_PAGE_SIZE), 67 stack_size) != 0) { 68 perror(ERROR_PREFIX "out of memory while " 69 "allocating the stack memory"); 70 exit(PTS_UNRESOLVED); 71 } 72 /* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */ 73 74 rc = pthread_attr_setstack(&attr, stack_addr, stack_size); 75 if (rc != 0) { 76 perror(ERROR_PREFIX "pthread_attr_setstack"); 77 exit(PTS_UNRESOLVED); 78 } 79 80 rc = pthread_attr_getstack(&attr, &saddr, &ssize); 81 if (rc != 0) { 82 perror(ERROR_PREFIX "pthread_attr_getstack"); 83 exit(PTS_UNRESOLVED); 84 } 85 /* printf("saddr = %p, ssize = %u\n", saddr, ssize); */ 86 87 rc = pthread_create(&new_th, &attr, thread_func, NULL); 88 if (rc != 0) { 89 perror(ERROR_PREFIX "failed to create a thread"); 90 exit(PTS_FAIL); 91 } 92 93 rc = pthread_join(new_th, NULL); 94 if (rc != 0) { 95 perror(ERROR_PREFIX "pthread_join"); 96 exit(PTS_UNRESOLVED); 97 } 98 99 rc = pthread_attr_destroy(&attr); 100 if (rc != 0) { 101 perror(ERROR_PREFIX "pthread_attr_destroy"); 102 exit(PTS_UNRESOLVED); 103 } 104 105 printf("Test PASSED\n"); 106 return PTS_PASS; 107 } 108