1 /*****************************************************************************
2 * x265: singleton thread pool and interface classes
3 *****************************************************************************
4 * Copyright (C) 2013 x265 project
6 * Authors: Steve Borho <steve@borho.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA.
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at license @ x265.com
24 *****************************************************************************/
26 #ifndef X265_THREADPOOL_H
27 #define X265_THREADPOOL_H
32 // x265 private namespace
38 // Any class that wants to distribute work to the thread pool must
39 // derive from JobProvider and implement FindJob().
46 JobProvider
*m_nextProvider
;
47 JobProvider
*m_prevProvider
;
51 JobProvider(ThreadPool
*p
) : m_pool(p
), m_nextProvider(0), m_prevProvider(0) {}
53 virtual ~JobProvider() {}
55 void setThreadPool(ThreadPool
*p
) { m_pool
= p
; }
57 // Register this job provider with the thread pool, jobs are available
60 // Remove this job provider from the thread pool, all jobs complete
63 // Worker threads will call this method to find a job. Must return true if
64 // work was completed. False if no work was available.
65 virtual bool findJob(int threadId
) = 0;
67 // All derived objects that call Enqueue *MUST* call flush before allowing
68 // their object to be destroyed, otherwise you will see random crashes involving
69 // partially freed vtables and you will be unhappy
72 friend class ThreadPoolImpl
;
73 friend class PoolThread
;
76 // Abstract interface to ThreadPool. Each encoder instance should call
77 // AllocThreadPool() to get a handle to the singleton object and then make
78 // it available to their job provider structures (wave-front frame encoders,
84 // Destructor is inaccessable, force the use of reference counted Release()
87 virtual void enqueueJobProvider(JobProvider
&) = 0;
89 virtual void dequeueJobProvider(JobProvider
&) = 0;
93 // When numthreads == 0, a default thread count is used. A request may grow
94 // an existing pool but it will never shrink.
95 static ThreadPool
*allocThreadPool(int numthreads
= 0);
97 static ThreadPool
*getThreadPool();
99 virtual void pokeIdleThread() = 0;
101 // The pool is reference counted so all calls to AllocThreadPool() should be
102 // followed by a call to Release()
103 virtual void release() = 0;
105 virtual int getThreadCount() const = 0;
107 friend class JobProvider
;
109 } // end namespace x265
111 #endif // ifndef X265_THREADPOOL_H