1 // ANNA - Anna is Not Nothingness Anymore //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo //
5 // See project site at http://redmine.teslayout.com/projects/anna-suite //
6 // See accompanying file LICENSE or copy at http://www.teslayout.com/projects/public/anna.LICENSE //
12 #include <anna/core/functions.hpp>
13 #include <anna/core/tracing/TraceMethod.hpp>
14 #include <anna/core/tracing/TraceFunction.hpp>
15 #include <anna/core/tracing/Logger.hpp>
16 #include <anna/core/mt/Runnable.hpp>
17 #include <anna/core/mt/Thread.hpp>
18 #include <anna/core/mt/ThreadManager.hpp>
26 if(isRunning() == true) {
28 string msg(asString());
29 msg += " | Destroying active thread";
30 Logger::warning(msg, ANNA_FILE_LOCATION);
32 // pthread_cancel (a_id);
33 a_id = (pthread_t) - 1;
39 void Thread::start(Runnable& runnable)
41 if(isRunning() == true) {
42 std::string msg(asString());
43 msg += " | Already activated";
44 throw RuntimeException(msg, ANNA_FILE_LOCATION);
48 a_data.runnable = &runnable;
51 pthread_attr_init(&attr);
53 if(isJoinable() == false)
54 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
58 if((errorCode = pthread_create(&a_id, &attr, Thread::exec, &a_data)) != 0) {
59 if(a_data.thread->a_manager != NULL)
60 a_data.thread->a_manager->releaseThread(a_data.thread);
62 throw RuntimeException(std::string("Thread::start"), errorCode, ANNA_FILE_LOCATION);
74 const pthread_t self(pthread_self());
76 if(pthread_equal(a_id, self) != 0) {
77 string msg("Thread::join | ");
79 msg += " | Cannot call join from itself";
80 throw RuntimeException(msg, ANNA_FILE_LOCATION);
83 if((errorCode = pthread_join(a_id, NULL)) != 0)
84 throw RuntimeException(std::string("Thread::join"), errorCode, ANNA_FILE_LOCATION);
89 /* Código correspondiente al thread */
90 void* Thread::exec(void* arg) {
94 pthread_sigmask(SIG_SETMASK, &mask, NULL);
96 Data* data = reinterpret_cast <Data*>(arg);
98 string msg("Starting thread | ");
99 msg += data->thread->asString();
100 msg += data->runnable->asString();
101 Logger::debug(msg, ANNA_FILE_LOCATION);
105 data->runnable->initialize();
106 data->runnable->setIsRunning(true);
107 data->runnable->run();
108 data->runnable->setIsRunning(false);
110 string msg("Finishing thread | ");
111 msg += data->thread->asString();
112 msg += data->runnable->asString();
113 Logger::debug(msg, ANNA_FILE_LOCATION);
115 data->runnable->terminate();
116 } catch(Exception& ex) {
117 data->runnable->setIsRunning(false);
119 string msg("Finishing thread | ");
120 msg += data->thread->asString();
121 msg += data->runnable->asString();
122 Logger::debug(msg, ANNA_FILE_LOCATION);
124 data->runnable->terminate();
128 data->thread->a_id = (pthread_t) - 1;
130 if(data->thread->a_manager != NULL)
131 data->thread->a_manager->releaseThread(data->thread);
136 std::string Thread::asString() const
138 std::string result("anna::Thread { Id: ");
139 result += functions::asHexString((int) a_id);
140 result += functions::asString(" | Flags (0x%x): ", a_flags);
141 result += (isJoinable()) ? "Joinable" : "None";
142 result += " | Running: ";
143 result += functions::asString(isRunning());
144 result += " | ThreadManager: ";
146 if(a_manager == NULL)
149 result += a_manager->getName();
151 return result += " }";