Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
11 : : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : : * contributor license agreements. See the NOTICE file distributed
13 : : * with this work for additional information regarding copyright
14 : : * ownership. The ASF licenses this file to you under the Apache
15 : : * License, Version 2.0 (the "License"); you may not use this file
16 : : * except in compliance with the License. You may obtain a copy of
17 : : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : : */
19 : :
20 : : #include "jobqueue.hxx"
21 : : #include "threadpool.hxx"
22 : :
23 : : #include <osl/diagnose.h>
24 : :
25 : : using namespace ::osl;
26 : :
27 : : namespace cppu_threadpool {
28 : :
29 : 308167 : JobQueue::JobQueue() :
30 : : m_nToDo( 0 ),
31 : : m_bSuspended( sal_False ),
32 [ + - ][ + - ]: 308167 : m_cndWait( osl_createCondition() )
[ + - ][ + - ]
33 : : {
34 [ + - ]: 308167 : osl_resetCondition( m_cndWait );
35 [ + - ][ + - ]: 308167 : m_DisposedCallerAdmin = DisposedCallerAdmin::getInstance();
[ + - ]
36 : 308167 : }
37 : :
38 [ + - ]: 308114 : JobQueue::~JobQueue()
39 : : {
40 [ + - ]: 308114 : osl_destroyCondition( m_cndWait );
41 : 308114 : }
42 : :
43 : :
44 : 476763 : void JobQueue::add( void *pThreadSpecificData, RequestFun * doRequest )
45 : : {
46 [ + - ]: 476763 : MutexGuard guard( m_mutex );
47 : 476763 : Job job = { pThreadSpecificData , doRequest };
48 [ + - ]: 476763 : m_lstJob.push_back( job );
49 [ + + ]: 476763 : if( ! m_bSuspended )
50 : : {
51 [ + - ]: 476750 : osl_setCondition( m_cndWait );
52 : : }
53 [ + - ]: 476763 : m_nToDo ++;
54 : 476763 : }
55 : :
56 : 321710 : void *JobQueue::enter( sal_Int64 nDisposeId , sal_Bool bReturnWhenNoJob )
57 : : {
58 : 321710 : void *pReturn = 0;
59 : : {
60 : : // synchronize with the dispose calls
61 [ + - ]: 321710 : MutexGuard guard( m_mutex );
62 [ + - ][ - + ]: 321717 : if( m_DisposedCallerAdmin->isDisposed( nDisposeId ) )
63 : : {
64 : 0 : return 0;
65 : : }
66 [ + - ][ + - ]: 321720 : m_lstCallstack.push_front( nDisposeId );
[ + - ]
67 : : }
68 : :
69 : :
70 : 463157 : while( sal_True )
71 : : {
72 [ + + ]: 784877 : if( bReturnWhenNoJob )
73 : : {
74 [ + - ]: 763251 : MutexGuard guard( m_mutex );
75 [ + + ]: 763251 : if( m_lstJob.empty() )
76 : : {
77 : : break;
78 [ + - ][ + + ]: 763252 : }
79 : : }
80 : :
81 [ + - ]: 476765 : osl_waitCondition( m_cndWait , 0 );
82 : :
83 : 476758 : struct Job job={0,0};
84 : : {
85 : : // synchronize with add and dispose calls
86 [ + - ]: 476758 : MutexGuard guard( m_mutex );
87 : :
88 [ + - ][ - + ]: 476763 : if( 0 == m_lstCallstack.front() )
89 : : {
90 : : // disposed !
91 [ # # # # ]: 0 : if( m_lstJob.empty()
[ # # ][ # # ]
92 : 0 : && (m_lstCallstack.empty()
93 [ # # ]: 0 : || m_lstCallstack.front() != 0) )
94 : : {
95 [ # # ]: 0 : osl_resetCondition( m_cndWait );
96 : : }
97 : : break;
98 : : }
99 : :
100 : : OSL_ASSERT( ! m_lstJob.empty() );
101 [ + - ]: 476760 : if( ! m_lstJob.empty() )
102 : : {
103 [ + - ]: 476761 : job = m_lstJob.front();
104 [ + - ]: 476760 : m_lstJob.pop_front();
105 : : }
106 [ + + + - ]: 1341063 : if( m_lstJob.empty()
[ + - ][ + + ]
107 [ + - ]: 864300 : && (m_lstCallstack.empty() || m_lstCallstack.front() != 0) )
108 : : {
109 [ + - ]: 476763 : osl_resetCondition( m_cndWait );
110 [ + - ][ + - ]: 476763 : }
111 : : }
112 : :
113 [ + + ]: 476763 : if( job.doRequest )
114 : : {
115 [ + - ]: 463157 : job.doRequest( job.pThreadSpecificData );
116 [ + - ]: 463155 : MutexGuard guard( m_mutex );
117 [ + - ]: 463157 : m_nToDo --;
118 : : }
119 : : else
120 : : {
121 : 13606 : pReturn = job.pThreadSpecificData;
122 [ + - ]: 13606 : MutexGuard guard( m_mutex );
123 : 13606 : m_nToDo --;
124 [ + - ]: 13606 : break;
125 : : }
126 : : }
127 : :
128 : : {
129 : : // synchronize with the dispose calls
130 [ + - ]: 321720 : MutexGuard guard( m_mutex );
131 [ + - ][ + - ]: 321720 : m_lstCallstack.pop_front();
132 : : }
133 : :
134 : 321720 : return pReturn;
135 : : }
136 : :
137 : 61 : void JobQueue::dispose( sal_Int64 nDisposeId )
138 : : {
139 [ + - ]: 61 : MutexGuard guard( m_mutex );
140 [ + - + - ]: 138 : for( CallStackList::iterator ii = m_lstCallstack.begin() ;
[ + + ]
141 : 69 : ii != m_lstCallstack.end() ;
142 : : ++ii )
143 : : {
144 [ + - ][ - + ]: 8 : if( (*ii) == nDisposeId )
145 : : {
146 [ # # ]: 0 : (*ii) = 0;
147 : : }
148 : : }
149 : :
150 [ + + ][ + - ]: 61 : if( !m_lstCallstack.empty() && ! m_lstCallstack.front() )
[ - + ][ - + ]
151 : : {
152 : : // The thread is waiting for a disposed pCallerId, let it go
153 [ # # ]: 0 : osl_setCondition( m_cndWait );
154 [ + - ]: 61 : }
155 : 61 : }
156 : :
157 : 13 : void JobQueue::suspend()
158 : : {
159 [ + - ]: 13 : MutexGuard guard( m_mutex );
160 [ + - ]: 13 : m_bSuspended = sal_True;
161 : 13 : }
162 : :
163 : 124 : void JobQueue::resume()
164 : : {
165 [ + - ]: 124 : MutexGuard guard( m_mutex );
166 : 124 : m_bSuspended = sal_False;
167 [ + + ]: 124 : if( ! m_lstJob.empty() )
168 : : {
169 [ + - ]: 43 : osl_setCondition( m_cndWait );
170 [ + - ]: 124 : }
171 : 124 : }
172 : :
173 : 1232355 : sal_Bool JobQueue::isEmpty() const
174 : : {
175 [ + - ]: 1232355 : MutexGuard guard( m_mutex );
176 [ + - ]: 1232362 : return m_lstJob.empty();
177 : : }
178 : :
179 : 13606 : sal_Bool JobQueue::isCallstackEmpty() const
180 : : {
181 [ + - ]: 13606 : MutexGuard guard( m_mutex );
182 [ + - ]: 13606 : return m_lstCallstack.empty();
183 : : }
184 : :
185 : 14 : sal_Bool JobQueue::isBusy() const
186 : : {
187 [ + - ]: 14 : MutexGuard guard( m_mutex );
188 [ + - ]: 14 : return m_nToDo > 0;
189 : : }
190 : :
191 : :
192 : : }
193 : :
194 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|