proxygen
UnboundedBlockingQueue.h
Go to the documentation of this file.
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 
17 #pragma once
18 
22 
23 namespace folly {
24 
25 template <class T>
27  public:
29 
30  BlockingQueueAddResult add(T item) override {
31  queue_.enqueue(std::move(item));
32  return sem_.post();
33  }
34 
35  T take() override {
36  T item;
37  while (!queue_.try_dequeue(item)) {
38  sem_.wait();
39  }
40  return item;
41  }
42 
43  folly::Optional<T> try_take_for(std::chrono::milliseconds time) override {
44  T item;
45  while (!queue_.try_dequeue(item)) {
46  if (!sem_.try_wait_for(time)) {
47  return folly::none;
48  }
49  }
50  return std::move(item);
51  }
52 
53  size_t size() override {
54  return queue_.size();
55  }
56 
57  private:
60 };
61 
62 } // namespace folly
UMPMCQueue< T, false, 6 > queue_
constexpr detail::Map< Move > move
Definition: Base-inl.h:2567
bool post()
Silently saturates if value is already 2^32-1.
Definition: LifoSem.h:361
folly::std T
—— Concurrent Priority Queue Implementation ——
Definition: AtomicBitSet.h:29
folly::Optional< T > try_take_for(std::chrono::milliseconds time) override
bool try_wait_for(const std::chrono::duration< Rep, Period > &timeout)
Definition: LifoSem.h:472
BlockingQueueAddResult add(T item) override
std::chrono::nanoseconds time()
constexpr None none
Definition: Optional.h:87