forked from sysml/mini-os
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock.c
111 lines (99 loc) · 2.12 KB
/
lock.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*
* locks for newlib
*
* Samuel Thibault <[email protected]>, July 20008
*/
#ifdef HAVE_LIBC
#include <sys/lock.h>
#include <sched.h>
#include <wait.h>
int ___lock_init(_LOCK_T *lock)
{
lock->busy = 0;
init_waitqueue_head(&lock->wait);
return 0;
}
int ___lock_acquire(_LOCK_T *lock)
{
unsigned long flags;
while(1) {
wait_event(lock->wait, !lock->busy);
local_irq_save(flags);
if (!lock->busy)
break;
local_irq_restore(flags);
}
lock->busy = 1;
local_irq_restore(flags);
return 0;
}
int ___lock_try_acquire(_LOCK_T *lock)
{
unsigned long flags;
int ret = -1;
local_irq_save(flags);
if (!lock->busy) {
lock->busy = 1;
ret = 0;
}
local_irq_restore(flags);
return ret;
}
int ___lock_release(_LOCK_T *lock)
{
unsigned long flags;
local_irq_save(flags);
lock->busy = 0;
wake_up(&lock->wait);
local_irq_restore(flags);
return 0;
}
int ___lock_init_recursive(_LOCK_RECURSIVE_T *lock)
{
lock->owner = NULL;
init_waitqueue_head(&lock->wait);
return 0;
}
int ___lock_acquire_recursive(_LOCK_RECURSIVE_T *lock)
{
unsigned long flags;
if (lock->owner != get_current()) {
while (1) {
wait_event(lock->wait, lock->owner == NULL);
local_irq_save(flags);
if (lock->owner == NULL)
break;
local_irq_restore(flags);
}
lock->owner = get_current();
local_irq_restore(flags);
}
lock->count++;
return 0;
}
int ___lock_try_acquire_recursive(_LOCK_RECURSIVE_T *lock)
{
unsigned long flags;
int ret = -1;
local_irq_save(flags);
if (!lock->owner) {
ret = 0;
lock->owner = get_current();
lock->count++;
}
local_irq_restore(flags);
return ret;
}
int ___lock_release_recursive(_LOCK_RECURSIVE_T *lock)
{
unsigned long flags;
BUG_ON(lock->owner != get_current());
if (--lock->count)
return 0;
local_irq_save(flags);
lock->owner = NULL;
wake_up(&lock->wait);
local_irq_restore(flags);
return 0;
}
#endif