blob: a6256c58f130cd0d972d2152acc891b5bf21c0a2 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* Machine-dependent pthreads configuration and inline functions.
2 i386 version.
3 Copyright (C) 1996-2001, 2002, 2003 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
5 Contributed by Richard Henderson <rth@tamu.edu>.
6
7 The GNU C Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
11
12 The GNU C Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with the GNU C Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22#ifndef _PT_MACHINE_H
23#define _PT_MACHINE_H 1
24
25#include <features.h>
26
27#ifndef __ASSEMBLER__
28#ifndef PT_EI
29# define PT_EI __extern_always_inline __attribute__((visibility("hidden")))
30#endif
31
32extern long int testandset (int *spinlock);
33extern int __compare_and_swap (long int *p, long int oldval, long int newval);
34
35/* Get some notion of the current stack. Need not be exactly the top
36 of the stack, just something somewhere in the current frame. */
37#define CURRENT_STACK_FRAME __builtin_frame_address (0)
38
39
40/* See if we can optimize for newer cpus... */
41#if defined __GNUC__ && __GNUC__ >= 2 && \
42 (defined __i486__ || defined __pentium__ || defined __pentiumpro__ || defined __pentium4__ || \
43 defined __athlon__ || defined __k8__)
44
45/* Spinlock implementation; required. */
46PT_EI long int
47testandset (int *spinlock)
48{
49 long int ret;
50
51 __asm__ __volatile__ (
52 "xchgl %0, %1"
53 : "=r" (ret), "=m" (*spinlock)
54 : "0" (1), "m" (*spinlock)
55 : "memory");
56
57 return ret;
58}
59
60/* Compare-and-swap for semaphores. It's always available on i686. */
61#define HAS_COMPARE_AND_SWAP
62
63PT_EI int
64__compare_and_swap (long int *p, long int oldval, long int newval)
65{
66 char ret;
67 long int readval;
68
69 __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
70 : "=q" (ret), "=m" (*p), "=a" (readval)
71 : "r" (newval), "m" (*p), "a" (oldval)
72 : "memory");
73 return ret;
74}
75
76#if defined(__ASSUME_LDT_WORKS) && __ASSUME_LDT_WORKS > 0
77#include "useldt.h"
78#endif
79
80/* The P4 and above really want some help to prevent overheating. */
81#define BUSY_WAIT_NOP __asm__ ("rep; nop")
82
83
84#else /* Generic i386 implementation */
85
86extern int compare_and_swap_is_available (void);
87
88/* Spinlock implementation; required. */
89PT_EI long int
90testandset (int *spinlock)
91{
92 long int ret;
93
94 __asm__ __volatile__(
95 "xchgl %0, %1"
96 : "=r"(ret), "=m"(*spinlock)
97 : "0"(1), "m"(*spinlock)
98 : "memory");
99
100 return ret;
101}
102
103
104/* Compare-and-swap for semaphores.
105 Available on the 486 and above, but not on the 386.
106 We test dynamically whether it's available or not. */
107
108#define HAS_COMPARE_AND_SWAP
109#define TEST_FOR_COMPARE_AND_SWAP
110
111PT_EI int
112__compare_and_swap (long int *p, long int oldval, long int newval)
113{
114 char ret;
115 long int readval;
116
117 __asm__ __volatile__ ("lock; cmpxchgl %3, %1; sete %0"
118 : "=q" (ret), "=m" (*p), "=a" (readval)
119 : "r" (newval), "m" (*p), "a" (oldval)
120 : "memory");
121 return ret;
122}
123
124PT_EI int
125compare_and_swap_is_available (void)
126{
127 int changed;
128 int oldflags;
129 /* get EFLAGS */
130 __asm__ __volatile__ ("pushfl; popl %0" : "=r" (oldflags) : );
131 /* Flip AC bit in EFLAGS. */
132 __asm__ __volatile__ ("pushl %0; popfl" : : "r" (oldflags ^ 0x40000) : "cc");
133 /* reread EFLAGS */
134 __asm__ __volatile__ ("pushfl; popl %0" : "=r" (changed) : );
135 /* See if bit changed. */
136 changed = (changed ^ oldflags) & 0x40000;
137 /* Restore EFLAGS. */
138 __asm__ __volatile__ ("pushl %0; popfl" : : "r" (oldflags) : "cc");
139 /* If the AC flag did not change, it's a 386 and it lacks cmpxchg.
140 Otherwise, it's a 486 or above and it has cmpxchg. */
141 return changed != 0;
142}
143#endif /* Generic i386 implementation */
144
145#endif /* __ASSEMBLER__ */
146
147#endif /* pt-machine.h */