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"
37 # define AO_PTRDIFF_T long
38 #elif defined(__int64)
39 # define AO_PTRDIFF_T __int64
41 # define AO_PTRDIFF_T long long
44 typedef void * (* thr_func)(void *);
46 typedef int (* test_func)(void); /* Returns != 0 on success */
48 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name);
51 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
58 fprintf(stderr, "Testing %s\n", name);
61 fprintf(stderr, "run_parallel: requested too many threads\n");
66 /* Default stack size is too small, especially with the 64 bit ABI */
68 if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
69 fprintf(stderr, "pthread_default_stacksize_np failed. "
70 "OK after first call.\n");
74 pthread_attr_init(&attr);
76 for (i = 0; i < nthreads; ++i)
78 if ((code = pthread_create(thr + i, &attr, f1, (void *)(long)i)) != 0)
80 perror("Thread creation failed");
81 fprintf(stderr, "Pthread_create returned %d, thread %d\n", code, i);
85 for (i = 0; i < nthreads; ++i)
87 if ((code = pthread_join(thr[i], NULL)) != 0)
89 perror("Thread join failed");
90 fprintf(stderr, "Pthread_join returned %d, thread %d\n", code, i);
96 fprintf(stderr, "Succeeded\n");
100 fprintf(stderr, "Failed\n");
105 #endif /* USE_PTHREADS */
108 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
113 fprintf(stderr, "Testing %s\n", name);
116 fprintf(stderr, "run_parallel: requested too many threads\n");
120 for (i = 0; i < nthreads; ++i)
122 thr[i] = taskSpawn((char*) name, 180, 0, 32768, (FUNCPTR) f1, i,
123 1, 2, 3, 4, 5, 6, 7, 8, 9);
126 fprintf(stderr, "taskSpawn failed with %d, thread %d\n",
131 for (i = 0; i < nthreads; ++i)
133 while (taskIdVerify(thr[i]) == OK)
138 fprintf(stderr, "Succeeded\n");
142 fprintf(stderr, "Failed\n");
147 #endif /* USE_VXTHREADS */
149 #ifdef USE_WINTHREADS
156 DWORD WINAPI tramp(LPVOID param)
158 struct tramp_args *args = (struct tramp_args *)param;
160 return (DWORD)(AO_PTRDIFF_T)(*args->fn)((LPVOID)(AO_PTRDIFF_T)args->arg);
163 void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
166 struct tramp_args args[100];
170 fprintf(stderr, "Testing %s\n", name);
173 fprintf(stderr, "run_parallel: requested too many threads\n");
177 for (i = 0; i < nthreads; ++i)
181 if ((thr[i] = CreateThread(NULL, 0, tramp, (LPVOID)(args+i), 0, NULL))
184 perror("Thread creation failed");
185 fprintf(stderr, "CreateThread failed with %lu, thread %d\n",
186 (unsigned long)GetLastError(), i);
190 for (i = 0; i < nthreads; ++i)
192 if ((code = WaitForSingleObject(thr[i], INFINITE)) != WAIT_OBJECT_0)
194 perror("Thread join failed");
195 fprintf(stderr, "WaitForSingleObject returned %lu, thread %d\n",
196 (unsigned long)code, i);
202 fprintf(stderr, "Succeeded\n");
206 fprintf(stderr, "Failed\n");
211 #endif /* USE_WINTHREADS */