digiKam
threads.h
Go to the documentation of this file.
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 #ifdef HAVE_STDBOOL_H
31 #include <stdbool.h>
32 #endif
33 
34 #include <deque>
35 #include <string>
36 #include <atomic>
37 
38 #ifndef _WIN32
39 #include <pthread.h>
40 
41 typedef pthread_t de265_thread;
42 typedef pthread_mutex_t de265_mutex;
43 typedef pthread_cond_t de265_cond;
44 
45 #else // _WIN32
46 #if !defined(NOMINMAX)
47 #define NOMINMAX 1
48 #endif
49 #include <windows.h>
50 #include "../extra/win32cond.h"
51 #if _MSC_VER > 1310
52 #include <intrin.h>
53 #endif
54 
55 typedef HANDLE de265_thread;
56 typedef HANDLE de265_mutex;
57 typedef win32_cond_t de265_cond;
58 #endif // _WIN32
59 
60 #ifndef _WIN32
61 int de265_thread_create(de265_thread* t, void *(*start_routine) (void *), void *arg);
62 #else
63 int de265_thread_create(de265_thread* t, LPTHREAD_START_ROUTINE start_routine, void *arg);
64 #endif
76 
77 
79 {
80 public:
83 
84  void wait_for_progress(int progress);
85  void set_progress(int progress);
86  void increase_progress(int progress);
87  int get_progress() const;
88  void reset(int value=0) { mProgress=value; }
89 
90 private:
91  int mProgress;
92 
93  // private data
94 
95  de265_mutex mutex;
96  de265_cond cond;
97 };
98 
99 
100 
102 {
103 public:
105  virtual ~thread_task() { }
106 
108 
109  virtual void work() = 0;
110 
111  virtual std::string name() const { return "noname"; }
112 };
113 
114 
115 #define MAX_THREADS 32
116 
117 /* TODO NOTE: When unblocking a task, we have to check first
118  if there are threads waiting because of the run-count limit.
119  If there are higher-priority tasks, those should be run instead
120  of the just unblocked task.
121  */
122 
124 {
125  public:
126  bool stopped;
127 
128  std::deque<thread_task*> tasks; // we are not the owner
129 
132 
134 
135  int ctbx[MAX_THREADS]; // the CTB the thread is working on
137 
140 };
141 
142 
143 de265_error start_thread_pool(thread_pool* pool, int num_threads);
144 void stop_thread_pool(thread_pool* pool); // do not process remaining tasks
145 
146 void add_task(thread_pool* pool, thread_task* task); // TOCO: can make thread_task const
147 
148 #endif
Definition: threads.h:79
void reset(int value=0)
Definition: threads.h:88
int get_progress() const
void increase_progress(int progress)
void set_progress(int progress)
void wait_for_progress(int progress)
Definition: threads.h:124
std::deque< thread_task * > tasks
Definition: threads.h:128
int ctbx[MAX_THREADS]
Definition: threads.h:135
de265_mutex mutex
Definition: threads.h:138
bool stopped
Definition: threads.h:126
int ctby[MAX_THREADS]
Definition: threads.h:136
de265_cond cond_var
Definition: threads.h:139
int num_threads_working
Definition: threads.h:133
de265_thread thread[MAX_THREADS]
Definition: threads.h:130
int num_threads
Definition: threads.h:131
Definition: threads.h:102
virtual void work()=0
@ Running
Definition: threads.h:107
@ Blocked
Definition: threads.h:107
@ Finished
Definition: threads.h:107
@ Queued
Definition: threads.h:107
enum thread_task::@46 state
virtual std::string name() const
Definition: threads.h:111
thread_task()
Definition: threads.h:104
virtual ~thread_task()
Definition: threads.h:105
de265_error
Definition: de265.h:82
qulonglong value
Definition: itemviewutilities.cpp:592
de265_error start_thread_pool(thread_pool *pool, int num_threads)
void add_task(thread_pool *pool, thread_task *task)
void de265_cond_wait(de265_cond *c, de265_mutex *m)
pthread_mutex_t de265_mutex
Definition: threads.h:42
void de265_mutex_lock(de265_mutex *m)
void de265_mutex_unlock(de265_mutex *m)
void de265_cond_init(de265_cond *c)
pthread_cond_t de265_cond
Definition: threads.h:43
void de265_thread_join(de265_thread t)
void stop_thread_pool(thread_pool *pool)
int de265_thread_create(de265_thread *t, void *(*start_routine)(void *), void *arg)
#define MAX_THREADS
Definition: threads.h:115
void de265_cond_destroy(de265_cond *c)
void de265_thread_destroy(de265_thread *t)
void de265_cond_broadcast(de265_cond *c, de265_mutex *m)
pthread_t de265_thread
Definition: threads.h:41
void de265_mutex_init(de265_mutex *m)
void de265_mutex_destroy(de265_mutex *m)
void de265_cond_signal(de265_cond *c)