DABC (Data Acquisition Backbone Core)  2.9.9
CommandsQueue.cxx
Go to the documentation of this file.
1 // $Id: CommandsQueue.cxx 1921 2013-12-11 16:44:46Z linev $
2 
3 #include "dabc/CommandsQueue.h"
4 
5 #include "dabc/Worker.h"
6 
8  fList(),
9  fKind(kind),
10  fIdCounter(0)
11 {
12 }
13 
15 {
16  Cleanup();
17 }
18 
19 void dabc::CommandsQueue::Cleanup(Mutex* m, Worker* proc, int res)
20 {
21  do {
22 
23  Command cmd;
24  int kind;
25 
26  {
27  LockGuard lock(m);
28 
29  if (fList.size()==0) return;
30 
31  cmd << fList.front().cmd;
32  kind = fList.front().kind;
33  fList.pop_front();
34  }
35 
36  switch (kind) {
37  case kindNone:
38  break;
39  case kindPostponed:
40  case kindSubmit:
41  cmd.Reply(res);
42  break;
43  case kindReply:
44  // we should reply on the command without changing result
45  cmd.Reply();
46  break;
47  case kindAssign:
48  cmd.RemoveCaller(proc);
49  break;
50  }
51  cmd.Release();
52  } while (true);
53 }
54 
55 
57 {
58  QueueRecsList::iterator iter = fList.begin();
59  while (iter != fList.end()) {
60 
61  if (!iter->cmd.IsTimedout()) { iter++; continue; }
62 
63  iter->cmd.Reply(dabc::cmd_timedout);
64 
65  QueueRecsList::iterator curr = iter++;
66  fList.erase(curr);
67  }
68 }
69 
70 
72 {
73  if (kind == kindNone) kind = fKind;
74 
75  // exclude zero id
76  do { fIdCounter++; } while (fIdCounter==0);
77 
78  fList.push_back(QueueRec());
79 
80  fList.back().cmd << cmd;
81  fList.back().kind = kind;
82  fList.back().id = fIdCounter;
83 
84  return fIdCounter;
85 }
86 
88 {
89  if (fList.size()==0) return dabc::Command();
90 
91  dabc::Command cmd;
92 
93  cmd << fList.front().cmd;
94 
95  fList.pop_front();
96 
97  return cmd;
98 }
99 
101 {
102  for (QueueRecsList::iterator iter = fList.begin(); iter != fList.end(); iter++) {
103  if (iter->kind==kind) {
104  dabc::Command cmd;
105  cmd << iter->cmd;
106  fList.erase(iter);
107  return cmd;
108  }
109  }
110 
111  return dabc::Command();
112 }
113 
115 {
116  for (QueueRecsList::iterator iter = fList.begin(); iter != fList.end(); iter++) {
117  if (iter->id==id) {
118  dabc::Command cmd;
119  cmd << iter->cmd;
120  fList.erase(iter);
121  return cmd;
122  }
123  }
124 
125  return dabc::Command();
126 }
127 
129 {
130  return fList.size()>0 ? fList.front().cmd : dabc::Command();
131 }
132 
134 {
135  return fList.size()>0 ? fList.front().kind : kindNone;
136 }
137 
139 {
140  for (QueueRecsList::iterator iter = fList.begin(); iter != fList.end(); iter++) {
141  if (iter->cmd==cmd) {
142  iter->kind = kind;
143  return iter->id;
144  }
145  }
146 
147  return Push(cmd, kind);
148 }
149 
151 {
152  Cleanup(0, 0, res);
153 }
Represents command with its arguments.
Definition: Command.h:99
void Release()
Method used to clean command - all internal data will be cleaned, command container will be released.
Definition: Command.cxx:198
void Reply(int res=cmd_noresult)
Replied on the command.
Definition: Command.cxx:225
void RemoveCaller(Worker *worker, bool *exe_ready=nullptr)
Definition: Command.cxx:78
uint32_t ChangeKind(Command &cmd, EKind kind)
Change kind of the entry for specified command.
void Cleanup(Mutex *m=nullptr, Worker *proc=nullptr, int res=cmd_false)
void ReplyTimedout()
Reply timed-out commands.
Command PopWithId(uint32_t id)
CommandsQueue(EKind kind=kindNone)
Normal constructor.
Command PopWithKind(EKind kind)
void ReplyAll(int res)
Reply all commands.
uint32_t Push(Command cmd, EKind kind=kindNone)
Add reference on the command in the queue.
Lock guard for posix mutex.
Definition: threads.h:127
posix pthread mutex
Definition: threads.h:61
Active object, which is working inside dabc::Thread.
Definition: Worker.h:116
@ cmd_timedout
Definition: Command.h:40