[clang] [llvm] [Support] Introduce new `AdvisoryLock` interface (PR #130989)

Michael Spencer via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 12 13:13:33 PDT 2025


================
@@ -0,0 +1,54 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_SUPPORT_ADVISORYLOCK_H
+#define LLVM_SUPPORT_ADVISORYLOCK_H
+
+#include "llvm/Support/Error.h"
+
+namespace llvm {
+/// Describes the result of waiting for the owner to release the lock.
+enum class WaitForUnlockResult {
+  /// The lock was released successfully.
+  Success,
+  /// Owner died while holding the lock.
+  OwnerDied,
+  /// Reached timeout while waiting for the owner to release the lock.
+  Timeout,
+};
+
+/// A synchronization primitive with weak mutual exclusion guarantees.
+/// Implementations of this interface may allow multiple threads/processes to
+/// acquire the lock simultaneously. Typically, threads/processes waiting for
+/// the lock to be unlocked will validate the computation produced valid result.
+class AdvisoryLock {
+public:
+  /// Tries to acquire the lock without blocking.
+  ///
+  /// \returns true if the lock was successfully acquired (owned lock), false if
+  /// the lock is already held by someone else (shared lock), or \c Error in
----------------
Bigcheese wrote:

I think the term "shared lock" is misleading. Generally that means that you have a read lock, but here it just means you don't own the lock.

https://github.com/llvm/llvm-project/pull/130989


More information about the cfe-commits mailing list