ATLAS Offline Software
ONCRPCThreadCollection.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2017 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 
7 
8 namespace JiveXML{
9 
10  //Constructor
12 
13  //intialize the mutex with default attributes
14  pthread_mutex_init(&m_mutex, NULL);
15 
16  //initialize the semaphore
17  sem_init(&m_semaphore,0,0);
18 
19  }
20 
21  //Constructor
23 
24  //destroy the mutex
25  pthread_mutex_destroy(&m_mutex);
26 
27  //and the semaphore
28  sem_destroy(&m_semaphore);
29 
30  }
31 
32  //Add a thread to the container - don't need to be too strict
33  //If a thread is double added, joinAll will still remove it.
34  void ThreadCollection::AddThread( const pthread_t& thread ){
35 
36  //First get a mutex //RETVAL for all of them!
37  pthread_mutex_lock(&m_mutex);
38 
39  //Now add the thread to the list of vectors
40  push_back(thread);
41 
42  //Then remove mutex again
43  pthread_mutex_unlock(&m_mutex);
44 
45  //And signal any potentially waiting threads
46  sem_post(&m_semaphore);
47 
48  }
49 
50  //Wait until a thread has been added
52 
53  //simply wait for the access semaphore to be set
54  sem_wait(&m_semaphore);
55  }
56 
57  //Remove a thread
58  void ThreadCollection::RemoveThread( const pthread_t& thread ){
59 
60  //First get a mutex
61  pthread_mutex_lock(&m_mutex);
62 
63  //Loop over list and find that entry
64  ThreadCollection::iterator threadItr = begin();
65  while ( threadItr != end() ){
66  //See if this is the thread we are looking for
67  if ( *threadItr == thread ){
68  //remove it from the collection
69  erase(threadItr);
70  //iterator is invalid, we removed thread, so stop looping
71  break ;
72  }
73  //Go to next thread
74  ++threadItr;
75  }
76 
77  //Then remove mutex again
78  pthread_mutex_unlock(&m_mutex);
79 
80  //Set this threads state to detached, so its
81  //resources are reclaimed once it
82  //finishes.
83  pthread_detach(thread);
84 
85  }
86 
87  //Wait for all threads to finish
89 
90  //The threads are removing themselves from the list,
91  //so iterators are getting invalid while we are looping.
92  //However, some threads may have crashed w/o being able to remove themselves
93  //So wait for all of them to finish w/o keeping the mutex locked while
94  //waiting
95 
96  //Loop till all threads are gone
97  while ( size() > 0 ){
98 
99  //First get a mutex //RETVAL for all of them!
100  pthread_mutex_lock(&m_mutex);
101 
102  //Order is not important - take the first element
103  pthread_t thread = *begin();
104 
105  //Then remove mutex again
106  pthread_mutex_unlock(&m_mutex);
107 
108  //Wait for that thread to finish
109  pthread_join(thread,NULL);
110 
111  //Now remove it - if it has already removed itself, nothing will happen
112  //If it hadn't removed itself, we will remove it
113  RemoveThread(thread);
114  }
115  }
116 
117 
118  //Return number of elements in the vector
120 
121  //First get a mutex
122  pthread_mutex_lock(&m_mutex);
123 
124  //Get number of elements
125  int NThreads = size();
126 
127  //Then remove mutex again
128  pthread_mutex_unlock(&m_mutex);
129 
130  //finally return size
131  return NThreads;
132  }
133 
134 }//namespace
xAOD::iterator
JetConstituentVector::iterator iterator
Definition: JetConstituentVector.cxx:68
ONCRPCThreadCollection.h
PlotCalibFromCool.begin
begin
Definition: PlotCalibFromCool.py:94
JiveXML::ThreadCollection::ThreadCollection
ThreadCollection()
Definition: ONCRPCThreadCollection.cxx:11
JiveXML::ThreadCollection::NumberOfThreads
int NumberOfThreads()
Definition: ONCRPCThreadCollection.cxx:119
mergePhysValFiles.end
end
Definition: DataQuality/DataQualityUtils/scripts/mergePhysValFiles.py:93
JiveXML::ThreadCollection::AddThread
void AddThread(const pthread_t &thread)
Definition: ONCRPCThreadCollection.cxx:34
JiveXML::ThreadCollection::m_mutex
pthread_mutex_t m_mutex
Definition: ONCRPCThreadCollection.h:48
python.setupRTTAlg.size
int size
Definition: setupRTTAlg.py:39
JiveXML::ThreadCollection::JoinAll
void JoinAll()
Definition: ONCRPCThreadCollection.cxx:88
JiveXML::ThreadCollection::~ThreadCollection
~ThreadCollection()
Definition: ONCRPCThreadCollection.cxx:22
JiveXML
This header is shared inbetween the C-style server thread and the C++ Athena ServerSvc.
Definition: BadLArRetriever.cxx:21
JiveXML::ThreadCollection::WaitAdd
void WaitAdd()
Definition: ONCRPCThreadCollection.cxx:51
JiveXML::ThreadCollection::m_semaphore
sem_t m_semaphore
Definition: ONCRPCThreadCollection.h:50
JiveXML::ThreadCollection::RemoveThread
void RemoveThread(const pthread_t &thread)
Definition: ONCRPCThreadCollection.cxx:58