]> sigrok.org Git - pulseview.git/blob - pv/data/segment.hpp
Better segment handling in math signal and lock avoidance
[pulseview.git] / pv / data / segment.hpp
1 /*
2  * This file is part of the PulseView project.
3  *
4  * Copyright (C) 2017 Soeren Apel <soeren@apelpie.net>
5  * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program 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 General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #ifndef PULSEVIEW_PV_DATA_SEGMENT_HPP
22 #define PULSEVIEW_PV_DATA_SEGMENT_HPP
23
24 #include "pv/util.hpp"
25
26 #include <atomic>
27 #include <mutex>
28 #include <thread>
29 #include <deque>
30
31 #include <QObject>
32
33 using std::atomic;
34 using std::recursive_mutex;
35 using std::deque;
36
37 namespace SegmentTest {
38 struct SmallSize8Single;
39 struct MediumSize8Single;
40 struct MaxSize8Single;
41 struct MediumSize24Single;
42 struct MediumSize32Single;
43 struct MaxSize32Single;
44 struct MediumSize32Multi;
45 struct MaxSize32Multi;
46 struct MaxSize32MultiAtOnce;
47 struct MaxSize32MultiIterated;
48 }  // namespace SegmentTest
49
50 namespace pv {
51 namespace data {
52
53 typedef struct {
54         uint64_t sample_index, chunk_num, chunk_offs;
55         uint8_t* chunk;
56 } SegmentDataIterator;
57
58 class Segment : public QObject
59 {
60         Q_OBJECT
61
62 private:
63         static const uint64_t MaxChunkSize;
64
65 public:
66         Segment(uint32_t segment_id, uint64_t samplerate, unsigned int unit_size);
67
68         virtual ~Segment();
69
70         uint64_t get_sample_count() const;
71
72         const pv::util::Timestamp& start_time() const;
73
74         double samplerate() const;
75         void set_samplerate(double samplerate);
76
77         unsigned int unit_size() const;
78
79         uint32_t segment_id() const;
80
81         void set_complete();
82         bool is_complete() const;
83
84         void free_unused_memory();
85
86 Q_SIGNALS:
87         void completed();
88
89 protected:
90         void append_single_sample(void *data);
91         void append_samples(void *data, uint64_t samples);
92         const uint8_t* get_raw_sample(uint64_t sample_num) const;
93         void get_raw_samples(uint64_t start, uint64_t count, uint8_t *dest) const;
94
95         SegmentDataIterator* begin_sample_iteration(uint64_t start);
96         void continue_sample_iteration(SegmentDataIterator* it, uint64_t increase);
97         void end_sample_iteration(SegmentDataIterator* it);
98         uint8_t* get_iterator_value(SegmentDataIterator* it);
99         uint64_t get_iterator_valid_length(SegmentDataIterator* it);
100
101         uint32_t segment_id_;
102         mutable recursive_mutex mutex_;
103         deque<uint8_t*> data_chunks_;
104         uint8_t* current_chunk_;
105         uint64_t used_samples_, unused_samples_;
106         atomic<uint64_t> sample_count_;
107         pv::util::Timestamp start_time_;
108         double samplerate_;
109         uint64_t chunk_size_;
110         unsigned int unit_size_;
111         int iterator_count_;
112         bool mem_optimization_requested_;
113         bool is_complete_;
114
115         friend struct SegmentTest::SmallSize8Single;
116         friend struct SegmentTest::MediumSize8Single;
117         friend struct SegmentTest::MaxSize8Single;
118         friend struct SegmentTest::MediumSize24Single;
119         friend struct SegmentTest::MediumSize32Single;
120         friend struct SegmentTest::MaxSize32Single;
121         friend struct SegmentTest::MediumSize32Multi;
122         friend struct SegmentTest::MaxSize32Multi;
123         friend struct SegmentTest::MaxSize32MultiAtOnce;
124         friend struct SegmentTest::MaxSize32MultiIterated;
125 };
126
127 } // namespace data
128 } // namespace pv
129
130 typedef std::shared_ptr<pv::data::Segment> SharedPtrToSegment;
131
132 Q_DECLARE_METATYPE(SharedPtrToSegment);
133
134 #endif // PULSEVIEW_PV_DATA_SEGMENT_HPP