Home | History | Annotate | Download | only in pthread_attr_setstacksize
      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_setstacksizesize()
      9  *
     10  * Steps:
     11  * 1.  Initialize pthread_attr_t object (attr)
     12  * 2.  set stacksize to attr
     13  * 3.  create a thread with the attr
     14  */
     15 
     16 #include <sys/param.h>
     17 #include <errno.h>
     18 #include <limits.h>
     19 #include <pthread.h>
     20 #include <stdio.h>
     21 #include <string.h>
     22 #include <stdlib.h>
     23 #include <unistd.h>
     24 #include "posixtest.h"
     25 
     26 #define TEST "1-1"
     27 #define FUNCTION "pthread_attr_setstacksize"
     28 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     29 
     30 #define STACKADDROFFSET 0x8000000
     31 
     32 void *thread_func()
     33 {
     34 	pthread_exit(0);
     35 	return NULL;
     36 }
     37 
     38 int main(void)
     39 {
     40 	pthread_t new_th;
     41 	pthread_attr_t attr;
     42 	size_t stack_size = PTHREAD_STACK_MIN;
     43 	size_t ssize;
     44 	void *saddr;
     45 	int rc;
     46 
     47 	/* Initialize attr */
     48 	rc = pthread_attr_init(&attr);
     49 	if (rc != 0) {
     50 		perror(ERROR_PREFIX "pthread_attr_init");
     51 		exit(PTS_UNRESOLVED);
     52 	}
     53 
     54 	/* printf("stack_size = %lu\n", stack_size); */
     55 
     56 	rc = posix_memalign(&saddr, sysconf(_SC_PAGE_SIZE), stack_size);
     57 
     58 	if (rc != 0) {
     59 		printf(ERROR_PREFIX "out of memory while "
     60 		       "allocating the stack memory: %s", strerror(rc));
     61 		exit(PTS_UNRESOLVED);
     62 	}
     63 
     64 	rc = pthread_attr_setstacksize(&attr, stack_size);
     65 	if (rc != 0) {
     66 		printf(ERROR_PREFIX "pthread_attr_setstacksize: %s",
     67 		       strerror(rc));
     68 		exit(PTS_UNRESOLVED);
     69 	}
     70 
     71 	rc = pthread_attr_getstacksize(&attr, &ssize);
     72 	if (rc != 0) {
     73 		printf(ERROR_PREFIX "pthread_attr_getstacksize: %s",
     74 		       strerror(rc));
     75 		exit(PTS_UNRESOLVED);
     76 	}
     77 	/* printf("stack_size = %lu\n", ssize); */
     78 
     79 	rc = pthread_create(&new_th, &attr, thread_func, NULL);
     80 	if (rc != 0) {
     81 		printf(ERROR_PREFIX "failed to create a thread: %s",
     82 		       strerror(rc));
     83 		exit(PTS_FAIL);
     84 	}
     85 
     86 	rc = pthread_join(new_th, NULL);
     87 	if (rc != 0) {
     88 		printf(ERROR_PREFIX "pthread_join: %s", strerror(rc));
     89 		exit(PTS_UNRESOLVED);
     90 	}
     91 
     92 	rc = pthread_attr_destroy(&attr);
     93 	if (rc != 0) {
     94 		printf(ERROR_PREFIX "pthread_attr_destroy: %s", strerror(rc));
     95 		exit(PTS_UNRESOLVED);
     96 	}
     97 
     98 	printf("Test PASSED\n");
     99 	return PTS_PASS;
    100 }
    101