xref: /haiku/headers/private/kernel/util/atomic.h (revision 830f67ef991407f287dbc1238aa5f5906d90c991)
1 /*
2  * Copyright 2008, Axel Dörfler, axeld@pinc-software.de.
3  * Distributed under the terms of the MIT License.
4  */
5 #ifndef _KERNEL_UTIL_ATOMIC_H
6 #define _KERNEL_UTIL_ATOMIC_H
7 
8 
9 #include <limits.h>
10 
11 #include <SupportDefs.h>
12 
13 #include <debug.h>
14 
15 
16 #ifdef __cplusplus
17 
18 template<typename PointerType> PointerType*
19 atomic_pointer_test_and_set(PointerType** _pointer, const PointerType* set,
20 	const PointerType* test)
21 {
22 #if LONG_MAX == INT_MAX
23 	return (PointerType*)atomic_test_and_set((int32*)_pointer, (int32)set,
24 		(int32)test);
25 #else
26 	return (PointerType*)atomic_test_and_set64((int64*)_pointer, (int64)set,
27 		(int64)test);
28 #endif
29 }
30 
31 
32 template<typename PointerType> PointerType*
33 atomic_pointer_get_and_set(PointerType** _pointer, const PointerType* set)
34 {
35 #if LONG_MAX == INT_MAX
36 	return (PointerType*)atomic_get_and_set((int32*)_pointer, (int32)set);
37 #else
38 	return (PointerType*)atomic_get_and_set64((int64*)_pointer, (int64)set);
39 #endif
40 }
41 
42 
43 template<typename PointerType> void
44 atomic_pointer_set(PointerType** _pointer, const PointerType* set)
45 {
46 	ASSERT((addr_t(_pointer) & (sizeof(PointerType*) - 1)) == 0);
47 #if LONG_MAX == INT_MAX
48 	atomic_set((int32*)_pointer, (int32)set);
49 #else
50 	atomic_set64((int64*)_pointer, (int64)set);
51 #endif
52 }
53 
54 
55 template<typename PointerType> PointerType*
56 atomic_pointer_get(PointerType* const* _pointer)
57 {
58 	ASSERT((addr_t(_pointer) & (sizeof(PointerType*) - 1)) == 0);
59 #if LONG_MAX == INT_MAX
60 	return (PointerType*)atomic_get((int32*)_pointer);
61 #else
62 	return (PointerType*)atomic_get64((int64*)_pointer);
63 #endif
64 }
65 
66 #endif	// __cplusplus
67 
68 #endif	/* _KERNEL_UTIL_ATOMIC_H */
69