blob: 64b59252a48fe60b9aeea72482cd5d6742befa6d [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* POSIX spinlock implementation. x86 version.
2 Copyright (C) 2000, 2002, 2006 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public License as
7 published by the Free Software Foundation; either version 2.1 of the
8 License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
19
20#include <errno.h>
21#include <pthread.h>
22#include "internals.h"
23#include <bits/kernel-features.h>
24
25
26/* This implementation is similar to the one used in the Linux kernel.
27 But the kernel is byte instructions for the memory access. This is
28 faster but unusable here. The problem is that only 128
29 threads/processes could use the spinlock at the same time. If (by
30 a design error in the program) a thread/process would hold the
31 spinlock for a time long enough to accumulate 128 waiting
32 processes, the next one will find a positive value in the spinlock
33 and assume it is unlocked. We cannot accept that. */
34
35int
36__pthread_spin_lock (pthread_spinlock_t *lock)
37{
38 __asm__ __volatile__
39 ("\n"
40 "1:\n\t"
41 "lock; decl %0\n\t"
42 "js 2f\n\t"
43 ".section .text.spinlock,\"ax\"\n"
44 "2:\n\t"
45 "cmpl $0,%0\n\t"
46 "rep; nop\n\t"
47 "jle 2b\n\t"
48 "jmp 1b\n\t"
49 ".previous"
50 : "=m" (*lock));
51 return 0;
52}
53weak_alias (__pthread_spin_lock, pthread_spin_lock)
54
55
56int
57__pthread_spin_trylock (pthread_spinlock_t *lock)
58{
59 int oldval;
60
61 __asm__ __volatile__
62 ("xchgl %0,%1"
63 : "=r" (oldval), "=m" (*lock)
64 : "0" (0));
65 return oldval > 0 ? 0 : EBUSY;
66}
67weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
68
69
70int
71__pthread_spin_unlock (pthread_spinlock_t *lock)
72{
73 __asm__ __volatile__
74 ("movl $1,%0"
75 : "=m" (*lock));
76 return 0;
77}
78weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
79
80
81int
82__pthread_spin_init (pthread_spinlock_t *lock, int pshared)
83{
84 /* We can ignore the `pshared' parameter. Since we are busy-waiting
85 all processes which can access the memory location `lock' points
86 to can use the spinlock. */
87 *lock = 1;
88 return 0;
89}
90weak_alias (__pthread_spin_init, pthread_spin_init)
91
92
93int
94__pthread_spin_destroy (pthread_spinlock_t *lock)
95{
96 /* Nothing to do. */
97 return 0;
98}
99weak_alias (__pthread_spin_destroy, pthread_spin_destroy)
100
101#ifndef __ASSUME_SET_THREAD_AREA_SYSCALL
102int __have_no_set_thread_area;
103#endif