~ubuntu-branches/ubuntu/quantal/libgc/quantal

« back to all changes in this revision

Viewing changes to libatomic_ops-1.2/tests/run_parallel.inc

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-19 12:19:56 UTC
  • mfrom: (1.3.2 upstream) (0.1.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20110219121956-67rb69xlt5nud3v2
Tags: 1:7.1-5
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
typedef void * (* thr_func)(void *);
 
37
 
 
38
typedef int (* test_func)(void);        /* Returns != 0 on success */
 
39
 
 
40
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name);
 
41
 
 
42
#ifdef USE_PTHREADS
 
43
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
 
44
{
 
45
  pthread_attr_t attr;
 
46
  pthread_t thr[100];
 
47
  int i;
 
48
  int code;
 
49
 
 
50
  fprintf(stderr, "Testing %s\n", name);
 
51
  if (nthreads > 100) 
 
52
    {
 
53
      fprintf(stderr, "run_parallel: requested too many threads\n");
 
54
      abort();
 
55
    }
 
56
 
 
57
# ifdef _HPUX_SOURCE
 
58
   /* Default stack size is too small, especially with the 64 bit ABI */
 
59
   /* Increase it.                                                    */
 
60
    if (pthread_default_stacksize_np(1024*1024, 0) != 0) {
 
61
      fprintf(stderr, "pthread_default_stacksize_np failed. "
 
62
                      "OK after first call.\n");
 
63
    }
 
64
# endif
 
65
 
 
66
  pthread_attr_init(&attr);
 
67
 
 
68
  for (i = 0; i < nthreads; ++i)
 
69
    {
 
70
      if ((code = pthread_create(thr + i, &attr, f1, (void *)(long)i)) != 0)
 
71
        {
 
72
          perror("Thread creation failed");
 
73
          fprintf(stderr, "Pthread_create returned %d, thread %d\n", code, i);
 
74
          abort();
 
75
        }
 
76
    }
 
77
  for (i = 0; i < nthreads; ++i)
 
78
    {
 
79
      if ((code = pthread_join(thr[i], NULL)) != 0)
 
80
        {
 
81
          perror("Thread join failed");
 
82
          fprintf(stderr, "Pthread_join returned %d, thread %d\n", code, i);
 
83
          abort();
 
84
        }
 
85
    }
 
86
  if (t())
 
87
    {
 
88
      fprintf(stderr, "Succeeded\n");
 
89
    }
 
90
  else
 
91
    {
 
92
      fprintf(stderr, "Failed\n");
 
93
      abort();
 
94
    }
 
95
  return 0;
 
96
}
 
97
#endif /* USE_PTHREADS */
 
98
 
 
99
#ifdef USE_VXTHREADS
 
100
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
 
101
{
 
102
  int thr[100];
 
103
  int i;
 
104
 
 
105
  fprintf(stderr, "Testing %s\n", name);
 
106
  if (nthreads > 100) 
 
107
    {
 
108
      fprintf(stderr, "run_parallel: requested too many threads\n");
 
109
      taskSuspend(0);
 
110
    }
 
111
 
 
112
  for (i = 0; i < nthreads; ++i)
 
113
    {
 
114
      thr[i] = taskSpawn((char*) name, 180, 0, 32768, (FUNCPTR) f1, i,
 
115
                         1, 2, 3, 4, 5, 6, 7, 8, 9);
 
116
      if (thr[i] == ERROR)
 
117
        {
 
118
          fprintf(stderr, "taskSpawn failed with %d, thread %d\n",
 
119
                          errno, i);
 
120
          taskSuspend(0);
 
121
        }
 
122
    }
 
123
  for (i = 0; i < nthreads; ++i)
 
124
    {
 
125
      while (taskIdVerify(thr[i]) == OK)
 
126
        taskDelay(60);
 
127
    }
 
128
  if (t())
 
129
    {
 
130
      fprintf(stderr, "Succeeded\n");
 
131
    }
 
132
  else
 
133
    {
 
134
      fprintf(stderr, "Failed\n");
 
135
      taskSuspend(0);
 
136
    }
 
137
  return 0;
 
138
}
 
139
#endif /* USE_VXTHREADS */
 
140
 
 
141
#ifdef USE_WINTHREADS
 
142
 
 
143
struct tramp_args {
 
144
  thr_func fn;
 
145
  long arg;
 
146
};
 
147
 
 
148
DWORD WINAPI tramp(LPVOID param)
 
149
{
 
150
  struct tramp_args *args = (struct tramp_args *)param;
 
151
 
 
152
  return (DWORD)(args -> fn)((LPVOID)(args -> arg));
 
153
}
 
154
 
 
155
void * run_parallel(int nthreads, thr_func f1, test_func t, const char *name)
 
156
{
 
157
  HANDLE thr[100];
 
158
  struct tramp_args args[100];
 
159
  int i;
 
160
  DWORD code;
 
161
 
 
162
  fprintf(stderr, "Testing %s\n", name);
 
163
  if (nthreads > 100) 
 
164
    {
 
165
      fprintf(stderr, "run_parallel: requested too many threads\n");
 
166
      abort();
 
167
    }
 
168
 
 
169
  for (i = 0; i < nthreads; ++i)
 
170
    {
 
171
      args[i].fn = f1;
 
172
      args[i].arg = i;
 
173
      if ((thr[i] = CreateThread(NULL, 0, tramp, (LPVOID)(args+i), 0, NULL))
 
174
          == NULL)
 
175
        {
 
176
          perror("Thread creation failed");
 
177
          fprintf(stderr, "CreateThread failed with %d, thread %d\n",
 
178
                          GetLastError(), i);
 
179
          abort();
 
180
        }
 
181
    }
 
182
  for (i = 0; i < nthreads; ++i)
 
183
    {
 
184
      if ((code = WaitForSingleObject(thr[i], INFINITE)) != WAIT_OBJECT_0)
 
185
        {
 
186
          perror("Thread join failed");
 
187
          fprintf(stderr, "WaitForSingleObject returned %d, thread %d\n",
 
188
                          code, i);
 
189
          abort();
 
190
        }
 
191
    }
 
192
  if (t())
 
193
    {
 
194
      fprintf(stderr, "Succeeded\n");
 
195
    }
 
196
  else
 
197
    {
 
198
      fprintf(stderr, "Failed\n");
 
199
      abort();
 
200
    }
 
201
  return 0;
 
202
}
 
203
#endif /* USE_WINTHREADS */
 
204