File: kff_kmc2_reader_utils.h

package info (click to toggle)
kmc 3.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,716 kB
  • sloc: cpp: 38,308; python: 664; makefile: 216; perl: 179; sh: 34
file content (118 lines) | stat: -rw-r--r-- 2,992 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
This file is a part of KMC software distributed under GNU GPL 3 licence.
The homepage of the KMC project is http://sun.aei.polsl.pl/kmc

Authors: Marek Kokot

Version: 3.2.4
Date   : 2024-02-09
*/

#ifndef _KFF_KMC2_UTILS_H
#define _KFF_KMC2_UTILS_H

#include "defs.h"
#include <mutex>
#include <thread>

//************************************************************************************************************
// CParentSubthreadSynchronizer - Synchronize subthreads created by CMergerParent
//************************************************************************************************************
class CParentSubthreadSynchronizer
{
	uint32 n_tasks = 0;
	std::mutex mtx;
	std::condition_variable cv;
public:
	void decrement()
	{
		std::lock_guard<std::mutex> lck(mtx);
		--n_tasks;
	}
	void increment()
	{
		std::lock_guard<std::mutex> lck(mtx);
		++n_tasks;
	}

	void wait()
	{
		std::unique_lock<std::mutex> lck(mtx);
		cv.wait(lck, [this] {return !n_tasks; });
	}

	void notify_task_finished()
	{
		std::lock_guard<std::mutex> lck(mtx);
		--n_tasks;
		if (!n_tasks)
			cv.notify_all();
	}
};

//************************************************************************************************************
// CParentSubthreadPartDesc - Contains current state of buffers
//************************************************************************************************************
struct CParentSubthreadPartDesc
{
	uint32 start, end, part_end;
	uint32 left()
	{
		return end - part_end;
	};
};


//************************************************************************************************************
// CParentSubthreadDesc - Input data of subthreads of CMergerParent
//************************************************************************************************************
template<unsigned SIZE>
struct CParentSubthreadDesc
{
	std::vector<CBundleData<SIZE>>* inputs;
	CBundleData<SIZE>* out;
	std::vector<CParentSubthreadPartDesc> desc;
	uint32 o_start;
};


//************************************************************************************************************
// CParentSubthreadDescQueue - Passes data to subthreads from CMergerParent
//************************************************************************************************************
template<unsigned SIZE>
class CParentSubthreadDescQueue
{
	mutable std::mutex mtx;
	std::condition_variable cv;
	bool empty = true;
	bool completed = false;
public:
	CParentSubthreadDesc<SIZE> desc;
	void start()
	{
		std::unique_lock<std::mutex> lck(mtx);
		cv.wait(lck, [this] {return empty; });
		empty = false;
		cv.notify_all();
	}

	bool pop(CParentSubthreadDesc<SIZE>& _desc)
	{
		std::unique_lock<std::mutex> lck(mtx);
		cv.wait(lck, [this] {return !empty || completed; });
		if (completed)
			return false;
		_desc = desc;
		empty = true;
		cv.notify_all();
		return true;
	}
	void mark_completed()
	{
		std::lock_guard<std::mutex> lck(mtx);
		completed = true;
		cv.notify_all();
	}
};

#endif