UbixOS  2.0
spinlock.c
Go to the documentation of this file.
1 /*-
2  * Copyright (c) 2002-2018 The UbixOS Project.
3  * All rights reserved.
4  *
5  * This was developed by Christopher W. Olsen for the UbixOS Project.
6  *
7  * Redistribution and use in source and binary forms, with or without modification, are permitted
8  * provided that the following conditions are met:
9  *
10  * 1) Redistributions of source code must retain the above copyright notice, this list of
11  * conditions, the following disclaimer and the list of authors.
12  * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13  * conditions, the following disclaimer and the list of authors in the documentation and/or
14  * other materials provided with the distribution.
15  * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16  * endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/_null.h>
30 #include <ubixos/spinlock.h>
31 #include <ubixos/sched.h>
32 #include <string.h>
33 
34 #define atomic_xadd(P, V) __sync_fetch_and_add((P), (V))
35 #define cmpxchg(P, O, N) __sync_val_compare_and_swap((P), (O), (N))
36 #define atomic_inc(P) __sync_add_and_fetch((P), 1)
37 #define atomic_dec(P) __sync_add_and_fetch((P), -1)
38 #define atomic_add(P, V) __sync_add_and_fetch((P), (V))
39 #define atomic_set_bit(P, V) __sync_or_and_fetch((P), 1<<(V))
40 #define atomic_clear_bit(P, V) __sync_and_and_fetch((P), ~(1<<(V)))
41 
42 #define barrier() asm volatile("": : :"memory")
43 
44 /* Pause instruction to prevent excess processor bus usage */
45 #define cpu_relax() asm volatile("pause\n": : :"memory")
46 
48  memset(lock, 0x0, sizeof(struct spinLock));
49 }
50 
52  if (!cmpxchg(&lock->locked, NULL, LLOCK_FLAG))
53  return 0;
54 
55  /* Failure! */
56  return LOCKED;
57 }
58 
59 void spinUnlock(spinLock_t lock) {
60  barrier();
61  lock->locked = 0x0;
62 }
63 
64 void spinLock(spinLock_t lock) {
65  while (1) {
66  if (!xchg_32(&lock->locked, LOCKED))
67  return;
68  while (lock->locked == 1)
69  sched_yield();
70  }
71 }
72 
spinLockInit
void spinLockInit(spinLock_t *lock)
Definition: spinlock.c:32
spinlock.h
spinLock::locked
uint32_t locked
Definition: spinlock.h:43
string.h
spinUnlock
void spinUnlock(spinLock_t *lock)
Definition: spinlock.c:36
LLOCK_FLAG
#define LLOCK_FLAG
Definition: spinlock.h:37
sched.h
LOCKED
#define LOCKED
Definition: spinlock.h:34
cmpxchg
#define cmpxchg(P, O, N)
Definition: spinlock.c:35
spinLock
void spinLock(spinLock_t *lock)
Definition: spinlock.c:55
barrier
#define barrier()
Definition: spinlock.c:42
spinLock
Definition: spinlock.h:41
memset
void * memset(void *dst, int c, size_t length)
_null.h
spinTryLock
int spinTryLock(spinLock_t *lock)
Definition: spinlock.c:47
sched_yield
void sched_yield()
Definition: sched.c:244
NULL
#define NULL
Definition: fat_string.h:17