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
32static void *stack_addr;
33size_t stack_size;
34
35void *thread_func()
36{
37	pthread_exit(0);
38	return NULL;
39}
40int main()
41{
42	pthread_t new_th;
43	pthread_attr_t attr;
44	size_t ssize;
45	void *saddr;
46	int rc;
47
48	/* Initialize attr */
49	rc = pthread_attr_init(&attr);
50	if( rc != 0) {
51		perror(ERROR_PREFIX "pthread_attr_init");
52		exit(PTS_UNRESOLVED);
53	}
54
55	/* Get the default stack_addr and stack_size value */
56	rc = pthread_attr_getstack(&attr, &stack_addr, &stack_size);
57	if( rc != 0) {
58		perror(ERROR_PREFIX "pthread_attr_getstack");
59		exit(PTS_UNRESOLVED);
60	}
61	/* printf("stack_addr = %p, stack_size = %u\n", stack_addr, stack_size); */
62
63	stack_size = PTHREAD_STACK_MIN;
64
65	if (posix_memalign (&stack_addr, sysconf(_SC_PAGE_SIZE),
66            stack_size) != 0)
67    	{
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        {
96                perror(ERROR_PREFIX "pthread_join");
97		exit(PTS_UNRESOLVED);
98        }
99
100	rc = pthread_attr_destroy(&attr);
101	if(rc != 0)
102        {
103                perror(ERROR_PREFIX "pthread_attr_destroy");
104		exit(PTS_UNRESOLVED);
105        }
106
107	printf("Test PASSED\n");
108	return PTS_PASS;
109}
110
111
112