]> git.saurik.com Git - apple/libc.git/blob - pthreads.subproj/lock.s
Libc-166.tar.gz
[apple/libc.git] / pthreads.subproj / lock.s
1 /*
2 * Copyright 1996 1995 by Open Software Foundation, Inc. 1997 1996 1995 1994 1993 1992 1991
3 * All Rights Reserved
4 *
5 * Permission to use, copy, modify, and distribute this software and
6 * its documentation for any purpose and without fee is hereby granted,
7 * provided that the above copyright notice appears in all copies and
8 * that both the copyright notice and this permission notice appear in
9 * supporting documentation.
10 *
11 * OSF DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE
12 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
13 * FOR A PARTICULAR PURPOSE.
14 *
15 * IN NO EVENT SHALL OSF BE LIABLE FOR ANY SPECIAL, INDIRECT, OR
16 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
17 * LOSS OF USE, DATA OR PROFITS, WHETHER IN ACTION OF CONTRACT,
18 * NEGLIGENCE, OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
19 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 *
21 */
22 /*
23 * MkLinux
24 */
25
26 #if defined(__ppc__)
27
28 #import <architecture/ppc/asm_help.h>
29 #import <architecture/ppc/pseudo_inst.h>
30
31 /* void spin_lock(int *p);
32 *
33 * Lock the lock pointed to by `p'. Spin (possibly forever) until
34 * the lock is available. Test and test and set logic used.
35 */
36
37 .text
38
39 LEAF(__spin_lock_try)
40 1:
41 lwarx r5,0,r3 // Read the lock
42 addi r4,0,0x1 // Lock value
43 cmpwi r5,0x0 // Is it busy?
44 bne- 2f // Yes, return 0
45 stwcx. r4,0,r3 // Try to lock the lock
46 bne- 1b // Lost reservation, try again
47 addi r3,0,1 // Got the lock
48 isync // Sync instruction stream
49 blr // Return 1
50 2: addi r3,0,0 // Could not get the lock
51 blr // Return 0
52 END(__spin_lock_try)
53
54 .globl _spin_lock
55 LEAF(__spin_lock)
56 _spin_lock:
57 addi r4,0,0x1 // Lock value
58 1:
59 lwarx r5,0,r3 // Read the lock
60 cmpwi r5,0x0 // Is it busy?
61 bne- 1b // Yes, spin
62 stwcx. r4,0,r3 // Try to lock the lock
63 bne- 1b // Lost reservation, try again
64 isync // Sync instruction stream
65 blr // Got it, return
66 END(__spin_lock)
67
68 /* void spin_unlock(int *p);
69 *
70 * Unlock the lock pointed to by p.
71 */
72 .globl _spin_unlock
73 LEAF(__spin_unlock)
74 _spin_unlock:
75 sync
76 li32 r4,0
77 stw r4,0(r3)
78 blr
79 END(__spin_unlock)
80
81 #elif defined(__i386__)
82
83 #include <architecture/i386/asm_help.h>
84
85 /*
86 * void
87 * _spin_lock(p)
88 * int *p;
89 *
90 * Lock the lock pointed to by p. Spin (possibly forever) until the next
91 * lock is available.
92 */
93 TEXT
94
95 .globl _spin_lock_try
96 LEAF(__spin_lock_try, 0)
97 _spin_lock_try:
98 movl 4(%esp),%ecx
99 movl $1,%eax
100 xchgl (%ecx),%eax
101 xorl $1,%eax
102 END(__spin_lock_try)
103
104 .globl _spin_lock
105 LEAF(__spin_lock, 0)
106 _spin_lock:
107 movl 4(%esp), %ecx
108 1:
109 movl (%ecx), %eax
110 orl %eax, %eax
111 jnz 1b
112 movl $0xFFFFFFFF, %eax
113 xchgl %eax, (%ecx)
114 orl %eax, %eax
115 jnz 1b
116 END(__spin_lock)
117
118
119 /*
120 * void
121 * _spin_unlock(p)
122 * int *p;
123 *
124 * Unlock the lock pointed to by p.
125 */
126 .globl _spin_unlock
127 LEAF(__spin_unlock, 0)
128 _spin_unlock:
129 movl $0, %eax
130 movl 4(%esp), %ecx
131 xchgl %eax, (%ecx)
132 END(__spin_unlock)
133
134 #else
135 #error spin_locks not defined for this architecture
136 #endif