LockedQueue.h 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. /*
  2. * Copyright (C) 2019 The Android Open Source Project
  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. #ifndef _DNS_LOCKED_QUEUE_H
  17. #define _DNS_LOCKED_QUEUE_H
  18. #include <algorithm>
  19. #include <deque>
  20. #include <mutex>
  21. #include <android-base/thread_annotations.h>
  22. namespace android {
  23. namespace net {
  24. template <typename T>
  25. class LockedQueue {
  26. public:
  27. // Push an item onto the queue.
  28. void push(T item) {
  29. std::lock_guard guard(mLock);
  30. mQueue.push_front(std::move(item));
  31. }
  32. // Swap out the contents of the queue
  33. void swap(std::deque<T>& other) {
  34. std::lock_guard guard(mLock);
  35. mQueue.swap(other);
  36. }
  37. private:
  38. std::mutex mLock;
  39. std::deque<T> mQueue GUARDED_BY(mLock);
  40. };
  41. } // end of namespace net
  42. } // end of namespace android
  43. #endif // _DNS_LOCKEDQUEUE_H