tlds
Transactional Operations for Linked Data Structures
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thread.h
Go to the documentation of this file.
1 /* =============================================================================
2  *
3  * thread.h
4  *
5  * =============================================================================
6  *
7  * Copyright (C) Stanford University, 2006. All Rights Reserved.
8  * Author: Chi Cao Minh
9  *
10  * =============================================================================
11  *
12  * For the license of bayes/sort.h and bayes/sort.c, please see the header
13  * of the files.
14  *
15  * ------------------------------------------------------------------------
16  *
17  * For the license of kmeans, please see kmeans/LICENSE.kmeans
18  *
19  * ------------------------------------------------------------------------
20  *
21  * For the license of ssca2, please see ssca2/COPYRIGHT
22  *
23  * ------------------------------------------------------------------------
24  *
25  * For the license of lib/mt19937ar.c and lib/mt19937ar.h, please see the
26  * header of the files.
27  *
28  * ------------------------------------------------------------------------
29  *
30  * For the license of lib/rbtree.h and lib/rbtree.c, please see
31  * lib/LEGALNOTICE.rbtree and lib/LICENSE.rbtree
32  *
33  * ------------------------------------------------------------------------
34  *
35  * Unless otherwise noted, the following license applies to STAMP files:
36  *
37  * Copyright (c) 2007, Stanford University
38  * All rights reserved.
39  *
40  * Redistribution and use in source and binary forms, with or without
41  * modification, are permitted provided that the following conditions are
42  * met:
43  *
44  * * Redistributions of source code must retain the above copyright
45  * notice, this list of conditions and the following disclaimer.
46  *
47  * * Redistributions in binary form must reproduce the above copyright
48  * notice, this list of conditions and the following disclaimer in
49  * the documentation and/or other materials provided with the
50  * distribution.
51  *
52  * * Neither the name of Stanford University nor the names of its
53  * contributors may be used to endorse or promote products derived
54  * from this software without specific prior written permission.
55  *
56  * THIS SOFTWARE IS PROVIDED BY STANFORD UNIVERSITY ``AS IS'' AND ANY
57  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
59  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL STANFORD UNIVERSITY BE LIABLE
60  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
61  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
62  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
63  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
64  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
65  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
66  * THE POSSIBILITY OF SUCH DAMAGE.
67  *
68  * =============================================================================
69  */
70 
71 
72 #ifndef THREAD_H
73 #define THREAD_H 1
74 
75 
76 #include <pthread.h>
77 #include <stdlib.h>
78 #include "types.h"
79 #ifdef OTM
80 #include "omp.h"
81 #endif
82 
83 #ifdef __cplusplus
84 extern "C" {
85 #endif
86 
87 
88 #define THREAD_T pthread_t
89 #define THREAD_ATTR_T pthread_attr_t
90 
91 #define THREAD_ATTR_INIT(attr) pthread_attr_init(&attr)
92 #define THREAD_JOIN(tid) pthread_join(tid, (void**)NULL)
93 #define THREAD_CREATE(tid, attr, fn, arg) pthread_create(&(tid), \
94  &(attr), \
95  (void* (*)(void*))(fn), \
96  (void*)(arg))
97 
98 #define THREAD_LOCAL_T pthread_key_t
99 #define THREAD_LOCAL_INIT(key) pthread_key_create(&key, NULL)
100 #define THREAD_LOCAL_SET(key, val) pthread_setspecific(key, (void*)(val))
101 #define THREAD_LOCAL_GET(key) pthread_getspecific(key)
102 
103 #define THREAD_MUTEX_T pthread_mutex_t
104 #define THREAD_MUTEX_INIT(lock) pthread_mutex_init(&(lock), NULL)
105 #define THREAD_MUTEX_LOCK(lock) pthread_mutex_lock(&(lock))
106 #define THREAD_MUTEX_UNLOCK(lock) pthread_mutex_unlock(&(lock))
107 
108 #define THREAD_COND_T pthread_cond_t
109 #define THREAD_COND_INIT(cond) pthread_cond_init(&(cond), NULL)
110 #define THREAD_COND_SIGNAL(cond) pthread_cond_signal(&(cond))
111 #define THREAD_COND_BROADCAST(cond) pthread_cond_broadcast(&(cond))
112 #define THREAD_COND_WAIT(cond, lock) pthread_cond_wait(&(cond), &(lock))
113 
114 #ifdef SIMULATOR
115 # define THREAD_BARRIER_T pthread_barrier_t
116 # define THREAD_BARRIER_ALLOC(N) ((THREAD_BARRIER_T*)malloc(sizeof(THREAD_BARRIER_T)))
117 # define THREAD_BARRIER_INIT(bar, N) pthread_barrier_init(bar, 0, N)
118 # define THREAD_BARRIER(bar, tid) pthread_barrier_wait(bar)
119 # define THREAD_BARRIER_FREE(bar) free(bar)
120 #else /* !SIMULATOR */
121 
122 #ifdef LOG_BARRIER
123 # define THREAD_BARRIER_T thread_barrier_t
124 # define THREAD_BARRIER_ALLOC(N) thread_barrier_alloc(N)
125 # define THREAD_BARRIER_INIT(bar, N) thread_barrier_init(bar)
126 # define THREAD_BARRIER(bar, tid) thread_barrier(bar, tid)
127 # define THREAD_BARRIER_FREE(bar) thread_barrier_free(bar)
128 #else
129 # define THREAD_BARRIER_T barrier_t
130 # define THREAD_BARRIER_ALLOC(N) barrier_alloc()
131 # define THREAD_BARRIER_INIT(bar, N) barrier_init(bar, N)
132 # define THREAD_BARRIER(bar, tid) barrier_cross(bar)
133 # define THREAD_BARRIER_FREE(bar) barrier_free(bar)
134 #endif /* !LOG_BARRIER */
135 #endif /* !SIMULATOR */
136 
137 
138 
139 #ifdef LOG_BARRIER
140 typedef struct thread_barrier {
141  THREAD_MUTEX_T countLock;
142  THREAD_COND_T proceedCond;
143  THREAD_COND_T proceedAllCond;
144  long count;
145  long numThread;
146 } thread_barrier_t;
147 #else
148 
149 struct barrier;
150 typedef struct barrier barrier_t;
151 
153 
154 void barrier_free(barrier_t *b);
155 
156 void barrier_init(barrier_t *b, int n);
157 
158 void barrier_cross(barrier_t *b);
159 
160 #endif /* LOG_BARRIER */
161 
162 
163 /* =============================================================================
164  * thread_startup
165  * -- Create pool of secondary threads
166  * -- numThread is total number of threads (primary + secondary)
167  * =============================================================================
168  */
169 void
170 thread_startup (long numThread);
171 
172 
173 /* =============================================================================
174  * thread_start
175  * -- Make primary and secondary threads execute work
176  * -- Should only be called by primary thread
177  * -- funcPtr takes one arguments: argPtr
178  * =============================================================================
179  */
180 void
181 thread_start (void (*funcPtr)(void*), void* argPtr);
182 
183 
184 /* =============================================================================
185  * thread_shutdown
186  * -- Primary thread kills pool of secondary threads
187  * =============================================================================
188  */
189 void
190 thread_shutdown ();
191 
192 
193 #ifdef LOG_BARRIER
194 /* =============================================================================
195  * thread_barrier_alloc
196  * =============================================================================
197  */
198 thread_barrier_t*
199 thread_barrier_alloc (long numThreads);
200 
201 
202 /* =============================================================================
203  * thread_barrier_free
204  * =============================================================================
205  */
206 void
207 thread_barrier_free (thread_barrier_t* barrierPtr);
208 
209 
210 /* =============================================================================
211  * thread_barrier_init
212  * =============================================================================
213  */
214 void
215 thread_barrier_init (thread_barrier_t* barrierPtr);
216 
217 
218 /* =============================================================================
219  * thread_barrier
220  * -- Simple logarithmic barrier
221  * =============================================================================
222  */
223 void
224 thread_barrier (thread_barrier_t* barrierPtr, long threadId);
225 
226 #endif /* LOG_BARRIER */
227 
228 
229 /* =============================================================================
230  * thread_barrier_wait
231  * -- Call after thread_start() to synchronize threads inside parallel region
232  * =============================================================================
233  */
234 void
236 
237 /* =============================================================================
238  * thread_getId
239  * -- Call after thread_start() to get thread ID inside parallel region
240  * =============================================================================
241  */
242 long
243 thread_getId();
244 
245 
246 /* =============================================================================
247  * thread_getNumThread
248  * -- Call after thread_start() to get number of threads inside parallel region
249  * =============================================================================
250  */
251 long
253 
254 
255 
256 #ifdef __cplusplus
257 }
258 #endif
259 
260 
261 #endif /* THREAD_H */
262 
263 
264 /* =============================================================================
265  *
266  * End of thread.h
267  *
268  * =============================================================================
269  */
void thread_startup(long numThread)
Definition: thread.c:121
#define THREAD_MUTEX_T
Definition: thread.h:103
void barrier_cross(barrier_t *b)
Definition: thread.c:343
void thread_shutdown()
Definition: library.hpp:199
Definition: barrier.hpp:28
long thread_getId()
Definition: thread.c:382
long thread_getNumThread()
Definition: thread.c:394
void thread_barrier_wait()
Definition: thread.c:367
void thread_start(void(*funcPtr)(void *), void *argPtr)
Definition: thread.c:171
void barrier_init(barrier_t *b, int n)
Definition: thread.c:336
void barrier_free(barrier_t *b)
Definition: thread.c:332
#define THREAD_COND_T
Definition: thread.h:108
barrier_t * barrier_alloc()
Definition: thread.c:328
volatile unsigned long count
Definition: queues.cpp:53