MADARA  3.4.1
Queue.cpp
Go to the documentation of this file.
1 #include <sstream>
2 
3 #include "Queue.h"
5 
7  const knowledge::KnowledgeRecord& record)
8 {
9  bool result(false);
10 
11  if (context_)
12  {
13  ContextGuard context_guard(*context_);
14  MADARA_GUARD_TYPE guard(mutex_);
15 
16  if (count_ < 0)
17  count_ = 0;
18 
19  // avoid double retrieval of count_ during check
21 
22  if (context_ && name_ != "" && queue_.size() > 0 &&
24  {
25  // there's no point in signaling inside of a locked context
27  settings_.signal_changes = false;
28 
30 
31  context_->set(queue_.vector_[tail], record, settings_);
32  tail_ = increment(tail, 1);
33  ++count_;
34  }
35  }
36 
37  // now that we no longer have a lock on context, signal
38  if (context_)
39  context_->signal();
40 
41  return result;
42 }
43 
46 {
48 
49  if (context_ && name_ != "")
50  {
51  ContextGuard context_guard(*context_);
52  MADARA_GUARD_TYPE guard(mutex_);
53 
54  if (wait)
55  {
56  while (count_ <= 0)
57  context_->wait_for_change(true);
58  }
59 
60  if (count_ > 0)
61  {
63 
64  result = context_->get(queue_.vector_[head], settings_);
65 
66  head_ = increment(head, 1);
67  --count_;
68  }
69  }
70 
71  // now that we no longer have a lock on context, signal
72  if (context_)
73  context_->signal();
74 
75  return result;
76 }
77 
79  const std::string& var_name, KnowledgeBase& knowledge)
80 {
81  KnowledgeUpdateSettings keep_local(true);
82  context_ = &(knowledge.get_context());
83 
84  ContextGuard context_guard(*context_);
85  MADARA_GUARD_TYPE guard(mutex_);
86 
87  name_ = var_name;
88 
89  this->count_.set_name(var_name + ".count", knowledge);
90  this->head_.set_name(var_name + ".head", knowledge);
91  this->tail_.set_name(var_name + ".tail", knowledge);
92  this->queue_.set_name(var_name, knowledge);
93 }
94 
96  const std::string& var_name, Variables& knowledge)
97 {
98  KnowledgeUpdateSettings keep_local(true);
99  context_ = knowledge.get_context();
100 
101  ContextGuard context_guard(*context_);
102  MADARA_GUARD_TYPE guard(mutex_);
103 
104  name_ = var_name;
105 
106  this->count_.set_name(var_name + ".count", knowledge);
107  this->head_.set_name(var_name + ".head", knowledge);
108  this->tail_.set_name(var_name + ".tail", knowledge);
109  this->queue_.set_name(var_name, knowledge);
110 }
const ThreadSafeContext * context_
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:24
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
This class encapsulates an entry in a KnowledgeBase.
Settings for applying knowledge updates.
bool signal_changes
Toggle whether to signal changes have happened.
void signal(bool lock=true) const
Signals that this thread is done with the context.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
Provides an interface for external functions into the MADARA KaRL variable settings.
Definition: Variables.h:53
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Queue.h:233
bool enqueue(const knowledge::KnowledgeRecord &record)
Enqueues a record to the end of the queue.
Definition: Queue.cpp:6
Integer count_
Count of elements in queue.
Definition: Queue.h:243
size_t count(void)
Returns the number of records in the queue.
Definition: Queue.inl:151
Integer tail_
Tail of the queue.
Definition: Queue.h:253
knowledge::KnowledgeRecord::Integer increment(knowledge::KnowledgeRecord::Integer base, knowledge::KnowledgeRecord::Integer value)
Increments the base by the value, using size as a boundary.
Definition: Queue.h:216
knowledge::KnowledgeRecord dequeue(bool wait=true)
Dequeues a record from the front of the queue.
Definition: Queue.cpp:45
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Queue.cpp:78
std::string name_
Prefix of variable.
Definition: Queue.h:238
MADARA_LOCK_TYPE mutex_
guard for access and changes
Definition: Queue.h:228
KnowledgeUpdateSettings settings_
Settings for modifications.
Definition: Queue.h:263
Vector queue_
Underlying array of records.
Definition: Queue.h:258
std::vector< VariableReference > vector_
Values of the array.
Definition: Vector.h:616
size_t size(void) const
Returns the size of the local vector.
Definition: Vector.inl:23
constexpr string_t string
Provides functions and classes for the distributed knowledge base.