1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: asyncrequests.cxx,v $
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 //-----------------------------------------------------------------------------
37 //-----------------------------------------------------------------------------
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)
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())
77 //-----------------------------------------------------------------------------
78 void Request::notify()
83 //-----------------------------------------------------------------------------
84 AsyncRequests::AsyncRequests(const RequestHandlerRef
& rHandler
)
85 : ::cppu::BaseMutex( )
87 , m_bFinish (sal_False
)
88 , m_rHandler (rHandler
)
93 //-----------------------------------------------------------------------------
94 AsyncRequests::~AsyncRequests()
97 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
105 void AsyncRequests::triggerRequestProcessMessages (const RequestRef
& rRequest
)
108 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
109 m_lRequests
.push(rRequest
);
116 rRequest
->waitProcessMessages();
119 //-----------------------------------------------------------------------------
120 void AsyncRequests::triggerRequestBlocked(const RequestRef
& rRequest
)
123 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
124 m_lRequests
.push(rRequest
);
131 rRequest
->wait(Request::WAIT_INFINITE
);
134 //-----------------------------------------------------------------------------
135 void AsyncRequests::triggerRequestNonBlocked(const RequestRef
& rRequest
)
138 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
139 m_lRequests
.push(rRequest
);
147 //-----------------------------------------------------------------------------
148 void AsyncRequests::triggerRequestDirectly(const RequestRef
& rRequest
)
151 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
152 RequestHandlerRef rHandler
= m_rHandler
;
156 if (rHandler
!= NULL
)
157 rHandler
->doRequest(rRequest
);
160 //-----------------------------------------------------------------------------
161 void AsyncRequests::triggerRequestThreadAware(const RequestRef
& rRequest
,
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
);
173 triggerRequestNonBlocked(rRequest
);
176 //-----------------------------------------------------------------------------
179 //-----------------------------------------------------------------------------
180 void SAL_CALL
AsyncRequests::run()
182 static const ::sal_Int32 TIME_TO_WAIT_FOR_NEW_REQUESTS
= 250;
185 ::osl::ResettableMutexGuard
aLock(m_aMutex
);
186 RequestHandlerRef rHandler
= m_rHandler
;
187 ::sal_Bool bFinished
= m_bFinish
;
191 if (rHandler
!= NULL
)
194 ::osl::Condition aWait
;
202 if ( ! m_lRequests
.empty())
204 rRequest
= m_lRequests
.front();
207 bFinished
= m_bFinish
;
212 if (rRequest
== NULL
)
214 lcl_sleep(aWait
, TIME_TO_WAIT_FOR_NEW_REQUESTS
);
218 if (rHandler
!= NULL
)
220 rHandler
->doRequest(rRequest
);
225 if (rHandler
!= NULL
)
231 } // namespace fpicker