merged tag ooo/DEV300_m102
[LibreOffice.git] / sal / osl / os2 / signal.c
blob881db13fe1e1e95a791d10f2e688a537aa0a007e
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
29 /* system headers */
30 #include "system.h"
32 #include <osl/diagnose.h>
33 #include <osl/mutex.h>
34 #include <osl/signal.h>
36 typedef struct _oslSignalHandlerImpl
38 oslSignalHandlerFunction Handler;
39 void* pData;
40 struct _oslSignalHandlerImpl* pNext;
41 } oslSignalHandlerImpl;
43 static sal_Bool bErrorReportingEnabled = sal_True;
44 static sal_Bool bInitSignal = sal_False;
45 static oslMutex SignalListMutex;
46 static oslSignalHandlerImpl* SignalList;
48 /*static*//* ULONG _Export APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
49 PEXCEPTIONREGISTRATIONRECORD,
50 PCONTEXTRECORD, PVOID);
52 /*static*/ ULONG __declspec(dllexport) APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
53 PEXCEPTIONREGISTRATIONRECORD,
54 PCONTEXTRECORD, PVOID);
55 static EXCEPTIONREGISTRATIONRECORD ExcptHandler = { 0, SignalHandlerFunction };
57 static sal_Bool InitSignal( void )
59 SignalListMutex = osl_createMutex();
61 ExcptHandler.ExceptionHandler = (_ERR *) &SignalHandlerFunction;
62 /* DosSetExceptionHandler(&ExcptHandler); */
64 return sal_True;
67 static sal_Bool DeInitSignal( void )
69 /* DosUnsetExceptionHandler(&ExcptHandler); */
71 osl_destroyMutex(SignalListMutex);
73 return sal_False;
76 static oslSignalAction CallSignalHandler(oslSignalInfo *pInfo)
78 oslSignalHandlerImpl* pHandler = SignalList;
79 oslSignalAction Action = osl_Signal_ActCallNextHdl;
81 while (pHandler != NULL)
83 if ((Action = pHandler->Handler(pHandler->pData, pInfo)) != osl_Signal_ActCallNextHdl)
84 break;
86 pHandler = pHandler->pNext;
89 return Action;
92 /*****************************************************************************/
93 /* SignalHandlerFunction */
94 /*****************************************************************************/
95 /*static*/ ULONG __declspec(dllexport) APIENTRY SignalHandlerFunction(PEXCEPTIONREPORTRECORD pERepRec,
96 PEXCEPTIONREGISTRATIONRECORD pERegRec,
97 PCONTEXTRECORD pConRec, PVOID pReserved)
99 oslSignalInfo Info;
101 Info.UserSignal = pERepRec->ExceptionNum;
102 Info.UserData = NULL;
104 switch (pERepRec->ExceptionNum)
106 case XCPT_ACCESS_VIOLATION:
107 Info.Signal = osl_Signal_AccessViolation;
108 break;
110 case XCPT_INTEGER_DIVIDE_BY_ZERO:
111 Info.Signal = osl_Signal_IntegerDivideByZero;
112 break;
114 case XCPT_BREAKPOINT:
115 Info.Signal = osl_Signal_DebugBreak;
116 break;
118 default:
119 Info.Signal = osl_Signal_System;
120 break;
123 switch (CallSignalHandler(&Info))
125 case osl_Signal_ActCallNextHdl:
126 return (XCPT_CONTINUE_SEARCH);
128 case osl_Signal_ActAbortApp:
129 return (XCPT_CONTINUE_SEARCH);
131 case osl_Signal_ActKillApp:
132 exit(255);
133 break;
136 return (XCPT_CONTINUE_SEARCH);
139 /*****************************************************************************/
140 /* osl_addSignalHandler */
141 /*****************************************************************************/
142 oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction Handler, void* pData)
144 oslSignalHandlerImpl* pHandler;
146 OSL_ASSERT(Handler != NULL);
148 if (! bInitSignal)
149 bInitSignal = InitSignal();
151 pHandler = (oslSignalHandlerImpl*) calloc(1, sizeof(oslSignalHandlerImpl));
153 if (pHandler != NULL)
155 pHandler->Handler = Handler;
156 pHandler->pData = pData;
158 osl_acquireMutex(SignalListMutex);
160 pHandler->pNext = SignalList;
161 SignalList = pHandler;
163 osl_releaseMutex(SignalListMutex);
165 return (pHandler);
168 return (NULL);
171 /*****************************************************************************/
172 /* osl_removeSignalHandler */
173 /*****************************************************************************/
174 sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler Handler)
176 oslSignalHandlerImpl *pHandler, *pPrevious = NULL;
178 OSL_ASSERT(Handler != NULL);
180 if (! bInitSignal)
181 bInitSignal = InitSignal();
183 osl_acquireMutex(SignalListMutex);
185 pHandler = SignalList;
187 while (pHandler != NULL)
189 if (pHandler == Handler)
191 if (pPrevious)
192 pPrevious->pNext = pHandler->pNext;
193 else
194 SignalList = pHandler->pNext;
196 osl_releaseMutex(SignalListMutex);
198 if (SignalList == NULL )
199 bInitSignal = DeInitSignal();
201 free(pHandler);
203 return (sal_True);
206 pPrevious = pHandler;
207 pHandler = pHandler->pNext;
210 osl_releaseMutex(SignalListMutex);
212 return (sal_False);
215 /*****************************************************************************/
216 /* osl_raiseSignal */
217 /*****************************************************************************/
218 oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 UserSignal, void* UserData)
220 oslSignalInfo Info;
221 oslSignalAction Action;
223 if (! bInitSignal)
224 bInitSignal = InitSignal();
226 osl_acquireMutex(SignalListMutex);
228 Info.Signal = osl_Signal_User;
229 Info.UserSignal = UserSignal;
230 Info.UserData = UserData;
232 Action = CallSignalHandler(&Info);
234 osl_releaseMutex(SignalListMutex);
236 return (Action);
239 /*****************************************************************************/
240 /* osl_setErrorReporting */
241 /*****************************************************************************/
242 sal_Bool SAL_CALL osl_setErrorReporting( sal_Bool bEnable )
244 sal_Bool bOld = bErrorReportingEnabled;
245 bErrorReportingEnabled = bEnable;
247 return bOld;