1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #include "asyncrequests.hxx"
29 #include <vcl/svapp.hxx>
30 #include <vos/mutex.hxx>
32 //-----------------------------------------------------------------------------
34 //-----------------------------------------------------------------------------
40 namespace css
= ::com::sun::star
;
42 //-----------------------------------------------------------------------------
43 void lcl_sleep(::osl::Condition
& aCondition
,
44 ::sal_Int32 nMilliSeconds
)
46 sal_uLong nAcquireCount
= Application::ReleaseSolarMutex();
48 if (nMilliSeconds
< 1)
53 aTime
.Seconds
= (nMilliSeconds
/ 1000);
54 aTime
.Nanosec
= (nMilliSeconds
% 1000) * 1000000;
55 aCondition
.wait(&aTime
);
58 Application::AcquireSolarMutex( nAcquireCount
);
61 //-----------------------------------------------------------------------------
62 void Request::wait(::sal_Int32 nMilliSeconds
)
64 lcl_sleep(m_aJoiner
, nMilliSeconds
);
67 void Request::waitProcessMessages()
69 ::vos::OGuard
aGuard( Application::GetSolarMutex() );
70 while (!m_aJoiner
.check())
74 //-----------------------------------------------------------------------------
75 void Request::notify()
80 //-----------------------------------------------------------------------------
81 AsyncRequests::AsyncRequests(const RequestHandlerRef
& rHandler
)
82 : ::cppu::BaseMutex( )
84 , m_bFinish (sal_False
)
85 , m_rHandler (rHandler
)
90 //-----------------------------------------------------------------------------
91 AsyncRequests::~AsyncRequests()
94 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
102 void AsyncRequests::triggerRequestProcessMessages (const RequestRef
& rRequest
)
105 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
106 m_lRequests
.push(rRequest
);
113 rRequest
->waitProcessMessages();
116 //-----------------------------------------------------------------------------
117 void AsyncRequests::triggerRequestBlocked(const RequestRef
& rRequest
)
120 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
121 m_lRequests
.push(rRequest
);
128 rRequest
->wait(Request::WAIT_INFINITE
);
131 //-----------------------------------------------------------------------------
132 void AsyncRequests::triggerRequestNonBlocked(const RequestRef
& rRequest
)
135 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
136 m_lRequests
.push(rRequest
);
144 //-----------------------------------------------------------------------------
145 void AsyncRequests::triggerRequestDirectly(const RequestRef
& rRequest
)
148 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
149 RequestHandlerRef rHandler
= m_rHandler
;
153 if (rHandler
!= NULL
)
154 rHandler
->doRequest(rRequest
);
157 //-----------------------------------------------------------------------------
158 void AsyncRequests::triggerRequestThreadAware(const RequestRef
& rRequest
,
161 oslThreadIdentifier nOurThreadId
= getIdentifier();
162 oslThreadIdentifier nCallerThreadId
= ::osl::Thread::getCurrentIdentifier();
163 if (nOurThreadId
== nCallerThreadId
)
164 triggerRequestDirectly(rRequest
);
165 else if (nWait
== BLOCKED
)
166 triggerRequestBlocked(rRequest
);
167 else if (nWait
== PROCESS_MESSAGES
)
168 triggerRequestProcessMessages(rRequest
);
170 triggerRequestNonBlocked(rRequest
);
173 //-----------------------------------------------------------------------------
176 //-----------------------------------------------------------------------------
177 void SAL_CALL
AsyncRequests::run()
179 static const ::sal_Int32 TIME_TO_WAIT_FOR_NEW_REQUESTS
= 250;
182 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
183 RequestHandlerRef rHandler
= m_rHandler
;
184 ::sal_Bool bFinished
= m_bFinish
;
188 if (rHandler
!= NULL
)
191 ::osl::Condition aWait
;
199 if ( ! m_lRequests
.empty())
201 rRequest
= m_lRequests
.front();
204 bFinished
= m_bFinish
;
209 if (rRequest
== NULL
)
211 lcl_sleep(aWait
, TIME_TO_WAIT_FOR_NEW_REQUESTS
);
215 if (rHandler
!= NULL
)
217 rHandler
->doRequest(rRequest
);
222 if (rHandler
!= NULL
)
228 } // namespace fpicker