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

commands_queue.cpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8a784031a5b1fd0ab13f9843f2e8657e86b9a914 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#include "../base/SRC_FIRST.hpp"

#include "../base/logging.hpp"
#include "../base/assert.hpp"

#include "../std/bind.hpp"

#include "commands_queue.hpp"

namespace core
{
  CommandsQueue::Environment::Environment(int threadNum)
    : m_threadNum(threadNum), m_isCancelled(false)
  {}

  void CommandsQueue::Environment::cancel()
  {
    m_isCancelled = true;
  }

  bool CommandsQueue::Environment::isCancelled() const
  {
    return m_isCancelled;
  }

  int CommandsQueue::Environment::threadNum() const
  {
    return m_threadNum;
  }

  CommandsQueue::BaseCommand::BaseCommand(bool isWaitable)
    : m_waitCount(0), m_isCompleted(false)
  {
    if (isWaitable)
      m_cond.reset(new threads::Condition());
  }

  void CommandsQueue::BaseCommand::join()
  {
    if (m_cond)
    {
      threads::ConditionGuard g(*m_cond.get());
      m_waitCount++;
      if (!m_isCompleted)
        g.Wait();
    }
    else
      LOG(LERROR, ("command isn't waitable"));
  }

  void CommandsQueue::BaseCommand::finish() const
  {
    if (m_cond)
    {
      threads::ConditionGuard g(*m_cond.get());
      m_isCompleted = true;
      CHECK(m_waitCount < 2, ("only one thread could wait for the queued command"));
      if (m_waitCount)
        g.Signal(true);
    }
  }

  CommandsQueue::Chain::Chain()
  {}

  void CommandsQueue::Chain::operator()(CommandsQueue::Environment const & env)
  {
    for (list<function_t>::const_iterator it = m_fns.begin(); it != m_fns.end(); ++it)
      (*it)(env);
  }

  CommandsQueue::Command::Command(bool isWaitable)
    : BaseCommand(isWaitable)
  {}

  void CommandsQueue::Command::perform(Environment const & env) const
  {
    m_fn(env);
    finish();
  }

  CommandsQueue::Routine::Routine(CommandsQueue * parent, int idx)
    : m_parent(parent), m_env(idx)
  {}

  void CommandsQueue::Routine::Do()
  {
    // performing initialization tasks
    for(list<shared_ptr<Command> >::const_iterator it = m_parent->m_initCommands.begin();
        it != m_parent->m_initCommands.end();
        ++it)
      (*it)->perform(m_env);

    // main loop
    while (!IsCancelled())
    {
      shared_ptr<CommandsQueue::Command> cmd = m_parent->m_commands.Front(true);

      if (m_parent->m_commands.IsCancelled())
        break;

      m_env.m_isCancelled = false;

      cmd->perform(m_env);

      m_parent->FinishCommand();
    }

    // performing finalization tasks
    for(list<shared_ptr<Command> >::const_iterator it = m_parent->m_finCommands.begin();
        it != m_parent->m_finCommands.end();
        ++it)
      (*it)->perform(m_env);
  }

  void CommandsQueue::Routine::Cancel()
  {
    m_env.cancel();

    // performing cancellation tasks
    for(list<shared_ptr<Command> >::const_iterator it = m_parent->m_cancelCommands.begin();
        it != m_parent->m_cancelCommands.end();
        ++it)
      (*it)->perform(m_env);

    IRoutine::Cancel();
  }

  void CommandsQueue::Routine::CancelCommand()
  {
    m_env.cancel();
  }

  CommandsQueue::Executor::Executor() : m_routine(0)
  {}

  void CommandsQueue::Executor::Cancel()
  {
    if (m_routine != 0)
      m_thread.Cancel();
    delete m_routine;
    m_routine = 0;
  }

  void CommandsQueue::Executor::CancelCommand()
  {
    m_routine->CancelCommand();
  }

  CommandsQueue::CommandsQueue(size_t executorsCount)
    : m_executors(new Executor[executorsCount]), m_executorsCount(executorsCount),
      m_activeCommands(0)
  {
  }

  CommandsQueue::~CommandsQueue()
  {
    /// @todo memory leak in m_executors? call Cancel()?
    //CHECK ( m_executors == 0, () );
  }

  void CommandsQueue::Cancel()
  {
    m_commands.Cancel();

    for (size_t i = 0; i < m_executorsCount; ++i)
      m_executors[i].Cancel();

    delete [] m_executors;
    m_executors = 0;
  }

  void CommandsQueue::CancelCommands()
  {
    for (size_t i = 0; i < m_executorsCount; ++i)
      m_executors[i].CancelCommand();
  }

  void CommandsQueue::Start()
  {
    for (size_t i = 0; i < m_executorsCount; ++i)
    {
      m_executors[i].m_routine = new CommandsQueue::Routine(this, i);
      m_executors[i].m_thread.Create(m_executors[i].m_routine);
    }
  }

  void CommandsQueue::AddCommand(shared_ptr<Command> const & cmd)
  {
    threads::ConditionGuard g(m_cond);
    m_commands.PushBack(cmd);
    ++m_activeCommands;
  }

  void CommandsQueue::AddInitCommand(shared_ptr<Command> const & cmd)
  {
    m_initCommands.push_back(cmd);
  }

  void CommandsQueue::AddFinCommand(shared_ptr<Command> const & cmd)
  {
    m_finCommands.push_back(cmd);
  }

  void CommandsQueue::AddCancelCommand(shared_ptr<Command> const & cmd)
  {
    m_cancelCommands.push_back(cmd);
  }

  void CommandsQueue::FinishCommand()
  {
    threads::ConditionGuard g(m_cond);

    --m_activeCommands;

    if (m_activeCommands == 0)
      g.Signal(true);
  }

  void CommandsQueue::Join()
  {
    threads::ConditionGuard g(m_cond);
    while (m_activeCommands != 0)
      g.Wait();
  }

  void CommandsQueue::ClearImpl(list<shared_ptr<CommandsQueue::Command> > & l)
  {
    threads::ConditionGuard g(m_cond);
    size_t s = l.size();
    l.clear();
    m_activeCommands -= s;
    if (m_activeCommands == 0)
      g.Signal(true);
  }

  void CommandsQueue::Clear()
  {
    /// let us assume that decreasing m_activeCommands is an "operation A"
    /// and clearing the list of commands is an "operation B"
    /// we should perform them atomically (both or none at the same time)
    /// to prevent the situation when Executor could start processing some command
    /// between "operation A" and "operation B" which could lead to underflow of m_activeCommands

    m_commands.ProcessList(bind(&CommandsQueue::ClearImpl, this, _1));
  }

  int CommandsQueue::ExecutorsCount() const
  {
    return m_executorsCount;
  }
}