1
/* $Id: utils.h,v 1.1.2.3 2007-07-02 05:35:31 d3p687 Exp $
3
* primitives for transparent handling of multi-threading
10
* This header file describes the "barrier" synchronization
11
* construct. The type barrier_t describes the full state of the
12
* barrier including the POSIX 1003.1c synchronization objects
15
* A barrier causes threads to wait until a set of threads has
16
* all "reached" the barrier. The number of threads required is
17
* set when the barrier is initialized, and cannot be changed
18
* except by reinitializing.
28
typedef pthread_mutex_t thread_lock_t;
29
# define THREAD_LOCK_INIT(x) pthread_mutex_init(&x,NULL)
30
# define THREAD_LOCK_DESTROY(x) pthread_mutex_destroy(&x)
31
# define THREAD_LOCK(x) pthread_mutex_lock(&x)
32
# define THREAD_UNLOCK(x) pthread_mutex_unlock(&x)
37
# include "spinlock.h"
40
# include "spinlock.h"
43
typedef LOCK_T thread_lock_t;
44
# define THREAD_LOCK_INIT(x) armci_init_spinlock(&x)
45
# define THREAD_LOCK_DESTROY(x) 0
46
# define THREAD_LOCK(x) armci_acquire_spinlock(&x)
47
# define THREAD_UNLOCK(x) armci_release_spinlock(&x)
49
typedef pthread_t thread_t;
50
# define THREAD_CREATE(th_,func_,arg_) pthread_create(th_,NULL,func_,arg_)
51
# define THREAD_JOIN(th_,ret_) pthread_join(th_,ret_)
53
/* structure describing a barrier */
54
typedef struct thread_barrier_tag {
55
pthread_mutex_t mutex; /* Control access to barrier */
56
pthread_cond_t cv; /* wait for barrier */
57
int valid; /* set when valid */
58
int threshold; /* number of threads required */
59
int counter; /* current number of threads */
60
int cycle; /* alternate wait cycles (0 or 1) */
63
# define BARRIER_VALID 0xdbcafe
65
/* support static initialization of barriers */
66
# define BARRIER_INITIALIZER(cnt) {\
67
PTHREAD_MUTEX_INITIALIZER, PTHREAD_COND_INITIALIZER,\
68
BARRIER_VALID, cnt, cnt, 0}
71
# error ONLY PTHREADS SUPPORT HAS BEEN IMPLEMENTED
74
# define TH2PROC(th_) (th_/mt_tpp) /* computes processor from thread id */
76
/* barrier functions */
77
int thread_barrier_init (thread_barrier_t *barrier, int count);
78
int thread_barrier_destroy (thread_barrier_t *barrier);
79
int thread_barrier_wait (thread_barrier_t *barrier);
81
/* multi-threaded memory functions */
82
int armci_malloc_mt(void *ptr[], int bytes);
83
int armci_free_mt(void *ptr, int th_idx);
84
# define ARMCI_MALLOC_MT armci_malloc_mt
85
# define ARMCI_FREE_MT armci_free_mt
88
# define TH_INIT(p_,t_) mt_size=p_;mt_tpp=t_;\
89
thread_barrier_init(&mt_barrier,mt_tpp)
90
# define TH_FINALIZE() thread_barrier_destroy(&mt_barrier)
91
# define MT_BARRIER() if (thread_barrier_wait(&mt_barrier)==-1) armci_msg_barrier();\
92
thread_barrier_wait(&mt_barrier)
96
extern thread_barrier_t mt_barrier;
98
# define THREAD_LOCK_INIT(x)
99
# define THREAD_LOCK_DESTROY(x)
100
# define THREAD_LOCK(x)
101
# define THREAD_UNLOCK(x)
102
# define TH_INIT(p_,t_)
103
# define TH_FINALIZE()
104
# define MT_BARRIER armci_msg_barrier
105
# define ARMCI_MALLOC_MT PARMCI_Malloc
106
# define ARMCI_FREE_MT(p_,th_) PARMCI_Free(p_)