[dali_2.3.25] Merge branch 'devel/master'
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / abort-handler.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 // CLASS HEADER
19 #include <dali/internal/system/common/abort-handler.h>
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23
24 namespace Dali
25 {
26 namespace Internal
27 {
28 namespace Adaptor
29 {
30 AbortHandler* AbortHandler::gInstance(NULL);
31
32 AbortHandler::AbortHandler(CallbackBase* callback)
33 : mSignalMask(0),
34   mCallback(callback)
35 {
36   DALI_ASSERT_ALWAYS(gInstance == NULL && "Only one instance of abort handler allowed");
37   gInstance = this;
38
39   memset(mSignalOldHandlers, 0, sizeof(SignalHandlerFuncPtr) * (_NSIG - 1));
40 }
41
42 AbortHandler::~AbortHandler()
43 {
44   delete mCallback;
45
46   int signum;
47   for(signum = 1; signum < _NSIG; signum++)
48   {
49     if(mSignalMask & (1ULL << (signum - 1)))
50     {
51       // set signals back to default handling
52       signal(signum, mSignalOldHandlers[signum - 1]);
53     }
54   }
55   gInstance = NULL;
56 }
57
58 bool AbortHandler::AbortOnSignal(int signum)
59 {
60   bool status = false;
61
62   if(signum < _NSIG)
63   {
64     SignalHandlerFuncPtr signalHandlerPrevious = signal(signum, &AbortHandler::SignalHandler);
65
66 // SIG_ERR is a macro with C cast
67 #pragma GCC diagnostic push
68 #pragma GCC diagnostic ignored "-Wold-style-cast"
69     if(SIG_ERR != signalHandlerPrevious)
70     {
71       mSignalOldHandlers[signum - 1] = signalHandlerPrevious;
72       mSignalMask |= (1ULL << (signum - 1));
73       status = true;
74     }
75   }
76 #pragma GCC diagnostic pop
77   return status;
78 }
79
80 void AbortHandler::SignalHandler(int signum)
81 {
82   if(gInstance)
83   {
84     if(gInstance->mCallback)
85     {
86       CallbackBase::Execute(*gInstance->mCallback);
87     }
88   }
89 }
90
91 } // namespace Adaptor
92 } // namespace Internal
93 } // namespace Dali