/src/libreoffice/sal/osl/unx/conditn.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 <assert.h> |
23 | | #include <condition_variable> |
24 | | #include <mutex> |
25 | | |
26 | | #include <sal/log.hxx> |
27 | | #include <sal/types.h> |
28 | | |
29 | | #include <osl/conditn.h> |
30 | | #include <osl/time.h> |
31 | | |
32 | | namespace { |
33 | | |
34 | | struct oslConditionImpl |
35 | | { |
36 | | std::condition_variable m_Condition; |
37 | | std::mutex m_Lock; |
38 | | bool m_State = false; |
39 | | }; |
40 | | |
41 | | } |
42 | | |
43 | | oslCondition SAL_CALL osl_createCondition() |
44 | 667k | { |
45 | 667k | oslConditionImpl* pCond = new oslConditionImpl; |
46 | | |
47 | 667k | SAL_INFO( "sal.osl.condition", "osl_createCondition(): " << pCond ); |
48 | | |
49 | 667k | return static_cast<oslCondition>(pCond); |
50 | 667k | } |
51 | | |
52 | | void SAL_CALL osl_destroyCondition(oslCondition Condition) |
53 | 667k | { |
54 | 667k | oslConditionImpl* pCond; |
55 | | |
56 | 667k | pCond = static_cast<oslConditionImpl*>(Condition); |
57 | | |
58 | 667k | SAL_INFO( "sal.osl.condition", "osl_destroyCondition(" << pCond << ")" ); |
59 | | |
60 | 667k | if ( pCond ) |
61 | 667k | delete pCond; |
62 | 667k | } |
63 | | |
64 | | sal_Bool SAL_CALL osl_setCondition(oslCondition Condition) |
65 | 438k | { |
66 | 438k | oslConditionImpl* pCond; |
67 | | |
68 | 438k | assert(Condition); |
69 | 438k | pCond = static_cast<oslConditionImpl*>(Condition); |
70 | | |
71 | 438k | { |
72 | 438k | std::unique_lock g(pCond->m_Lock); |
73 | | |
74 | 438k | pCond->m_State = true; |
75 | 438k | pCond->m_Condition.notify_all(); |
76 | 438k | } |
77 | 438k | SAL_INFO( "sal.osl.condition", "osl_setCondition(" << pCond << ")" ); |
78 | | |
79 | 438k | return true; |
80 | | |
81 | 438k | } |
82 | | |
83 | | sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition) |
84 | 51.4k | { |
85 | 51.4k | oslConditionImpl* pCond; |
86 | | |
87 | 51.4k | assert(Condition); |
88 | | |
89 | 51.4k | pCond = static_cast<oslConditionImpl*>(Condition); |
90 | | |
91 | 51.4k | { |
92 | 51.4k | std::unique_lock g(pCond->m_Lock); |
93 | | |
94 | 51.4k | pCond->m_State = false; |
95 | 51.4k | } |
96 | 51.4k | SAL_INFO( "sal.osl.condition", "osl_resetCondition(" << pCond << ")" ); |
97 | | |
98 | 51.4k | return true; |
99 | 51.4k | } |
100 | | |
101 | | oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout) |
102 | 45.5M | { |
103 | 45.5M | oslConditionImpl* pCond; |
104 | | |
105 | 45.5M | assert(Condition); |
106 | 45.5M | pCond = static_cast<oslConditionImpl*>(Condition); |
107 | | |
108 | 45.5M | SAL_INFO( "sal.osl.condition", "osl_waitCondition(" << pCond << ")" ); |
109 | | |
110 | 45.5M | { |
111 | 45.5M | std::unique_lock g(pCond->m_Lock); |
112 | | |
113 | 45.5M | if ( pTimeout ) |
114 | 0 | { |
115 | 0 | if ( ! pCond->m_State ) |
116 | 0 | { |
117 | 0 | auto duration = std::chrono::seconds(pTimeout->Seconds) |
118 | 0 | + std::chrono::nanoseconds(pTimeout->Nanosec); |
119 | 0 | if (!pCond->m_Condition.wait_for(g, duration, [&pCond](){return pCond->m_State;})) |
120 | 0 | return osl_cond_result_timeout; |
121 | 0 | } |
122 | 0 | } |
123 | 45.5M | else |
124 | 45.5M | { |
125 | 45.5M | pCond->m_Condition.wait(g, [&pCond](){return pCond->m_State;}); |
126 | 45.5M | } |
127 | 45.5M | } |
128 | 45.5M | SAL_INFO( "sal.osl.condition", "osl_waitCondition(" << pCond << "): OK" ); |
129 | | |
130 | 45.5M | return osl_cond_result_ok; |
131 | 45.5M | } |
132 | | |
133 | | sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition) |
134 | 102k | { |
135 | 102k | bool State; |
136 | 102k | oslConditionImpl* pCond; |
137 | | |
138 | 102k | assert(Condition); |
139 | 102k | pCond = static_cast<oslConditionImpl*>(Condition); |
140 | | |
141 | 102k | { |
142 | 102k | std::unique_lock g(pCond->m_Lock); |
143 | | |
144 | 102k | State = pCond->m_State; |
145 | 102k | } |
146 | 102k | SAL_INFO( "sal.osl.condition", "osl_checkCondition(" << pCond << "): " << (State ? "YES" : "NO") ); |
147 | | |
148 | 102k | return State; |
149 | 102k | } |
150 | | |
151 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |