KWStyle - itkBarrier.cxx
 
Matrix View
Description

1 /*=========================================================================
2
3   Program:   Insight Segmentation & Registration Toolkit
4   Module:    $RCSfile: itkBarrier.cxx.html,v $
5   Language:  C++
6   Date:      $Date: 2006/01/17 19:15:33 $
7   Version:   $Revision: 1.4 $
8
9   Copyright (c) Insight Software Consortium. All rights reserved.
10   See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11
12      This software is distributed WITHOUT ANY WARRANTY; without even 
13      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
14      PURPOSE.  See the above copyright notices for more information.
15
16 =========================================================================*/
17 DEF #ifndef __itkBarrier_cxx_
18 DEF #define __itkBarrier_cxx_
19
20 #include "itkBarrier.h"
21 #include "itkMacro.h"
22
23 #ifdef ITK_USE_SPROC
24 #include "itkMultiThreader.h"
25 #endif
26
27 namespace itk {
28
29 #ifdef ITK_USE_FETCHOP_BARRIERS
30 atomic_reservoir_t Barrier::m_Reservoir = 0;
31 bool Barrier::m_ReservoirInitialized = false;
32 int Barrier::m_MaxBarriers = 1024;
33 #endif
34
35 Barrier::Barrier()
36 {
37   m_NumberExpected = 0;
38 #ifndef ITK_USE_SPROC
39 #ifndef ITK_USE_FETCHOP_BARRIERS
40   m_NumberArrived  = 0;
41   m_ConditionVariable = ConditionVariable::New();
42 #endif
43 #endif
44 }
45
46 Barrier::~Barrier()
47 {
48 #if defined ITK_USE_FETCHOP_BARRIERS
49   if (m_Pvar !=0 && Barrier::m_Reservoir != 0 )
50     {
51     atomic_free_variable(Barrier::m_Reservoir, m_Pvar);
52     m_Pvar = 0;
53     }
54
55   if (Barrier::m_Reservoir != 0)
56     {
57     atomic_free_reservoir(Barrier::m_Reservoir);
58     Barrier::m_Reservoir = 0;
59     }
60 #elif defined ITK_USE_SPROC
61   if (m_Barrier != 0)
62     {
63     // Note: free_barrier should be called here
64     // but is buggy and causes a seg fault. jc 7/29/03
65     
66     //    free_barrier( m_Barrier );
67     }
68 #endif
69 }
70
71 void Barrier::Initialize( unsigned int n )
72 {
73   m_NumberExpected = n;
74
75 #if defined ITK_USE_FETCHOP_BARRIERS
76   // Create the reservoir.
77   if (Barrier::m_ReservoirInitialized == false)
78     {
79 LEN     Barrier::m_Reservoir = atomic_alloc_reservoir(USE_DEFAULT_PM, m_MaxBarriers, 0);
80     if (Barrier::m_Reservoir != 0)
81       {
82       Barrier::m_ReservoirInitialized = true;
83       }
84     else
85       {
86       itkExceptionMacro( << "atomic_alloc_reservoir call failed!" );
87       }
88     }
89
90   m_FetchopFlag = 0;
91   m_Pvar = atomic_alloc_variable(Barrier::m_Reservoir, 0);
92   storeop_store(m_Pvar, 0);
93 #elif defined ITK_USE_SPROC
94   if (MultiThreader::GetInitialized() == false)
95     {
96     MultiThreader::Initialize();
97     }
98   m_Barrier = new_barrier( MultiThreader::GetThreadArena() );
99 #endif  
100 }
101
102 void Barrier::Wait()
103 {
104 #if defined ITK_USE_FETCHOP_BARRIERS
105   int gen = m_FetchopFlag;
106   atomic_var_t val = atomic_fetch_and_increment(m_Pvar);
107   if (val == m_NumberExpected - 1)
108     {
109     storeop_store(m_Pvar, 0);
110     m_FetchopFlag++;
111     }
112   while (m_FetchopFlag == gen)
113     { // spin
114     }
115 #elif defined ITK_USE_SPROC
116   barrier(m_Barrier, m_NumberExpected);
117 #else
118   m_Mutex.Lock();
119   m_NumberArrived++;
120   if ( m_NumberArrived == m_NumberExpected )
121     {
122     // Clear all blocked threads
123     m_NumberArrived = 0;
124     m_ConditionVariable->Broadcast();
125     }
126   else
127     {
128     // Block this thread
129     m_ConditionVariable->Wait( &m_Mutex );
130     }
131   m_Mutex.Unlock();
132 #endif
133 }
134
135 }// end namespace itk
136
137 #endif
138

Generated by KWStyle 1.0b on Tuesday January,17 at 02:13:59PM
© Kitware Inc.