| 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 (2→3) taken 12 times.
✓ Branch 1 (2→8) taken 4 times.
✓ Branch 2 (5→6) taken 12 times.
|
40 | if(exitStatus == PEXIT_STATUS_NOT_FIHISHED){return "\033[33mStill Running\033[0m";} |
| 15 |
3/3✓ Branch 0 (8→9) taken 2 times.
✓ Branch 1 (8→14) taken 2 times.
✓ Branch 2 (11→12) taken 2 times.
|
8 | else if(exitStatus == PEXIT_STATUS_OK){return "\033[32mSUCCESS\033[0m";} |
| 16 | else{ | ||
| 17 |
1/1✓ Branch 0 (16→17) taken 2 times.
|
4 | 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 0 (21→7) taken 12 times.
✓ Branch 1 (21→22) taken 3 times.
|
15 | for(ListThreadDetachInfo::const_iterator it(listThreadInfo.begin()); it != listThreadInfo.end(); ++it){ |
| 29 |
8/8✓ Branch 0 (7→8) taken 12 times.
✓ Branch 2 (8→9) taken 12 times.
✓ Branch 4 (9→10) taken 12 times.
✓ Branch 6 (11→12) taken 12 times.
✓ Branch 8 (12→13) taken 12 times.
✓ Branch 10 (14→15) taken 12 times.
✓ Branch 12 (15→16) taken 12 times.
✓ Branch 14 (16→17) 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 0 (5→6) 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 0 (5→6) taken 1 times.
|
1 | copyDetachPool(other); |
| 44 | 1 | } | |
| 45 | |||
| 46 | ///Destructor of DetachPool | ||
| 47 | 6 | DetachPool::~DetachPool(){ | |
| 48 | 6 | waitUntilAllFinish(p_destructorWaitTime); | |
| 49 | 6 | } | |
| 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 0 (3→4) not taken.
✓ Branch 1 (3→5) taken 2 times.
|
2 | if(p_listThreadInfo.size() == 0lu){return;} |
| 103 |
1/1✓ Branch 0 (5→6) taken 2 times.
|
2 | p_mtx.lock(); |
| 104 | 2 | std::list<ThreadDetachInfo>::iterator it(p_listThreadInfo.begin()); | |
| 105 |
2/2✓ Branch 0 (30→8) taken 12 times.
✓ Branch 1 (30→31) taken 2 times.
|
14 | while(it != p_listThreadInfo.end()){ |
| 106 |
1/2✗ Branch 0 (9→10) not taken.
✓ Branch 1 (9→26) 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 (3→4) taken 10 times.
✗ Branch 1 (3→5) 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 0 (3→4) taken 5 times.
✓ Branch 1 (3→5) taken 4 times.
|
9 | if(p_listThreadInfo.size() == 0lu){return;} |
| 133 | |||
| 134 |
1/1✓ Branch 0 (5→6) taken 4 times.
|
4 | PhoenixTime startWaitingUnitThreadEnd = phoenix_thread_clock(); |
| 135 | |||
| 136 |
6/7✓ Branch 0 (10→11) taken 6 times.
✓ Branch 2 (11→12) taken 2 times.
✓ Branch 3 (11→15) taken 4 times.
✓ Branch 4 (13→14) taken 2 times.
✗ Branch 5 (13→15) not taken.
✓ Branch 6 (16→7) taken 2 times.
✓ Branch 7 (16→17) taken 4 times.
|
6 | while(phoenix_ellapsedTime<PNanoSecond>(startWaitingUnitThreadEnd)/1000l < maxWaitTime && p_listThreadInfo.size() != 0lu){ |
| 137 |
1/1✓ Branch 0 (7→8) taken 2 times.
|
2 | removeFinishedThread(); |
| 138 |
1/1✓ Branch 0 (8→9) 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 |