OSDN Git Service

80c1b05a8770573b4e53d085e7eb05a62c9ef897
[uclinux-h8/uClibc.git] / libpthread / linuxthreads / sysdeps / x86_64 / pspinlock.c
1 /* POSIX spinlock implementation.  x86-64 version.
2    Copyright (C) 2001 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
7    License as published by the Free Software Foundation; either
8    version 2.1 of the 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; if not, see
17    <http://www.gnu.org/licenses/>.  */
18
19 #include <errno.h>
20 #include <pthread.h>
21 #include "internals.h"
22
23 /* This implementation is similar to the one used in the Linux kernel.
24    But the kernel is byte instructions for the memory access.  This is
25    faster but unusable here.  The problem is that only 128
26    threads/processes could use the spinlock at the same time.  If (by
27    a design error in the program) a thread/process would hold the
28    spinlock for a time long enough to accumulate 128 waiting
29    processes, the next one will find a positive value in the spinlock
30    and assume it is unlocked.  We cannot accept that.  */
31
32 int
33 __pthread_spin_lock (pthread_spinlock_t *lock)
34 {
35   __asm__ __volatile__
36     ("\n"
37      "1:\n\t"
38      "lock; decl %0\n\t"
39      "js 2f\n\t"
40      ".section .text.spinlock,\"ax\"\n"
41      "2:\n\t"
42      "cmpl $0,%0\n\t"
43      "rep; nop\n\t"
44      "jle 2b\n\t"
45      "jmp 1b\n\t"
46      ".previous"
47      : "=m" (*lock));
48   return 0;
49 }
50 weak_alias (__pthread_spin_lock, pthread_spin_lock)
51
52
53 int
54 __pthread_spin_trylock (pthread_spinlock_t *lock)
55 {
56   int oldval;
57
58   __asm__ __volatile__
59     ("xchgl %0,%1"
60      : "=r" (oldval), "=m" (*lock)
61      : "0" (0));
62   return oldval > 0 ? 0 : EBUSY;
63 }
64 weak_alias (__pthread_spin_trylock, pthread_spin_trylock)
65
66
67 int
68 __pthread_spin_unlock (pthread_spinlock_t *lock)
69 {
70   __asm__ __volatile__
71     ("movl $1,%0"
72      : "=m" (*lock));
73   return 0;
74 }
75 weak_alias (__pthread_spin_unlock, pthread_spin_unlock)
76
77
78 int
79 __pthread_spin_init (pthread_spinlock_t *lock, int pshared)
80 {
81   /* We can ignore the `pshared' parameter.  Since we are busy-waiting
82      all processes which can access the memory location `lock' points
83      to can use the spinlock.  */
84   *lock = 1;
85   return 0;
86 }
87 weak_alias (__pthread_spin_init, pthread_spin_init)
88
89
90 int
91 __pthread_spin_destroy (pthread_spinlock_t *lock)
92 {
93   /* Nothing to do.  */
94   return 0;
95 }
96 weak_alias (__pthread_spin_destroy, pthread_spin_destroy)