NOVA
Stripped down NOVA kernel for the OSY course
Loading...
Searching...
No Matches
atomic.h
1/*
2 * Atomic Operations
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
23class Atomic
24{
25 public:
26 template <typename T>
27 ALWAYS_INLINE
28 static inline bool cmp_swap (T &ptr, T o, T n) { return __sync_bool_compare_and_swap (&ptr, o, n); }
29
30 template <typename T>
31 ALWAYS_INLINE
32 static inline T add (T &ptr, T v) { return __sync_add_and_fetch (&ptr, v); }
33
34 template <typename T>
35 ALWAYS_INLINE
36 static inline T sub (T &ptr, T v) { return __sync_sub_and_fetch (&ptr, v); }
37
38 template <typename T>
39 ALWAYS_INLINE
40 static inline void set_mask (T &ptr, T v) { __sync_or_and_fetch (&ptr, v); }
41
42 template <typename T>
43 ALWAYS_INLINE
44 static inline void clr_mask (T &ptr, T v) { __sync_and_and_fetch (&ptr, ~v); }
45
46 template <typename T>
47 ALWAYS_INLINE
48 static inline bool test_set_bit (T &val, unsigned long bit)
49 {
50 bool ret;
51 asm volatile ("lock; bts%z1 %2, %1; setc %0" : "=q" (ret), "+m" (val) : "ir" (bit) : "cc");
52 return ret;
53 }
54
55 template <typename T>
56 ALWAYS_INLINE
57 static inline bool test_clr_bit (T &val, unsigned long bit)
58 {
59 bool ret;
60 asm volatile ("lock; btr%z1 %2, %1; setc %0" : "=q" (ret), "+m" (val) : "ir" (bit) : "cc");
61 return ret;
62 }
63};
Definition atomic.h:24