Changes to attempt to silence bcc64x
[ACE_TAO.git] / ACE / ace / Barrier.cpp
bloba886b06876c347e05110e5c2045b3fd1859a1d06
1 #include "ace/Barrier.h"
3 #if defined (ACE_HAS_THREADS)
5 #include "ace/Guard_T.h"
6 #include "ace/OS_NS_errno.h"
8 #if defined (ACE_HAS_ALLOC_HOOKS)
9 # include "ace/Malloc_Base.h"
10 #endif /* ACE_HAS_ALLOC_HOOKS */
12 #if defined (ACE_HAS_DUMP)
13 # include "ace/Log_Category.h"
14 #endif /* ACE_HAS_DUMP */
16 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
18 ACE_ALLOC_HOOK_DEFINE(ACE_Sub_Barrier)
20 void
21 ACE_Sub_Barrier::dump () const
23 #if defined (ACE_HAS_DUMP)
24 // ACE_TRACE ("ACE_Sub_Barrier::dump");
26 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
27 this->barrier_finished_.dump ();
28 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("running_threads_ = %d\n"), this->running_threads_));
29 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
30 #endif /* ACE_HAS_DUMP */
33 ACE_Sub_Barrier::ACE_Sub_Barrier (unsigned int count,
34 ACE_Thread_Mutex &lock,
35 const ACE_TCHAR *name,
36 void *arg)
37 : barrier_finished_ (lock, name, arg),
38 running_threads_ (count)
40 // ACE_TRACE ("ACE_Sub_Barrier::ACE_Sub_Barrier");
43 ACE_ALLOC_HOOK_DEFINE(ACE_Barrier)
45 void
46 ACE_Barrier::dump () const
48 #if defined (ACE_HAS_DUMP)
49 // ACE_TRACE ("ACE_Barrier::dump");
51 ACELIB_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
52 this->lock_.dump ();
53 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("current_generation_ = %d"), this->current_generation_));
54 ACELIB_DEBUG ((LM_DEBUG, ACE_TEXT ("\ncount_ = %d"), this->count_));
55 this->sub_barrier_1_.dump ();
56 this->sub_barrier_2_.dump ();
57 ACELIB_DEBUG ((LM_DEBUG, ACE_END_DUMP));
58 #endif /* ACE_HAS_DUMP */
61 ACE_Barrier::ACE_Barrier (unsigned int count,
62 const ACE_TCHAR *name,
63 void *arg)
64 : lock_ (name, (ACE_mutexattr_t *) arg),
65 current_generation_ (0),
66 count_ (count),
67 sub_barrier_1_ (count, lock_, name, arg),
68 sub_barrier_2_ (count, lock_, name, arg)
70 ACE_TRACE ("ACE_Barrier::ACE_Barrier");
71 this->sub_barrier_[0] = &this->sub_barrier_1_;
72 this->sub_barrier_[1] = &this->sub_barrier_2_;
75 int
76 ACE_Barrier::wait ()
78 ACE_TRACE ("ACE_Barrier::wait");
79 ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);
81 ACE_Sub_Barrier *sbp = this->sub_barrier_[this->current_generation_];
83 // Check for shutdown...
84 if (sbp == nullptr)
86 errno = ESHUTDOWN;
87 return -1;
90 int retval = 0;
92 if (sbp->running_threads_ == 1)
94 // We're the last running thread, so swap generations and tell
95 // all the threads waiting on the barrier to continue on their
96 // way.
97 sbp->running_threads_ = this->count_;
98 // Swap generations.
99 this->current_generation_ = 1 - this->current_generation_;
100 sbp->barrier_finished_.broadcast ();
102 else
104 --sbp->running_threads_;
106 // Block until all the other threads wait().
107 while (sbp->running_threads_ != this->count_)
108 sbp->barrier_finished_.wait ();
110 // We're awake and the count has completed. See if it completed
111 // because all threads hit the barrier, or because the barrier
112 // was shut down.
113 if (this->sub_barrier_[this->current_generation_] == 0)
115 errno = ESHUTDOWN;
116 retval = -1;
120 return retval;
124 ACE_Barrier::shutdown ()
126 ACE_TRACE ("ACE_Barrier::shutdown");
127 ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);
129 ACE_Sub_Barrier *sbp = this->sub_barrier_[this->current_generation_];
131 // Check for shutdown...
132 if (sbp == nullptr)
134 errno = ESHUTDOWN;
135 return -1;
138 // Flag the shutdown
139 this->sub_barrier_[0] = nullptr;
140 this->sub_barrier_[1] = nullptr;
141 // Tell all the threads waiting on the barrier to continue on their way.
142 sbp->running_threads_ = this->count_;
143 sbp->barrier_finished_.broadcast ();
145 return 0;
148 ACE_ALLOC_HOOK_DEFINE(ACE_Thread_Barrier)
150 ACE_Thread_Barrier::ACE_Thread_Barrier (unsigned int count,
151 const ACE_TCHAR *name)
152 : ACE_Barrier (count, name)
154 // ACE_TRACE ("ACE_Thread_Barrier::ACE_Thread_Barrier");
157 void
158 ACE_Thread_Barrier::dump () const
160 #if defined (ACE_HAS_DUMP)
161 // ACE_TRACE ("ACE_Thread_Barrier::dump");
162 ACE_Barrier::dump ();
163 #endif /* ACE_HAS_DUMP */
166 ACE_END_VERSIONED_NAMESPACE_DECL
168 #endif /* ACE_HAS_THREADS */