Update ooo320-m1
[ooovba.git] / sal / osl / os2 / signal.c
blob0106ac28e1069de5febdd007b331fef12dc17ca4
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: signal.c,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
32 /* system headers */
33 #include "system.h"
35 #include <osl/diagnose.h>
36 #include <osl/mutex.h>
37 #include <osl/signal.h>
39 typedef struct _oslSignalHandlerImpl
41 oslSignalHandlerFunction Handler;
42 void* pData;
43 struct _oslSignalHandlerImpl* pNext;
44 } oslSignalHandlerImpl;
46 static sal_Bool bErrorReportingEnabled = sal_True;
47 static sal_Bool bInitSignal = sal_False;
48 static oslMutex SignalListMutex;
49 static oslSignalHandlerImpl* SignalList;
51 /*static*//* ULONG _Export APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
52 PEXCEPTIONREGISTRATIONRECORD,
53 PCONTEXTRECORD, PVOID);
55 /*static*/ ULONG __declspec(dllexport) APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
56 PEXCEPTIONREGISTRATIONRECORD,
57 PCONTEXTRECORD, PVOID);
58 static EXCEPTIONREGISTRATIONRECORD ExcptHandler = { 0, SignalHandlerFunction };
60 static sal_Bool InitSignal( void )
62 SignalListMutex = osl_createMutex();
64 ExcptHandler.ExceptionHandler = (_ERR *) &SignalHandlerFunction;
65 /* DosSetExceptionHandler(&ExcptHandler); */
67 return sal_True;
70 static sal_Bool DeInitSignal( void )
72 /* DosUnsetExceptionHandler(&ExcptHandler); */
74 osl_destroyMutex(SignalListMutex);
76 return sal_False;
79 static oslSignalAction CallSignalHandler(oslSignalInfo *pInfo)
81 oslSignalHandlerImpl* pHandler = SignalList;
82 oslSignalAction Action = osl_Signal_ActCallNextHdl;
84 while (pHandler != NULL)
86 if ((Action = pHandler->Handler(pHandler->pData, pInfo)) != osl_Signal_ActCallNextHdl)
87 break;
89 pHandler = pHandler->pNext;
92 return Action;
95 /*****************************************************************************/
96 /* SignalHandlerFunction */
97 /*****************************************************************************/
98 /*static*/ ULONG __declspec(dllexport) APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
99 PEXCEPTIONREGISTRATIONRECORD pERegRec,
100 PCONTEXTRECORD pConRec, PVOID pReserved)
102 oslSignalInfo Info;
104 Info.UserSignal = pERepRec->ExceptionNum;
105 Info.UserData = NULL;
107 switch (pERepRec->ExceptionNum)
109 case XCPT_ACCESS_VIOLATION:
110 Info.Signal = osl_Signal_AccessViolation;
111 break;
113 case XCPT_INTEGER_DIVIDE_BY_ZERO:
114 Info.Signal = osl_Signal_IntegerDivideByZero;
115 break;
117 case XCPT_BREAKPOINT:
118 Info.Signal = osl_Signal_DebugBreak;
119 break;
121 default:
122 Info.Signal = osl_Signal_System;
123 break;
126 switch (CallSignalHandler(&Info))
128 case osl_Signal_ActCallNextHdl:
129 return (XCPT_CONTINUE_SEARCH);
131 case osl_Signal_ActAbortApp:
132 return (XCPT_CONTINUE_SEARCH);
134 case osl_Signal_ActKillApp:
135 exit(255);
136 break;
139 return (XCPT_CONTINUE_SEARCH);
142 /*****************************************************************************/
143 /* osl_addSignalHandler */
144 /*****************************************************************************/
145 oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction Handler, void* pData)
147 oslSignalHandlerImpl* pHandler;
149 OSL_ASSERT(Handler != NULL);
151 if (! bInitSignal)
152 bInitSignal = InitSignal();
154 pHandler = (oslSignalHandlerImpl*) calloc(1, sizeof(oslSignalHandlerImpl));
156 if (pHandler != NULL)
158 pHandler->Handler = Handler;
159 pHandler->pData = pData;
161 osl_acquireMutex(SignalListMutex);
163 pHandler->pNext = SignalList;
164 SignalList = pHandler;
166 osl_releaseMutex(SignalListMutex);
168 return (pHandler);
171 return (NULL);
174 /*****************************************************************************/
175 /* osl_removeSignalHandler */
176 /*****************************************************************************/
177 sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler Handler)
179 oslSignalHandlerImpl *pHandler, *pPrevious = NULL;
181 OSL_ASSERT(Handler != NULL);
183 if (! bInitSignal)
184 bInitSignal = InitSignal();
186 osl_acquireMutex(SignalListMutex);
188 pHandler = SignalList;
190 while (pHandler != NULL)
192 if (pHandler == Handler)
194 if (pPrevious)
195 pPrevious->pNext = pHandler->pNext;
196 else
197 SignalList = pHandler->pNext;
199 osl_releaseMutex(SignalListMutex);
201 if (SignalList == NULL )
202 bInitSignal = DeInitSignal();
204 free(pHandler);
206 return (sal_True);
209 pPrevious = pHandler;
210 pHandler = pHandler->pNext;
213 osl_releaseMutex(SignalListMutex);
215 return (sal_False);
218 /*****************************************************************************/
219 /* osl_raiseSignal */
220 /*****************************************************************************/
221 oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 UserSignal, void* UserData)
223 oslSignalInfo Info;
224 oslSignalAction Action;
226 if (! bInitSignal)
227 bInitSignal = InitSignal();
229 osl_acquireMutex(SignalListMutex);
231 Info.Signal = osl_Signal_User;
232 Info.UserSignal = UserSignal;
233 Info.UserData = UserData;
235 Action = CallSignalHandler(&Info);
237 osl_releaseMutex(SignalListMutex);
239 return (Action);
242 /*****************************************************************************/
243 /* osl_setErrorReporting */
244 /*****************************************************************************/
245 sal_Bool SAL_CALL osl_setErrorReporting( sal_Bool bEnable )
247 sal_Bool bOld = bErrorReportingEnabled;
248 bErrorReportingEnabled = bEnable;
250 return bOld;