source: mainline/kernel/arch/ia32/include/atomic.h@ 0c3e63f

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0c3e63f was d99c1d2, checked in by Martin Decky <martin@…>, 15 years ago

use [u]int{8|16|32|64}_t type definitions as detected by the autotool
replace direct usage of arch/types.h with typedefs.h

  • Property mode set to 100644
File size: 3.4 KB
Line 
1/*
2 * Copyright (c) 2001-2004 Jakub Jermar
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup ia32
30 * @{
31 */
32/** @file
33 */
34
35#ifndef KERN_ia32_ATOMIC_H_
36#define KERN_ia32_ATOMIC_H_
37
38#include <typedefs.h>
39#include <arch/barrier.h>
40#include <preemption.h>
41
42static inline void atomic_inc(atomic_t *val)
43{
44#ifdef CONFIG_SMP
45 asm volatile (
46 "lock incl %[count]\n"
47 : [count] "+m" (val->count)
48 );
49#else
50 asm volatile (
51 "incl %[count]\n"
52 : [count] "+m" (val->count)
53 );
54#endif /* CONFIG_SMP */
55}
56
57static inline void atomic_dec(atomic_t *val)
58{
59#ifdef CONFIG_SMP
60 asm volatile (
61 "lock decl %[count]\n"
62 : [count] "+m" (val->count)
63 );
64#else
65 asm volatile (
66 "decl %[count]\n"
67 : [count] "+m" (val->count)
68 );
69#endif /* CONFIG_SMP */
70}
71
72static inline atomic_count_t atomic_postinc(atomic_t *val)
73{
74 atomic_count_t r = 1;
75
76 asm volatile (
77 "lock xaddl %[r], %[count]\n"
78 : [count] "+m" (val->count),
79 [r] "+r" (r)
80 );
81
82 return r;
83}
84
85static inline atomic_count_t atomic_postdec(atomic_t *val)
86{
87 atomic_count_t r = -1;
88
89 asm volatile (
90 "lock xaddl %[r], %[count]\n"
91 : [count] "+m" (val->count),
92 [r] "+r" (r)
93 );
94
95 return r;
96}
97
98#define atomic_preinc(val) (atomic_postinc(val) + 1)
99#define atomic_predec(val) (atomic_postdec(val) - 1)
100
101static inline atomic_count_t test_and_set(atomic_t *val)
102{
103 atomic_count_t v = 1;
104
105 asm volatile (
106 "xchgl %[v], %[count]\n"
107 : [v] "+r" (v),
108 [count] "+m" (val->count)
109 );
110
111 return v;
112}
113
114/** ia32 specific fast spinlock */
115static inline void atomic_lock_arch(atomic_t *val)
116{
117 atomic_count_t tmp;
118
119 preemption_disable();
120 asm volatile (
121 "0:\n"
122 "pause\n" /* Pentium 4's HT love this instruction */
123 "mov %[count], %[tmp]\n"
124 "testl %[tmp], %[tmp]\n"
125 "jnz 0b\n" /* lightweight looping on locked spinlock */
126
127 "incl %[tmp]\n" /* now use the atomic operation */
128 "xchgl %[count], %[tmp]\n"
129 "testl %[tmp], %[tmp]\n"
130 "jnz 0b\n"
131 : [count] "+m" (val->count),
132 [tmp] "=&r" (tmp)
133 );
134
135 /*
136 * Prevent critical section code from bleeding out this way up.
137 */
138 CS_ENTER_BARRIER();
139}
140
141#endif
142
143/** @}
144 */
Note: See TracBrowser for help on using the repository browser.