NOVA
Stripped down NOVA kernel for the OSY course
Loading...
Searching...
No Matches
spinlock.h
1/*
2 * Generic Spinlock
3 *
4 * Copyright (C) 2009-2011 Udo Steinberg <udo@hypervisor.org>
5 * Economic rights: Technische Universitaet Dresden (Germany)
6 *
7 * This file is part of the NOVA microhypervisor.
8 *
9 * NOVA is free software: you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * NOVA is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License version 2 for more details.
17 */
18
19#pragma once
20
21#include "compiler.h"
22#include "types.h"
23
24class Spinlock
25{
26 private:
27 uint16 val;
28
29 public:
30 ALWAYS_INLINE
31 inline Spinlock() : val (0) {}
32
33 NOINLINE
34 void lock()
35 {
36 uint16 tmp = 0x100;
37
38 asm volatile (" lock; xadd %0, %1; "
39 "1: cmpb %h0, %b0; "
40 " je 2f; "
41 " pause; "
42 " movb %1, %b0; "
43 " jmp 1b; "
44 "2: "
45 : "+Q" (tmp), "+m" (val) : : "memory");
46 }
47
48 ALWAYS_INLINE
49 inline void unlock()
50 {
51 asm volatile ("incb %0" : "=m" (val) : : "memory");
52 }
53};