Line data Source code
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 "warnings_guard_ne_locks.h"
31 : #include <ne_uri.h>
32 : #include "rtl/ustring.hxx"
33 : #include "osl/time.h"
34 : #include "osl/thread.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 :
43 0 : class TickerThread : public salhelper::Thread
44 : {
45 : bool m_bFinish;
46 : NeonLockStore & m_rLockStore;
47 :
48 : public:
49 :
50 0 : TickerThread( NeonLockStore & rLockStore )
51 : : Thread( "NeonTickerThread" ), m_bFinish( false ),
52 0 : m_rLockStore( rLockStore ) {}
53 :
54 0 : void finish() { m_bFinish = true; }
55 :
56 : private:
57 :
58 : virtual void execute();
59 : };
60 :
61 : } // namespace webdav_ucp
62 :
63 0 : void TickerThread::execute()
64 : {
65 : OSL_TRACE( "TickerThread: start." );
66 :
67 : // we have to go through the loop more often to be able to finish ~quickly
68 0 : const int nNth = 25;
69 :
70 0 : int nCount = nNth;
71 0 : while ( !m_bFinish )
72 : {
73 0 : if ( nCount-- <= 0 )
74 : {
75 0 : m_rLockStore.refreshLocks();
76 0 : nCount = nNth;
77 : }
78 :
79 : TimeValue aTV;
80 0 : aTV.Seconds = 0;
81 0 : aTV.Nanosec = 1000000000 / nNth;
82 0 : salhelper::Thread::wait( aTV );
83 : }
84 :
85 : OSL_TRACE( "TickerThread: stop." );
86 0 : }
87 :
88 1 : NeonLockStore::NeonLockStore()
89 1 : : m_pNeonLockStore( ne_lockstore_create() )
90 : {
91 : OSL_ENSURE( m_pNeonLockStore, "Unable to create neon lock store!" );
92 1 : }
93 :
94 2 : NeonLockStore::~NeonLockStore()
95 : {
96 1 : osl::ResettableMutexGuard aGuard(m_aMutex);
97 1 : stopTicker(aGuard);
98 1 : aGuard.reset(); // actually no threads should even try to access members now
99 :
100 : // release active locks, if any.
101 : OSL_ENSURE( m_aLockInfoMap.empty(),
102 : "NeonLockStore::~NeonLockStore - Releasing active locks!" );
103 :
104 1 : LockInfoMap::const_iterator it( m_aLockInfoMap.begin() );
105 1 : const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
106 2 : while ( it != end )
107 : {
108 0 : NeonLock * pLock = (*it).first;
109 0 : (*it).second.xSession->UNLOCK( pLock );
110 :
111 0 : ne_lockstore_remove( m_pNeonLockStore, pLock );
112 0 : ne_lock_destroy( pLock );
113 :
114 0 : ++it;
115 : }
116 :
117 1 : ne_lockstore_destroy( m_pNeonLockStore );
118 1 : }
119 :
120 0 : void NeonLockStore::startTicker()
121 : {
122 0 : osl::MutexGuard aGuard( m_aMutex );
123 :
124 0 : if ( !m_pTickerThread.is() )
125 : {
126 0 : m_pTickerThread = new TickerThread( *this );
127 0 : m_pTickerThread->launch();
128 0 : }
129 0 : }
130 :
131 1 : void NeonLockStore::stopTicker(osl::ClearableMutexGuard & rGuard)
132 : {
133 1 : rtl::Reference<TickerThread> pTickerThread;
134 :
135 1 : if (m_pTickerThread.is())
136 : {
137 0 : m_pTickerThread->finish(); // needs mutex
138 : // the TickerThread may run refreshLocks() at most once after this
139 0 : pTickerThread = m_pTickerThread;
140 0 : m_pTickerThread.clear();
141 : }
142 :
143 1 : rGuard.clear();
144 :
145 1 : if (pTickerThread.is())
146 0 : pTickerThread->join(); // without m_aMutex locked (to prevent deadlock)
147 1 : }
148 :
149 0 : void NeonLockStore::registerSession( HttpSession * pHttpSession )
150 : {
151 0 : osl::MutexGuard aGuard( m_aMutex );
152 :
153 0 : ne_lockstore_register( m_pNeonLockStore, pHttpSession );
154 0 : }
155 :
156 0 : NeonLock * NeonLockStore::findByUri( OUString const & rUri )
157 : {
158 0 : osl::MutexGuard aGuard( m_aMutex );
159 :
160 : ne_uri aUri;
161 : ne_uri_parse( OUStringToOString(
162 0 : rUri, RTL_TEXTENCODING_UTF8 ).getStr(), &aUri );
163 0 : return ne_lockstore_findbyuri( m_pNeonLockStore, &aUri );
164 : }
165 :
166 0 : void NeonLockStore::addLock( NeonLock * pLock,
167 : rtl::Reference< NeonSession > const & xSession,
168 : sal_Int32 nLastChanceToSendRefreshRequest )
169 : {
170 0 : osl::MutexGuard aGuard( m_aMutex );
171 :
172 0 : ne_lockstore_add( m_pNeonLockStore, pLock );
173 0 : m_aLockInfoMap[ pLock ]
174 0 : = LockInfo( xSession, nLastChanceToSendRefreshRequest );
175 :
176 0 : startTicker();
177 0 : }
178 :
179 0 : void NeonLockStore::updateLock( NeonLock * pLock,
180 : sal_Int32 nLastChanceToSendRefreshRequest )
181 : {
182 0 : osl::MutexGuard aGuard( m_aMutex );
183 :
184 0 : LockInfoMap::iterator it( m_aLockInfoMap.find( pLock ) );
185 : OSL_ENSURE( it != m_aLockInfoMap.end(),
186 : "NeonLockStore::updateLock: lock not found!" );
187 :
188 0 : if ( it != m_aLockInfoMap.end() )
189 : {
190 0 : (*it).second.nLastChanceToSendRefreshRequest
191 0 : = nLastChanceToSendRefreshRequest;
192 0 : }
193 0 : }
194 :
195 0 : void NeonLockStore::removeLock( NeonLock * pLock )
196 : {
197 0 : osl::ClearableMutexGuard aGuard( m_aMutex );
198 :
199 0 : m_aLockInfoMap.erase( pLock );
200 0 : ne_lockstore_remove( m_pNeonLockStore, pLock );
201 :
202 0 : if ( m_aLockInfoMap.empty() )
203 0 : stopTicker(aGuard);
204 0 : }
205 :
206 0 : void NeonLockStore::refreshLocks()
207 : {
208 0 : osl::MutexGuard aGuard( m_aMutex );
209 :
210 0 : LockInfoMap::iterator it( m_aLockInfoMap.begin() );
211 0 : const LockInfoMap::const_iterator end( m_aLockInfoMap.end() );
212 0 : while ( it != end )
213 : {
214 0 : LockInfo & rInfo = (*it).second;
215 0 : if ( rInfo.nLastChanceToSendRefreshRequest != -1 )
216 : {
217 : // 30 seconds or less remaining until lock expires?
218 : TimeValue t1;
219 0 : osl_getSystemTime( &t1 );
220 0 : if ( rInfo.nLastChanceToSendRefreshRequest - 30
221 0 : <= sal_Int32( t1.Seconds ) )
222 : {
223 : // refresh the lock.
224 0 : sal_Int32 nlastChanceToSendRefreshRequest = -1;
225 0 : if ( rInfo.xSession->LOCK(
226 0 : (*it).first,
227 0 : /* out param */ nlastChanceToSendRefreshRequest ) )
228 : {
229 : rInfo.nLastChanceToSendRefreshRequest
230 0 : = nlastChanceToSendRefreshRequest;
231 : }
232 : else
233 : {
234 : // refresh failed. stop auto-refresh.
235 0 : rInfo.nLastChanceToSendRefreshRequest = -1;
236 : }
237 : }
238 : }
239 0 : ++it;
240 0 : }
241 0 : }
242 :
243 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|