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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
/*
*
* (C) 2006 by Argonne National Laboratory.
* See COPYRIGHT in top-level directory.
*/
/*
Define macro to override gcc strict flags,
-D_POSIX_C_SOURCE=199506L, -std=c89 and -std=c99,
that disallow pthread_barrier_t and friends.
*/
#if defined(_POSIX_C_SOURCE) && _POSIX_C_SOURCE < 200112L
#undef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 200112L
#endif
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
#include "mpitest.h"
#include "mpithreadtest.h"
/* This file provides a portability layer for using threads. Currently,
it supports POSIX threads (pthreads) and Windows threads. Testing has
been performed for pthreads.
*/
/* We remember all of the threads we create; this similifies terminating
(joining) them. */
#ifndef MTEST_MAX_THREADS
#define MTEST_MAX_THREADS 16
#endif
static MTEST_THREAD_HANDLE threads[MTEST_MAX_THREADS];
/* access w/o a lock is broken, but "volatile" should help reduce the amount of
* speculative loading/storing */
static volatile int nthreads = 0;
#ifdef HAVE_WINDOWS_H
int MTest_Start_thread(MTEST_THREAD_RETURN_TYPE(*fn) (void *p), void *arg)
{
if (nthreads >= MTEST_MAX_THREADS) {
fprintf(stderr, "Too many threads already created: max is %d\n", MTEST_MAX_THREADS);
return 1;
}
threads[nthreads] = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) fn, (LPVOID) arg, 0, NULL);
if (threads[nthreads] == NULL) {
return GetLastError();
} else {
nthreads++;
}
return MTestReturnValue(errs);
}
int MTest_Join_threads(void)
{
int i, err = 0;
for (i = 0; i < nthreads; i++) {
if (threads[i] != INVALID_HANDLE_VALUE) {
if (WaitForSingleObject(threads[i], INFINITE) == WAIT_FAILED) {
err = GetLastError();
fprintf(stderr, "Error WaitForSingleObject(), err = %d\n", err);
} else {
CloseHandle(threads[i]);
}
}
}
nthreads = 0;
return err;
}
int MTest_thread_lock_create(MTEST_THREAD_LOCK_TYPE * lock)
{
if (lock == NULL)
return -1;
/* Create an unnamed uninheritable mutex */
*lock = CreateMutex(NULL, FALSE, NULL);
if (*lock == NULL)
return -1;
return MTestReturnValue(errs);
}
int MTest_thread_lock(MTEST_THREAD_LOCK_TYPE * lock)
{
if (lock == NULL)
return -1;
/* Wait infinitely for the mutex */
if (WaitForSingleObject(*lock, INFINITE) != WAIT_OBJECT_0) {
return -1;
}
return MTestReturnValue(errs);
}
int MTest_thread_unlock(MTEST_THREAD_LOCK_TYPE * lock)
{
if (lock == NULL)
return -1;
if (ReleaseMutex(*lock) == 0) {
return -1;
}
return MTestReturnValue(errs);
}
int MTest_thread_lock_free(MTEST_THREAD_LOCK_TYPE * lock)
{
if (lock != NULL) {
if (CloseHandle(*lock) == 0) {
return -1;
}
}
return MTestReturnValue(errs);
}
#else
int MTest_Start_thread(MTEST_THREAD_RETURN_TYPE(*fn) (void *p), void *arg)
{
int err;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
if (nthreads >= MTEST_MAX_THREADS) {
fprintf(stderr, "Too many threads already created: max is %d\n", MTEST_MAX_THREADS);
return 1;
}
err = pthread_create(threads + nthreads, &attr, fn, arg);
if (!err) {
nthreads++;
}
pthread_attr_destroy(&attr);
return err;
}
int MTest_Join_threads(void)
{
int i, rc, err = 0;
for (i = 0; i < nthreads; i++) {
rc = pthread_join(threads[i], 0);
if (rc)
err = rc;
}
nthreads = 0;
return err;
}
int MTest_thread_lock_create(MTEST_THREAD_LOCK_TYPE * lock)
{
int err;
err = pthread_mutex_init(lock, NULL);
if (err) {
perror("Failed to initialize lock:");
}
return err;
}
int MTest_thread_lock(MTEST_THREAD_LOCK_TYPE * lock)
{
int err;
err = pthread_mutex_lock(lock);
if (err) {
perror("Failed to acquire lock:");
}
return err;
}
int MTest_thread_unlock(MTEST_THREAD_LOCK_TYPE * lock)
{
int err;
err = pthread_mutex_unlock(lock);
if (err) {
perror("Failed to release lock:");
}
return err;
}
int MTest_thread_lock_free(MTEST_THREAD_LOCK_TYPE * lock)
{
int err;
err = pthread_mutex_destroy(lock);
if (err) {
perror("Failed to free lock:");
}
return err;
}
#endif
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_BARRIER_INIT)
static MTEST_THREAD_LOCK_TYPE barrierLock;
static pthread_barrier_t barrier;
static int bcount = -1;
int MTest_thread_barrier_init(void)
{
bcount = -1; /* must reset to force barrier re-creation */
return MTest_thread_lock_create(&barrierLock);
}
int MTest_thread_barrier_free(void)
{
MTest_thread_lock_free(&barrierLock);
return pthread_barrier_destroy(&barrier);
}
/* FIXME this barrier interface should be changed to more closely match the
* pthread interface. Specifically, nt should not be a barrier-time
* parameter but an init-time parameter. The double-checked locking below
* isn't valid according to pthreads, and it isn't guaranteed to be robust
* in the presence of aggressive CPU/compiler optimization. */
int MTest_thread_barrier(int nt)
{
int err;
if (nt < 0)
nt = nthreads;
if (bcount != nt) {
/* One thread needs to initialize the barrier */
MTest_thread_lock(&barrierLock);
/* Test again in case another thread already fixed the problem */
if (bcount != nt) {
if (bcount > 0) {
err = pthread_barrier_destroy(&barrier);
if (err)
return err;
}
err = pthread_barrier_init(&barrier, NULL, nt);
if (err)
return err;
bcount = nt;
}
err = MTest_thread_unlock(&barrierLock);
if (err)
return err;
}
return pthread_barrier_wait(&barrier);
}
#else
static MTEST_THREAD_LOCK_TYPE barrierLock;
static volatile int phase = 0;
static volatile int c[2] = { -1, -1 };
int MTest_thread_barrier_init(void)
{
return MTest_thread_lock_create(&barrierLock);
}
int MTest_thread_barrier_free(void)
{
return MTest_thread_lock_free(&barrierLock);
}
/* This is a generic barrier implementation. To ensure that tests don't
silently fail, this both prints an error message and returns an error
result on any failure. */
int MTest_thread_barrier(int nt)
{
volatile int *cntP;
int err = 0;
if (nt < 0)
nt = nthreads;
/* Force a write barrier by using lock/unlock */
err = MTest_thread_lock(&barrierLock);
if (err) {
fprintf(stderr, "Lock failed in barrier!\n");
return err;
}
cntP = &c[phase];
err = MTest_thread_unlock(&barrierLock);
if (err) {
fprintf(stderr, "Unlock failed in barrier!\n");
return err;
}
/* printf("[%d] cnt = %d, phase = %d\n", pthread_self(), *cntP, phase); */
err = MTest_thread_lock(&barrierLock);
if (err) {
fprintf(stderr, "Lock failed in barrier!\n");
return err;
}
/* The first thread to enter will reset the counter */
if (*cntP < 0)
*cntP = nt;
/* printf("phase = %d, cnt = %d\n", phase, *cntP); */
/* The last thread to enter will force the counter to be negative */
if (*cntP == 1) {
/* printf("[%d] changing phase from %d\n", pthread_self(), phase); */
phase = !phase;
c[phase] = -1;
*cntP = 0;
}
/* Really need a write barrier here */
*cntP = *cntP - 1;
err = MTest_thread_unlock(&barrierLock);
if (err) {
fprintf(stderr, "Unlock failed in barrier!\n");
return err;
}
while (*cntP > 0);
return err;
}
#endif /* Default barrier routine */
|