fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / ucb / source / ucp / webdav / SerfLockStore.cxx
blob5314cee3cfa90b7f0dceac54da10b4c260ded314
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 <osl/time.h>
22 #include <osl/thread.hxx>
23 #include "SerfSession.hxx"
24 #include "SerfLockStore.hxx"
26 using namespace http_dav_ucp;
28 namespace http_dav_ucp {
30 class TickerThread : public osl::Thread
32 bool m_bFinish;
33 SerfLockStore & m_rLockStore;
35 public:
37 TickerThread( SerfLockStore & rLockStore )
38 : osl::Thread(), m_bFinish( false ), m_rLockStore( rLockStore ) {}
40 void finish() { m_bFinish = true; }
42 protected:
44 virtual void SAL_CALL run() SAL_OVERRIDE;
47 } // namespace http_dav_ucp
50 void TickerThread::run()
52 osl_setThreadName("http_dav_ucp::TickerThread");
54 SAL_INFO("ucb.ucp.webdav", "TickerThread: start." );
56 // we have to go through the loop more often to be able to finish ~quickly
57 const int nNth = 25;
59 int nCount = nNth;
60 while ( !m_bFinish )
62 if ( nCount-- <= 0 )
64 m_rLockStore.refreshLocks();
65 nCount = nNth;
68 TimeValue aTV;
69 aTV.Seconds = 0;
70 aTV.Nanosec = 1000000000 / nNth;
71 wait( aTV );
74 SAL_INFO("ucb.ucp.webdav", "TickerThread: stop." );
78 SerfLockStore::SerfLockStore()
79 : m_pTickerThread( 0 )
80 , m_bFinishing( false )
85 SerfLockStore::~SerfLockStore()
87 stopTicker();
88 m_bFinishing = true;
90 // release active locks, if any.
91 SAL_WARN_IF( !m_aLockInfoMap.empty(), "ucb.ucp.webdav",
92 "SerfLockStore::~SerfLockStore - Releasing active locks!" );
94 LockInfoMap::const_iterator it( m_aLockInfoMap.begin() );
95 const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
96 while ( it != end )
98 (*it).second.m_xSession->UNLOCK( (*it).first );
99 ++it;
103 bool SerfLockStore::finishing() const
105 return m_bFinishing;
108 void SerfLockStore::startTicker()
110 osl::MutexGuard aGuard( m_aMutex );
112 if ( !m_pTickerThread )
114 m_pTickerThread = new TickerThread( *this );
115 m_pTickerThread->create();
120 void SerfLockStore::stopTicker()
122 osl::MutexGuard aGuard( m_aMutex );
124 if ( m_pTickerThread )
126 m_pTickerThread->finish();
127 m_pTickerThread->join();
128 delete m_pTickerThread;
129 m_pTickerThread = 0;
133 OUString SerfLockStore::getLockToken( const OUString& rLock )
135 osl::MutexGuard aGuard( m_aMutex );
137 LockInfoMap::const_iterator it( m_aLockInfoMap.find( rLock ) );
138 if ( it != m_aLockInfoMap.end() )
139 return (*it).second.m_sToken;
141 SAL_WARN("ucb.ucp.webdav", "SerfLockStore::getLockToken: lock not found!" );
142 return OUString();
145 void SerfLockStore::addLock( const OUString& rLock,
146 const OUString& sToken,
147 rtl::Reference< SerfSession > const & xSession,
148 sal_Int32 nLastChanceToSendRefreshRequest )
150 osl::MutexGuard aGuard( m_aMutex );
152 m_aLockInfoMap[ rLock ]
153 = LockInfo( sToken, xSession, nLastChanceToSendRefreshRequest );
155 startTicker();
159 void SerfLockStore::updateLock( const OUString& rLock,
160 sal_Int32 nLastChanceToSendRefreshRequest )
162 osl::MutexGuard aGuard( m_aMutex );
164 LockInfoMap::iterator it( m_aLockInfoMap.find( rLock ) );
165 SAL_WARN_IF( it == m_aLockInfoMap.end(), "ucb.ucp.webdav",
166 "SerfLockStore::updateLock: lock not found!" );
168 if ( it != m_aLockInfoMap.end() )
170 (*it).second.m_nLastChanceToSendRefreshRequest
171 = nLastChanceToSendRefreshRequest;
176 void SerfLockStore::removeLock( const OUString& rLock )
178 osl::MutexGuard aGuard( m_aMutex );
180 m_aLockInfoMap.erase( rLock );
182 if ( m_aLockInfoMap.empty() )
183 stopTicker();
187 void SerfLockStore::refreshLocks()
189 osl::MutexGuard aGuard( m_aMutex );
191 LockInfoMap::iterator it( m_aLockInfoMap.begin() );
192 const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
193 while ( it != end )
195 LockInfo & rInfo = (*it).second;
196 if ( rInfo.m_nLastChanceToSendRefreshRequest != -1 )
198 // 30 seconds or less remaining until lock expires?
199 TimeValue t1;
200 osl_getSystemTime( &t1 );
201 if ( rInfo.m_nLastChanceToSendRefreshRequest - 30
202 <= sal_Int32( t1.Seconds ) )
204 // refresh the lock.
205 sal_Int32 nlastChanceToSendRefreshRequest = -1;
206 if ( rInfo.m_xSession->LOCK(
207 (*it).first, &nlastChanceToSendRefreshRequest ) )
209 rInfo.m_nLastChanceToSendRefreshRequest
210 = nlastChanceToSendRefreshRequest;
212 else
214 // refresh failed. stop auto-refresh.
215 rInfo.m_nLastChanceToSendRefreshRequest = -1;
219 ++it;
223 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */