Line data Source code
1 : #include "flow_token.h" 2 : #include "flow_proto.h" 3 : 4 0 : Token::Token(TokenPool *pool) : 5 0 : pool_(pool){ 6 0 : } 7 : 8 0 : Token::~Token() { 9 0 : if (pool_) 10 0 : pool_->FreeToken(); 11 0 : } 12 : 13 7 : TokenPool::TokenPool(const std::string &name, Proto *proto, 14 7 : int count) : 15 14 : name_(name), max_tokens_(count), min_tokens_(count), 16 7 : low_water_mark_((count * 10)/100), failures_(0), restarts_(0), 17 7 : proto_(proto) { 18 7 : token_count_ = count; 19 7 : } 20 : 21 7 : TokenPool::~TokenPool() { 22 7 : } 23 : 24 0 : void TokenPool::FreeToken() { 25 0 : int val = token_count_.fetch_add(1); 26 0 : assert(val <= max_tokens_); 27 0 : if (val == low_water_mark_) { 28 0 : proto_->TokenAvailable(this); 29 : } 30 0 : } 31 : 32 12 : bool TokenPool::TokenCheck() const { 33 12 : if (token_count_ > 0) { 34 12 : return true; 35 : } 36 : 37 0 : failures_++; 38 0 : return false; 39 : } 40 : 41 0 : TokenPtr TokenPool::GetToken() { 42 0 : int val = token_count_.fetch_sub(1); 43 0 : if (val < min_tokens_) 44 0 : min_tokens_ = val; 45 0 : return TokenPtr(new Token(this)); 46 : } 47 : 48 0 : TokenPtr FlowTokenPool::GetToken(FlowEntry *entry) { 49 0 : int val = token_count_.fetch_sub(1); 50 0 : if (val < min_tokens_) 51 0 : min_tokens_ = val; 52 0 : return TokenPtr(new FlowToken(this, entry)); 53 : }