]> git.ipfire.org Git - thirdparty/glibc.git/blame - linuxthreads/Examples/ex17.c
Test for stack alignment.
[thirdparty/glibc.git] / linuxthreads / Examples / ex17.c
CommitLineData
df4cada7
UD
1#include <errno.h>
2#include <stdio.h>
3#include <string.h>
4#include <pthread.h>
5#include <unistd.h>
6#include <limits.h>
7#include <sys/mman.h>
8
ad71126b
UD
9static pthread_mutex_t synch = PTHREAD_MUTEX_INITIALIZER;
10
df4cada7
UD
11static void *
12test_thread (void *v_param)
13{
ad71126b 14 pthread_mutex_lock (&synch);
df4cada7
UD
15 return NULL;
16}
17
18#define STACKSIZE 0x100000
19
20int
21main (void)
22{
23 pthread_t thread;
24 pthread_attr_t attr;
25 int status;
26 void *stack, *stack2;
27 size_t stacksize;
28
29 pthread_attr_init (&attr);
30 stack = mmap (NULL, STACKSIZE,
31 PROT_READ | PROT_WRITE | PROT_EXEC,
32 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
33
93a4b7ca 34 if (stack == MAP_FAILED)
df4cada7
UD
35 {
36 perror ("mmap failed");
37 return 1;
38 }
39
40 status = pthread_attr_setstack (&attr, stack, STACKSIZE);
41 if (status != 0)
42 {
43 printf ("pthread_attr_setstack failed: %s\n", strerror (status));
44 return 1;
45 }
46
47 status = pthread_attr_getstack (&attr, &stack2, &stacksize);
48 if (status != 0)
49 {
50 printf ("pthread_attr_getstack failed: %s\n", strerror (status));
51 return 1;
52 }
53
54 if (stack2 != stack || stacksize != STACKSIZE)
55 {
6d497bbe 56 printf ("first pthread_attr_getstack returned different stack (%p,%zx)\n"
df4cada7
UD
57 "than was set by setstack (%p,%x)\n",
58 stack2, stacksize, stack, STACKSIZE);
59 return 2;
60 }
61
ad71126b
UD
62 status = pthread_mutex_lock (&synch);
63 if (status != 0)
64 {
65 printf ("cannot get lock: %s\n", strerror (status));
66 return 1;
67 }
68
df4cada7
UD
69 status = pthread_create (&thread, &attr, test_thread, NULL);
70 if (status != 0)
71 {
72 printf ("pthread_create failed: %s\n", strerror (status));
73 return 1;
74 }
75
76 status = pthread_getattr_np (thread, &attr);
77 if (status != 0)
78 {
79 printf ("pthread_getattr_np failed: %s\n", strerror (status));
80 return 1;
81 }
82
83 status = pthread_attr_getstack (&attr, &stack2, &stacksize);
84 if (status != 0)
85 {
86 printf ("pthread_attr_getstack failed: %s\n", strerror (status));
87 return 1;
88 }
89
90 if (stack2 != stack || stacksize != STACKSIZE)
91 {
6d497bbe 92 printf ("second pthread_attr_getstack returned different stack (%p,%zx)\n"
df4cada7
UD
93 "than was set by setstack (%p,%x)\n",
94 stack2, stacksize, stack, STACKSIZE);
95 return 3;
96 }
97
ad71126b
UD
98 status = pthread_mutex_unlock (&synch);
99 if (status != 0)
100 {
101 printf ("cannot release lock: %s\n", strerror (status));
102 return 1;
103 }
104
df4cada7
UD
105 /* pthread_detach (thread); */
106 if (pthread_join (thread, NULL) != 0)
107 {
108 printf ("join failed\n");
109 return 1;
110 }
111 return 0;
112}