(CFLAGS-tst-align.c): Add -mpreferred-stack-boundary=4.
[glibc.git] / linuxthreads / Examples / ex17.c
blob1bc09a5bda3b35084dcb37a68f0473ce87af5d4c
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>
9 static pthread_mutex_t synch = PTHREAD_MUTEX_INITIALIZER;
11 static void *
12 test_thread (void *v_param)
14 pthread_mutex_lock (&synch);
15 return NULL;
18 #define STACKSIZE 0x100000
20 int
21 main (void)
23 pthread_t thread;
24 pthread_attr_t attr;
25 int status;
26 void *stack, *stack2;
27 size_t stacksize;
29 pthread_attr_init (&attr);
30 stack = mmap (NULL, STACKSIZE,
31 PROT_READ | PROT_WRITE | PROT_EXEC,
32 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
34 if (stack == MAP_FAILED)
36 perror ("mmap failed");
37 return 1;
40 status = pthread_attr_setstack (&attr, stack, STACKSIZE);
41 if (status != 0)
43 printf ("pthread_attr_setstack failed: %s\n", strerror (status));
44 return 1;
47 status = pthread_attr_getstack (&attr, &stack2, &stacksize);
48 if (status != 0)
50 printf ("pthread_attr_getstack failed: %s\n", strerror (status));
51 return 1;
54 if (stack2 != stack || stacksize != STACKSIZE)
56 printf ("first pthread_attr_getstack returned different stack (%p,%zx)\n"
57 "than was set by setstack (%p,%x)\n",
58 stack2, stacksize, stack, STACKSIZE);
59 return 2;
62 status = pthread_mutex_lock (&synch);
63 if (status != 0)
65 printf ("cannot get lock: %s\n", strerror (status));
66 return 1;
69 status = pthread_create (&thread, &attr, test_thread, NULL);
70 if (status != 0)
72 printf ("pthread_create failed: %s\n", strerror (status));
73 return 1;
76 status = pthread_getattr_np (thread, &attr);
77 if (status != 0)
79 printf ("pthread_getattr_np failed: %s\n", strerror (status));
80 return 1;
83 status = pthread_attr_getstack (&attr, &stack2, &stacksize);
84 if (status != 0)
86 printf ("pthread_attr_getstack failed: %s\n", strerror (status));
87 return 1;
90 if (stack2 != stack || stacksize != STACKSIZE)
92 printf ("second pthread_attr_getstack returned different stack (%p,%zx)\n"
93 "than was set by setstack (%p,%x)\n",
94 stack2, stacksize, stack, STACKSIZE);
95 return 3;
98 status = pthread_mutex_unlock (&synch);
99 if (status != 0)
101 printf ("cannot release lock: %s\n", strerror (status));
102 return 1;
105 /* pthread_detach (thread); */
106 if (pthread_join (thread, NULL) != 0)
108 printf ("join failed\n");
109 return 1;
111 return 0;