xref: /haiku/src/system/libroot/os/arch/generic/generic_atomic.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2  * Copyright 2014, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  */
6 
7 #include <atomic>
8 #include <sys/types.h>
9 
10 
11 extern "C" [[gnu::optimize("omit-frame-pointer")]] void
12 atomic_set(int32_t* ptr, int32_t value)
13 {
14 	auto& obj = *reinterpret_cast<std::atomic<int32_t>*>(ptr);
15 	obj.store(value, std::memory_order_release);
16 }
17 
18 
19 extern "C" [[gnu::optimize("omit-frame-pointer")]] int32_t
20 atomic_get_and_set(int32_t* ptr, int32_t value)
21 {
22 	auto& obj = *reinterpret_cast<std::atomic<int32_t>*>(ptr);
23 	return obj.exchange(value);
24 }
25 
26 
27 extern "C" [[gnu::optimize("omit-frame-pointer")]] int32_t
28 atomic_test_and_set(int32_t* ptr, int32_t desired, int32_t expected)
29 {
30 	auto& obj = *reinterpret_cast<std::atomic<int32_t>*>(ptr);
31 	obj.compare_exchange_strong(expected, desired);
32 	return expected;
33 }
34 
35 
36 extern "C" [[gnu::optimize("omit-frame-pointer")]] int32_t
37 atomic_add(int32_t* ptr, int32_t value)
38 {
39 	auto& obj = *reinterpret_cast<std::atomic<int32_t>*>(ptr);
40 	return obj.fetch_add(value);
41 }
42 
43 
44 extern "C" [[gnu::optimize("omit-frame-pointer")]] int32_t
45 atomic_and(int32_t* ptr, int32_t value)
46 {
47 	auto& obj = *reinterpret_cast<std::atomic<int32_t>*>(ptr);
48 	return obj.fetch_and(value);
49 }
50 
51 
52 extern "C" [[gnu::optimize("omit-frame-pointer")]] int32_t
53 atomic_or(int32_t* ptr, int32_t value)
54 {
55 	auto& obj = *reinterpret_cast<std::atomic<int32_t>*>(ptr);
56 	return obj.fetch_or(value);
57 }
58 
59 
60 extern "C" [[gnu::optimize("omit-frame-pointer")]] int32_t
61 atomic_get(int32_t* ptr)
62 {
63 	auto& obj = *reinterpret_cast<std::atomic<int32_t>*>(ptr);
64 	return obj.load(std::memory_order_acquire);
65 }
66 
67 
68 extern "C" [[gnu::optimize("omit-frame-pointer")]] void
69 atomic_set64(int64_t* ptr, int64_t value)
70 {
71 	auto& obj = *reinterpret_cast<std::atomic<int64_t>*>(ptr);
72 	obj.store(value, std::memory_order_release);
73 }
74 
75 
76 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
77 atomic_get_and_set64(int64_t* ptr, int64_t value)
78 {
79 	auto& obj = *reinterpret_cast<std::atomic<int64_t>*>(ptr);
80 	return obj.exchange(value);
81 }
82 
83 
84 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
85 atomic_test_and_set64(int64_t* ptr, int64_t desired, int64_t expected)
86 {
87 	auto& obj = *reinterpret_cast<std::atomic<int64_t>*>(ptr);
88 	obj.compare_exchange_strong(expected, desired);
89 	return expected;
90 }
91 
92 
93 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
94 atomic_add64(int64_t* ptr, int64_t value)
95 {
96 	auto& obj = *reinterpret_cast<std::atomic<int64_t>*>(ptr);
97 	return obj.fetch_add(value);
98 }
99 
100 
101 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
102 atomic_and64(int64_t* ptr, int64_t value)
103 {
104 	auto& obj = *reinterpret_cast<std::atomic<int64_t>*>(ptr);
105 	return obj.fetch_and(value);
106 }
107 
108 
109 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
110 atomic_or64(int64_t* ptr, int64_t value)
111 {
112 	auto& obj = *reinterpret_cast<std::atomic<int64_t>*>(ptr);
113 	return obj.fetch_or(value);
114 }
115 
116 
117 extern "C" [[gnu::optimize("omit-frame-pointer")]] int64_t
118 atomic_get64(int64_t* ptr)
119 {
120 	auto& obj = *reinterpret_cast<std::atomic<int64_t>*>(ptr);
121 	return obj.load(std::memory_order_acquire);
122 }
123 
124