OpenMPI  0.1.1
threads.h
1 /*
2  * Copyright (c) 2004-2005 The Trustees of Indiana University and Indiana
3  * University Research and Technology
4  * Corporation. All rights reserved.
5  * Copyright (c) 2004-2006 The University of Tennessee and The University
6  * of Tennessee Research Foundation. All rights
7  * reserved.
8  * Copyright (c) 2004-2005 High Performance Computing Center Stuttgart,
9  * University of Stuttgart. All rights reserved.
10  * Copyright (c) 2004-2005 The Regents of the University of California.
11  * All rights reserved.
12  * Copyright (c) 2010 Cisco Systems, Inc. All rights reserved.
13  * $COPYRIGHT$
14  *
15  * Additional copyrights may follow
16  *
17  * $HEADER$
18  */
19 
20 #ifndef ORTE_THREAD_H
21 #define ORTE_THREAD_H
22 
23 #include "orte_config.h"
24 
25 #include "opal/class/opal_object.h"
26 #if OPAL_ENABLE_DEBUG
27 #include "opal/util/output.h"
28 #endif
29 #include "opal/util/fd.h"
30 #include "opal/mca/event/event.h"
31 
32 #include "mutex.h"
33 #include "condition.h"
34 
35 BEGIN_C_DECLS
36 
37 typedef struct {
38  opal_object_t super;
39  opal_mutex_t lock;
40  opal_condition_t cond;
41  volatile bool active;
42  volatile bool running;
43  volatile bool stop;
44  opal_event_base_t *evbase;
45  char *name;
48 
49 #if OPAL_ENABLE_DEBUG
50 #define ORTE_ACQUIRE_THREAD(ctl) \
51  do { \
52  ORTE_THREAD_LOCK(&(ctl)->lock); \
53  if (opal_debug_threads) { \
54  opal_output(0, "Waiting for thread %s at %s:%d:%s", \
55  (NULL == (ctl)->name) ? "NULL" : (ctl)->name, \
56  __FILE__, __LINE__, \
57  ((ctl)->active) ? "TRUE" : "FALSE"); \
58  } \
59  while ((ctl)->active) { \
60  ORTE_CONDITION_WAIT(&(ctl)->cond, &(ctl)->lock); \
61  } \
62  if (opal_debug_threads) { \
63  opal_output(0, "Thread %s acquired at %s:%d", \
64  (NULL == (ctl)->name) ? "NULL" : (ctl)->name, \
65  __FILE__, __LINE__); \
66  } \
67  (ctl)->active = true; \
68  } while(0);
69 #else
70 #define ORTE_ACQUIRE_THREAD(ctl) \
71  do { \
72  ORTE_THREAD_LOCK(&(ctl)->lock); \
73  while ((ctl)->active) { \
74  ORTE_CONDITION_WAIT(&(ctl)->cond, &(ctl)->lock); \
75  } \
76  (ctl)->active = true; \
77  } while(0);
78 #endif
79 
80 
81 #if OPAL_ENABLE_DEBUG
82 #define ORTE_RELEASE_THREAD(ctl) \
83  do { \
84  if (opal_debug_threads) { \
85  opal_output(0, "Releasing thread %s at %s:%d", \
86  (NULL == (ctl)->name) ? "NULL" : (ctl)->name, \
87  __FILE__, __LINE__); \
88  } \
89  (ctl)->active = false; \
90  ORTE_CONDITION_BROADCAST(&(ctl)->cond); \
91  OPAL_UPDATE_EVBASE((ctl)->evbase, NULL, OPAL_EVENT_NOOP); \
92  ORTE_THREAD_UNLOCK(&(ctl)->lock); \
93  } while(0);
94 #else
95 #define ORTE_RELEASE_THREAD(ctl) \
96  do { \
97  (ctl)->active = false; \
98  ORTE_CONDITION_BROADCAST(&(ctl)->cond); \
99  OPAL_UPDATE_EVBASE((ctl)->evbase, NULL, OPAL_EVENT_NOOP); \
100  ORTE_THREAD_UNLOCK(&(ctl)->lock); \
101  } while(0);
102 #endif
103 
104 #if OPAL_ENABLE_DEBUG
105 #define ORTE_WAKEUP_THREAD(ctl) \
106  do { \
107  ORTE_THREAD_LOCK(&(ctl)->lock); \
108  if (opal_debug_threads) { \
109  opal_output(0, "Waking up thread %s at %s:%d", \
110  (NULL == (ctl)->name) ? "NULL" : (ctl)->name, \
111  __FILE__, __LINE__); \
112  } \
113  (ctl)->active = false; \
114  ORTE_CONDITION_BROADCAST(&(ctl)->cond); \
115  OPAL_UPDATE_EVBASE((ctl)->evbase, NULL, OPAL_EVENT_NOOP); \
116  ORTE_THREAD_UNLOCK(&(ctl)->lock); \
117  } while(0);
118 #else
119 #define ORTE_WAKEUP_THREAD(ctl) \
120  do { \
121  ORTE_THREAD_LOCK(&(ctl)->lock); \
122  (ctl)->active = false; \
123  ORTE_CONDITION_BROADCAST(&(ctl)->cond); \
124  OPAL_UPDATE_EVBASE((ctl)->evbase, NULL, OPAL_EVENT_NOOP); \
125  ORTE_THREAD_UNLOCK(&(ctl)->lock); \
126  } while(0);
127 #endif
128 
129 END_C_DECLS
130 
131 #endif /* ORTE_THREAD_H */
Definition: threads.h:37
OPAL output stream facility.
Definition: condition.h:49
Definition: mutex_unix.h:53
Definition: libevent2013.h:73
Base object.
Definition: opal_object.h:182
A simple C-language object-oriented system with single inheritance and ownership-based memory managem...
#define OBJ_CLASS_DECLARATION(NAME)
Declaration for class descriptor.
Definition: opal_object.h:236