merged tag ooo/DEV300_m102
[LibreOffice.git] / cppu / source / threadpool / threadpool.hxx
blobe1c9a127fa5526836d0cea99510bd3f13875b558
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 ************************************************************************/
27 #include <hash_map>
29 #include <osl/conditn.h>
31 #include <rtl/byteseq.hxx>
33 #include <boost/shared_ptr.hpp>
35 #include "jobqueue.hxx"
38 using namespace ::rtl;
39 namespace cppu_threadpool {
40 class ORequestThread;
42 struct EqualThreadId
44 sal_Int32 operator () ( const ::rtl::ByteSequence &a , const ::rtl::ByteSequence &b ) const
46 return a == b;
50 struct HashThreadId
52 sal_Int32 operator () ( const ::rtl::ByteSequence &a ) const
54 if( a.getLength() >= 4 )
56 return *(sal_Int32 *)a.getConstArray();
58 return 0;
62 typedef ::std::hash_map
64 ByteSequence, // ThreadID
65 ::std::pair < JobQueue * , JobQueue * >,
66 HashThreadId,
67 EqualThreadId
68 > ThreadIdHashMap;
70 typedef ::std::list < sal_Int64 > DisposedCallerList;
73 struct WaitingThread
75 oslCondition condition;
76 ORequestThread *thread;
79 typedef ::std::list < struct ::cppu_threadpool::WaitingThread * > WaitingThreadList;
81 class DisposedCallerAdmin;
82 typedef boost::shared_ptr<DisposedCallerAdmin> DisposedCallerAdminHolder;
84 class DisposedCallerAdmin
86 public:
87 ~DisposedCallerAdmin();
89 static DisposedCallerAdminHolder getInstance();
91 void dispose( sal_Int64 nDisposeId );
92 void stopDisposing( sal_Int64 nDisposeId );
93 sal_Bool isDisposed( sal_Int64 nDisposeId );
95 private:
96 ::osl::Mutex m_mutex;
97 DisposedCallerList m_lst;
100 class ThreadPool;
101 typedef boost::shared_ptr<ThreadPool> ThreadPoolHolder;
103 class ThreadPool
105 public:
106 ThreadPool();
107 ~ThreadPool();
108 static ThreadPoolHolder getInstance();
110 void dispose( sal_Int64 nDisposeId );
111 void stopDisposing( sal_Int64 nDisposeId );
113 void addJob( const ByteSequence &aThreadId,
114 sal_Bool bAsynchron,
115 void *pThreadSpecificData,
116 RequestFun * doRequest );
118 void prepare( const ByteSequence &aThreadId );
119 void * enter( const ByteSequence &aThreadId, sal_Int64 nDisposeId );
121 /********
122 * @return true, if queue could be succesfully revoked.
123 ********/
124 sal_Bool revokeQueue( const ByteSequence & aThreadId , sal_Bool bAsynchron );
126 void waitInPool( ORequestThread *pThread );
127 private:
128 void createThread( JobQueue *pQueue, const ByteSequence &aThreadId, sal_Bool bAsynchron);
131 ThreadIdHashMap m_mapQueue;
132 ::osl::Mutex m_mutex;
134 ::osl::Mutex m_mutexWaitingThreadList;
135 WaitingThreadList m_lstThreads;
137 DisposedCallerAdminHolder m_DisposedCallerAdmin;
140 } // end namespace cppu_threadpool