Fix local server for multiple applications
[anna.git] / source / core / mt / Thread.cpp
1 // ANNA - Anna is Not Nothingness Anymore                                                         //
2 //                                                                                                //
3 // (c) Copyright 2005-2015 Eduardo Ramos Testillano & Francisco Ruiz Rayo                         //
4 //                                                                                                //
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 //
7
8
9 #include <unistd.h>
10 #include <signal.h>
11
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>
19
20 using namespace std;
21 using namespace anna;
22
23 Thread::~Thread() {
24 #ifdef _MT
25
26   if(isRunning() == true) {
27     LOGWARNING(
28       string msg(asString());
29       msg += " | Destroying active thread";
30       Logger::warning(msg, ANNA_FILE_LOCATION);
31     );
32 //      pthread_cancel (a_id);
33     a_id = (pthread_t) - 1;
34   }
35
36 #endif
37 }
38
39 void Thread::start(Runnable& runnable)
40 noexcept(false) {
41   if(isRunning() == true) {
42     std::string msg(asString());
43     msg += " | Already activated";
44     throw RuntimeException(msg, ANNA_FILE_LOCATION);
45   }
46
47   a_data.thread = this;
48   a_data.runnable = &runnable;
49 #ifdef _MT
50   pthread_attr_t attr;
51   pthread_attr_init(&attr);
52
53   if(isJoinable() == false)
54     pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
55
56   int errorCode;
57
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);
61
62     throw RuntimeException(std::string("Thread::start"), errorCode, ANNA_FILE_LOCATION);
63   }
64
65 #else
66   exec(&a_data);
67 #endif
68 }
69
70 void Thread::join()
71 noexcept(false) {
72 #ifdef _MT
73   int errorCode;
74   const pthread_t self(pthread_self());
75
76   if(pthread_equal(a_id, self) != 0) {
77     string msg("Thread::join | ");
78     msg += asString();
79     msg += " | Cannot call join from itself";
80     throw RuntimeException(msg, ANNA_FILE_LOCATION);
81   }
82
83   if((errorCode = pthread_join(a_id, NULL)) != 0)
84     throw RuntimeException(std::string("Thread::join"), errorCode, ANNA_FILE_LOCATION);
85
86 #endif
87 }
88
89 /* Código correspondiente al thread */
90 void* Thread::exec(void* arg) {
91   WHEN_MULTITHREAD(
92     sigset_t mask;
93     sigemptyset(&mask);
94     pthread_sigmask(SIG_SETMASK, &mask, NULL);
95   );
96   Data* data = reinterpret_cast <Data*>(arg);
97   LOGDEBUG(
98     string msg("Starting thread | ");
99     msg += data->thread->asString();
100     msg += data->runnable->asString();
101     Logger::debug(msg, ANNA_FILE_LOCATION);
102   );
103
104   try {
105     data->runnable->initialize();
106     data->runnable->setIsRunning(true);
107     data->runnable->run();
108     data->runnable->setIsRunning(false);
109     LOGDEBUG(
110       string msg("Finishing thread | ");
111       msg += data->thread->asString();
112       msg += data->runnable->asString();
113       Logger::debug(msg, ANNA_FILE_LOCATION);
114     );
115     data->runnable->terminate();
116   } catch(Exception& ex) {
117     data->runnable->setIsRunning(false);
118     LOGDEBUG(
119       string msg("Finishing thread | ");
120       msg += data->thread->asString();
121       msg += data->runnable->asString();
122       Logger::debug(msg, ANNA_FILE_LOCATION);
123     );
124     data->runnable->terminate();
125     ex.trace();
126   }
127
128   data->thread->a_id = (pthread_t) - 1;
129
130   if(data->thread->a_manager != NULL)
131     data->thread->a_manager->releaseThread(data->thread);
132
133   return NULL;
134 }
135
136 std::string Thread::asString() const
137 {
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: ";
145
146   if(a_manager == NULL)
147     result += "<none>";
148   else
149     result += a_manager->getName();
150
151   return result += " }";
152 }
153