Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sal / osl / all / signalshared.cxx
blob530413f2604c335306522025780f83cfe1190350
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>
27 #include <mutex>
29 namespace
31 oslSignalHandlerImpl* SignalList;
32 bool bInitSignal = false;
34 std::mutex& getSignalMutex()
36 static std::mutex aMutex;
37 return aMutex;
41 oslSignalAction callSignalHandler(oslSignalInfo* pInfo)
43 oslSignalHandlerImpl* pHandler = SignalList;
44 oslSignalAction Action = osl_Signal_ActCallNextHdl;
46 while (pHandler)
48 if ((Action = pHandler->Handler(pHandler->pData, pInfo)) != osl_Signal_ActCallNextHdl)
49 break;
51 pHandler = pHandler->pNext;
54 return Action;
57 oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction handler, void* pData)
59 if (!handler)
60 return nullptr;
62 oslSignalHandlerImpl* pHandler
63 = static_cast<oslSignalHandlerImpl*>(calloc(1, sizeof(oslSignalHandlerImpl)));
65 std::scoped_lock aGuard(getSignalMutex());
67 if (!bInitSignal)
68 bInitSignal = onInitSignal();
70 if (pHandler)
72 pHandler->Handler = handler;
73 pHandler->pData = pData;
75 pHandler->pNext = SignalList;
76 SignalList = pHandler;
78 return pHandler;
81 return nullptr;
84 sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler handler)
86 std::scoped_lock aGuard(getSignalMutex());
88 if (!bInitSignal)
89 bInitSignal = onInitSignal();
91 oslSignalHandlerImpl* pHandler = SignalList;
92 oslSignalHandlerImpl* pPrevious = nullptr;
94 while (pHandler)
96 if (pHandler == handler)
98 if (pPrevious)
99 pPrevious->pNext = pHandler->pNext;
100 else
101 SignalList = pHandler->pNext;
103 if (SignalList == nullptr)
104 bInitSignal = onDeInitSignal();
106 free(pHandler);
108 return true;
111 pPrevious = pHandler;
112 pHandler = pHandler->pNext;
115 return false;
118 oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 userSignal, void* userData)
120 std::scoped_lock aGuard(getSignalMutex());
122 if (!bInitSignal)
123 bInitSignal = onInitSignal();
125 oslSignalInfo info;
126 info.Signal = osl_Signal_User;
127 info.UserSignal = userSignal;
128 info.UserData = userData;
130 oslSignalAction action = callSignalHandler(&info);
132 return action;
135 sal_Bool SAL_CALL osl_setErrorReporting(sal_Bool /*bEnable*/)
137 // this is part of the stable API
138 return false;
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */