blob: d54a2a98b560566bf1da62d29eec46e81e976850 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/* POSIX spinlock implementation. Alpha version.
2 Copyright (C) 2000 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
24
25/* This implementation is similar to the one used in the Linux kernel.
26 But the kernel is byte instructions for the memory access. This is
27 faster but unusable here. The problem is that only 128
28 threads/processes could use the spinlock at the same time. If (by
29 a design error in the program) a thread/process would hold the
30 spinlock for a time long enough to accumulate 128 waiting
31 processes, the next one will find a positive value in the spinlock
32 and assume it is unlocked. We cannot accept that. */
33
34int
35__pthread_spin_lock (pthread_spinlock_t *lock)
36{
37 unsigned int tmp;
38 __asm__ __volatile__
39 ("1: ldl_l %0,%1\n"
40 " blbs %0,2f\n"
41 " or %0,1,%0\n"
42 " stl_c %0,%1\n"
43 " beq %0,2f\n"
44 " mb\n"
45 ".subsection 2\n"
46 "2: ldl %0,%1\n"
47 " blbs %0,2b\n"
48 " br 1b\n"
49 ".previous"
50 : "=r" (tmp), "=m" (lock)
51 : "m" (lock));
52 return 0;
53}
54weak_alias (__pthread_spin_lock, pthread_spin_lock)
55
56
57int
58__pthread_spin_trylock (pthread_spinlock_t *lock)
59{
60 unsigned long int oldval;
61 unsigned long int temp;
62
63 __asm__ __volatile__
64 ("1: ldl_l %0,%1\n"
65 " and %0,%3,%2\n"
66 " bne %2,2f\n"
67 " xor %0,%3,%0\n"
68 " stl_c %0,%1\n"
69 " beq %0,3f\n"
70 " mb\n"
71 "2:\n"
72 ".subsection 2\n"
73 "3: br 1b\n"
74 ".previous"
75 : "=&r" (temp), "=m" (*lock), "=&r" (oldval)
76 : "Ir" (1UL), "m" (*lock));
77
78 return oldval == 0 ? 0 : EBUSY;
79}
80weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
81
82
83int
84__pthread_spin_unlock (pthread_spinlock_t *lock)
85{
86 __asm__ __volatile__ ("mb");
87 return *lock = 0;
88}
89weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
90
91
92int
93__pthread_spin_init (pthread_spinlock_t *lock, int pshared)
94{
95 /* We can ignore the `pshared' parameter. Since we are busy-waiting
96 all processes which can access the memory location `lock' points
97 to can use the spinlock. */
98 *lock = 0;
99 return 0;
100}
101weak_alias (__pthread_spin_init, pthread_spin_init)
102
103
104int
105__pthread_spin_destroy (pthread_spinlock_t *lock)
106{
107 /* Nothing to do. */
108 return 0;
109}
110weak_alias (__pthread_spin_destroy, pthread_spin_destroy)