1 // ANNA - Anna is Not Nothingness Anymore
3 // (c) Copyright 2005-2014 Eduardo Ramos Testillano & Francisco Ruiz Rayo
5 // http://redmine.teslayout.com/projects/anna-suite
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions
11 // * Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following disclaimer
15 // in the documentation and/or other materials provided with the
17 // * Neither the name of the copyright holder nor the names of its
18 // contributors may be used to endorse or promote products derived from
19 // this software without specific prior written permission.
21 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 // Authors: eduardo.ramos.testillano@gmail.com
34 // cisco.tierra@gmail.com
41 #include <anna/core/functions.hpp>
43 #include <anna/core/mt/ThreadManager.hpp>
44 #include <anna/core/mt/Semaphore.hpp>
45 #include <anna/core/mt/Thread.hpp>
46 #include <anna/core/tracing/TraceMethod.hpp>
51 anna_assign_enum(ThreadManager::Mode) = { "None", "Unlimit", "ExceptionWhenFull", "LockWhenFull", NULL };
53 ThreadManager::ThreadManager(const char* name, const Mode::_v mode, const int maxSize, const int flags) :
58 a_threadFlags(flags) {
59 if(a_mode == Mode::LockWhenFull)
60 a_semaphore = new Semaphore(0);
63 ThreadManager::ThreadManager(const char* name, const int flags) :
65 a_mode(Mode::Unlimit),
68 a_threadFlags(flags) {
69 if(a_mode == Mode::LockWhenFull)
70 a_semaphore = new Semaphore(0);
73 ThreadManager::~ThreadManager() {
77 // Para que no intenten sacar el nombre del ThreadManager que los creĆ³
78 for(thread_iterator ii = thread_begin(), maxii = thread_end(); ii != maxii; ii ++) {
79 thread(ii)->a_manager = NULL;
83 a_semaphore->signal();
86 } catch(RuntimeException& ex) {
98 Thread* ThreadManager::createThread()
99 throw(RuntimeException) {
102 if(a_mode == Mode::None) {
103 string msg(asString());
104 msg += " | Invalid mode";
105 throw RuntimeException(msg, ANNA_FILE_LOCATION);
108 if(a_mode != Mode::Unlimit && a_maxSize <= 0) {
109 string msg(asString());
110 msg += " | Invalid max thread number";
111 throw RuntimeException(msg, ANNA_FILE_LOCATION);
114 Guard guard(this, "anna::ThreadManager::createThread");
115 const int size = Recycler <Thread>::size();
118 case Mode::ExceptionWhenFull:
120 if(a_maxSize == size) {
121 string msg(asString());
122 msg += " | No available threads";
123 throw RuntimeException(msg, ANNA_FILE_LOCATION);
126 // No hay "break" para que siga procesando
128 result = Recycler <Thread>::create();
130 case Mode::LockWhenFull:
132 if(a_maxSize == size) {
135 string msg(asString());
136 msg += " | Waiting for thread release";
137 Logger::debug(msg, ANNA_FILE_LOCATION);
141 string msg(asString());
142 msg += " | Achieve thread release";
143 Logger::debug(msg, ANNA_FILE_LOCATION);
145 Guard reopen(this, "anna::ThreadManager::createThread (after signal)");
146 result = Recycler <Thread>::create();
148 result = Recycler <Thread>::create();
153 result->a_manager = this;
154 result->setFlags(a_threadFlags);
156 string msg("ThreadManager::createThread | ");
159 msg += result->asString();
160 Logger::debug(msg, ANNA_FILE_LOCATION);
165 void ThreadManager::join()
166 throw(RuntimeException) {
168 string msg("ThreadManager::join (init) | ");
170 Logger::debug(msg, ANNA_FILE_LOCATION);
174 const pthread_t self(pthread_self());
175 pthread_t* threads = new pthread_t [Recycler <Thread>::size()];
178 for(thread_iterator ii = thread_begin(), maxii = thread_end(); ii != maxii; ii ++) {
179 threads [index] = thread(ii)->getId();
181 if(pthread_equal(threads [index], self) != 0) {
182 string msg(asString());
184 thread(ii)->asString();
185 msg += " | Threads owns to this Manager";
187 throw RuntimeException(msg, ANNA_FILE_LOCATION);
196 for(int ii = 0; ii < index; ii ++) {
197 if((errorCode = pthread_join(threads [ii], NULL)) != 0) {
198 string msg(asString());
199 msg += " | Bad join";
200 throw RuntimeException(msg, errorCode, ANNA_FILE_LOCATION);
207 string msg("ThreadManager::join (final) | ");
209 Logger::debug(msg, ANNA_FILE_LOCATION);
213 void ThreadManager::releaseThread(Thread* thread)
214 throw(RuntimeException) {
215 if(thread->isRunning() == true) {
216 string msg(thread->asString());
217 msg += " | Still activated";
218 throw RuntimeException(msg, ANNA_FILE_LOCATION);
221 if(a_mode == Mode::LockWhenFull) {
222 Guard guard(this, "anna::ThreadManager::releaseThread");
223 // No hace falta acceder mediate SafeRecycler porque ya tenemos una SSCC establecida.
224 const int size = Recycler <Thread>::getSize();
225 Recycler <Thread>::release(thread);
227 if(size == a_maxSize)
228 a_semaphore->signal();
230 SafeRecycler <Thread>::release(thread);
233 string msg("ThreadManager::releaseThread | ");
235 Logger::debug(msg, ANNA_FILE_LOCATION);
239 string ThreadManager::asString() const
241 string result("anna::ThreadManager { Name: ");
243 result += " | Mode: ";
244 result += Mode::asCString(a_mode);
245 result += " | MaxSize: ";
248 result += functions::asString(a_maxSize);
250 result += "<unlimited>";
252 result += functions::asText(" | Size: ", Recycler <Thread>::size());
253 return result += " }";