MADARA  3.2.3
Queue.cpp
Go to the documentation of this file.
1 #include <sstream>
2 
3 #include "Queue.h"
5 
6 bool
8  const knowledge::KnowledgeRecord & record)
9 {
10  bool result (false);
11 
12  if (context_)
13  {
14  ContextGuard context_guard (*context_);
15  MADARA_GUARD_TYPE guard (mutex_);
16 
17  if (count_ < 0)
18  count_ = 0;
19 
20  // avoid double retrieval of count_ during check
22 
23  if (context_ && name_ != "" && queue_.size () > 0 &&
25  {
26  // there's no point in signaling inside of a locked context
28  settings_.signal_changes = false;
29 
31 
32  context_->set (queue_.vector_[tail], record, settings_);
33  tail_ = increment (tail, 1);
34  ++count_;
35  }
36  }
37 
38  // now that we no longer have a lock on context, signal
39  if (context_)
40  context_->signal ();
41 
42  return result;
43 }
44 
47 {
49 
50  if (context_ && name_ != "")
51  {
52  ContextGuard context_guard (*context_);
53  MADARA_GUARD_TYPE guard (mutex_);
54 
55  if (wait)
56  {
57  while (count_ <= 0)
58  context_->wait_for_change (true);
59  }
60 
61  if (count_ > 0)
62  {
64 
65  result = context_->get (queue_.vector_[head], settings_);
66 
67  head_ = increment (head, 1);
68  --count_;
69  }
70  }
71 
72  // now that we no longer have a lock on context, signal
73  if (context_)
74  context_->signal ();
75 
76  return result;
77 }
78 
79 void
81  const std::string & var_name,
83 {
84  KnowledgeUpdateSettings keep_local (true);
85  context_ = &(knowledge.get_context ());
86 
87  ContextGuard context_guard (*context_);
88  MADARA_GUARD_TYPE guard (mutex_);
89 
90  name_ = var_name;
91 
92  this->count_.set_name (var_name + ".count", knowledge);
93  this->head_.set_name (var_name + ".head", knowledge);
94  this->tail_.set_name (var_name + ".tail", knowledge);
95  this->queue_.set_name (var_name, knowledge);
96 }
97 
98 void
100  const std::string & var_name,
102 {
103  KnowledgeUpdateSettings keep_local (true);
104  context_ = knowledge.get_context ();
105 
106  ContextGuard context_guard (*context_);
107  MADARA_GUARD_TYPE guard (mutex_);
108 
109  name_ = var_name;
110 
111  this->count_.set_name (var_name + ".count", knowledge);
112  this->head_.set_name (var_name + ".head", knowledge);
113  this->tail_.set_name (var_name + ".tail", knowledge);
114  this->queue_.set_name (var_name, knowledge);
115 }
This class encapsulates an entry in a KnowledgeBase.
int set(const std::string &key, T &&value, const KnowledgeUpdateSettings &settings=KnowledgeUpdateSettings())
Atomically sets the value of a variable to the specific record.
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Queue.cpp:80
void set_name(const std::string &var_name, KnowledgeBase &knowledge)
Sets the variable name that this refers to.
Definition: Integer.inl:58
bool signal_changes
Toggle whether to signal changes have happened.
Integer tail_
Tail of the queue.
Definition: Queue.h:269
size_t count(void)
Returns the number of records in the queue.
Definition: Queue.inl:169
size_t size(void) const
Returns the size of the local vector.
Definition: Vector.cpp:310
void signal(bool lock=true) const
Signals that this thread is done with the context.
std::vector< VariableReference > vector_
Values of the array.
Definition: Vector.h:612
Integer count_
Count of elements in queue.
Definition: Queue.h:259
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:230
std::string name_
Prefix of variable.
Definition: Queue.h:254
A thread-safe guard for a context or knowledge base.
Definition: ContextGuard.h:23
ThreadSafeContext * context_
Variable context that we are modifying.
Definition: Queue.h:249
bool enqueue(const knowledge::KnowledgeRecord &record)
Enqueues a record to the end of the queue.
Definition: Queue.cpp:7
static struct madara::knowledge::tags::string_t string
Vector queue_
Underlying array of records.
Definition: Queue.h:274
This class provides a distributed knowledge base to users.
Definition: KnowledgeBase.h:45
Integer head_
Head of the queue.
Definition: Queue.h:264
knowledge::KnowledgeRecord dequeue(bool wait=true)
Dequeues a record from the front of the queue.
Definition: Queue.cpp:46
void wait_for_change(bool extra_release=false)
Wait for a change to happen to the context.
KnowledgeUpdateSettings settings_
Settings for modifications.
Definition: Queue.h:279
ThreadSafeContext & get_context(void)
Returns the ThreadSafeContext associated with this Knowledge Base.
Provides functions and classes for the distributed knowledge base.
void set_name(const std::string &var_name, KnowledgeBase &knowledge, int size=-1)
Sets the variable name that this refers to.
Definition: Vector.cpp:317
Settings for applying knowledge updates.
madara::knowledge::KnowledgeRecord get(const std::string &key, const KnowledgeReferenceSettings &settings=KnowledgeReferenceSettings()) const
Atomically returns the value of a variable.
MADARA_LOCK_TYPE mutex_
guard for access and changes
Definition: Queue.h:244
Provides an interface for external functions into the MADARA KaRL variable settings.
ThreadSafeContext * get_context(void)
Returns the ThreadSafeContext associated with this Variables facade.