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