LibreOffice Module ucb (master)  1
NeonLockStore.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 
30 #include <ne_uri.h>
31 #include <rtl/ustring.hxx>
32 #include <osl/time.h>
33 #include <osl/thread.hxx>
34 #include <sal/log.hxx>
35 #include <salhelper/thread.hxx>
36 #include "NeonSession.hxx"
37 #include "NeonLockStore.hxx"
38 
39 using namespace webdav_ucp;
40 
41 namespace webdav_ucp {
42 
44 {
45  bool m_bFinish;
47 
48 public:
49 
50  explicit TickerThread( NeonLockStore & rLockStore )
51  : Thread( "NeonTickerThread" ), m_bFinish( false )
52  , m_rLockStore( rLockStore )
53  {
54  }
55 
56  void finish() { m_bFinish = true; }
57 
58 private:
59 
60  virtual void execute() override;
61 };
62 
63 } // namespace webdav_ucp
64 
66 {
67  SAL_INFO( "ucb.ucp.webdav", "TickerThread: start." );
68 
69  // we have to go through the loop more often to be able to finish ~quickly
70  const int nNth = 25;
71 
72  int nCount = nNth;
73  while ( !m_bFinish )
74  {
75  if (--nCount < 0)
76  {
78  nCount = nNth;
79  }
80 
81  salhelper::Thread::wait(TimeValue(0, 1000000000 / nNth));
82  }
83 
84  SAL_INFO( "ucb.ucp.webdav", "TickerThread: stop." );
85 }
86 
88  : m_pNeonLockStore( ne_lockstore_create() )
89 {
90  /*
91  * ne_lockstore_create() never returns a NULL; neon calls abort() in case of an out-of-memory
92  * situation.
93  * Please see:
94  * <http://www.webdav.org/neon/doc/html/refneon.html>
95  * topic title "Memory handling", copied here verbatim:
96  *
97  * "neon does not attempt to cope gracefully with an out-of-memory situation;
98  * instead, by default, the abort function is called to immediately terminate
99  * the process. An application may register a custom function which will be
100  * called before abort in such a situation; see ne_oom_callback."
101  */
102 }
103 
105 {
106  {
107  osl::ClearableMutexGuard aGuard(m_aMutex);
108  stopTicker(aGuard);
109  } // actually no threads should even try to access members now
110 
111  // release active locks, if any.
112  SAL_WARN_IF( !m_aLockInfoMap.empty(), "ucb.ucp.webdav", "NeonLockStore::~NeonLockStore - Releasing active locks!" );
113 
114  for ( auto& rLockInfo : m_aLockInfoMap )
115  {
116  NeonLock * pLock = rLockInfo.first;
117  rLockInfo.second.xSession->UNLOCK( pLock );
118 
119  ne_lockstore_remove( m_pNeonLockStore, pLock );
120  ne_lock_destroy( pLock );
121  }
122 
123  ne_lockstore_destroy( m_pNeonLockStore );
124 }
125 
127 {
128  osl::MutexGuard aGuard( m_aMutex );
129 
130  if ( !m_pTickerThread.is() )
131  {
132  m_pTickerThread = new TickerThread( *this );
133  m_pTickerThread->launch();
134  }
135 }
136 
137 void NeonLockStore::stopTicker(osl::ClearableMutexGuard & rGuard)
138 {
139  rtl::Reference<TickerThread> pTickerThread;
140 
141  if (m_pTickerThread.is())
142  {
143  m_pTickerThread->finish(); // needs mutex
144  // the TickerThread may run refreshLocks() at most once after this
145  pTickerThread = m_pTickerThread;
146  m_pTickerThread.clear();
147  }
148 
149  rGuard.clear();
150 
151  if (pTickerThread.is() && pTickerThread->getIdentifier() != osl::Thread::getCurrentIdentifier())
152  pTickerThread->join(); // without m_aMutex locked (to prevent deadlock)
153 }
154 
156 {
157  osl::MutexGuard aGuard( m_aMutex );
158 
159  ne_lockstore_register( m_pNeonLockStore, pHttpSession );
160 }
161 
162 NeonLock * NeonLockStore::findByUri( OUString const & rUri )
163 {
164  osl::MutexGuard aGuard( m_aMutex );
165 
166  ne_uri aUri;
167  ne_uri_parse( OUStringToOString(
168  rUri, RTL_TEXTENCODING_UTF8 ).getStr(), &aUri );
169  return ne_lockstore_findbyuri( m_pNeonLockStore, &aUri );
170 }
171 
173  rtl::Reference< NeonSession > const & xSession,
174  sal_Int32 nLastChanceToSendRefreshRequest )
175 {
176  osl::MutexGuard aGuard( m_aMutex );
177 
178  ne_lockstore_add( m_pNeonLockStore, pLock );
179  m_aLockInfoMap[ pLock ]
180  = LockInfo( xSession, nLastChanceToSendRefreshRequest );
181 
182  startTicker();
183 }
184 
186 {
187  osl::ClearableMutexGuard aGuard( m_aMutex );
188 
189  m_aLockInfoMap.erase( pLock );
190  ne_lockstore_remove( m_pNeonLockStore, pLock );
191 
192  if ( m_aLockInfoMap.empty() )
193  stopTicker(aGuard);
194 }
195 
197 {
198  osl::MutexGuard aGuard(m_aMutex);
199 
200  m_aRemoveDeferred.push_back(pLock);
201 }
202 
204 {
205  osl::MutexGuard aGuard( m_aMutex );
206 
207  for ( auto& rLockInfo : m_aLockInfoMap )
208  {
209  LockInfo & rInfo = rLockInfo.second;
210  if ( rInfo.nLastChanceToSendRefreshRequest != -1 )
211  {
212  // 30 seconds or less remaining until lock expires?
213  TimeValue t1;
214  osl_getSystemTime( &t1 );
215  if ( rInfo.nLastChanceToSendRefreshRequest - 30
216  <= sal_Int32( t1.Seconds ) )
217  {
218  // refresh the lock.
219  sal_Int32 nlastChanceToSendRefreshRequest = -1;
220  if ( rInfo.xSession->LOCK(
221  rLockInfo.first,
222  /* out param */ nlastChanceToSendRefreshRequest ) )
223  {
225  = nlastChanceToSendRefreshRequest;
226  }
227  else
228  {
229  // refresh failed. stop auto-refresh.
231  }
232  }
233  }
234  }
235  // removeLock will not need to actually release the lock, because this is run from TickerThread
236  for (auto pLock : m_aRemoveDeferred)
237  removeLock(pLock);
238  m_aRemoveDeferred.clear();
239 }
240 
241 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void registerSession(HttpSession *pHttpSession)
NeonLockStore & m_rLockStore
rtl::Reference< NeonSession > xSession
void removeLockDeferred(NeonLock *pLock)
Thread(char const *name)
ne_session HttpSession
Definition: NeonTypes.hxx:47
struct ne_lock NeonLock
Definition: NeonTypes.hxx:53
sal_Int32 nLastChanceToSendRefreshRequest
void stopTicker(osl::ClearableMutexGuard &rGuard)
NeonLock * findByUri(OUString const &rUri)
rtl::Reference< TickerThread > m_pTickerThread
ne_lock_store *const m_pNeonLockStore
OString OUStringToOString(const OUString &str, ConnectionSettings const *settings)
void addLock(NeonLock *pLock, rtl::Reference< NeonSession > const &xSession, sal_Int32 nLastChanceToSendRefreshRequest)
#define SAL_WARN_IF(condition, area, stream)
virtual void execute() override
TickerThread(NeonLockStore &rLockStore)
#define SAL_INFO(area, stream)
void removeLock(NeonLock *pLock)
std::vector< NeonLock * > m_aRemoveDeferred