Avoid potential negative array index access to cached text.
[LibreOffice.git] / sal / osl / unx / conditn.cxx
blob16c4ad11b15f9c90bd942122973657e4fc977bbf
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 <assert.h>
23 #include <condition_variable>
24 #include <mutex>
26 #include <sal/log.hxx>
27 #include <sal/types.h>
29 #include <osl/conditn.h>
30 #include <osl/time.h>
32 namespace {
34 struct oslConditionImpl
36 std::condition_variable m_Condition;
37 std::mutex m_Lock;
38 bool m_State = false;
43 oslCondition SAL_CALL osl_createCondition()
45 oslConditionImpl* pCond = new oslConditionImpl;
47 SAL_INFO( "sal.osl.condition", "osl_createCondition(): " << pCond );
49 return static_cast<oslCondition>(pCond);
52 void SAL_CALL osl_destroyCondition(oslCondition Condition)
54 oslConditionImpl* pCond;
56 pCond = static_cast<oslConditionImpl*>(Condition);
58 SAL_INFO( "sal.osl.condition", "osl_destroyCondition(" << pCond << ")" );
60 if ( pCond )
61 delete pCond;
64 sal_Bool SAL_CALL osl_setCondition(oslCondition Condition)
66 oslConditionImpl* pCond;
68 assert(Condition);
69 pCond = static_cast<oslConditionImpl*>(Condition);
72 std::unique_lock g(pCond->m_Lock);
74 pCond->m_State = true;
75 pCond->m_Condition.notify_all();
77 SAL_INFO( "sal.osl.condition", "osl_setCondition(" << pCond << ")" );
79 return true;
83 sal_Bool SAL_CALL osl_resetCondition(oslCondition Condition)
85 oslConditionImpl* pCond;
87 assert(Condition);
89 pCond = static_cast<oslConditionImpl*>(Condition);
92 std::unique_lock g(pCond->m_Lock);
94 pCond->m_State = false;
96 SAL_INFO( "sal.osl.condition", "osl_resetCondition(" << pCond << ")" );
98 return true;
101 oslConditionResult SAL_CALL osl_waitCondition(oslCondition Condition, const TimeValue* pTimeout)
103 oslConditionImpl* pCond;
105 assert(Condition);
106 pCond = static_cast<oslConditionImpl*>(Condition);
108 SAL_INFO( "sal.osl.condition", "osl_waitCondition(" << pCond << ")" );
111 std::unique_lock g(pCond->m_Lock);
113 if ( pTimeout )
115 if ( ! pCond->m_State )
117 auto duration = std::chrono::seconds(pTimeout->Seconds)
118 + std::chrono::nanoseconds(pTimeout->Nanosec);
119 if (!pCond->m_Condition.wait_for(g, duration, [&pCond](){return pCond->m_State;}))
120 return osl_cond_result_timeout;
123 else
125 pCond->m_Condition.wait(g, [&pCond](){return pCond->m_State;});
128 SAL_INFO( "sal.osl.condition", "osl_waitCondition(" << pCond << "): OK" );
130 return osl_cond_result_ok;
133 sal_Bool SAL_CALL osl_checkCondition(oslCondition Condition)
135 bool State;
136 oslConditionImpl* pCond;
138 assert(Condition);
139 pCond = static_cast<oslConditionImpl*>(Condition);
142 std::unique_lock g(pCond->m_Lock);
144 State = pCond->m_State;
146 SAL_INFO( "sal.osl.condition", "osl_checkCondition(" << pCond << "): " << (State ? "YES" : "NO") );
148 return State;
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */