Home | History | Annotate | Download | only in pthread_attr_setstack
      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 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 "6-1"
     26 #define FUNCTION "pthread_attr_setstack"
     27 #define ERROR_PREFIX "unexpected error: " FUNCTION " " TEST ": "
     28 
     29 #define STACKSIZE PTHREAD_STACK_MIN - sysconf(_SC_PAGE_SIZE)
     30 
     31 static void *stack_addr;
     32 size_t stack_size;
     33 
     34 void *thread_func()
     35 {
     36 	pthread_exit(0);
     37 	return NULL;
     38 }
     39 
     40 int main(void)
     41 {
     42 	pthread_attr_t attr;
     43 	int rc;
     44 
     45 	/* Initialize attr */
     46 	rc = pthread_attr_init(&attr);
     47 	if (rc != 0) {
     48 		perror(ERROR_PREFIX "pthread_attr_init");
     49 		exit(PTS_UNRESOLVED);
     50 	}
     51 
     52 	/* Get the default stack_addr and stack_size value */
     53 	rc = pthread_attr_getstack(&attr, &stack_addr, &stack_size);
     54 	if (rc != 0) {
     55 		perror(ERROR_PREFIX "pthread_attr_getstack");
     56 		exit(PTS_UNRESOLVED);
     57 	}
     58 	/* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */
     59 
     60 	stack_size = STACKSIZE;
     61 
     62 	if (posix_memalign(&stack_addr, sysconf(_SC_PAGE_SIZE),
     63 			   stack_size) != 0) {
     64 		perror(ERROR_PREFIX "out of memory while "
     65 		       "allocating the stack memory");
     66 		exit(PTS_UNRESOLVED);
     67 	}
     68 
     69 	/* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */
     70 	rc = pthread_attr_setstack(&attr, stack_addr, stack_size);
     71 	if (rc != EINVAL) {
     72 		perror(ERROR_PREFIX "Got the wrong return value");
     73 		exit(PTS_FAIL);
     74 	}
     75 
     76 	rc = pthread_attr_destroy(&attr);
     77 	if (rc != 0) {
     78 		perror(ERROR_PREFIX "pthread_attr_destroy");
     79 		exit(PTS_UNRESOLVED);
     80 	}
     81 
     82 	printf("Test PASSED\n");
     83 	return PTS_PASS;
     84 }
     85