All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
DBEnvironmentQueue.cpp
Go to the documentation of this file.
1 /*
2  * DBEnvironmentQueue.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
23 
24 #include <unistd.h>
25 
26 namespace HSEPData {
27 
29  aFree.clear();
30  aUsed.clear();
31  } // DBEnvironmentQueue constructor
32 
34 
36 
37  ExclusiveScope vScope(&aMutex);
38 
39  for (vIter = aFree.begin(); vIter != aFree.end(); vIter++) {
40  delete (*vIter);
41  }
42 
43  for (vIter = aUsed.begin(); vIter != aUsed.end(); vIter++) {
44  delete (*vIter);
45  }
46 
47 
48  } // DBEnvironmentQueue destructor
49 
51 
52  DBEnvironment* vResult = nullptr;
53  bool vEndLoop = false;
54 
55  while (!vEndLoop) {
56 
57  {
58  ExclusiveScope vScope(&aMutex);
59 
60  if (aFree.size() > 0) {
61  vResult = aFree.front();
62  aFree.pop_front();
63  aUsed.push_back(vResult);
64  vEndLoop = true;
65  }
66  }
67 
68  if (!vEndLoop) {
69  usleep(10);
70  // TODO: C++11 sleeping thread functions are not ready in this moment ... when ready, update this.
71  }
72 
73  } // while (!vEndLoop)
74 
75  return vResult;
76 
77  } // DBEnvironmentQueue::getEnvironment
78 
80  ExclusiveScope vScope(&aMutex);
81  aUsed.remove (pEnvironmentPtr);
82  aFree.push_back(pEnvironmentPtr);
83  } // DBEnvironmentQueue::releaseEnvironment
84 
86  ExclusiveScope vScope(&aMutex);
87  aFree.push_back(pEnvironmentPtr);
88  } // DBEnvironmentQueue::addEnvironment
89 
90 } // HSEPData namespace
91 
92 
93