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_setstacksize()
      9  *
     10  * Steps:
     11  * 1.  Initialize pthread_attr_t object (attr)
     12  * 2.  set the stacksize less tha PTHREAD_STACK_MIN
     13  */
     14 
     15 #include <pthread.h>
     16 #include <limits.h>
     17 #include <stdio.h>
     18 #include <string.h>
     19 #include <stdlib.h>
     20 #include <sys/param.h>
     21 #include <errno.h>
     22 #include <unistd.h>
     23 #include "posixtest.h"
     24 
     25 #define TEST "4-1"
     26 #define FUNCTION "pthread_attr_setstacksize"
     27 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     28 
     29 #define STACKSIZE PTHREAD_STACK_MIN - sysconf(_SC_PAGE_SIZE)
     30 
     31 void *thread_func()
     32 {
     33 	pthread_exit(0);
     34 	return NULL;
     35 }
     36 
     37 int main(void)
     38 {
     39 	pthread_attr_t attr;
     40 	void *saddr;
     41 	size_t stack_size;
     42 	int rc;
     43 
     44 	/* Initialize attr */
     45 	rc = pthread_attr_init(&attr);
     46 	if (rc != 0) {
     47 		perror(ERROR_PREFIX "pthread_attr_init");
     48 		exit(PTS_UNRESOLVED);
     49 	}
     50 
     51 	stack_size = STACKSIZE;
     52 
     53 	if (posix_memalign(&saddr, sysconf(_SC_PAGE_SIZE), stack_size) != 0) {
     54 		perror(ERROR_PREFIX "out of memory while "
     55 		       "allocating the stack memory");
     56 		exit(PTS_UNRESOLVED);
     57 	}
     58 
     59 	rc = pthread_attr_setstacksize(&attr, stack_size);
     60 	if (rc != EINVAL) {
     61 		perror(ERROR_PREFIX "Got the wrong return value");
     62 		exit(PTS_FAIL);
     63 	}
     64 
     65 	rc = pthread_attr_destroy(&attr);
     66 	if (rc != 0) {
     67 		perror(ERROR_PREFIX "pthread_attr_destroy");
     68 		exit(PTS_UNRESOLVED);
     69 	}
     70 
     71 	printf("Test PASSED\n");
     72 	return PTS_PASS;
     73 }
     74