OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
flow_token.cc
Go to the documentation of this file.
1 #include "flow_token.h"
2 #include "flow_proto.h"
3 
5  pool_(pool){
6 }
7 
9  if (pool_)
10  pool_->FreeToken();
11 }
12 
13 TokenPool::TokenPool(const std::string &name, Proto *proto,
14  int count) :
15  name_(name), max_tokens_(count), min_tokens_(count),
16  low_water_mark_((count * 10)/100), failures_(0), restarts_(0),
17  proto_(proto) {
18  token_count_ = count;
19 }
20 
22 }
23 
25  int val = token_count_.fetch_and_increment();
26  assert(val <= max_tokens_);
27  if (val == low_water_mark_) {
28  proto_->TokenAvailable(this);
29  }
30 }
31 
32 bool TokenPool::TokenCheck() const {
33  if (token_count_ > 0) {
34  return true;
35  }
36 
37  failures_++;
38  return false;
39 }
40 
42  int val = token_count_.fetch_and_decrement();
43  if (val < min_tokens_)
44  min_tokens_ = val;
45  return TokenPtr(new Token(this));
46 }
47 
49  int val = token_count_.fetch_and_decrement();
50  if (val < min_tokens_)
51  min_tokens_ = val;
52  return TokenPtr(new FlowToken(this, entry));
53 }
friend class Token
Definition: flow_token.h:37
uint64_t failures_
Definition: flow_token.h:47
TokenPool(const std::string &name, Proto *proto, int count)
Definition: flow_token.cc:13
Proto * proto_
Definition: flow_token.h:49
void FreeToken()
Definition: flow_token.cc:24
int max_tokens_
Definition: flow_token.h:43
tbb::atomic< int > token_count_
Definition: flow_token.h:46
TokenPool * pool_
Definition: flow_token.h:21
int low_water_mark_
Definition: flow_token.h:45
Token(TokenPool *pool)
Definition: flow_token.cc:4
virtual void TokenAvailable(TokenPool *pool)
virtual TokenPtr GetToken()
Definition: flow_token.cc:41
int min_tokens_
Definition: flow_token.h:44
boost::shared_ptr< Token > TokenPtr
Definition: flow_token.h:11
virtual ~TokenPool()
Definition: flow_token.cc:21
virtual ~Token()
Definition: flow_token.cc:8
bool TokenCheck() const
Definition: flow_token.cc:32