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