bump product version to 6.1.0.2
[LibreOffice.git] / sal / osl / all / signalshared.cxx
blobd6998eb006cad0d766718f03b39c713f09942d13
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <sal/config.h>
22 #include <stdlib.h>
24 #include <signalshared.hxx>
26 #include <osl/diagnose.h>
28 bool bInitSignal = false;
30 namespace
32 oslSignalHandlerImpl* SignalList;
33 oslMutex SignalListMutex;
35 bool initSignal()
37 SignalListMutex = osl_createMutex();
39 return onInitSignal();
42 bool deInitSignal()
44 bool bRet = onDeInitSignal();
46 osl_destroyMutex(SignalListMutex);
48 return bRet;
53 oslSignalAction callSignalHandler(oslSignalInfo* pInfo)
55 oslSignalHandlerImpl* pHandler = SignalList;
56 oslSignalAction Action = osl_Signal_ActCallNextHdl;
58 while (pHandler)
60 if ((Action = pHandler->Handler(pHandler->pData, pInfo))
61 != osl_Signal_ActCallNextHdl)
62 break;
64 pHandler = pHandler->pNext;
67 return Action;
70 oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction handler, void* pData)
72 if (!handler)
73 return nullptr;
75 if (!bInitSignal)
76 bInitSignal = initSignal();
78 oslSignalHandlerImpl* pHandler = static_cast<oslSignalHandlerImpl*>(calloc(1, sizeof(oslSignalHandlerImpl)));
80 if (pHandler)
82 pHandler->Handler = handler;
83 pHandler->pData = pData;
85 osl_acquireMutex(SignalListMutex);
87 pHandler->pNext = SignalList;
88 SignalList = pHandler;
90 osl_releaseMutex(SignalListMutex);
92 return pHandler;
95 return nullptr;
98 sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler handler)
100 if (!bInitSignal)
101 bInitSignal = initSignal();
103 osl_acquireMutex(SignalListMutex);
105 oslSignalHandlerImpl* pHandler = SignalList;
106 oslSignalHandlerImpl* pPrevious = nullptr;
108 while (pHandler)
110 if (pHandler == handler)
112 if (pPrevious)
113 pPrevious->pNext = pHandler->pNext;
114 else
115 SignalList = pHandler->pNext;
117 osl_releaseMutex(SignalListMutex);
119 if (!SignalList)
120 bInitSignal = deInitSignal();
122 free(pHandler);
124 return true;
127 pPrevious = pHandler;
128 pHandler = pHandler->pNext;
131 osl_releaseMutex(SignalListMutex);
133 return false;
136 oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 userSignal, void* userData)
138 if (!bInitSignal)
139 bInitSignal = initSignal();
141 osl_acquireMutex(SignalListMutex);
143 oslSignalInfo info;
144 info.Signal = osl_Signal_User;
145 info.UserSignal = userSignal;
146 info.UserData = userData;
148 oslSignalAction action = callSignalHandler(&info);
150 osl_releaseMutex(SignalListMutex);
152 return action;
155 sal_Bool SAL_CALL osl_setErrorReporting( sal_Bool /*bEnable*/ )
157 // this is part of the stable API
158 return false;
161 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */