/src/libreoffice/sal/osl/all/signalshared.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
2 | | /* |
3 | | * This file is part of the LibreOffice project. |
4 | | * |
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/. |
8 | | * |
9 | | * This file incorporates work covered by the following license notice: |
10 | | * |
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 . |
18 | | */ |
19 | | |
20 | | #include <sal/config.h> |
21 | | |
22 | | #include <stdlib.h> |
23 | | |
24 | | #include <signalshared.hxx> |
25 | | |
26 | | #include <mutex> |
27 | | |
28 | | namespace |
29 | | { |
30 | | oslSignalHandlerImpl* SignalList; |
31 | | bool bInitSignal = false; |
32 | | |
33 | | std::mutex& getSignalMutex() |
34 | 104 | { |
35 | 104 | static std::mutex aMutex; |
36 | 104 | return aMutex; |
37 | 104 | } |
38 | | } |
39 | | |
40 | | oslSignalAction callSignalHandler(oslSignalInfo* pInfo) |
41 | 0 | { |
42 | 0 | oslSignalHandlerImpl* pHandler = SignalList; |
43 | 0 | oslSignalAction Action = osl_Signal_ActCallNextHdl; |
44 | |
|
45 | 0 | while (pHandler) |
46 | 0 | { |
47 | 0 | if ((Action = pHandler->Handler(pHandler->pData, pInfo)) != osl_Signal_ActCallNextHdl) |
48 | 0 | break; |
49 | | |
50 | 0 | pHandler = pHandler->pNext; |
51 | 0 | } |
52 | |
|
53 | 0 | return Action; |
54 | 0 | } |
55 | | |
56 | | oslSignalHandler SAL_CALL osl_addSignalHandler(oslSignalHandlerFunction handler, void* pData) |
57 | 104 | { |
58 | 104 | if (!handler) |
59 | 0 | return nullptr; |
60 | | |
61 | 104 | oslSignalHandlerImpl* pHandler |
62 | 104 | = static_cast<oslSignalHandlerImpl*>(calloc(1, sizeof(oslSignalHandlerImpl))); |
63 | | |
64 | 104 | std::scoped_lock aGuard(getSignalMutex()); |
65 | | |
66 | 104 | if (!bInitSignal) |
67 | 104 | bInitSignal = onInitSignal(); |
68 | | |
69 | 104 | if (pHandler) |
70 | 104 | { |
71 | 104 | pHandler->Handler = handler; |
72 | 104 | pHandler->pData = pData; |
73 | | |
74 | 104 | pHandler->pNext = SignalList; |
75 | 104 | SignalList = pHandler; |
76 | | |
77 | 104 | return pHandler; |
78 | 104 | } |
79 | | |
80 | 0 | return nullptr; |
81 | 104 | } |
82 | | |
83 | | sal_Bool SAL_CALL osl_removeSignalHandler(oslSignalHandler handler) |
84 | 0 | { |
85 | 0 | std::scoped_lock aGuard(getSignalMutex()); |
86 | |
|
87 | 0 | if (!bInitSignal) |
88 | 0 | bInitSignal = onInitSignal(); |
89 | |
|
90 | 0 | oslSignalHandlerImpl* pHandler = SignalList; |
91 | 0 | oslSignalHandlerImpl* pPrevious = nullptr; |
92 | |
|
93 | 0 | while (pHandler) |
94 | 0 | { |
95 | 0 | if (pHandler == handler) |
96 | 0 | { |
97 | 0 | if (pPrevious) |
98 | 0 | pPrevious->pNext = pHandler->pNext; |
99 | 0 | else |
100 | 0 | SignalList = pHandler->pNext; |
101 | |
|
102 | 0 | if (SignalList == nullptr) |
103 | 0 | bInitSignal = onDeInitSignal(); |
104 | |
|
105 | 0 | free(pHandler); |
106 | |
|
107 | 0 | return true; |
108 | 0 | } |
109 | | |
110 | 0 | pPrevious = pHandler; |
111 | 0 | pHandler = pHandler->pNext; |
112 | 0 | } |
113 | | |
114 | 0 | return false; |
115 | 0 | } |
116 | | |
117 | | oslSignalAction SAL_CALL osl_raiseSignal(sal_Int32 userSignal, void* userData) |
118 | 0 | { |
119 | 0 | std::scoped_lock aGuard(getSignalMutex()); |
120 | |
|
121 | 0 | if (!bInitSignal) |
122 | 0 | bInitSignal = onInitSignal(); |
123 | |
|
124 | 0 | oslSignalInfo info; |
125 | 0 | info.Signal = osl_Signal_User; |
126 | 0 | info.UserSignal = userSignal; |
127 | 0 | info.UserData = userData; |
128 | |
|
129 | 0 | oslSignalAction action = callSignalHandler(&info); |
130 | |
|
131 | 0 | return action; |
132 | 0 | } |
133 | | |
134 | | sal_Bool SAL_CALL osl_setErrorReporting(sal_Bool /*bEnable*/) |
135 | 0 | { |
136 | | // this is part of the stable API |
137 | 0 | return false; |
138 | 0 | } |
139 | | |
140 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |