implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / libatomic_ops / tests / run_parallel.inc
1 /*
2  * Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
3  *
4  * This file is covered by the GNU general public license, version 2.
5  * see doc/COPYING for details.
6  */
7
8 #if defined(_MSC_VER) || \
9     defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__) || \
10     defined(_WIN32_WINCE)
11 #  define USE_WINTHREADS
12 #elif defined(__vxworks)
13 #  define USE_VXTHREADS
14 #else
15 #  define USE_PTHREADS
16 #endif
17
18 #include <stdlib.h>
19 #include <stdio.h>
20
21 #ifdef USE_PTHREADS
22 # include <pthread.h>
23 #endif
24
25 #ifdef USE_VXTHREADS
26 # include <vxworks.h>
27 # include <taskLib.h>
28 #endif
29
30 #ifdef USE_WINTHREADS
31 # include <windows.h>
32 #endif
33
34 #include "atomic_ops.h"
35
36 #ifndef _WIN64
37 # define AO_PTRDIFF_T long
38 #elif defined(__int64)
39 # define AO_PTRDIFF_T __int64
40 #else
41 # define AO_PTRDIFF_T long long
42 #endif
43
44 typedef void * (* thr_func)(void *);
45
46 typedef int (* test_func)(void);    /* Returns != 0 on success */
47
48 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name);
49
50 #ifdef USE_PTHREADS
51 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
52 {
53   pthread_attr_t attr;
54   pthread_t thr[100];
55   int i;
56   int code;
57
58   fprintf(stderr, "Testing %s\n", name);
59   if (nthreads > 100)
60     {
61       fprintf(stderr, "run_parallel: requested too many threads\n");
62       abort();
63     }
64
65 # ifdef _HPUX_SOURCE
66    /* Default stack size is too small, especially with the 64 bit ABI */
67    /* Increase it.                                                    */
68     if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
69       fprintf(stderr, "pthread_default_stacksize_np failed. "
70                   "OK after first call.\n");
71     }
72 # endif
73
74   pthread_attr_init(&attr);
75
76   for (i = 0; i < nthreads; ++i)
77     {
78       if ((code = pthread_create(thr + i, &attr, f1, (void *)(long)i)) != 0)
79     {
80       perror("Thread creation failed");
81       fprintf(stderr, "Pthread_create returned %d, thread %d\n", code, i);
82       abort();
83         }
84     }
85   for (i = 0; i < nthreads; ++i)
86     {
87       if ((code = pthread_join(thr[i], NULL)) != 0)
88     {
89       perror("Thread join failed");
90       fprintf(stderr, "Pthread_join returned %d, thread %d\n", code, i);
91       abort();
92         }
93     }
94   if (t())
95     {
96       fprintf(stderr, "Succeeded\n");
97     }
98   else
99     {
100       fprintf(stderr, "Failed\n");
101       abort();
102     }
103   return 0;
104 }
105 #endif /* USE_PTHREADS */
106
107 #ifdef USE_VXTHREADS
108 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
109 {
110   int thr[100];
111   int i;
112
113   fprintf(stderr, "Testing %s\n", name);
114   if (nthreads > 100)
115     {
116       fprintf(stderr, "run_parallel: requested too many threads\n");
117       taskSuspend(0);
118     }
119
120   for (i = 0; i < nthreads; ++i)
121     {
122       thr[i] = taskSpawn((char*) name, 180, 0, 32768, (FUNCPTR) f1, i,
123                          1, 2, 3, 4, 5, 6, 7, 8, 9);
124       if (thr[i] == ERROR)
125     {
126       fprintf(stderr, "taskSpawn failed with %d, thread %d\n",
127               errno, i);
128       taskSuspend(0);
129         }
130     }
131   for (i = 0; i < nthreads; ++i)
132     {
133       while (taskIdVerify(thr[i]) == OK)
134         taskDelay(60);
135     }
136   if (t())
137     {
138       fprintf(stderr, "Succeeded\n");
139     }
140   else
141     {
142       fprintf(stderr, "Failed\n");
143       taskSuspend(0);
144     }
145   return 0;
146 }
147 #endif /* USE_VXTHREADS */
148
149 #ifdef USE_WINTHREADS
150
151 struct tramp_args {
152   thr_func fn;
153   long arg;
154 };
155
156 DWORD WINAPI tramp(LPVOID param)
157 {
158   struct tramp_args *args = (struct tramp_args *)param;
159
160   return (DWORD)(AO_PTRDIFF_T)(*args->fn)((LPVOID)(AO_PTRDIFF_T)args->arg);
161 }
162
163 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
164 {
165   HANDLE thr[100];
166   struct tramp_args args[100];
167   int i;
168   DWORD code;
169
170   fprintf(stderr, "Testing %s\n", name);
171   if (nthreads > 100)
172     {
173       fprintf(stderr, "run_parallel: requested too many threads\n");
174       abort();
175     }
176
177   for (i = 0; i < nthreads; ++i)
178     {
179       args[i].fn = f1;
180       args[i].arg = i;
181       if ((thr[i] = CreateThread(NULL, 0, tramp, (LPVOID)(args+i), 0, NULL))
182       == NULL)
183     {
184       perror("Thread creation failed");
185       fprintf(stderr, "CreateThread failed with %lu, thread %d\n",
186               (unsigned long)GetLastError(), i);
187       abort();
188         }
189     }
190   for (i = 0; i < nthreads; ++i)
191     {
192       if ((code = WaitForSingleObject(thr[i], INFINITE)) != WAIT_OBJECT_0)
193     {
194       perror("Thread join failed");
195       fprintf(stderr, "WaitForSingleObject returned %lu, thread %d\n",
196               (unsigned long)code, i);
197       abort();
198         }
199     }
200   if (t())
201     {
202       fprintf(stderr, "Succeeded\n");
203     }
204   else
205     {
206       fprintf(stderr, "Failed\n");
207       abort();
208     }
209   return 0;
210 }
211 #endif /* USE_WINTHREADS */