]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/bind9/lib/isc/x86_64/include/isc/atomic.h
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / bind9 / lib / isc / x86_64 / include / isc / atomic.h
1 /*
2  * Copyright (C) 2005  Internet Systems Consortium, Inc. ("ISC")
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14  * PERFORMANCE OF THIS SOFTWARE.
15  */
16
17 /* $Id: atomic.h,v 1.2.20.1 2005/09/02 13:27:12 marka Exp $ */
18
19 #ifndef ISC_ATOMIC_H
20 #define ISC_ATOMIC_H 1
21
22 #include <isc/platform.h>
23 #include <isc/types.h>
24
25 #ifdef ISC_PLATFORM_USEGCCASM
26
27 /* We share the gcc-version with x86_32 */
28 #error "impossible case.  check build configuration"
29
30 #elif defined(ISC_PLATFORM_USESTDASM)
31 /*
32  * The followings are "generic" assembly code which implements the same
33  * functionality in case the gcc extension cannot be used.  It should be
34  * better to avoid inlining below, since we directly refer to specific
35  * registers for arguments, which would not actually correspond to the
36  * intended address or value in the embedded mnemonic.
37  */
38 #include <isc/util.h>           /* for 'UNUSED' macro */
39
40 static isc_int32_t
41 isc_atomic_xadd(isc_int32_t *p, isc_int32_t val) {
42         UNUSED(p);
43         UNUSED(val);
44
45         __asm (
46                 "movq %rdi, %rdx\n"
47                 "movl %esi, %eax\n"
48 #ifdef ISC_PLATFORM_USETHREADS
49                 "lock;"
50 #endif
51                 "xadd %eax, (%rdx)\n"
52
53                 /*
54                  * set the return value directly in the register so that we
55                  * can avoid guessing the correct position in the stack for a
56                  * local variable.
57                  */
58                 );
59 }
60
61 static void
62 isc_atomic_store(isc_int32_t *p, isc_int32_t val) {
63         UNUSED(p);
64         UNUSED(val);
65
66         __asm (
67                 "movq %rdi, %rax\n"
68                 "movl %esi, %edx\n"
69 #ifdef ISC_PLATFORM_USETHREADS
70                 "lock;"
71 #endif
72                 "xchgl (%rax), %edx\n"
73                 );
74 }
75
76 static isc_int32_t
77 isc_atomic_cmpxchg(isc_int32_t *p, isc_int32_t cmpval, isc_int32_t val) {
78         UNUSED(p);
79         UNUSED(cmpval);
80         UNUSED(val);
81
82         __asm (
83                 "movl %edx, %ecx\n"
84                 "movl %esi, %eax\n"
85                 "movq %rdi, %rdx\n"
86
87 #ifdef ISC_PLATFORM_USETHREADS
88                 "lock;"
89 #endif
90                 /*
91                  * If (%rdi) == %eax then (%rdi) := %edx.
92                  % %eax is set to old (%ecx), which will be the return value.
93                  */
94                 "cmpxchgl %ecx, (%rdx)"
95                 );
96 }
97
98 #else /* !ISC_PLATFORM_USEGCCASM && !ISC_PLATFORM_USESTDASM */
99
100 #error "unsupported compiler.  disable atomic ops by --disable-atomic"
101
102 #endif
103 #endif /* ISC_ATOMIC_H */