1/*
2 * Copyright 2017-present Facebook, Inc.
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16#pragma once
17
18#include <chrono>
19#include <exception>
20#include <stdexcept>
21
22#include <glog/logging.h>
23
24#include <folly/CPortability.h>
25#include <folly/Optional.h>
26
27namespace folly {
28
29// Some queue implementations (for example, LifoSemMPMCQueue or
30// PriorityLifoSemMPMCQueue) support both blocking (BLOCK) and
31// non-blocking (THROW) behaviors.
32enum class QueueBehaviorIfFull { THROW, BLOCK };
33
34class FOLLY_EXPORT QueueFullException : public std::runtime_error {
35 using std::runtime_error::runtime_error; // Inherit constructors.
36};
37
38struct BlockingQueueAddResult {
39 BlockingQueueAddResult(bool reused = false) : reusedThread(reused) {}
40 bool reusedThread;
41};
42
43template <class T>
44class BlockingQueue {
45 public:
46 virtual ~BlockingQueue() = default;
47 // Adds item to the queue (with priority).
48 //
49 // Returns true if an existing thread was able to work on it (used
50 // for dynamically sizing thread pools), false otherwise. Return false
51 // if this feature is not supported.
52 virtual BlockingQueueAddResult add(T item) = 0;
53 virtual BlockingQueueAddResult addWithPriority(
54 T item,
55 int8_t /* priority */) {
56 return add(std::move(item));
57 }
58 virtual uint8_t getNumPriorities() {
59 return 1;
60 }
61 virtual T take() = 0;
62 virtual folly::Optional<T> try_take_for(std::chrono::milliseconds time) = 0;
63 virtual size_t size() = 0;
64};
65
66} // namespace folly
67