bump product version to 7.2.5.1
[LibreOffice.git] / ucb / source / ucp / webdav / SerfLockStore.cxx
blobec2f6ae1bea7dd74e721f25701136f1499966008
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 <rtl/ustring.hxx>
21 #include <sal/log.hxx>
22 #include <osl/time.h>
23 #include <osl/thread.hxx>
24 #include "SerfSession.hxx"
25 #include "SerfLockStore.hxx"
27 using namespace http_dav_ucp;
29 namespace http_dav_ucp {
31 class TickerThread : public osl::Thread
33 bool m_bFinish;
34 SerfLockStore & m_rLockStore;
36 public:
38 explicit TickerThread( SerfLockStore & rLockStore )
39 : osl::Thread(), m_bFinish( false ), m_rLockStore( rLockStore ) {}
41 void finish() { m_bFinish = true; }
43 protected:
45 virtual void SAL_CALL run() override;
48 } // namespace http_dav_ucp
51 void TickerThread::run()
53 osl_setThreadName("http_dav_ucp::TickerThread");
55 SAL_INFO("ucb.ucp.webdav", "TickerThread: start." );
57 // we have to go through the loop more often to be able to finish ~quickly
58 const int nNth = 25;
60 int nCount = nNth;
61 while ( !m_bFinish )
63 if ( nCount-- <= 0 )
65 m_rLockStore.refreshLocks();
66 nCount = nNth;
69 TimeValue aTV;
70 aTV.Seconds = 0;
71 aTV.Nanosec = 1000000000 / nNth;
72 wait( aTV );
75 SAL_INFO("ucb.ucp.webdav", "TickerThread: stop." );
79 SerfLockStore::SerfLockStore()
80 : m_pTickerThread( nullptr )
81 , m_bFinishing( false )
86 SerfLockStore::~SerfLockStore()
88 stopTicker();
89 m_bFinishing = true;
91 // release active locks, if any.
92 SAL_WARN_IF( !m_aLockInfoMap.empty(), "ucb.ucp.webdav",
93 "SerfLockStore::~SerfLockStore - Releasing active locks!" );
95 for ( auto& rLockInfo : m_aLockInfoMap )
97 rLockInfo.second.m_xSession->UNLOCK( rLockInfo.first );
101 bool SerfLockStore::finishing() const
103 return m_bFinishing;
106 void SerfLockStore::startTicker()
108 osl::MutexGuard aGuard( m_aMutex );
110 if ( !m_pTickerThread )
112 m_pTickerThread = new TickerThread( *this );
113 m_pTickerThread->create();
118 void SerfLockStore::stopTicker()
120 osl::MutexGuard aGuard( m_aMutex );
122 if ( m_pTickerThread )
124 m_pTickerThread->finish();
125 m_pTickerThread->join();
126 delete m_pTickerThread;
127 m_pTickerThread = nullptr;
131 OUString SerfLockStore::getLockToken( const OUString& rLock )
133 osl::MutexGuard aGuard( m_aMutex );
135 LockInfoMap::const_iterator it( m_aLockInfoMap.find( rLock ) );
136 if ( it != m_aLockInfoMap.end() )
137 return (*it).second.m_sToken;
139 SAL_WARN("ucb.ucp.webdav", "SerfLockStore::getLockToken: lock not found!" );
140 return OUString();
143 void SerfLockStore::addLock( const OUString& rLock,
144 const OUString& sToken,
145 rtl::Reference< SerfSession > const & xSession,
146 sal_Int32 nLastChanceToSendRefreshRequest )
148 osl::MutexGuard aGuard( m_aMutex );
150 m_aLockInfoMap[ rLock ]
151 = LockInfo( sToken, xSession, nLastChanceToSendRefreshRequest );
153 startTicker();
157 void SerfLockStore::updateLock( const OUString& rLock,
158 sal_Int32 nLastChanceToSendRefreshRequest )
160 osl::MutexGuard aGuard( m_aMutex );
162 LockInfoMap::iterator it( m_aLockInfoMap.find( rLock ) );
163 SAL_WARN_IF( it == m_aLockInfoMap.end(), "ucb.ucp.webdav",
164 "SerfLockStore::updateLock: lock not found!" );
166 if ( it != m_aLockInfoMap.end() )
168 (*it).second.m_nLastChanceToSendRefreshRequest
169 = nLastChanceToSendRefreshRequest;
174 void SerfLockStore::removeLock( const OUString& rLock )
176 osl::MutexGuard aGuard( m_aMutex );
178 m_aLockInfoMap.erase( rLock );
180 if ( m_aLockInfoMap.empty() )
181 stopTicker();
185 void SerfLockStore::refreshLocks()
187 osl::MutexGuard aGuard( m_aMutex );
189 for ( auto& rLockInfo : m_aLockInfoMap )
191 LockInfo & rInfo = rLockInfo.second;
192 if ( rInfo.m_nLastChanceToSendRefreshRequest != -1 )
194 // 30 seconds or less remaining until lock expires?
195 TimeValue t1;
196 osl_getSystemTime( &t1 );
197 if ( rInfo.m_nLastChanceToSendRefreshRequest - 30
198 <= sal_Int32( t1.Seconds ) )
200 // refresh the lock.
201 sal_Int32 nlastChanceToSendRefreshRequest = -1;
202 if ( rInfo.m_xSession->LOCK(
203 rLockInfo.first, &nlastChanceToSendRefreshRequest ) )
205 rInfo.m_nLastChanceToSendRefreshRequest
206 = nlastChanceToSendRefreshRequest;
208 else
210 // refresh failed. stop auto-refresh.
211 rInfo.m_nLastChanceToSendRefreshRequest = -1;
218 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */