1 /* smpprim.h: SMP locking primitives on the Sparc
2 *
3 * God knows we won't be actually using this code for some time
4 * but I thought I'd write it since I knew how.
5 *
6 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
7 */
8
9 #ifndef __SPARC_SMPPRIM_H
10 #define __SPARC_SMPPRIM_H
11
12 /* Test and set the unsigned byte at ADDR to 1. Returns the previous
13 * value. On the Sparc we use the ldstub instruction since it is
14 * atomic.
15 */
16
17 extern inline volatile char test_and_set(void *addr)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
18 {
19 char state = 0;
20
21 __asm__ __volatile__("ldstub [%0], %1 ! test_and_set\n\t"
22 "=r" (addr), "=r" (state) :
23 "0" (addr), "1" (state) : "memory");
24
25 return state;
26 }
27
28 /* Initialize a spin-lock. */
29 extern inline volatile smp_initlock(void *spinlock)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
30 {
31 /* Unset the lock. */
32 *((unsigned char *) spinlock) = 0;
33
34 return;
35 }
36
37 /* This routine spins until it acquires the lock at ADDR. */
38 extern inline volatile smp_lock(void *addr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
39 {
40 while(test_and_set(addr) == 0xff)
41 ;
42
43 /* We now have the lock */
44 return;
45 }
46
47 /* This routine releases the lock at ADDR. */
48 extern inline volatile smp_unlock(void *addr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
49 {
50 *((unsigned char *) addr) = 0;
51 }
52
53 #endif /* !(__SPARC_SMPPRIM_H) */