Coverage Report

Created: 2026-05-30 06:08

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
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
constexpr int 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
  de265_error start(int num_threads);
98
  void        stop(); // do not process remaining tasks
99
  void        add_task(thread_task* task);
100
101
102
  bool stopped;
103
104
  std::deque<thread_task*> tasks;  // we are not the owner
105
106
  int num_threads_working;
107
108
  std::mutex  mutex;
109
  std::condition_variable  cond_var;
110
111
private:
112
  std::thread thread[MAX_THREADS];
113
  int num_threads;
114
115
  //int ctbx[MAX_THREADS]; // the CTB the thread is working on
116
  //int ctby[MAX_THREADS];
117
};
118
119
#endif