X-Git-Url: http://plrg.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2FTokenBucket.h;h=6c3c2e1c85a03f9db5671f48cf9b70c6e4600ab5;hb=19db503e08e4ea46a8b4d9a272605006b6245f88;hp=7ddf8fc23345359764f2a60dc98a84380fb3a8ed;hpb=dd6fff7166b6fe157ae5bc64366969ffb5fd1b7d;p=folly.git diff --git a/folly/TokenBucket.h b/folly/TokenBucket.h index 7ddf8fc2..6c3c2e1c 100644 --- a/folly/TokenBucket.h +++ b/folly/TokenBucket.h @@ -1,5 +1,5 @@ /* - * Copyright 2016 Facebook, Inc. + * Copyright 2015-present Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -21,7 +21,7 @@ #include #include -#include +#include namespace folly { @@ -104,6 +104,15 @@ class ParameterizedDynamicTokenBucket { zeroTime_ = zeroTime; } + /** + * Returns the current time in seconds since Epoch. + */ + static double defaultClockNow() noexcept(noexcept(ClockT::timeSinceEpoch())) { + return std::chrono::duration_cast>( + ClockT::timeSinceEpoch()) + .count(); + } + /** * Attempts to consume some number of tokens. Tokens are first added to the * bucket based on the time elapsed since the last attempt to consume tokens. @@ -191,15 +200,6 @@ class ParameterizedDynamicTokenBucket { return std::min((nowInSeconds - this->zeroTime_) * rate, burstSize); } - /** - * Returns the current time in seconds since Epoch. - */ - static double defaultClockNow() noexcept(noexcept(ClockT::timeSinceEpoch())) { - return std::chrono::duration_cast>( - ClockT::timeSinceEpoch()) - .count(); - } - private: template bool consumeImpl( @@ -221,7 +221,7 @@ class ParameterizedDynamicTokenBucket { return true; } - FOLLY_ALIGN_TO_AVOID_FALSE_SHARING std::atomic zeroTime_; + alignas(hardware_destructive_interference_size) std::atomic zeroTime_; }; /** @@ -268,6 +268,13 @@ class ParameterizedTokenBucket { ParameterizedTokenBucket& operator=( const ParameterizedTokenBucket& other) noexcept = default; + /** + * Returns the current time in seconds since Epoch. + */ + static double defaultClockNow() noexcept(noexcept(Impl::defaultClockNow())) { + return Impl::defaultClockNow(); + } + /** * Change rate and burst size. * @@ -370,13 +377,6 @@ class ParameterizedTokenBucket { return burstSize_; } - /** - * Returns the current time in seconds since Epoch. - */ - static double defaultClockNow() noexcept(noexcept(Impl::defaultClockNow())) { - return Impl::defaultClockNow(); - } - private: Impl tokenBucket_; double rate_; @@ -385,4 +385,4 @@ class ParameterizedTokenBucket { using TokenBucket = ParameterizedTokenBucket<>; using DynamicTokenBucket = ParameterizedDynamicTokenBucket<>; -} +} // namespace folly