Free Electron
SpannedRange.h
Go to the documentation of this file.
1 /* Copyright (C) 2003-2021 Free Electron Organization
2  Any use of this software requires a license. If a valid license
3  was not distributed with this file, visit freeelectron.org. */
4 
5 /** @file */
6 
7 #ifndef __thread_SpannedRange_h__
8 #define __thread_SpannedRange_h__
9 
10 namespace fe
11 {
12 namespace ext
13 {
14 
15 /**************************************************************************//**
16  @brief SpannedRange of jobs
17 
18  @ingroup thread
19 *//***************************************************************************/
20 class FE_DL_EXPORT SpannedRange: public Counted
21 {
22  public:
23 
24  class FE_DL_EXPORT Span: public Vector<2,Real>
25  {
26  public:
27  Span(void):
28  Vector<2,Real>(0.0,-1.0) {}
29  };
30 
31  class FE_DL_EXPORT MultiSpan
32  {
33  public:
34  MultiSpan(void) {}
35 
36  void clear(void)
37  { m_spanArray.clear(); }
38  U32 spanCount(void) const
39  { return m_spanArray.size(); }
40  Span& span(U32 a_spanIndex)
41  {
42  FEASSERT(a_spanIndex<m_spanArray.size());
43  return m_spanArray[a_spanIndex];
44  }
45  const Span& span(U32 a_spanIndex) const
46  {
47  FEASSERT(a_spanIndex<m_spanArray.size());
48  return m_spanArray[a_spanIndex];
49  }
50 
51  void add(Real a_start,Real a_end);
52  void add(const Span& a_rSpan)
53  { add(a_rSpan[0],a_rSpan[1]); }
54  void add(const MultiSpan& rOther);
55 
56  //* TODO MultiSpan::Iterator
57  Real valueAt(U32 a_valueIndex) const;
58  U32 valueCount(void) const;
59 
60  String brief(void) const;
61  String dump(void) const;
62 
63  private:
64 
65  Array<Span> m_spanArray;
66  };
67 
68  class FE_DL_EXPORT Iterator
69  {
70  public:
71  Iterator(sp<SpannedRange> a_spSpannedRange):
72  m_spSpannedRange(a_spSpannedRange),
73  m_atomicIndex(0),
74  m_spanIndex(-1),
75  m_valueCount(0),
76  m_valueIndex(0),
77  m_value(0.0)
78  {
79  FEASSERT(m_spSpannedRange.isValid());
80  m_atomicCount=
81  m_spSpannedRange->atomicCount();
82  advance();
83  }
84 
85  Real value(void) { return m_value; }
86  BWORD atEnd(void)
87  { return m_atomicIndex>m_atomicCount+1; }
88 
89  void step(void)
90  {
91  m_valueIndex++;
92  if(m_valueIndex>=m_valueCount)
93  {
94  advance();
95  }
96  else
97  {
98  m_value+=1.0;
99  }
100  }
101 
102  private:
103 
104  void advance(void);
105 
106  sp<SpannedRange> m_spSpannedRange;
107  U32 m_atomicCount;
108  U32 m_atomicIndex;
109  I32 m_spanIndex;
110  U32 m_valueCount;
111  U32 m_valueIndex;
112  Real m_value;
113  };
114 
115  SpannedRange(void) {}
116 
117  void clear(void)
118  {
119  m_atomArray.clear();
120  m_postAtomic.clear();
121  m_nonAtomic.clear();
122  }
123 
124  MultiSpan& addAtomic(void);
125 
126  U32 atomicCount(void) const { return m_atomArray.size(); }
127 
128  MultiSpan& atomic(U32 a_index)
129  { return m_atomArray[a_index]; }
130 const MultiSpan& atomic(U32 a_index) const
131  { return m_atomArray[a_index]; }
132 
133  MultiSpan& postAtomic(void) { return m_postAtomic; }
134  MultiSpan& nonAtomic(void) { return m_nonAtomic; }
135 const MultiSpan& nonAtomic(void) const { return m_nonAtomic; }
136 
137  BWORD empty(void) const
138  { return !m_atomArray.size() &&
139  !m_postAtomic.spanCount() &&
140  !m_nonAtomic.spanCount(); }
141 
142  Iterator begin(void) { return Iterator(sp<SpannedRange>(this)); }
143 
144  U32 valueCount(void) const;
145 
146  /** @brief merge atoms into counts of the given granularity
147 
148  The granularity size is only a goal.
149  The results can be various sizes
150  above and below the goal.
151  No atoms are broken up to make them smaller.
152  */
153  sp<SpannedRange> combineAtoms(I32 a_size) const;
154 
155  void split(sp<SpannedRange>& rspSpan0,
156  sp<SpannedRange>& rspSpan1) const;
157 
158  String brief(void) const;
159  String dump(void) const;
160 
161  private:
162 
163  Array<MultiSpan> m_atomArray; //* each thread-safe piece
164  MultiSpan m_postAtomic; //* unsafe to do unthreaded
165  MultiSpan m_nonAtomic; //* always safe
166 };
167 
168 inline String print(const sp<SpannedRange>& a_rspSpannedRange)
169 {
170  return a_rspSpannedRange.isNull()? "NULL": a_rspSpannedRange->brief();
171 }
172 
173 } /* namespace ext */
174 } /* namespace fe */
175 
176 #endif // __thread_SpannedRange_h__
Heap-based support for classes participating in fe::ptr <>
Definition: Counted.h:35
kernel
Definition: namespace.dox:3
SpannedRange of jobs.
Definition: SpannedRange.h:20
Dense vector - size fixed by template.
Definition: Vector.h:19
Automatically reference-counted string container.
Definition: String.h:128
Intrusive Smart Pointer.
Definition: src/core/ptr.h:53