Update ooo320-m1
[ooovba.git] / fpicker / source / win32 / filepicker / asyncrequests.cxx
blobbc4515c13b057e4001080bd46123d4a779754c85
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: asyncrequests.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "asyncrequests.hxx"
32 #include <vcl/svapp.hxx>
33 #include <vos/mutex.hxx>
35 //-----------------------------------------------------------------------------
36 // namespace
37 //-----------------------------------------------------------------------------
39 namespace fpicker{
40 namespace win32{
41 namespace vista{
43 namespace css = ::com::sun::star;
45 //-----------------------------------------------------------------------------
46 void lcl_sleep(::osl::Condition& aCondition ,
47 ::sal_Int32 nMilliSeconds)
49 ULONG nAcquireCount = Application::ReleaseSolarMutex();
51 if (nMilliSeconds < 1)
52 aCondition.wait(0);
53 else
55 TimeValue aTime;
56 aTime.Seconds = (nMilliSeconds / 1000);
57 aTime.Nanosec = (nMilliSeconds % 1000) * 1000000;
58 aCondition.wait(&aTime);
61 Application::AcquireSolarMutex( nAcquireCount );
64 //-----------------------------------------------------------------------------
65 void Request::wait(::sal_Int32 nMilliSeconds)
67 lcl_sleep(m_aJoiner, nMilliSeconds);
70 void Request::waitProcessMessages()
72 ::vos::OGuard aGuard( Application::GetSolarMutex() );
73 while (!m_aJoiner.check())
74 Application::Yield();
77 //-----------------------------------------------------------------------------
78 void Request::notify()
80 m_aJoiner.set();
83 //-----------------------------------------------------------------------------
84 AsyncRequests::AsyncRequests(const RequestHandlerRef& rHandler)
85 : ::cppu::BaseMutex( )
86 , ::osl::Thread ( )
87 , m_bFinish (sal_False)
88 , m_rHandler (rHandler )
89 , m_lRequests ( )
93 //-----------------------------------------------------------------------------
94 AsyncRequests::~AsyncRequests()
96 // SYNCHRONIZED ->
97 ::osl::ResettableMutexGuard aLock(m_aMutex);
98 m_bFinish = sal_True;
99 aLock.clear();
100 // <- SYNCHRONIZED
102 join();
105 void AsyncRequests::triggerRequestProcessMessages (const RequestRef& rRequest)
107 // SYNCHRONIZED ->
108 ::osl::ResettableMutexGuard aLock(m_aMutex);
109 m_lRequests.push(rRequest);
110 aLock.clear();
111 // <- SYNCHRONIZED
113 if ( ! isRunning())
114 create();
116 rRequest->waitProcessMessages();
119 //-----------------------------------------------------------------------------
120 void AsyncRequests::triggerRequestBlocked(const RequestRef& rRequest)
122 // SYNCHRONIZED ->
123 ::osl::ResettableMutexGuard aLock(m_aMutex);
124 m_lRequests.push(rRequest);
125 aLock.clear();
126 // <- SYNCHRONIZED
128 if ( ! isRunning())
129 create();
131 rRequest->wait(Request::WAIT_INFINITE);
134 //-----------------------------------------------------------------------------
135 void AsyncRequests::triggerRequestNonBlocked(const RequestRef& rRequest)
137 // SYNCHRONIZED ->
138 ::osl::ResettableMutexGuard aLock(m_aMutex);
139 m_lRequests.push(rRequest);
140 aLock.clear();
141 // <- SYNCHRONIZED
143 if ( ! isRunning())
144 create();
147 //-----------------------------------------------------------------------------
148 void AsyncRequests::triggerRequestDirectly(const RequestRef& rRequest)
150 // SYNCHRONIZED ->
151 ::osl::ResettableMutexGuard aLock(m_aMutex);
152 RequestHandlerRef rHandler = m_rHandler;
153 aLock.clear();
154 // <- SYNCHRONIZED
156 if (rHandler != NULL)
157 rHandler->doRequest(rRequest);
160 //-----------------------------------------------------------------------------
161 void AsyncRequests::triggerRequestThreadAware(const RequestRef& rRequest,
162 ::sal_Int16 nWait )
164 oslThreadIdentifier nOurThreadId = getIdentifier();
165 oslThreadIdentifier nCallerThreadId = ::osl::Thread::getCurrentIdentifier();
166 if (nOurThreadId == nCallerThreadId)
167 triggerRequestDirectly(rRequest);
168 else if (nWait == BLOCKED)
169 triggerRequestBlocked(rRequest);
170 else if (nWait == PROCESS_MESSAGES)
171 triggerRequestProcessMessages(rRequest);
172 else
173 triggerRequestNonBlocked(rRequest);
176 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
180 void SAL_CALL AsyncRequests::run()
182 static const ::sal_Int32 TIME_TO_WAIT_FOR_NEW_REQUESTS = 250;
184 // SYNCHRONIZED ->
185 ::osl::ResettableMutexGuard aLock(m_aMutex);
186 RequestHandlerRef rHandler = m_rHandler;
187 ::sal_Bool bFinished = m_bFinish;
188 aLock.clear();
189 // <- SYNCHRONIZED
191 if (rHandler != NULL)
192 rHandler->before();
194 ::osl::Condition aWait;
196 while ( ! bFinished)
198 // SYNCHRONIZED ->
199 aLock.reset();
201 RequestRef rRequest;
202 if ( ! m_lRequests.empty())
204 rRequest = m_lRequests.front();
205 m_lRequests.pop();
207 bFinished = m_bFinish;
209 aLock.clear();
210 // <- SYNCHRONIZED
212 if (rRequest == NULL)
214 lcl_sleep(aWait, TIME_TO_WAIT_FOR_NEW_REQUESTS);
215 continue;
218 if (rHandler != NULL)
220 rHandler->doRequest(rRequest);
221 rRequest->notify();
225 if (rHandler != NULL)
226 rHandler->after();
229 } // namespace vista
230 } // namespace win32
231 } // namespace fpicker