2
* Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
4
* This file is covered by the GNU general public license, version 2.
5
* see doc/COPYING for details.
8
#if defined(_MSC_VER) || \
9
defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__) || \
11
# define USE_WINTHREADS
12
#elif defined(__vxworks)
13
# define USE_VXTHREADS
34
#include "atomic_ops.h"
36
typedef void * (* thr_func)(void *);
38
typedef int (* test_func)(void); /* Returns != 0 on success */
40
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name);
43
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
50
fprintf(stderr, "Testing %s\n", name);
53
fprintf(stderr, "run_parallel: requested too many threads\n");
58
/* Default stack size is too small, especially with the 64 bit ABI */
60
if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
61
fprintf(stderr, "pthread_default_stacksize_np failed. "
62
"OK after first call.\n");
66
pthread_attr_init(&attr);
68
for (i = 0; i < nthreads; ++i)
70
if ((code = pthread_create(thr + i, &attr, f1, (void *)(long)i)) != 0)
72
perror("Thread creation failed");
73
fprintf(stderr, "Pthread_create returned %d, thread %d\n", code, i);
77
for (i = 0; i < nthreads; ++i)
79
if ((code = pthread_join(thr[i], NULL)) != 0)
81
perror("Thread join failed");
82
fprintf(stderr, "Pthread_join returned %d, thread %d\n", code, i);
88
fprintf(stderr, "Succeeded\n");
92
fprintf(stderr, "Failed\n");
97
#endif /* USE_PTHREADS */
100
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
105
fprintf(stderr, "Testing %s\n", name);
108
fprintf(stderr, "run_parallel: requested too many threads\n");
112
for (i = 0; i < nthreads; ++i)
114
thr[i] = taskSpawn((char*) name, 180, 0, 32768, (FUNCPTR) f1, i,
115
1, 2, 3, 4, 5, 6, 7, 8, 9);
118
fprintf(stderr, "taskSpawn failed with %d, thread %d\n",
123
for (i = 0; i < nthreads; ++i)
125
while (taskIdVerify(thr[i]) == OK)
130
fprintf(stderr, "Succeeded\n");
134
fprintf(stderr, "Failed\n");
139
#endif /* USE_VXTHREADS */
141
#ifdef USE_WINTHREADS
148
DWORD WINAPI tramp(LPVOID param)
150
struct tramp_args *args = (struct tramp_args *)param;
152
return (DWORD)(args -> fn)((LPVOID)(args -> arg));
155
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
158
struct tramp_args args[100];
162
fprintf(stderr, "Testing %s\n", name);
165
fprintf(stderr, "run_parallel: requested too many threads\n");
169
for (i = 0; i < nthreads; ++i)
173
if ((thr[i] = CreateThread(NULL, 0, tramp, (LPVOID)(args+i), 0, NULL))
176
perror("Thread creation failed");
177
fprintf(stderr, "CreateThread failed with %d, thread %d\n",
182
for (i = 0; i < nthreads; ++i)
184
if ((code = WaitForSingleObject(thr[i], INFINITE)) != WAIT_OBJECT_0)
186
perror("Thread join failed");
187
fprintf(stderr, "WaitForSingleObject returned %d, thread %d\n",
194
fprintf(stderr, "Succeeded\n");
198
fprintf(stderr, "Failed\n");
203
#endif /* USE_WINTHREADS */