libcamera v0.7.2 Supporting cameras in Linux since 2019

Loading...
Searching...
No Matches
mutex.h
Go to the documentation of this file.
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2/*
3 * Copyright (C) 2021, Google Inc.
4 *
5 * Mutex classes with clang thread safety annotation
6 */
7
8#pragma once
9
10#include <condition_variable>
11#include <mutex>
12
13#include <libcamera/base/private.h>
14
15#include <libcamera/base/thread_annotations.h>
16
17 namespace libcamera {
18
19/* \todo using Mutex = std::mutex if libc++ is used. */
20
21#ifndef __DOXYGEN__
22
23 class LIBCAMERA_TSA_CAPABILITY("mutex") Mutex final
24{
25 public:
26 void lock() LIBCAMERA_TSA_ACQUIRE()
27 {
28 mutex_.lock();
29 }
30
31 void unlock() LIBCAMERA_TSA_RELEASE()
32 {
33 mutex_.unlock();
34 }
35
36 private:
37 friend class MutexLocker;
38
39 std::mutex mutex_;
40};
41
42 class LIBCAMERA_TSA_SCOPED_CAPABILITY MutexLocker final
43{
44 public:
45 explicit MutexLocker(Mutex &mutex) LIBCAMERA_TSA_ACQUIRE(mutex)
46 : lock_(mutex.mutex_)
47 {
48 }
49
50 MutexLocker(Mutex &mutex, std::defer_lock_t t) noexcept LIBCAMERA_TSA_EXCLUDES(mutex)
51 : lock_(mutex.mutex_, t)
52 {
53 }
54
55 ~MutexLocker() LIBCAMERA_TSA_RELEASE()
56 {
57 }
58
59 void lock() LIBCAMERA_TSA_ACQUIRE()
60 {
61 lock_.lock();
62 }
63
64 bool try_lock() LIBCAMERA_TSA_TRY_ACQUIRE(true)
65 {
66 return lock_.try_lock();
67 }
68
69 void unlock() LIBCAMERA_TSA_RELEASE()
70 {
71 lock_.unlock();
72 }
73
74 private:
75 friend class ConditionVariable;
76
77 std::unique_lock<std::mutex> lock_;
78};
79
80 class ConditionVariable final
81{
82 public:
83 void notify_one() noexcept
84 {
85 cv_.notify_one();
86 }
87
88 void notify_all() noexcept
89 {
90 cv_.notify_all();
91 }
92
93 template<class Predicate>
94 void wait(MutexLocker &locker, Predicate stopWaiting)
95 {
96 cv_.wait(locker.lock_, stopWaiting);
97 }
98
99 template<class Rep, class Period, class Predicate>
100 bool wait_for(MutexLocker &locker,
101 const std::chrono::duration<Rep, Period> &relTime,
102 Predicate stopWaiting)
103 {
104 return cv_.wait_for(locker.lock_, relTime, stopWaiting);
105 }
106
107 private:
108 std::condition_variable cv_;
109};
110
111#else /* __DOXYGEN__ */
112
113 class Mutex final
114{
115};
116
117 class MutexLocker final
118{
119};
120
121 class ConditionVariable final
122{
123};
124
125#endif /* __DOXYGEN__ */
126} /* namespace libcamera */
libcamera::ConditionVariable
std::condition_variable wrapper integrating with MutexLocker
Definition mutex.h:122
libcamera::MutexLocker
std::unique_lock wrapper with clang thread safety annotation
Definition mutex.h:118
libcamera::Mutex
std::mutex wrapper with clang thread safety annotation
Definition mutex.h:114
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17