@@ -36,35 +36,37 @@ ThreadMgr::~ThreadMgr()
3636
3737void ThreadMgr::Reset (const int nThreads)
3838{
39- if (nThreads > numRealThreads)
39+ const unsigned n = static_cast <unsigned >(nThreads);
40+ if (n > numRealThreads)
4041 {
41- realThreads.resize (nThreads );
42- for (int t = numRealThreads; t < nThreads ; t++)
42+ realThreads.resize (n );
43+ for (unsigned t = numRealThreads; t < n ; t++)
4344 realThreads[t] = false ;
44- numRealThreads = nThreads ;
45+ numRealThreads = n ;
4546 }
4647
47- if (nThreads > numMachineThreads)
48+ if (n > numMachineThreads)
4849 {
49- machineThreads.resize (nThreads );
50- for (int t = numMachineThreads; t < nThreads ; t++)
50+ machineThreads.resize (n );
51+ for (unsigned t = numMachineThreads; t < n ; t++)
5152 machineThreads[t] = -1 ;
52- numMachineThreads = nThreads ;
53+ numMachineThreads = n ;
5354 }
5455}
5556
5657
5758int ThreadMgr::Occupy (const int machineId)
5859{
59- if (machineId >= numMachineThreads)
60+ const unsigned m = static_cast <unsigned >(machineId);
61+ if (m >= numMachineThreads)
6062 {
61- numMachineThreads = machineId + 1 ;
63+ numMachineThreads = m + 1 ;
6264 machineThreads.resize (numMachineThreads);
63- for (int t = machineId ; t < numMachineThreads; t++)
65+ for (unsigned t = m ; t < numMachineThreads; t++)
6466 machineThreads[t] = -1 ;
6567 }
6668
67- if (machineThreads[machineId ] != -1 )
69+ if (machineThreads[m ] != -1 )
6870 {
6971 // Error: Already in use.
7072 return -1 ;
@@ -75,13 +77,14 @@ int ThreadMgr::Occupy(const int machineId)
7577 do
7678 {
7779 mtx.lock ();
78- for (int t = 0 ; t < numRealThreads; t++)
80+ for (unsigned t = 0 ; t < numRealThreads; t++)
7981 {
8082 if (realThreads[t] == false )
8183 {
84+ const int ti = static_cast <int >(t);
8285 realThreads[t] = true ;
83- machineThreads[machineId ] = t ;
84- res = t ;
86+ machineThreads[m ] = ti ;
87+ res = ti ;
8588 break ;
8689 }
8790 }
@@ -102,24 +105,32 @@ int ThreadMgr::Occupy(const int machineId)
102105
103106bool ThreadMgr::Release (const int machineId)
104107{
105- const int r = machineThreads[machineId];
108+ mtx.lock ();
109+
110+ bool ret;
111+ const unsigned m = static_cast <unsigned >(machineId);
112+ const int r = machineThreads[m];
113+ const unsigned ru = static_cast <unsigned >(r);
114+
106115 if (r == -1 )
107116 {
108117 // Error: Not in use.
109- return false ;
118+ ret = false ;
110119 }
111-
112- if (! realThreads[r])
120+ else if (! realThreads[ru])
113121 {
114122 // Error: Refers to a real thread that is not in use.
115- return false ;
123+ ret = false ;
124+ }
125+ else
126+ {
127+ realThreads[ru] = false ;
128+ machineThreads[m] = -1 ;
129+ ret = true ;
116130 }
117131
118- mtx.lock ();
119- realThreads[r] = false ;
120- machineThreads[machineId] = -1 ;
121132 mtx.unlock ();
122- return true ;
133+ return ret ;
123134}
124135
125136
@@ -133,15 +144,15 @@ void ThreadMgr::Print(
133144
134145 fo << tag <<
135146 " : Real threads occupied (out of " << numRealThreads << " ):\n " ;
136- for (int t = 0 ; t < numRealThreads; t++)
147+ for (unsigned t = 0 ; t < numRealThreads; t++)
137148 {
138149 if (realThreads[t])
139150 fo << t << endl;
140151 }
141152 fo << endl;
142153
143154 fo << " Machine threads overview:\n " ;
144- for (int t = 0 ; t < numMachineThreads; t++)
155+ for (unsigned t = 0 ; t < numMachineThreads; t++)
145156 {
146157 if (machineThreads[t] != -1 )
147158 {
0 commit comments