Directory: | ./ |
---|---|
File: | src/DetachPool.cpp |
Date: | 2025-05-16 18:34:22 |
Exec | Total | Coverage | |
---|---|---|---|
Lines: | 65 | 70 | 92.9% |
Branches: | 36 | 56 | 64.3% |
Line | Branch | Exec | Source |
---|---|---|---|
1 | /*************************************** | ||
2 | Auteur : Pierre Aubert | ||
3 | Mail : pierre.aubert@lapp.in2p3.fr | ||
4 | Licence : CeCILL-C | ||
5 | ****************************************/ | ||
6 | |||
7 | #include "DetachPool.h" | ||
8 | |||
9 | ///Convert the current status of a thread into a string | ||
10 | /** @param exitStatus : status of the thread | ||
11 | * @return corresponding string | ||
12 | */ | ||
13 | 16 | std::string phoenix_statusToString(int exitStatus){ | |
14 |
3/3✓ Branch 0 taken 12 times.
✓ Branch 1 taken 4 times.
✓ Branch 4 taken 12 times.
|
16 | if(exitStatus == PEXIT_STATUS_NOT_FIHISHED){return "\033[33mStill Running\033[0m";} |
15 |
3/3✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
✓ Branch 4 taken 2 times.
|
4 | else if(exitStatus == PEXIT_STATUS_OK){return "\033[32mSUCCESS\033[0m";} |
16 | else{ | ||
17 |
1/1✓ Branch 2 taken 2 times.
|
2 | return "\033[31mFAIL\033[0m"; |
18 | } | ||
19 | } | ||
20 | |||
21 | ///Print the status of all remaning thread | ||
22 | /** @param listThreadInfo : list of thread information to be printed | ||
23 | * @param out : ostream to be used to print information | ||
24 | */ | ||
25 | 3 | void phoenix_printThreadStatus(const ListThreadDetachInfo & listThreadInfo, std::ostream & out){ | |
26 | 3 | out << "ListThreadDetachInfo : number of remaning threads : " << listThreadInfo.size() << std::endl; | |
27 | 3 | size_t i(0lu); | |
28 |
2/2✓ Branch 3 taken 12 times.
✓ Branch 4 taken 3 times.
|
15 | for(ListThreadDetachInfo::const_iterator it(listThreadInfo.begin()); it != listThreadInfo.end(); ++it){ |
29 |
8/8✓ Branch 1 taken 12 times.
✓ Branch 4 taken 12 times.
✓ Branch 7 taken 12 times.
✓ Branch 11 taken 12 times.
✓ Branch 14 taken 12 times.
✓ Branch 18 taken 12 times.
✓ Branch 21 taken 12 times.
✓ Branch 24 taken 12 times.
|
12 | out << "\tthread("<<i<<") '"<<it->description<<"' has status " << phoenix_statusToString(it->exitStatus) << std::endl; |
30 | 12 | ++i; | |
31 | } | ||
32 | 3 | } | |
33 | |||
34 | ///Default constructor of DetachPool | ||
35 | 5 | DetachPool::DetachPool(){ | |
36 |
1/1✓ Branch 1 taken 5 times.
|
5 | initialisationDetachPool(); |
37 | 5 | } | |
38 | |||
39 | ///Copy constructor of DetachPool | ||
40 | /** @param other : class to copy | ||
41 | */ | ||
42 | 1 | DetachPool::DetachPool(const DetachPool & other){ | |
43 |
1/1✓ Branch 1 taken 1 times.
|
1 | copyDetachPool(other); |
44 | 1 | } | |
45 | |||
46 | ///Destructor of DetachPool | ||
47 | 12 | DetachPool::~DetachPool(){ | |
48 | 12 | waitUntilAllFinish(p_destructorWaitTime); | |
49 | } | ||
50 | |||
51 | ///Definition of equal operator of DetachPool | ||
52 | /** @param other : class to copy | ||
53 | * @return copied class | ||
54 | */ | ||
55 | 1 | DetachPool & DetachPool::operator = (const DetachPool & other){ | |
56 | 1 | copyDetachPool(other); | |
57 | 1 | return *this; | |
58 | } | ||
59 | |||
60 | ///Set the ellasped time between two refresh in microseconds | ||
61 | /** @param refreshTime : ellasped time between two refresh in microseconds | ||
62 | */ | ||
63 | 1 | void DetachPool::setRefreshTime(PEllapsedTime refreshTime){ | |
64 | 1 | p_refreshTime = refreshTime; | |
65 | 1 | } | |
66 | |||
67 | ///Get the ellasped time between two refresh in microseconds | ||
68 | /** @return ellasped time between two refresh in microseconds | ||
69 | */ | ||
70 | 4 | PEllapsedTime DetachPool::getRefreshTime() const{ | |
71 | 4 | return p_refreshTime; | |
72 | } | ||
73 | |||
74 | ///Set the wait time on object destruction (in microseconds) | ||
75 | /** @param destructorWaitTime : wait time on object destruction (in microseconds) | ||
76 | */ | ||
77 | 1 | void DetachPool::setDestructionWaitTime(PEllapsedTime destructorWaitTime){ | |
78 | 1 | p_destructorWaitTime = destructorWaitTime; | |
79 | 1 | } | |
80 | |||
81 | ///Get the wait time on object destruction (in microseconds) | ||
82 | /** @return wait time on object destruction (in microseconds) | ||
83 | */ | ||
84 | 4 | PEllapsedTime DetachPool::getDestructorWaitTime() const{ | |
85 | 4 | return p_destructorWaitTime; | |
86 | } | ||
87 | |||
88 | ///Get the list of thread information | ||
89 | /** @return list of thread information | ||
90 | */ | ||
91 | 3 | ListThreadDetachInfo DetachPool::getListThreadInfo(){ | |
92 | 3 | p_mtx.lock(); | |
93 | 3 | ListThreadDetachInfo tmp = p_listThreadInfo; | |
94 | 3 | p_mtx.unlock(); | |
95 | 3 | return tmp; | |
96 | } | ||
97 | |||
98 | ///Remove all finished threads | ||
99 | /** @param[out] out : ostream to be used to print when a thread finishes | ||
100 | */ | ||
101 | 2 | void DetachPool::removeFinishedThread(std::ostream & out){ | |
102 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if(p_listThreadInfo.size() == 0lu){return;} |
103 |
1/1✓ Branch 1 taken 2 times.
|
2 | p_mtx.lock(); |
104 | 2 | std::list<ThreadDetachInfo>::iterator it(p_listThreadInfo.begin()); | |
105 |
2/2✓ Branch 2 taken 12 times.
✓ Branch 3 taken 2 times.
|
14 | while(it != p_listThreadInfo.end()){ |
106 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if(it->exitStatus != PEXIT_STATUS_NOT_FIHISHED){ //Thread is finished (in good or bad), so we can remove its informations |
107 | ✗ | if(it->description != ""){ | |
108 | ✗ | out << "DetachPool : thread '"<<it->description<<"' finished with status " << phoenix_statusToString(it->exitStatus) << std::endl; | |
109 | } | ||
110 | ✗ | it = p_listThreadInfo.erase(it); | |
111 | }else{ | ||
112 | 12 | ++it; | |
113 | } | ||
114 | } | ||
115 | 2 | p_mtx.unlock(); | |
116 | } | ||
117 | |||
118 | ///Refresh the list of the detached threads | ||
119 | 10 | void DetachPool::refresh(){ | |
120 | 10 | PEllapsedTime ellapsedTimeRefresh = phoenix_ellapsedTime<PNanoSecond>(p_timeLastRefresh)/1000l; | |
121 |
1/2✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
|
10 | if(ellapsedTimeRefresh < p_refreshTime){ |
122 | 10 | return; //Not time yet | |
123 | } | ||
124 | ✗ | removeFinishedThread(); | |
125 | ✗ | p_timeLastRefresh = phoenix_thread_clock(); | |
126 | } | ||
127 | |||
128 | ///wait until all detached thread have fnished theirs computation | ||
129 | /** @param maxWaitTime : maximum waiting time until we kill every remaning threads (in micro second, wait 10 seconds by default) | ||
130 | */ | ||
131 | 9 | void DetachPool::waitUntilAllFinish(PEllapsedTime maxWaitTime){ | |
132 |
2/2✓ Branch 1 taken 5 times.
✓ Branch 2 taken 4 times.
|
9 | if(p_listThreadInfo.size() == 0lu){return;} |
133 | |||
134 |
1/1✓ Branch 1 taken 4 times.
|
4 | PhoenixTime startWaitingUnitThreadEnd = phoenix_thread_clock(); |
135 | |||
136 |
6/7✓ Branch 1 taken 6 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 4 times.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 2 times.
✓ Branch 9 taken 4 times.
|
6 | while(phoenix_ellapsedTime<PNanoSecond>(startWaitingUnitThreadEnd)/1000l < maxWaitTime && p_listThreadInfo.size() != 0lu){ |
137 |
1/1✓ Branch 1 taken 2 times.
|
2 | removeFinishedThread(); |
138 |
1/1✓ Branch 1 taken 2 times.
|
2 | usleep(p_refreshTime); //Let's wait a bit until next check |
139 | } | ||
140 | } | ||
141 | |||
142 | ///Copy function of DetachPool | ||
143 | /** @param other : class to copy | ||
144 | */ | ||
145 | 2 | void DetachPool::copyDetachPool(const DetachPool & other){ | |
146 | 2 | p_refreshTime = other.p_refreshTime; | |
147 | 2 | p_timeLastRefresh = other.p_timeLastRefresh; | |
148 | 2 | p_destructorWaitTime = other.p_destructorWaitTime; | |
149 | 2 | } | |
150 | |||
151 | ///Initialisation function of the class DetachPool | ||
152 | 5 | void DetachPool::initialisationDetachPool(){ | |
153 | 5 | p_refreshTime = 1000000lu; //Refresh every second by default | |
154 | 5 | p_timeLastRefresh = phoenix_thread_clock(); | |
155 | 5 | p_destructorWaitTime = 0l; | |
156 | 5 | } | |
157 | |||
158 | |||
159 | |||
160 |