opuntiaOS - an operating system targeting x86 and ARMv7
at master 55 lines 1.3 kB view raw
1/* 2 * Copyright (C) 2020-2022 The opuntiaOS Project Authors. 3 * + Contributed by Nikita Melekhin <nimelehin@gmail.com> 4 * 5 * Use of this source code is governed by a BSD-style license that can be 6 * found in the LICENSE file. 7 */ 8 9#ifndef _KERNEL_LIBKERN_LOCK_H 10#define _KERNEL_LIBKERN_LOCK_H 11 12#include <libkern/c_attrs.h> 13#include <libkern/kassert.h> 14#include <libkern/log.h> 15#include <libkern/types.h> 16 17// #define DEBUG_LOCK 18 19struct lock { 20 int status; 21#ifdef DEBUG_LOCK 22 23#endif // DEBUG_LOCK 24}; 25typedef struct lock lock_t; 26 27static ALWAYS_INLINE void lock_init(lock_t* lock) 28{ 29 __atomic_store_n(&lock->status, 0, __ATOMIC_RELAXED); 30} 31 32static ALWAYS_INLINE void lock_acquire(lock_t* lock) 33{ 34 while (__atomic_exchange_n(&lock->status, 1, __ATOMIC_ACQUIRE) == 1) { 35 // TODO: May be some cpu sleep? 36 } 37} 38 39static ALWAYS_INLINE void lock_release(lock_t* lock) 40{ 41 ASSERT(lock->status == 1); 42 __atomic_store_n(&lock->status, 0, __ATOMIC_RELEASE); 43} 44 45#ifdef DEBUG_LOCK 46#define lock_acquire(x) \ 47 log("acquire lock %s %s:%d ", #x, __FILE__, __LINE__); \ 48 lock_acquire(x); 49 50#define lock_release(x) \ 51 log("release lock %s %s:%d ", #x, __FILE__, __LINE__); \ 52 lock_release(x); 53#endif 54 55#endif // _KERNEL_LIBKERN_LOCK_H