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