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
40 #include <anna/core/functions.hpp>
41 #include <anna/core/tracing/TraceMethod.hpp>
42 #include <anna/core/tracing/TraceFunction.hpp>
43 #include <anna/core/tracing/Logger.hpp>
44 #include <anna/core/mt/Runnable.hpp>
45 #include <anna/core/mt/Thread.hpp>
46 #include <anna/core/mt/ThreadManager.hpp>
54 if(isRunning() == true) {
56 string msg(asString());
57 msg += " | Destroying active thread";
58 Logger::warning(msg, ANNA_FILE_LOCATION);
60 // pthread_cancel (a_id);
61 a_id = (pthread_t) - 1;
67 void Thread::start(Runnable& runnable)
68 throw(RuntimeException) {
69 if(isRunning() == true) {
70 std::string msg(asString());
71 msg += " | Already activated";
72 throw RuntimeException(msg, ANNA_FILE_LOCATION);
76 a_data.runnable = &runnable;
79 pthread_attr_init(&attr);
81 if(isJoinable() == false)
82 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
86 if((errorCode = pthread_create(&a_id, &attr, Thread::exec, &a_data)) != 0) {
87 if(a_data.thread->a_manager != NULL)
88 a_data.thread->a_manager->releaseThread(a_data.thread);
90 throw RuntimeException(std::string("Thread::start"), errorCode, ANNA_FILE_LOCATION);
99 throw(RuntimeException) {
102 const pthread_t self(pthread_self());
104 if(pthread_equal(a_id, self) != 0) {
105 string msg("Thread::join | ");
107 msg += " | Cannot call join from itself";
108 throw RuntimeException(msg, ANNA_FILE_LOCATION);
111 if((errorCode = pthread_join(a_id, NULL)) != 0)
112 throw RuntimeException(std::string("Thread::join"), errorCode, ANNA_FILE_LOCATION);
117 /* Código correspondiente al thread */
118 void* Thread::exec(void* arg) {
122 pthread_sigmask(SIG_SETMASK, &mask, NULL);
124 Data* data = reinterpret_cast <Data*>(arg);
126 string msg("Starting thread | ");
127 msg += data->thread->asString();
128 msg += data->runnable->asString();
129 Logger::debug(msg, ANNA_FILE_LOCATION);
133 data->runnable->initialize();
134 data->runnable->setIsRunning(true);
135 data->runnable->run();
136 data->runnable->setIsRunning(false);
138 string msg("Finishing thread | ");
139 msg += data->thread->asString();
140 msg += data->runnable->asString();
141 Logger::debug(msg, ANNA_FILE_LOCATION);
143 data->runnable->terminate();
144 } catch(Exception& ex) {
145 data->runnable->setIsRunning(false);
147 string msg("Finishing thread | ");
148 msg += data->thread->asString();
149 msg += data->runnable->asString();
150 Logger::debug(msg, ANNA_FILE_LOCATION);
152 data->runnable->terminate();
156 data->thread->a_id = (pthread_t) - 1;
158 if(data->thread->a_manager != NULL)
159 data->thread->a_manager->releaseThread(data->thread);
164 std::string Thread::asString() const
166 std::string result("anna::Thread { Id: ");
167 result += functions::asHexString((int) a_id);
168 result += functions::asString(" | Flags (0x%x): ", a_flags);
169 result += (isJoinable()) ? "Joinable" : "None";
170 result += " | Running: ";
171 result += functions::asString(isRunning());
172 result += " | ThreadManager: ";
174 if(a_manager == NULL)
177 result += a_manager->getName();
179 return result += " }";