Welcome to mirror list, hosted at ThFree Co, Russian Federation.

threaded_list.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: fe168ff2642f1fbb7739e9ea5b29291164d17bc7 (plain)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#pragma once

#include "condition.hpp"
#include "logging.hpp"
#include "../std/list.hpp"

#include "threaded_container.hpp"

template <typename T>
class ThreadedList : public ThreadedContainer
{
private:

  list<T> m_list;
  bool m_isEmpty;
  string m_resName;

public:

  ThreadedList()
    : m_isEmpty(true)
  {}

  template <typename Fn>
  void ProcessList(Fn const & fn)
  {
    threads::ConditionGuard g(m_Cond);

    bool hadElements = !m_list.empty();

    fn(m_list);

    bool hasElements = !m_list.empty();

    m_isEmpty = !hasElements;

    if (!hadElements && hasElements)
      m_Cond.Signal(true);
  }

  void PushBack(T const & t)
  {
    threads::ConditionGuard g(m_Cond);

    bool doSignal = m_list.empty();

    m_list.push_back(t);
    m_isEmpty = false;

    if (doSignal)
      m_Cond.Signal(true);
  }

  void PushFront(T const & t)
  {
    threads::ConditionGuard g(m_Cond);

    bool doSignal = m_list.empty();

    m_list.push_front(t);
    m_isEmpty = false;

    if (doSignal)
      m_Cond.Signal(true);
  }

  void SetName(char const * name)
  {
    m_resName = name;
  }

  string const & GetName() const
  {
    return m_resName;
  }

  bool WaitNonEmpty()
  {
    double StartWaitTime = m_Timer.ElapsedSeconds();

    bool doFirstWait = true;

    while ((m_isEmpty = m_list.empty()))
    {
      if (IsCancelled())
        break;

      if (doFirstWait)
      {
        doFirstWait = false;
        if (!m_resName.empty())
          LOG(LINFO, ("consumer is waiting for", m_resName));
      }

      m_Cond.Wait();
    }

    m_WaitTime += m_Timer.ElapsedSeconds() - StartWaitTime;

    if (IsCancelled())
      return true;

    return false;
  }

  T const Front(bool doPop)
  {
    threads::ConditionGuard g(m_Cond);

    if (WaitNonEmpty())
      return T();

    T res = m_list.front();

    if (doPop)
      m_list.pop_front();

    m_isEmpty = m_list.empty();

    return res;
  }

  T const Back(bool doPop)
  {
    threads::ConditionGuard g(m_Cond);

    if (WaitNonEmpty())
      return T();

    T res = m_list.back();

    if (doPop)
      m_list.pop_back();

    m_isEmpty = m_list.empty();

    return res;
  }

  size_t Size() const
  {
    threads::ConditionGuard g(m_Cond);
    return m_list.size();
  }

  bool Empty() const
  {
    return m_isEmpty;
  }

  void Clear()
  {
    threads::ConditionGuard g(m_Cond);
    m_list.clear();
    m_isEmpty = true;
  }
};