2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
20 #include <sys/types.h>
30 #include <folly/io/async/EventBase.h>
31 #include <folly/io/async/EventHandler.h>
32 #include <folly/io/async/DelayedDestruction.h>
33 #include <folly/io/async/Request.h>
34 #include <folly/Likely.h>
35 #include <folly/ScopeGuard.h>
36 #include <folly/SpinLock.h>
38 #include <glog/logging.h>
40 #if __linux__ && !__ANDROID__
41 #define FOLLY_HAVE_EVENTFD
42 #include <folly/io/async/EventFDWrapper.h>
48 * A producer-consumer queue for passing messages between EventBase threads.
50 * Messages can be added to the queue from any thread. Multiple consumers may
51 * listen to the queue from multiple EventBase threads.
53 * A NotificationQueue may not be destroyed while there are still consumers
54 * registered to receive events from the queue. It is the user's
55 * responsibility to ensure that all consumers are unregistered before the
58 * MessageT should be MoveConstructible (i.e., must support either a move
59 * constructor or a copy constructor, or both). Ideally it's move constructor
60 * (or copy constructor if no move constructor is provided) should never throw
61 * exceptions. If the constructor may throw, the consumers could end up
62 * spinning trying to move a message off the queue and failing, and then
65 template<typename MessageT>
66 class NotificationQueue {
69 * A callback interface for consuming messages from the queue as they arrive.
71 class Consumer : public DelayedDestruction, private EventHandler {
73 enum : uint16_t { kDefaultMaxReadAtOnce = 10 };
77 destroyedFlagPtr_(nullptr),
78 maxReadAtOnce_(kDefaultMaxReadAtOnce) {}
80 // create a consumer in-place, without the need to build new class
81 template <typename TCallback>
82 static std::unique_ptr<Consumer, DelayedDestruction::Destructor> make(
83 TCallback&& callback);
86 * messageAvailable() will be invoked whenever a new
87 * message is available from the pipe.
89 virtual void messageAvailable(MessageT&& message) = 0;
92 * Begin consuming messages from the specified queue.
94 * messageAvailable() will be called whenever a message is available. This
95 * consumer will continue to consume messages until stopConsuming() is
98 * A Consumer may only consume messages from a single NotificationQueue at
99 * a time. startConsuming() should not be called if this consumer is
102 void startConsuming(EventBase* eventBase, NotificationQueue* queue) {
103 init(eventBase, queue);
104 registerHandler(READ | PERSIST);
108 * Same as above but registers this event handler as internal so that it
109 * doesn't count towards the pending reader count for the IOLoop.
111 void startConsumingInternal(
112 EventBase* eventBase, NotificationQueue* queue) {
113 init(eventBase, queue);
114 registerInternalHandler(READ | PERSIST);
118 * Stop consuming messages.
120 * startConsuming() may be called again to resume consumption of messages
121 * at a later point in time.
123 void stopConsuming();
126 * Consume messages off the queue until it is empty. No messages may be
127 * added to the queue while it is draining, so that the process is bounded.
128 * To that end, putMessage/tryPutMessage will throw an std::runtime_error,
129 * and tryPutMessageNoThrow will return false.
131 * @returns true if the queue was drained, false otherwise. In practice,
132 * this will only fail if someone else is already draining the queue.
134 bool consumeUntilDrained(size_t* numConsumed = nullptr) noexcept;
137 * Get the NotificationQueue that this consumer is currently consuming
138 * messages from. Returns nullptr if the consumer is not currently
139 * consuming events from any queue.
141 NotificationQueue* getCurrentQueue() const {
146 * Set a limit on how many messages this consumer will read each iteration
147 * around the event loop.
149 * This helps rate-limit how much work the Consumer will do each event loop
150 * iteration, to prevent it from starving other event handlers.
152 * A limit of 0 means no limit will be enforced. If unset, the limit
153 * defaults to kDefaultMaxReadAtOnce (defined to 10 above).
155 void setMaxReadAtOnce(uint32_t maxAtOnce) {
156 maxReadAtOnce_ = maxAtOnce;
158 uint32_t getMaxReadAtOnce() const {
159 return maxReadAtOnce_;
162 EventBase* getEventBase() {
166 void handlerReady(uint16_t events) noexcept override;
170 void destroy() override;
172 virtual ~Consumer() {}
176 * Consume messages off the the queue until
177 * - the queue is empty (1), or
178 * - until the consumer is destroyed, or
179 * - until the consumer is uninstalled, or
180 * - an exception is thrown in the course of dequeueing, or
181 * - unless isDrain is true, until the maxReadAtOnce_ limit is hit
183 * (1) Well, maybe. See logic/comments around "wasEmpty" in implementation.
185 void consumeMessages(bool isDrain, size_t* numConsumed = nullptr) noexcept;
187 void setActive(bool active, bool shouldLock = false) {
193 queue_->spinlock_.lock();
195 if (!active_ && active) {
196 ++queue_->numActiveConsumers_;
197 } else if (active_ && !active) {
198 --queue_->numActiveConsumers_;
202 queue_->spinlock_.unlock();
205 void init(EventBase* eventBase, NotificationQueue* queue);
207 NotificationQueue* queue_;
208 bool* destroyedFlagPtr_;
209 uint32_t maxReadAtOnce_;
216 #ifdef FOLLY_HAVE_EVENTFD
222 * Create a new NotificationQueue.
224 * If the maxSize parameter is specified, this sets the maximum queue size
225 * that will be enforced by tryPutMessage(). (This size is advisory, and may
226 * be exceeded if producers explicitly use putMessage() instead of
229 * The fdType parameter determines the type of file descriptor used
230 * internally to signal message availability. The default (eventfd) is
231 * preferable for performance and because it won't fail when the queue gets
232 * too long. It is not available on on older and non-linux kernels, however.
233 * In this case the code will fall back to using a pipe, the parameter is
234 * mostly for testing purposes.
236 explicit NotificationQueue(uint32_t maxSize = 0,
237 #ifdef FOLLY_HAVE_EVENTFD
238 FdType fdType = FdType::EVENTFD)
240 FdType fdType = FdType::PIPE)
244 advisoryMaxQueueSize_(maxSize),
248 RequestContext::saveContext();
250 #ifdef FOLLY_HAVE_EVENTFD
251 if (fdType == FdType::EVENTFD) {
252 eventfd_ = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK | EFD_SEMAPHORE);
253 if (eventfd_ == -1) {
254 if (errno == ENOSYS || errno == EINVAL) {
255 // eventfd not availalble
256 LOG(ERROR) << "failed to create eventfd for NotificationQueue: "
257 << errno << ", falling back to pipe mode (is your kernel "
259 fdType = FdType::PIPE;
262 folly::throwSystemError("Failed to create eventfd for "
263 "NotificationQueue", errno);
268 if (fdType == FdType::PIPE) {
269 if (pipe(pipeFds_)) {
270 folly::throwSystemError("Failed to create pipe for NotificationQueue",
274 // put both ends of the pipe into non-blocking mode
275 if (fcntl(pipeFds_[0], F_SETFL, O_RDONLY | O_NONBLOCK) != 0) {
276 folly::throwSystemError("failed to put NotificationQueue pipe read "
277 "endpoint into non-blocking mode", errno);
279 if (fcntl(pipeFds_[1], F_SETFL, O_WRONLY | O_NONBLOCK) != 0) {
280 folly::throwSystemError("failed to put NotificationQueue pipe write "
281 "endpoint into non-blocking mode", errno);
284 ::close(pipeFds_[0]);
285 ::close(pipeFds_[1]);
291 ~NotificationQueue() {
296 if (pipeFds_[0] >= 0) {
297 ::close(pipeFds_[0]);
300 if (pipeFds_[1] >= 0) {
301 ::close(pipeFds_[1]);
307 * Set the advisory maximum queue size.
309 * This maximum queue size affects calls to tryPutMessage(). Message
310 * producers can still use the putMessage() call to unconditionally put a
311 * message on the queue, ignoring the configured maximum queue size. This
312 * can cause the queue size to exceed the configured maximum.
314 void setMaxQueueSize(uint32_t max) {
315 advisoryMaxQueueSize_ = max;
319 * Attempt to put a message on the queue if the queue is not already full.
321 * If the queue is full, a std::overflow_error will be thrown. The
322 * setMaxQueueSize() function controls the maximum queue size.
324 * If the queue is currently draining, an std::runtime_error will be thrown.
326 * This method may contend briefly on a spinlock if many threads are
327 * concurrently accessing the queue, but for all intents and purposes it will
328 * immediately place the message on the queue and return.
330 * tryPutMessage() may throw std::bad_alloc if memory allocation fails, and
331 * may throw any other exception thrown by the MessageT move/copy
334 void tryPutMessage(MessageT&& message) {
335 putMessageImpl(std::move(message), advisoryMaxQueueSize_);
337 void tryPutMessage(const MessageT& message) {
338 putMessageImpl(message, advisoryMaxQueueSize_);
342 * No-throw versions of the above. Instead returns true on success, false on
345 * Only std::overflow_error (the common exception case) and std::runtime_error
346 * (which indicates that the queue is being drained) are prevented from being
347 * thrown. User code must still catch std::bad_alloc errors.
349 bool tryPutMessageNoThrow(MessageT&& message) {
350 return putMessageImpl(std::move(message), advisoryMaxQueueSize_, false);
352 bool tryPutMessageNoThrow(const MessageT& message) {
353 return putMessageImpl(message, advisoryMaxQueueSize_, false);
357 * Unconditionally put a message on the queue.
359 * This method is like tryPutMessage(), but ignores the maximum queue size
360 * and always puts the message on the queue, even if the maximum queue size
363 * putMessage() may throw
364 * - std::bad_alloc if memory allocation fails, and may
365 * - std::runtime_error if the queue is currently draining
366 * - any other exception thrown by the MessageT move/copy constructor.
368 void putMessage(MessageT&& message) {
369 putMessageImpl(std::move(message), 0);
371 void putMessage(const MessageT& message) {
372 putMessageImpl(message, 0);
376 * Put several messages on the queue.
378 template<typename InputIteratorT>
379 void putMessages(InputIteratorT first, InputIteratorT last) {
380 typedef typename std::iterator_traits<InputIteratorT>::iterator_category
382 putMessagesImpl(first, last, IterCategory());
386 * Try to immediately pull a message off of the queue, without blocking.
388 * If a message is immediately available, the result parameter will be
389 * updated to contain the message contents and true will be returned.
391 * If no message is available, false will be returned and result will be left
394 bool tryConsume(MessageT& result) {
399 folly::SpinLockGuard g(spinlock_);
401 if (UNLIKELY(queue_.empty())) {
405 auto data = std::move(queue_.front());
407 RequestContext::setContext(data.second);
411 // Handle an exception if the assignment operator happens to throw.
412 // We consumed an event but weren't able to pop the message off the
413 // queue. Signal the event again since the message is still in the
423 folly::SpinLockGuard g(spinlock_);
424 return queue_.size();
428 * Check that the NotificationQueue is being used from the correct process.
430 * If you create a NotificationQueue in one process, then fork, and try to
431 * send messages to the queue from the child process, you're going to have a
432 * bad time. Unfortunately users have (accidentally) run into this.
434 * Because we use an eventfd/pipe, the child process can actually signal the
435 * parent process that an event is ready. However, it can't put anything on
436 * the parent's queue, so the parent wakes up and finds an empty queue. This
437 * check ensures that we catch the problem in the misbehaving child process
438 * code, and crash before signalling the parent process.
440 void checkPid() const {
441 CHECK_EQ(pid_, getpid());
445 // Forbidden copy constructor and assignment operator
446 NotificationQueue(NotificationQueue const &) = delete;
447 NotificationQueue& operator=(NotificationQueue const &) = delete;
449 inline bool checkQueueSize(size_t maxSize, bool throws=true) const {
450 DCHECK(0 == spinlock_.trylock());
451 if (maxSize > 0 && queue_.size() >= maxSize) {
453 throw std::overflow_error("unable to add message to NotificationQueue: "
461 inline bool checkDraining(bool throws=true) {
462 if (UNLIKELY(draining_ && throws)) {
463 throw std::runtime_error("queue is draining, cannot add message");
468 inline void signalEvent(size_t numAdded = 1) const {
469 static const uint8_t kPipeMessage[] = {
470 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
473 ssize_t bytes_written = 0;
474 ssize_t bytes_expected = 0;
476 // eventfd(2) dictates that we must write a 64-bit integer
477 uint64_t numAdded64(numAdded);
478 bytes_expected = static_cast<ssize_t>(sizeof(numAdded64));
479 bytes_written = ::write(eventfd_, &numAdded64, sizeof(numAdded64));
481 // pipe semantics, add one message for each numAdded
482 bytes_expected = numAdded;
484 size_t messageSize = std::min(numAdded, sizeof(kPipeMessage));
485 ssize_t rc = ::write(pipeFds_[1], kPipeMessage, messageSize);
487 // TODO: if the pipe is full, write will fail with EAGAIN.
488 // See task #1044651 for how this could be handled
493 } while (numAdded > 0);
495 if (bytes_written != bytes_expected) {
496 folly::throwSystemError("failed to signal NotificationQueue after "
501 bool tryConsumeEvent() {
505 rc = ::read(eventfd_, &value, sizeof(value));
508 rc = ::read(pipeFds_[0], &value8, sizeof(value8));
512 // EAGAIN should pretty much be the only error we can ever get.
513 // This means someone else already processed the only available message.
514 CHECK_EQ(errno, EAGAIN);
521 bool putMessageImpl(MessageT&& message, size_t maxSize, bool throws=true) {
525 folly::SpinLockGuard g(spinlock_);
526 if (checkDraining(throws) || !checkQueueSize(maxSize, throws)) {
529 // We only need to signal an event if not all consumers are
531 if (numActiveConsumers_ < numConsumers_) {
534 queue_.emplace_back(std::move(message), RequestContext::saveContext());
543 const MessageT& message, size_t maxSize, bool throws=true) {
547 folly::SpinLockGuard g(spinlock_);
548 if (checkDraining(throws) || !checkQueueSize(maxSize, throws)) {
551 if (numActiveConsumers_ < numConsumers_) {
554 queue_.emplace_back(message, RequestContext::saveContext());
562 template<typename InputIteratorT>
563 void putMessagesImpl(InputIteratorT first, InputIteratorT last,
564 std::input_iterator_tag) {
569 folly::SpinLockGuard g(spinlock_);
571 while (first != last) {
572 queue_.emplace_back(*first, RequestContext::saveContext());
576 if (numActiveConsumers_ < numConsumers_) {
585 mutable folly::SpinLock spinlock_;
587 int pipeFds_[2]; // to fallback to on older/non-linux systems
588 uint32_t advisoryMaxQueueSize_;
590 std::deque<std::pair<MessageT, std::shared_ptr<RequestContext>>> queue_;
591 int numConsumers_{0};
592 std::atomic<int> numActiveConsumers_{0};
593 bool draining_{false};
596 template<typename MessageT>
597 void NotificationQueue<MessageT>::Consumer::destroy() {
598 // If we are in the middle of a call to handlerReady(), destroyedFlagPtr_
599 // will be non-nullptr. Mark the value that it points to, so that
600 // handlerReady() will know the callback is destroyed, and that it cannot
601 // access any member variables anymore.
602 if (destroyedFlagPtr_) {
603 *destroyedFlagPtr_ = true;
606 DelayedDestruction::destroy();
609 template<typename MessageT>
610 void NotificationQueue<MessageT>::Consumer::handlerReady(uint16_t /*events*/)
612 consumeMessages(false);
615 template<typename MessageT>
616 void NotificationQueue<MessageT>::Consumer::consumeMessages(
617 bool isDrain, size_t* numConsumed) noexcept {
618 DestructorGuard dg(this);
619 uint32_t numProcessed = 0;
620 bool firstRun = true;
622 SCOPE_EXIT { setActive(false, /* shouldLock = */ true); };
624 if (numConsumed != nullptr) {
625 *numConsumed = numProcessed;
629 // Try to decrement the eventfd.
631 // The eventfd is only used to wake up the consumer - there may or
632 // may not actually be an event available (another consumer may
633 // have read it). We don't really care, we only care about
634 // emptying the queue.
635 if (!isDrain && firstRun) {
636 queue_->tryConsumeEvent();
640 // Now pop the message off of the queue.
642 // We have to manually acquire and release the spinlock here, rather than
643 // using SpinLockHolder since the MessageT has to be constructed while
644 // holding the spinlock and available after we release it. SpinLockHolder
645 // unfortunately doesn't provide a release() method. (We can't construct
646 // MessageT first since we have no guarantee that MessageT has a default
648 queue_->spinlock_.lock();
652 if (UNLIKELY(queue_->queue_.empty())) {
653 // If there is no message, we've reached the end of the queue, return.
655 queue_->spinlock_.unlock();
659 // Pull a message off the queue.
660 auto& data = queue_->queue_.front();
662 MessageT msg(std::move(data.first));
664 RequestContext::setContext(data.second);
665 queue_->queue_.pop_front();
667 // Check to see if the queue is empty now.
668 // We use this as an optimization to see if we should bother trying to
669 // loop again and read another message after invoking this callback.
670 bool wasEmpty = queue_->queue_.empty();
675 // Now unlock the spinlock before we invoke the callback.
676 queue_->spinlock_.unlock();
680 bool callbackDestroyed = false;
681 CHECK(destroyedFlagPtr_ == nullptr);
682 destroyedFlagPtr_ = &callbackDestroyed;
683 messageAvailable(std::move(msg));
684 destroyedFlagPtr_ = nullptr;
686 RequestContext::setContext(old_ctx);
688 // If the callback was destroyed before it returned, we are done
689 if (callbackDestroyed) {
693 // If the callback is no longer installed, we are done.
694 if (queue_ == nullptr) {
698 // If we have hit maxReadAtOnce_, we are done.
700 if (!isDrain && maxReadAtOnce_ > 0 &&
701 numProcessed >= maxReadAtOnce_) {
702 queue_->signalEvent(1);
706 // If the queue was empty before we invoked the callback, it's probable
707 // that it is still empty now. Just go ahead and return, rather than
708 // looping again and trying to re-read from the eventfd. (If a new
709 // message had in fact arrived while we were invoking the callback, we
710 // will simply be woken up the next time around the event loop and will
711 // process the message then.)
715 } catch (const std::exception& ex) {
716 // This catch block is really just to handle the case where the MessageT
717 // constructor throws. The messageAvailable() callback itself is
718 // declared as noexcept and should never throw.
720 // If the MessageT constructor does throw we try to handle it as best as
721 // we can, but we can't work miracles. We will just ignore the error for
722 // now and return. The next time around the event loop we will end up
723 // trying to read the message again. If MessageT continues to throw we
724 // will never make forward progress and will keep trying each time around
727 // Unlock the spinlock.
728 queue_->spinlock_.unlock();
730 // Push a notification back on the eventfd since we didn't actually
731 // read the message off of the queue.
733 queue_->signalEvent(1);
742 template<typename MessageT>
743 void NotificationQueue<MessageT>::Consumer::init(
744 EventBase* eventBase,
745 NotificationQueue* queue) {
746 assert(eventBase->isInEventBaseThread());
747 assert(queue_ == nullptr);
748 assert(!isHandlerRegistered());
756 folly::SpinLockGuard g(queue_->spinlock_);
757 queue_->numConsumers_++;
759 queue_->signalEvent();
761 if (queue_->eventfd_ >= 0) {
762 initHandler(eventBase, queue_->eventfd_);
764 initHandler(eventBase, queue_->pipeFds_[0]);
768 template<typename MessageT>
769 void NotificationQueue<MessageT>::Consumer::stopConsuming() {
770 if (queue_ == nullptr) {
771 assert(!isHandlerRegistered());
776 folly::SpinLockGuard g(queue_->spinlock_);
777 queue_->numConsumers_--;
781 assert(isHandlerRegistered());
787 template<typename MessageT>
788 bool NotificationQueue<MessageT>::Consumer::consumeUntilDrained(
789 size_t* numConsumed) noexcept {
790 DestructorGuard dg(this);
792 folly::SpinLockGuard g(queue_->spinlock_);
793 if (queue_->draining_) {
796 queue_->draining_ = true;
798 consumeMessages(true, numConsumed);
800 folly::SpinLockGuard g(queue_->spinlock_);
801 queue_->draining_ = false;
807 * Creates a NotificationQueue::Consumer wrapping a function object
808 * Modeled after AsyncTimeout::make
814 template <typename MessageT, typename TCallback>
815 struct notification_queue_consumer_wrapper
816 : public NotificationQueue<MessageT>::Consumer {
818 template <typename UCallback>
819 explicit notification_queue_consumer_wrapper(UCallback&& callback)
820 : callback_(std::forward<UCallback>(callback)) {}
822 // we are being stricter here and requiring noexcept for callback
823 void messageAvailable(MessageT&& message) override {
825 noexcept(std::declval<TCallback>()(std::forward<MessageT>(message))),
826 "callback must be declared noexcept, e.g.: `[]() noexcept {}`"
829 callback_(std::forward<MessageT>(message));
836 } // namespace detail
838 template <typename MessageT>
839 template <typename TCallback>
840 std::unique_ptr<typename NotificationQueue<MessageT>::Consumer,
841 DelayedDestruction::Destructor>
842 NotificationQueue<MessageT>::Consumer::make(TCallback&& callback) {
843 return std::unique_ptr<NotificationQueue<MessageT>::Consumer,
844 DelayedDestruction::Destructor>(
845 new detail::notification_queue_consumer_wrapper<
847 typename std::decay<TCallback>::type>(
848 std::forward<TCallback>(callback)));