The Pedigree Project 0.1
Spinlock.h
1/*
2 * Copyright (c) 2008-2014, Pedigree Developers
3 *
4 * Please see the CONTRIB file in the root of the source tree for a full
5 * list of contributors.
6 *
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 */
19
20#ifndef KERNEL_SPINLOCK_H
21#define KERNEL_SPINLOCK_H
22
23#include "pedigree/kernel/SpinlockWord.h"
24#include "pedigree/kernel/compiler.h"
25#include "pedigree/kernel/processor/types.h"
26
27class EXPORTED_PUBLIC Spinlock {
28 friend class PerProcessorScheduler;
29 friend class LocksCommand;
30
31 public:
32 Spinlock();
33 Spinlock(bool bLocked, bool bAvoidTracking = false);
34
42 bool acquire(bool recurse = false, bool safe = true);
43
45 void exit(uintptr_t ra = 0);
46
48 void release();
49
50 bool acquired();
51
52 bool interrupts() const;
53
54 static const bool allow_recursion = true;
55
56 private:
58 bool acquireContended(bool recurse, bool safe, uintptr_t ra) NEVER_INLINE;
59
61 void unlock(uintptr_t ra) ALWAYS_INLINE;
62
64 void unwind() ALWAYS_INLINE;
65
67 volatile processor_register_t* deferredReleaseWord();
68 void unlockForScheduler();
69
71 void trackRelease(uintptr_t ra) const;
72
73 uintptr_t acquisitionAddress() const;
74 void badMagic(uintptr_t ra) const NEVER_INLINE;
75 void badReleaseInterrupts() const NEVER_INLINE;
76 void deadlock(uintptr_t ra, bool releasing, uintptr_t acquiredAt = 0) NEVER_INLINE NORETURN;
77
78 NOT_COPYABLE_OR_ASSIGNABLE(Spinlock);
79
80 SpinlockWord m_Lock;
81 bool m_bInterrupts = false;
82
83#if SPINLOCK_DIAGNOSTICS
84 uint64_t m_Sentinel = 0;
85 uint32_t m_Magic = 0xdeadbaba;
86 uint32_t m_MagicAlign = 0;
87 uintptr_t m_Ra = 0;
88#endif
89
90 void* m_pOwner = nullptr;
91 size_t m_Level = 0;
92 size_t m_OwnedProcessor = ~0;
93
94 bool m_bAvoidTracking = false;
95};
96
97#endif