Coverage Report

Created: 2025-06-16 07:00

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