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