]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/uclibc/lib/contrib/uclibc/libpthread/linuxthreads/sysdeps/s390/pspinlock.c
Inital import
[l4.git] / l4 / pkg / uclibc / lib / contrib / uclibc / libpthread / linuxthreads / sysdeps / s390 / pspinlock.c
1 /* POSIX spinlock implementation.  S/390 version.
2    Copyright (C) 2000, 2004 Free Software Foundation, Inc.
3    Contributed by Martin Schwidefsky (schwidefsky@de.ibm.com).
4    This file is part of the GNU C Library.
5
6    The GNU C Library is free software; you can redistribute it and/or
7    modify it under the terms of the GNU Lesser General Public License as
8    published by the Free Software Foundation; either version 2.1 of the
9    License, or (at your option) any later version.
10
11    The GNU C Library is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    Lesser General Public License for more details.
15
16    You should have received a copy of the GNU Lesser General Public
17    License along with the GNU C Library; see the file COPYING.LIB.  If not,
18    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #include <errno.h>
22 #include <pthread.h>
23 #include "internals.h"
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
34 int
35 __pthread_spin_lock (pthread_spinlock_t *lock)
36 {
37   __asm__ __volatile__("    basr  1,0\n"
38                "0:  slr   0,0\n"
39                "    cs    0,1,%1\n"
40                "    jl    0b\n"
41                : "=m" (*lock)
42                : "m" (*lock) : "0", "1", "cc" );
43   return 0;
44 }
45 weak_alias (__pthread_spin_lock, pthread_spin_lock)
46
47 int
48 __pthread_spin_trylock (pthread_spinlock_t *lock)
49 {
50   int oldval;
51
52   __asm__ __volatile__("    slr   %1,%1\n"
53                "    basr  1,0\n"
54                "0:  cs    %1,1,%0"
55                : "=m" (*lock), "=&d" (oldval)
56                : "m" (*lock) : "1", "cc" );
57   return oldval == 0 ? 0 : EBUSY;
58 }
59 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
60
61
62 int
63 __pthread_spin_unlock (pthread_spinlock_t *lock)
64 {
65   __asm__ __volatile__("    xc 0(4,%0),0(%0)\n"
66                "    bcr 15,0"
67                : : "a" (lock) : "memory" );
68   return 0;
69 }
70 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
71
72
73 int
74 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
75 {
76   /* We can ignore the `pshared' parameter.  Since we are busy-waiting
77      all processes which can access the memory location `lock' points
78      to can use the spinlock.  */
79   *lock = 0;
80   return 0;
81 }
82 weak_alias (__pthread_spin_init, pthread_spin_init)
83
84
85 int
86 __pthread_spin_destroy (pthread_spinlock_t *lock)
87 {
88   /* Nothing to do.  */
89   return 0;
90 }
91 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)