File: run_parallel.h

package info (click to toggle)
moarvm 2018.12%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 18,196 kB
  • sloc: ansic: 223,172; perl: 7,638; sh: 4,452; makefile: 1,089; python: 568; asm: 8
file content (217 lines) | stat: -rw-r--r-- 4,817 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 * Copyright (c) 2003-2005 Hewlett-Packard Development Company, L.P.
 *
 * This file is covered by the GNU general public license, version 2.
 * see COPYING for details.
 */

#if defined(_MSC_VER) || \
    defined(_WIN32) && !defined(__CYGWIN32__) && !defined(__CYGWIN__) || \
    defined(_WIN32_WINCE)
#  define USE_WINTHREADS
#elif defined(__vxworks)
#  define USE_VXTHREADS
#else
#  define USE_PTHREADS
#endif

#include <stdlib.h>
#include <stdio.h>

#ifdef USE_PTHREADS
# include <pthread.h>
#endif

#ifdef USE_VXTHREADS
# include <vxworks.h>
# include <taskLib.h>
#endif

#ifdef USE_WINTHREADS
# include <windows.h>
#endif

#include "atomic_ops.h"

#if !defined(AO_ATOMIC_OPS_H) && !defined(CPPCHECK)
# error Wrong atomic_ops.h included.
#endif

#if (defined(_WIN32_WCE) || defined(__MINGW32CE__)) && !defined(AO_HAVE_abort)
# define abort() _exit(-1) /* there is no abort() in WinCE */
#endif

#ifndef AO_PTRDIFF_T
# define AO_PTRDIFF_T ptrdiff_t
#endif

#ifndef MAX_NTHREADS
# define MAX_NTHREADS 100
#endif

typedef void * (* thr_func)(void *);

typedef int (* test_func)(void);    /* Returns != 0 on success */

void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name);

#ifdef USE_PTHREADS
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
{
  pthread_attr_t attr;
  pthread_t thr[MAX_NTHREADS];
  int i;

  printf("Testing %s\n", name);
  if (nthreads > MAX_NTHREADS)
    {
      fprintf(stderr, "run_parallel: requested too many threads\n");
      abort();
    }

# ifdef _HPUX_SOURCE
   /* Default stack size is too small, especially with the 64 bit ABI */
   /* Increase it.                                                    */
    if (pthread_default_stacksize_np(1024*1024, 0) != 0)
      {
        fprintf(stderr, "pthread_default_stacksize_np failed. "
                        "OK after first call.\n");
      }
# endif

  pthread_attr_init(&attr);

  for (i = 0; i < nthreads; ++i)
    {
      int code = pthread_create(thr + i, &attr, f1, (void *)(long)i);
      if (code != 0)
      {
        fprintf(stderr, "pthread_create returned %d, thread %d\n", code, i);
        abort();
      }
    }
  for (i = 0; i < nthreads; ++i)
    {
      int code = pthread_join(thr[i], NULL);
      if (code != 0)
      {
        fprintf(stderr, "pthread_join returned %d, thread %d\n", code, i);
        abort();
      }
    }
  if (t())
    {
      printf("Succeeded\n");
    }
  else
    {
      fprintf(stderr, "Failed\n");
      abort();
    }
  return 0;
}
#endif /* USE_PTHREADS */

#ifdef USE_VXTHREADS
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
{
  int thr[MAX_NTHREADS];
  int i;

  printf("Testing %s\n", name);
  if (nthreads > MAX_NTHREADS)
    {
      fprintf(stderr, "run_parallel: requested too many threads\n");
      taskSuspend(0);
    }

  for (i = 0; i < nthreads; ++i)
    {
      thr[i] = taskSpawn((char*) name, 180, 0, 32768, (FUNCPTR) f1, i,
                         1, 2, 3, 4, 5, 6, 7, 8, 9);
      if (thr[i] == ERROR)
      {
        fprintf(stderr, "taskSpawn failed with %d, thread %d\n",
                errno, i);
        taskSuspend(0);
      }
    }
  for (i = 0; i < nthreads; ++i)
    {
      while (taskIdVerify(thr[i]) == OK)
        taskDelay(60);
    }
  if (t())
    {
      printf("Succeeded\n");
    }
  else
    {
      fprintf(stderr, "Failed\n");
      taskSuspend(0);
    }
  return 0;
}
#endif /* USE_VXTHREADS */

#ifdef USE_WINTHREADS

struct tramp_args {
  thr_func fn;
  long arg;
};

DWORD WINAPI tramp(LPVOID param)
{
  struct tramp_args *args = (struct tramp_args *)param;

  return (DWORD)(AO_PTRDIFF_T)(*args->fn)((LPVOID)(AO_PTRDIFF_T)args->arg);
}

void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
{
  HANDLE thr[MAX_NTHREADS];
  struct tramp_args args[MAX_NTHREADS];
  int i;

  printf("Testing %s\n", name);
  if (nthreads > MAX_NTHREADS)
    {
      fprintf(stderr, "run_parallel: requested too many threads\n");
      abort();
    }

  for (i = 0; i < nthreads; ++i)
    {
      args[i].fn = f1;
      args[i].arg = i;
      if ((thr[i] = CreateThread(NULL, 0, tramp, (LPVOID)(args+i), 0, NULL))
          == NULL)
        {
          fprintf(stderr, "CreateThread failed with %lu, thread %d\n",
                  (unsigned long)GetLastError(), i);
          abort();
        }
    }
  for (i = 0; i < nthreads; ++i)
    {
      DWORD code = WaitForSingleObject(thr[i], INFINITE);
      if (code != WAIT_OBJECT_0)
      {
        fprintf(stderr, "WaitForSingleObject returned %lu, thread %d\n",
                (unsigned long)code, i);
        abort();
      }
    }
  if (t())
    {
      printf("Succeeded\n");
    }
  else
    {
      fprintf(stderr, "Failed\n");
      abort();
    }
  return 0;
}
#endif /* USE_WINTHREADS */