resetting manifest requested domain to floor
[platform/upstream/pth.git] / test_pthread.c
1 /*
2 **  GNU Pth - The GNU Portable Threads
3 **  Copyright (c) 1999-2006 Ralf S. Engelschall <rse@engelschall.com>
4 **
5 **  This file is part of GNU Pth, a non-preemptive thread scheduling
6 **  library which can be found at http://www.gnu.org/software/pth/.
7 **
8 **  This library is free software; you can redistribute it and/or
9 **  modify it under the terms of the GNU Lesser General Public
10 **  License as published by the Free Software Foundation; either
11 **  version 2.1 of the License, or (at your option) any later version.
12 **
13 **  This library is distributed in the hope that it will be useful,
14 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
15 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 **  Lesser General Public License for more details.
17 **
18 **  You should have received a copy of the GNU Lesser General Public
19 **  License along with this library; if not, write to the Free Software
20 **  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 **  USA, or contact Ralf S. Engelschall <rse@engelschall.com>.
22 **
23 **  test_pthread.c: Pth test program (pthread API)
24 */
25                              /* ``You can check out any time you
26                                 like, but you can never leave.''
27                                 -- The Eagles, Hotel California */
28 #ifdef GLOBAL
29 #include <pthread.h>
30 #else
31 #include "pthread.h"
32 #endif
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <errno.h>
39
40 #define die(str) \
41     do { \
42         fprintf(stderr, "**die: %s: errno=%d\n", str, errno); \
43         exit(1); \
44     } while (0)
45
46 static void *child(void *_arg)
47 {
48     char *name = (char *)_arg;
49     int i;
50
51     fprintf(stderr, "child: startup %s\n", name);
52     for (i = 0; i < 100; i++) {
53         if (i++ % 10 == 0)
54             sleep(1);
55         fprintf(stderr, "child: %s counts i=%d\n", name, i);
56     }
57     fprintf(stderr, "child: shutdown %s\n", name);
58     return _arg;
59 }
60
61 int main(int argc, char *argv[])
62 {
63     pthread_attr_t thread_attr;
64     pthread_t thread[4];
65     char *rc;
66
67     fprintf(stderr, "main: init\n");
68
69     fprintf(stderr, "main: initializing attribute object\n");
70     if (pthread_attr_init(&thread_attr) != 0)
71         die("pthread_attr_init");
72     if (pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE) != 0)
73         die("pthread_attr_setdetachstate");
74
75     fprintf(stderr, "main: create thread 1\n");
76     if (pthread_create(&thread[0], &thread_attr, child, (void *)"foo") != 0)
77         die("pthread_create");
78     fprintf(stderr, "main: create thread 2\n");
79     if (pthread_create(&thread[1], &thread_attr, child, (void *)"bar") != 0)
80         die("pthread_create");
81     fprintf(stderr, "main: create thread 3\n");
82     if (pthread_create(&thread[2], &thread_attr, child, (void *)"baz") != 0)
83         die("pthread_create");
84     fprintf(stderr, "main: create thread 4\n");
85     if (pthread_create(&thread[3], &thread_attr, child, (void *)"quux") != 0)
86         die("pthread_create");
87
88     fprintf(stderr, "main: destroying attribute object\n");
89     if (pthread_attr_destroy(&thread_attr) != 0)
90         die("pthread_attr_destroy");
91
92     sleep(3);
93
94     fprintf(stderr, "main: joining...\n");
95     if (pthread_join(thread[0], (void **)&rc) != 0)
96         die("pthread_join");
97     fprintf(stderr, "main: joined thread: %s\n", rc);
98     if (pthread_join(thread[1], (void **)&rc) != 0)
99         die("pthread_join");
100     fprintf(stderr, "main: joined thread: %s\n", rc);
101     if (pthread_join(thread[2], (void **)&rc) != 0)
102         die("pthread_join");
103     fprintf(stderr, "main: joined thread: %s\n", rc);
104     if (pthread_join(thread[3], (void **)&rc) != 0)
105         die("pthread_join");
106     fprintf(stderr, "main: joined thread: %s\n", rc);
107
108     fprintf(stderr, "main: exit\n");
109     return 0;
110 }
111