7616670b9b1110e886d00fbab75a906c962c981b
[deb_x265.git] / source / common / threadpool.h
1 /*****************************************************************************
2 * x265: singleton thread pool and interface classes
3 *****************************************************************************
4 * Copyright (C) 2013 x265 project
5 *
6 * Authors: Steve Borho <steve@borho.org>
7 *
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.
12 *
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.
17 *
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.
21 *
22 * This program is also available under a commercial proprietary license.
23 * For more information, contact us at license @ x265.com
24 *****************************************************************************/
25
26 #ifndef X265_THREADPOOL_H
27 #define X265_THREADPOOL_H
28
29 #include "common.h"
30
31 namespace x265 {
32 // x265 private namespace
33
34 class ThreadPool;
35
36 int getCpuCount();
37
38 // Any class that wants to distribute work to the thread pool must
39 // derive from JobProvider and implement FindJob().
40 class JobProvider
41 {
42 protected:
43
44 ThreadPool *m_pool;
45
46 JobProvider *m_nextProvider;
47 JobProvider *m_prevProvider;
48
49 public:
50
51 JobProvider(ThreadPool *p) : m_pool(p), m_nextProvider(0), m_prevProvider(0) {}
52
53 virtual ~JobProvider() {}
54
55 void setThreadPool(ThreadPool *p) { m_pool = p; }
56
57 // Register this job provider with the thread pool, jobs are available
58 void enqueue();
59
60 // Remove this job provider from the thread pool, all jobs complete
61 void dequeue();
62
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;
66
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
70 void flush();
71
72 friend class ThreadPoolImpl;
73 friend class PoolThread;
74 };
75
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,
79 // etc).
80 class ThreadPool
81 {
82 protected:
83
84 // Destructor is inaccessable, force the use of reference counted Release()
85 ~ThreadPool() {}
86
87 virtual void enqueueJobProvider(JobProvider &) = 0;
88
89 virtual void dequeueJobProvider(JobProvider &) = 0;
90
91 public:
92
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);
96
97 static ThreadPool *getThreadPool();
98
99 virtual void pokeIdleThread() = 0;
100
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;
104
105 virtual int getThreadCount() const = 0;
106
107 friend class JobProvider;
108 };
109 } // end namespace x265
110
111 #endif // ifndef X265_THREADPOOL_H