fixed_size_blocking_queue_impl.h revision ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464
1aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol/*
2aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * Copyright (C) 2016 The Android Open Source Project
3aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol *
4aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * Licensed under the Apache License, Version 2.0 (the "License");
5aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * you may not use this file except in compliance with the License.
6aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * You may obtain a copy of the License at
7aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol *
8aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol *      http://www.apache.org/licenses/LICENSE-2.0
9aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol *
10aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * Unless required by applicable law or agreed to in writing, software
11aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * distributed under the License is distributed on an "AS IS" BASIS,
12aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * See the License for the specific language governing permissions and
14aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol * limitations under the License.
15aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol */
16aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
17022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol#ifndef CHRE_UTIL_FIXED_SIZE_BLOCKING_QUEUE_IMPL_H_
18022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol#define CHRE_UTIL_FIXED_SIZE_BLOCKING_QUEUE_IMPL_H_
19aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
20022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol#include "chre/util/fixed_size_blocking_queue.h"
21af8a810ee31e82fb1a5b18d3caef98633ba22799Andrew Rossignol#include "chre/util/lock_guard.h"
22aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
23aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignolnamespace chre {
24aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
25022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignoltemplate<typename ElementType, size_t kSize>
26042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddiebool FixedSizeBlockingQueue<ElementType, kSize>::push(
27022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol    const ElementType& element) {
28042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie  bool success;
29aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol  {
30af8a810ee31e82fb1a5b18d3caef98633ba22799Andrew Rossignol    LockGuard<Mutex> lock(mMutex);
31042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie    success = mQueue.push(element);
32aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol  }
33042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie  if (success) {
34042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie    mConditionVariable.notify_one();
35042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie  }
36042f73e3b7f019c818f69b2c097c4b6f9db839b5Brian Duddie  return success;
37aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol}
38aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
39022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignoltemplate<typename ElementType, size_t kSize>
40022cc6c770c299bb364e35377a2661791475bbceAndrew RossignolElementType FixedSizeBlockingQueue<ElementType, kSize>::pop() {
41af8a810ee31e82fb1a5b18d3caef98633ba22799Andrew Rossignol  LockGuard<Mutex> lock(mMutex);
42aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol  while (mQueue.empty()) {
43aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol    mConditionVariable.wait(mMutex);
44aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol  }
45aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
46aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol  ElementType element(std::move(mQueue.front()));
47022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignol  mQueue.pop();
48aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol  return element;
49aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol}
50aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
51022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignoltemplate<typename ElementType, size_t kSize>
52022cc6c770c299bb364e35377a2661791475bbceAndrew Rossignolbool FixedSizeBlockingQueue<ElementType, kSize>::empty() {
53af8a810ee31e82fb1a5b18d3caef98633ba22799Andrew Rossignol  LockGuard<Mutex> lock(mMutex);
54aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol  return mQueue.empty();
55aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol}
56aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
57ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishigurotemplate<typename ElementType, size_t kSize>
58ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishigurosize_t FixedSizeBlockingQueue<ElementType, kSize>::size() {
59ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro  LockGuard<Mutex> lock(mMutex);
60ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro  return mQueue.size();
61ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro}
62ff1c1a2ab3ff2614bb6cea1f1d6f6e9c44a87464Arthur Ishiguro
63aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol}  // namespace chre
64aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol
65aa718a9536c0547620c183d90195bfb0f76f8284Andrew Rossignol#endif  // CHRE_UTIL_BLOCKING_QUEUE_IMPL_H_
66