blob: e42f60ed072816c92bfe212db6d7fa64d1c85174 [file] [log] [blame]
// Copyright 2020 The Pigweed Authors
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License. You may obtain a copy of
// the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations under
// the License.
#pragma once
#include <stdbool.h>
#include "pw_preprocessor/util.h"
#ifdef __cplusplus
#include "pw_sync_backend/interrupt_spin_lock_native.h"
namespace pw::sync {
// The InterruptSpinLock is a synchronization primitive that can be used to
// protect shared data from being simultaneously accessed by multiple threads
// and/or interrupts as a targeted global lock (except for NMIs).
// It offers exclusive, non-recursive ownership semantics where IRQs up to a
// backend defined level of "NMIs" will be masked to solve priority-inversion.
//
// NOTE: This InterruptSpinLock relies on built-in local interrupt masking to
// make it interrupt safe without requiring the caller to separately mask and
// unmask interrupts when using this primitive.
//
// Unlike global interrupt locks, this also works safely and efficiently on SMP
// systems. This entire API is IRQ safe.
//
// Precondition: Code that holds a specific InterruptSpinLock must not try to
// re-acquire it. However, it is okay to nest distinct spinlocks.
class InterruptSpinLock {
public:
using native_handle_type = backend::NativeInterruptSpinLockHandle;
constexpr InterruptSpinLock();
~InterruptSpinLock() = default;
InterruptSpinLock(const InterruptSpinLock&) = delete;
InterruptSpinLock(InterruptSpinLock&&) = delete;
InterruptSpinLock& operator=(const InterruptSpinLock&) = delete;
InterruptSpinLock& operator=(InterruptSpinLock&&) = delete;
// Locks the spinlock, blocking indefinitely. Failures are fatal.
//
// Precondition: Recursive locking is undefined behavior.
void lock();
// Attempts to lock the spinlock in a non-blocking manner.
// Returns true if the spinlock was successfully acquired.
//
// Precondition: Recursive locking is undefined behavior.
bool try_lock();
// Unlocks the spinlock. Failures are fatal.
void unlock();
native_handle_type native_handle();
private:
// This may be a wrapper around a native type with additional members.
backend::NativeInterruptSpinLock native_type_;
};
} // namespace pw::sync
#include "pw_sync_backend/interrupt_spin_lock_inline.h"
using pw_sync_InterruptSpinLock = pw::sync::InterruptSpinLock;
#else // !defined(__cplusplus)
typedef struct pw_sync_InterruptSpinLock pw_sync_InterruptSpinLock;
#endif // __cplusplus
PW_EXTERN_C_START
void pw_sync_InterruptSpinLock_Lock(pw_sync_InterruptSpinLock* spin_lock);
bool pw_sync_InterruptSpinLock_TryLock(pw_sync_InterruptSpinLock* spin_lock);
void pw_sync_InterruptSpinLock_Unlock(pw_sync_InterruptSpinLock* spin_lock);
PW_EXTERN_C_END