Use override/default for RTPortableServer
[ACE_TAO.git] / ACE / ace / Barrier.cpp
blobc516d1307d7d00989db037d38a4fca45f8b0e47a
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 =
82 this->sub_barrier_[this->current_generation_];
84 // Check for shutdown...
85 if (sbp == 0)
87 errno = ESHUTDOWN;
88 return -1;
91 int retval = 0;
93 if (sbp->running_threads_ == 1)
95 // We're the last running thread, so swap generations and tell
96 // all the threads waiting on the barrier to continue on their
97 // way.
98 sbp->running_threads_ = this->count_;
99 // Swap generations.
100 this->current_generation_ = 1 - this->current_generation_;
101 sbp->barrier_finished_.broadcast ();
103 else
105 --sbp->running_threads_;
107 // Block until all the other threads wait().
108 while (sbp->running_threads_ != this->count_)
109 sbp->barrier_finished_.wait ();
111 // We're awake and the count has completed. See if it completed
112 // because all threads hit the barrier, or because the barrier
113 // was shut down.
114 if (this->sub_barrier_[this->current_generation_] == 0)
116 errno = ESHUTDOWN;
117 retval = -1;
121 return retval;
125 ACE_Barrier::shutdown ()
127 ACE_TRACE ("ACE_Barrier::shutdown");
128 ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, this->lock_, -1);
130 ACE_Sub_Barrier *sbp =
131 this->sub_barrier_[this->current_generation_];
133 // Check for shutdown...
134 if (sbp == 0)
136 errno = ESHUTDOWN;
137 return -1;
140 // Flag the shutdown
141 this->sub_barrier_[0] = 0;
142 this->sub_barrier_[1] = 0;
143 // Tell all the threads waiting on the barrier to continue on their way.
144 sbp->running_threads_ = this->count_;
145 sbp->barrier_finished_.broadcast ();
147 return 0;
150 ACE_ALLOC_HOOK_DEFINE(ACE_Thread_Barrier)
152 ACE_Thread_Barrier::ACE_Thread_Barrier (unsigned int count,
153 const ACE_TCHAR *name)
154 : ACE_Barrier (count, name)
156 // ACE_TRACE ("ACE_Thread_Barrier::ACE_Thread_Barrier");
159 void
160 ACE_Thread_Barrier::dump () const
162 #if defined (ACE_HAS_DUMP)
163 // ACE_TRACE ("ACE_Thread_Barrier::dump");
164 ACE_Barrier::dump ();
165 #endif /* ACE_HAS_DUMP */
168 ACE_END_VERSIONED_NAMESPACE_DECL
170 #endif /* ACE_HAS_THREADS */