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