/work/libde265/libde265/threads.h
Line | Count | Source |
1 | | /* |
2 | | * H.265 video codec. |
3 | | * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de> |
4 | | * |
5 | | * This file is part of libde265. |
6 | | * |
7 | | * libde265 is free software: you can redistribute it and/or modify |
8 | | * it under the terms of the GNU Lesser General Public License as |
9 | | * published by the Free Software Foundation, either version 3 of |
10 | | * the License, or (at your option) any later version. |
11 | | * |
12 | | * libde265 is distributed in the hope that it will be useful, |
13 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | | * GNU Lesser General Public License for more details. |
16 | | * |
17 | | * You should have received a copy of the GNU Lesser General Public License |
18 | | * along with libde265. If not, see <http://www.gnu.org/licenses/>. |
19 | | */ |
20 | | |
21 | | #ifndef DE265_THREADS_H |
22 | | #define DE265_THREADS_H |
23 | | |
24 | | #include "libde265/de265.h" |
25 | | |
26 | | #ifdef HAVE_CONFIG_H |
27 | | #include "config.h" |
28 | | #endif |
29 | | |
30 | | #include <deque> |
31 | | #include <string> |
32 | | #include <atomic> |
33 | | |
34 | | #ifdef _WIN32 |
35 | | #if !defined(NOMINMAX) |
36 | | #define NOMINMAX 1 |
37 | | #endif |
38 | | #include <windows.h> |
39 | | #include "../extra/win32cond.h" |
40 | | #if _MSC_VER > 1310 |
41 | | #include <intrin.h> |
42 | | #endif |
43 | | #endif // _WIN32 |
44 | | |
45 | | #include <mutex> |
46 | | #include <condition_variable> |
47 | | #include <thread> |
48 | | |
49 | | class de265_progress_lock |
50 | | { |
51 | | public: |
52 | | de265_progress_lock(); |
53 | | ~de265_progress_lock(); |
54 | | |
55 | | void wait_for_progress(int progress); |
56 | | void set_progress(int progress); |
57 | | void increase_progress(int progress); |
58 | | int get_progress() const; |
59 | 0 | void reset(int value=0) { mProgress=value; } |
60 | | |
61 | | private: |
62 | | int mProgress; |
63 | | |
64 | | // private data |
65 | | |
66 | | std::mutex mutex; |
67 | | std::condition_variable cond; |
68 | | }; |
69 | | |
70 | | |
71 | | |
72 | | class thread_task |
73 | | { |
74 | | public: |
75 | 0 | thread_task() : state(Queued) { } |
76 | 0 | virtual ~thread_task() { } |
77 | | |
78 | | enum { Queued, Running, Blocked, Finished } state; |
79 | | |
80 | | virtual void work() = 0; |
81 | | |
82 | 0 | virtual std::string name() const { return "noname"; } |
83 | | }; |
84 | | |
85 | | |
86 | 0 | #define MAX_THREADS 32 |
87 | | |
88 | | /* TODO NOTE: When unblocking a task, we have to check first |
89 | | if there are threads waiting because of the run-count limit. |
90 | | If there are higher-priority tasks, those should be run instead |
91 | | of the just unblocked task. |
92 | | */ |
93 | | |
94 | | class thread_pool |
95 | | { |
96 | | public: |
97 | | bool stopped; |
98 | | |
99 | | std::deque<thread_task*> tasks; // we are not the owner |
100 | | |
101 | | std::thread thread[MAX_THREADS]; |
102 | | int num_threads; |
103 | | |
104 | | int num_threads_working; |
105 | | |
106 | | int ctbx[MAX_THREADS]; // the CTB the thread is working on |
107 | | int ctby[MAX_THREADS]; |
108 | | |
109 | | std::mutex mutex; |
110 | | std::condition_variable cond_var; |
111 | | }; |
112 | | |
113 | | |
114 | | de265_error start_thread_pool(thread_pool* pool, int num_threads); |
115 | | void stop_thread_pool(thread_pool* pool); // do not process remaining tasks |
116 | | |
117 | | void add_task(thread_pool* pool, thread_task* task); // TOCO: can make thread_task const |
118 | | |
119 | | #endif |