Remove dynamic exceptions
[anna.git] / source / core / mt / Guard.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 <anna/core/mt/Guard.hpp>
10 #include <anna/core/mt/Safe.hpp>
11 #include <anna/core/functions.hpp>
12 #include <anna/core/tracing/Logger.hpp>
13
14 using namespace std;
15 using namespace anna;
16
17 #define trace_local7(method,object,file,line) \
18    if (Logger::isActive (Logger::Local7)) { \
19       string msg = (method); \
20       msg += " | Reference: "; \
21       if (a_whatis == NULL)  \
22          msg += functions::asHexString (anna_ptrnumber_cast ((object))); \
23       else { \
24          msg += "("; \
25          msg += functions::asHexString (anna_ptrnumber_cast ((object))); \
26          msg += "): "; \
27          msg += a_whatis; \
28       } \
29       Logger::write (Logger::Local7, msg, (file), (line)); \
30    }
31
32 Guard::Guard(const Safe* object) noexcept(false) :
33   a_whatis(NULL) {
34   if(object == NULL)
35     throw RuntimeException("Can not lock NULL object", ANNA_FILE_LOCATION);
36
37   lock(a_safeObject = const_cast <Safe*>(object), NULL);
38 }
39
40 Guard::Guard(const Safe* object, const char* whatis) noexcept(false) :
41   a_whatis(whatis) {
42   if(object == NULL)
43     throw RuntimeException("Can not lock NULL object", ANNA_FILE_LOCATION);
44
45   lock(a_safeObject = const_cast <Safe*>(object), whatis);
46 }
47
48 void Guard::deactivate()
49 {
50   if(a_safeObject == NULL)
51     return;
52
53   a_safeObject->unlock();
54   trace_local7("Guard::deactivate", a_safeObject, __FILE__, __LINE__);
55   a_safeObject = NULL;
56 }
57
58 //--------------------------------------------------------------------------------------------------------------
59 // Solo sacmos trazas cuando nos mandan un 'whatis', de otro quedamos un bucle infinito porque el
60 // Logger::writer tambien usa una Guard.
61 //--------------------------------------------------------------------------------------------------------------
62 void Guard::lock(Safe* safe, const char* whatis)
63 noexcept(false) {
64   trace_local7("Guard::lock", safe, __FILE__, __LINE__);
65   safe->lock();
66 }
67