summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorHirokazu Honda <hiroh@chromium.org>2021-12-01 16:53:41 +0900
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>2021-12-01 13:46:47 +0200
commitc17f172842d17b6036e10b1775e89d0219c3b3f9 (patch)
tree6ce969f3be6e8d000c3ad9eb8eb6c351ac3b008c /include
parent16efd83f5dafb8261aac2e955576bc7d474043f9 (diff)
libcamera: base: Add mutex classes with thread safety annotations
This replaces Mutex and MutexLocker with our own defined classes. The classes are annotated by clang thread safety annotations. So we can add annotation to code where the classes are used. v4l2 code needs to be annotated, which violates Mutex capability. Signed-off-by: Hirokazu Honda <hiroh@chromium.org> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com> Reviewed-by: Umang Jain <umang.jain@ideasonboard.com> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'include')
-rw-r--r--include/libcamera/base/meson.build1
-rw-r--r--include/libcamera/base/mutex.h132
-rw-r--r--include/libcamera/base/thread.h7
3 files changed, 134 insertions, 6 deletions
diff --git a/include/libcamera/base/meson.build b/include/libcamera/base/meson.build
index 1a71ce5a..37c4435a 100644
--- a/include/libcamera/base/meson.build
+++ b/include/libcamera/base/meson.build
@@ -13,6 +13,7 @@ libcamera_base_headers = files([
'flags.h',
'log.h',
'message.h',
+ 'mutex.h',
'object.h',
'private.h',
'semaphore.h',
diff --git a/include/libcamera/base/mutex.h b/include/libcamera/base/mutex.h
new file mode 100644
index 00000000..2d23e49e
--- /dev/null
+++ b/include/libcamera/base/mutex.h
@@ -0,0 +1,132 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+/*
+ * Copyright (C) 2021, Google Inc.
+ *
+ * mutex.h - Mutex classes with clang thread safety annotation
+ */
+
+#pragma once
+
+#include <condition_variable>
+#include <mutex>
+
+#include <libcamera/base/thread_annotations.h>
+
+namespace libcamera {
+
+/* \todo using Mutex = std::mutex if libc++ is used. */
+
+#ifndef __DOXYGEN__
+
+class LIBCAMERA_TSA_CAPABILITY("mutex") Mutex final
+{
+public:
+ constexpr Mutex()
+ {
+ }
+
+ void lock() LIBCAMERA_TSA_ACQUIRE()
+ {
+ mutex_.lock();
+ }
+
+ void unlock() LIBCAMERA_TSA_RELEASE()
+ {
+ mutex_.unlock();
+ }
+
+private:
+ friend class MutexLocker;
+
+ std::mutex mutex_;
+};
+
+class LIBCAMERA_TSA_SCOPED_CAPABILITY MutexLocker final
+{
+public:
+ explicit MutexLocker(Mutex &mutex) LIBCAMERA_TSA_ACQUIRE(mutex)
+ : lock_(mutex.mutex_)
+ {
+ }
+
+ MutexLocker(Mutex &mutex, std::defer_lock_t t) noexcept LIBCAMERA_TSA_EXCLUDES(mutex)
+ : lock_(mutex.mutex_, t)
+ {
+ }
+
+ ~MutexLocker() LIBCAMERA_TSA_RELEASE()
+ {
+ }
+
+ void lock() LIBCAMERA_TSA_ACQUIRE()
+ {
+ lock_.lock();
+ }
+
+ bool try_lock() LIBCAMERA_TSA_TRY_ACQUIRE(true)
+ {
+ return lock_.try_lock();
+ }
+
+ void unlock() LIBCAMERA_TSA_RELEASE()
+ {
+ lock_.unlock();
+ }
+
+private:
+ friend class ConditionVariable;
+
+ std::unique_lock<std::mutex> lock_;
+};
+
+class ConditionVariable final
+{
+public:
+ ConditionVariable()
+ {
+ }
+
+ void notify_one() noexcept
+ {
+ cv_.notify_one();
+ }
+
+ void notify_all() noexcept
+ {
+ cv_.notify_all();
+ }
+
+ template<class Predicate>
+ void wait(MutexLocker &locker, Predicate stopWaiting)
+ {
+ cv_.wait(locker.lock_, stopWaiting);
+ }
+
+ template<class Rep, class Period, class Predicate>
+ bool wait_for(MutexLocker &locker,
+ const std::chrono::duration<Rep, Period> &relTime,
+ Predicate stopWaiting)
+ {
+ return cv_.wait_for(locker.lock_, relTime, stopWaiting);
+ }
+
+private:
+ std::condition_variable cv_;
+};
+
+#else /* __DOXYGEN__ */
+
+class Mutex final
+{
+};
+
+class MutexLocker final
+{
+};
+
+class ConditionVariable final
+{
+};
+
+#endif /* __DOXYGEN__ */
+} /* namespace libcamera */
diff --git a/include/libcamera/base/thread.h b/include/libcamera/base/thread.h
index 1ebf8363..44678c34 100644
--- a/include/libcamera/base/thread.h
+++ b/include/libcamera/base/thread.h
@@ -7,15 +7,14 @@
#pragma once
-#include <condition_variable>
#include <memory>
-#include <mutex>
#include <sys/types.h>
#include <thread>
#include <libcamera/base/private.h>
#include <libcamera/base/message.h>
+#include <libcamera/base/mutex.h>
#include <libcamera/base/signal.h>
#include <libcamera/base/utils.h>
@@ -27,10 +26,6 @@ class Object;
class ThreadData;
class ThreadMain;
-using ConditionVariable = std::condition_variable;
-using Mutex = std::mutex;
-using MutexLocker = std::unique_lock<std::mutex>;
-
class Thread
{
public: