b311480e |
1 | // Copyright (c) 1998-1999 Matra Datavision |
973c2be1 |
2 | // Copyright (c) 1999-2014 OPEN CASCADE SAS |
b311480e |
3 | // |
973c2be1 |
4 | // This file is part of Open CASCADE Technology software library. |
b311480e |
5 | // |
d5f74e42 |
6 | // This library is free software; you can redistribute it and/or modify it under |
7 | // the terms of the GNU Lesser General Public License version 2.1 as published |
973c2be1 |
8 | // by the Free Software Foundation, with special exception defined in the file |
9 | // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT |
10 | // distribution for complete text of the license and disclaimer of any warranty. |
b311480e |
11 | // |
973c2be1 |
12 | // Alternatively, this file may be used under the terms of Open CASCADE |
13 | // commercial license or contractual agreement. |
b311480e |
14 | |
7fd59977 |
15 | //============================================================================ |
16 | //==== Titre: Standard_ErrorHandler.cxx |
17 | //==== Role : class "Standard_ErrorHandler" implementation. |
18 | //============================================================================ |
19 | #include <Standard_ErrorHandler.hxx> |
20 | #include <Standard_Failure.hxx> |
21 | #include <Standard_ErrorHandlerCallback.hxx> |
22 | #include <Standard_Mutex.hxx> |
23 | #include <Standard.hxx> |
24 | |
57c28b61 |
25 | #ifndef _WIN32 |
7fd59977 |
26 | #include <pthread.h> |
27 | #else |
28 | #include <windows.h> |
29 | #endif |
30 | |
31 | // =========================================================================== |
32 | // The class "Standard_ErrorHandler" variables |
33 | // =========================================================================== |
34 | |
35 | // During [sig]setjmp()/[sig]longjmp() K_SETJMP is non zero (try) |
36 | // So if there is an abort request and if K_SETJMP is non zero, the abort |
37 | // request will be ignored. If the abort request do a raise during a setjmp |
38 | // or a longjmp, there will be a "terminating SEGV" impossible to handle. |
39 | |
40 | //==== The top of the Errors Stack =========================================== |
41 | static Standard_ErrorHandler* Top = 0; |
42 | |
43 | // A mutex to protect from concurrent access to Top |
44 | // Note that we should NOT use Sentry while in this class, as Sentry |
45 | // would register mutex as callback in the current exception handler |
46 | static Standard_Mutex theMutex; |
47 | |
48 | static inline Standard_ThreadId GetThreadID() |
49 | { |
57c28b61 |
50 | #ifndef _WIN32 |
7fd59977 |
51 | return pthread_self(); |
52 | #else |
53 | return GetCurrentThreadId(); |
54 | #endif |
55 | } |
56 | |
57 | //============================================================================ |
58 | //==== Constructor : Create a ErrorHandler structure. And add it at the |
59 | //==== 'Top' of "ErrorHandler's stack". |
60 | //============================================================================ |
61 | |
62 | Standard_ErrorHandler::Standard_ErrorHandler () : |
63 | myStatus(Standard_HandlerVoid), myCallbackPtr(0) |
64 | { |
65 | myThread = GetThreadID(); |
c9246067 |
66 | memset (&myLabel, 0, sizeof(myLabel)); |
7fd59977 |
67 | |
bd0c22ce |
68 | theMutex.Lock(); |
7fd59977 |
69 | myPrevious = Top; |
70 | Top = this; |
bd0c22ce |
71 | theMutex.Unlock(); |
7fd59977 |
72 | } |
73 | |
74 | |
75 | //============================================================================ |
76 | //==== Destructor : Delete the ErrorHandler and Abort if there is a 'Error'. |
77 | //============================================================================ |
78 | |
79 | void Standard_ErrorHandler::Destroy() |
80 | { |
81 | Unlink(); |
a01039b9 |
82 | if (myStatus == Standard_HandlerJumped) |
83 | { |
84 | // jumped, but not caught |
85 | Abort (myCaughtError); |
7fd59977 |
86 | } |
87 | } |
88 | |
89 | |
90 | //======================================================================= |
91 | //function : Unlink |
92 | //purpose : |
93 | //======================================================================= |
94 | |
95 | void Standard_ErrorHandler::Unlink() |
96 | { |
97 | // put a lock on the stack |
bd0c22ce |
98 | theMutex.Lock(); |
7fd59977 |
99 | |
100 | Standard_ErrorHandler* aPrevious = 0; |
101 | Standard_ErrorHandler* aCurrent = Top; |
102 | |
103 | // locate this handler in the stack |
104 | while(aCurrent!=0 && this!=aCurrent) { |
105 | aPrevious = aCurrent; |
106 | aCurrent = aCurrent->myPrevious; |
107 | } |
108 | |
109 | if(aCurrent==0) { |
bd0c22ce |
110 | theMutex.Unlock(); |
7fd59977 |
111 | return; |
112 | } |
113 | |
114 | if(aPrevious==0) { |
115 | // a top exception taken |
116 | Top = aCurrent->myPrevious; |
117 | } |
118 | else { |
119 | aPrevious->myPrevious=aCurrent->myPrevious; |
120 | } |
121 | myPrevious = 0; |
bd0c22ce |
122 | theMutex.Unlock(); |
7fd59977 |
123 | |
124 | // unlink and destroy all registered callbacks |
125 | Standard_Address aPtr = aCurrent->myCallbackPtr; |
126 | myCallbackPtr = 0; |
127 | while ( aPtr ) { |
128 | Standard_ErrorHandlerCallback* aCallback = (Standard_ErrorHandlerCallback*)aPtr; |
129 | aPtr = aCallback->myNext; |
130 | // Call destructor explicitly, as we know that it will not be called automatically |
131 | aCallback->DestroyCallback(); |
132 | } |
133 | } |
134 | |
135 | //======================================================================= |
136 | //function : IsInTryBlock |
137 | //purpose : test if the code is currently running in |
138 | //======================================================================= |
139 | |
140 | Standard_Boolean Standard_ErrorHandler::IsInTryBlock() |
141 | { |
142 | Standard_ErrorHandler* anActive = FindHandler(Standard_HandlerVoid, Standard_False); |
c9246067 |
143 | return anActive != NULL; |
7fd59977 |
144 | } |
145 | |
146 | |
147 | //============================================================================ |
148 | //==== Abort: make a longjmp to the saved Context. |
149 | //==== Abort if there is a non null 'Error' |
150 | //============================================================================ |
151 | |
a01039b9 |
152 | void Standard_ErrorHandler::Abort (const Handle(Standard_Failure)& theError) |
7fd59977 |
153 | { |
154 | Standard_ErrorHandler* anActive = FindHandler(Standard_HandlerVoid, Standard_True); |
a01039b9 |
155 | |
7fd59977 |
156 | //==== Check if can do the "longjmp" ======================================= |
c9246067 |
157 | if(anActive == NULL) { |
7fd59977 |
158 | cerr << "*** Abort *** an exception was raised, but no catch was found." << endl; |
a01039b9 |
159 | if (!theError.IsNull()) |
160 | cerr << "\t... The exception is:" << theError->GetMessageString() << endl; |
7fd59977 |
161 | exit(1); |
162 | } |
a01039b9 |
163 | |
7fd59977 |
164 | anActive->myStatus = Standard_HandlerJumped; |
165 | longjmp(anActive->myLabel, Standard_True); |
166 | } |
167 | |
168 | |
169 | //============================================================================ |
170 | //==== Catches: If there is a 'Error', and it is in good type |
171 | //==== returns True and clean 'Error', else returns False. |
172 | //============================================================================ |
173 | |
174 | Standard_Boolean Standard_ErrorHandler::Catches (const Handle(Standard_Type)& AType) |
175 | { |
176 | Standard_ErrorHandler* anActive = FindHandler(Standard_HandlerJumped, Standard_False); |
177 | if(anActive==0) |
178 | return Standard_False; |
179 | |
180 | if(anActive->myCaughtError.IsNull()) |
181 | return Standard_False; |
182 | |
183 | if(anActive->myCaughtError->IsKind(AType)){ |
184 | myStatus=Standard_HandlerProcessed; |
185 | return Standard_True; |
186 | } else { |
187 | return Standard_False; |
188 | } |
189 | } |
190 | |
191 | Handle(Standard_Failure) Standard_ErrorHandler::LastCaughtError() |
192 | { |
193 | Handle(Standard_Failure) aHandle; |
194 | Standard_ErrorHandler* anActive = FindHandler(Standard_HandlerProcessed, Standard_False); |
195 | if(anActive!=0) |
196 | aHandle = anActive->myCaughtError; |
197 | |
198 | return aHandle; |
199 | } |
200 | |
201 | Handle(Standard_Failure) Standard_ErrorHandler::Error() const |
202 | { |
203 | return myCaughtError; |
204 | } |
205 | |
206 | |
a01039b9 |
207 | void Standard_ErrorHandler::Error (const Handle(Standard_Failure)& theError) |
7fd59977 |
208 | { |
a01039b9 |
209 | Standard_ErrorHandler* anActive = FindHandler (Standard_HandlerVoid, Standard_False); |
210 | if (anActive == NULL) |
211 | Abort (theError); |
7fd59977 |
212 | |
a01039b9 |
213 | anActive->myCaughtError = theError; |
214 | } |
7fd59977 |
215 | |
216 | |
217 | Standard_ErrorHandler* Standard_ErrorHandler::FindHandler(const Standard_HandlerStatus theStatus, |
218 | const Standard_Boolean theUnlink) |
219 | { |
220 | // lock the stack |
bd0c22ce |
221 | theMutex.Lock(); |
7fd59977 |
222 | |
223 | // Find the current ErrorHandler Accordin tread |
224 | Standard_ErrorHandler* aPrevious = 0; |
225 | Standard_ErrorHandler* aCurrent = Top; |
226 | Standard_ErrorHandler* anActive = 0; |
227 | Standard_Boolean aStop = Standard_False; |
228 | Standard_ThreadId aTreadId = GetThreadID(); |
229 | |
230 | // searching an exception with correct ID number |
231 | // which is not processed for the moment |
232 | while(!aStop) { |
233 | while(aCurrent!=NULL && aTreadId!=aCurrent->myThread) { |
234 | aPrevious = aCurrent; |
235 | aCurrent = aCurrent->myPrevious; |
236 | } |
237 | |
238 | if(aCurrent!=NULL) { |
239 | if(theStatus!=aCurrent->myStatus) { |
240 | |
241 | if(theUnlink) { |
242 | //unlink current |
243 | if(aPrevious==0) { |
244 | // a top exception taken |
245 | Top = aCurrent->myPrevious; |
246 | } |
247 | else { |
248 | aPrevious->myPrevious=aCurrent->myPrevious; |
249 | } |
250 | } |
251 | |
252 | //shift |
253 | aCurrent = aCurrent->myPrevious; |
254 | } |
255 | else { |
256 | //found one |
257 | anActive = aCurrent; |
258 | aStop = Standard_True; |
259 | } |
260 | } |
261 | else { |
262 | //Current is NULL, means that no handlesr |
263 | aStop = Standard_True; |
264 | } |
265 | } |
bd0c22ce |
266 | theMutex.Unlock(); |
7fd59977 |
267 | |
268 | return anActive; |
269 | } |