summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/libogcfpc/src/ogc/machine/spinlock.inc
blob: a28cf962bd8f97bf07f8d0d1089764ea8c4f6e93 (plain)
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217

{$IFDEF OGC_INTERFACE}
type
  spinlock_t = record
    lock: cuint32;
  end;
  pspinlock_t = ^spinlock_t;


  rwlock_t = record
    lock: cuint32;
  end;
  prwlock_t = ^rwlock_t;


const
  SPIN_LOCK_UNLOCKED: spinlock_t =  (lock:0) ;
  RW_LOCK_UNLOCKED: rwlock_t = (lock:0);

procedure spin_lock_init(x: pspinlock_t); inline;
function _test_and_set(atomic: pcuint32): cuint32; inline;
function atomic_inc(_pint: pcuint32): cuint32; inline;
function atomic_dec(_pint: pcuint32): cuint32; inline;
procedure spin_lock(lock: pspinlock_t); inline;
procedure spin_lock_irqsave(lock: pspinlock_t; {register} p_isr_level: pcuint32); inline;
procedure spin_unlock(lock: pspinlock_t); inline;
procedure spin_unlock_irqrestore(lock: pspinlock_t; {register} isr_level: cuint32); inline;
procedure read_lock_init(lp: prwlock_t); inline;
procedure read_lock(rw: prwlock_t); inline;
procedure read_unlock(rw: prwlock_t); inline;
procedure write_lock(rw: prwlock_t); inline;
procedure write_unlock(rw: prwlock_t); inline;
{$ENDIF}


{$IFDEF OGC_IMPLEMENTATION}
procedure spin_lock_init(x: pspinlock_t); inline;
begin
 x^ := SPIN_LOCK_UNLOCKED;
end;

function _test_and_set(atomic: pcuint32): cuint32; inline;
var
  ret: cuint32;
begin
  asm
    .L1:
    lwarx r3,0,r4
    cmpwi   0,r3,0
    bne-    .L2
    stwcx.  r5,0,r4
    bne-    .L1
    isync
    .L2:
  end;
  result := ret;
end;


function atomic_inc(_pint: pcuint32): cuint32; inline;
var
	ret: cuint32;
begin
  asm
		.L1:
      lwarx	r3,0,r4
			addi	r3,r3,1
			stwcx.	r3,0,r4
			bne-	.L1
			isync
  end;
  result := ret;
end;

function atomic_dec(_pint: pcuint32): cuint32; inline;
var
  ret: cuint32;
begin
	asm
		.L1:
      lwarx	r3,0,r4
			addi	r3,r3,-1
			stwcx.	r3,0,r4
			bne-	.L1
			isync
  end;
	result := ret;
end;

procedure spin_lock(lock: pspinlock_t); inline;
var
  tmp: cuint32;
begin
  asm
	  b       .L1
  .L2:
      lwzx    r3,0,r4
      cmpwi   0,r3,0
      bne+    .L2
  .L1:
      lwarx   r3,0,r4
      cmpwi   0,r3,0
      bne-    .L2
      stwcx.  r5,0,r4
      bne-    .L2
      isync
  end;
end;

procedure spin_lock_irqsave(lock: pspinlock_t; {register} p_isr_level: pcuint32); inline;
var
  level: cuint32;
  tmp: cuint32;
begin
	_CPU_ISR_Disable(level);
  asm
    b       .L1
	.L2:
    lwzx    r3,0,r4
		cmpwi   0,r3,0
		bne+    .L2
	.L1:
    lwarx   r3,0,r4
		cmpwi   0,r3,0
		bne-    .L2
		stwcx.  r5,0,r4
		bne-    .L2
		isync
  end;
	p_isr_level^ := level;
end;

procedure spin_unlock(lock: pspinlock_t); inline;
begin
  asm
    eieio
  end;
  lock^.lock := 0;
end;

procedure spin_unlock_irqrestore(lock: pspinlock_t; {register} isr_level: cuint32); inline;
begin
  asm
    eieio
	end;
  lock^.lock := 0;
  _CPU_ISR_Restore(isr_level);
end;

procedure read_lock_init(lp: prwlock_t); inline;
begin
  lp^ := RW_LOCK_UNLOCKED
end;

procedure read_lock(rw: prwlock_t); inline;
var
  tmp: cuint32;
begin
  asm
    b   .L2
  .L1:
    lwzx            r3,0,r4
    cmpwi           0,r3,0
    blt+            .L1
  .L2:
    lwarx           r3,0,r4
    addic.          r3,r3,1
    ble-            .L1
    stwcx.          r3,0,r4
    bne-            .L2
    isync
  end;
end;

procedure read_unlock(rw: prwlock_t); inline;
var
  tmp: cuint32;
begin
  asm
    eieio
  .L1:
    lwarx           r3,0,r4
    addic           r3,r3,-1
    stwcx.          r3,0,r4
    bne-            .L1
  end;
end;

procedure write_lock(rw: prwlock_t); inline;
var
  tmp: cuint32;
begin
  asm
    b              .L2
  .L1:
    lwzx            r3,0,r4
    cmpwi           0,r3,0
    bne+            .L1
  .L2:
    lwarx           r3,0,r4
    cmpwi           0,r3,0
    bne-            .L1
    stwcx.          r5,0,r4
    bne-            .L2
    isync
  end;
end;

procedure write_unlock(rw: prwlock_t); inline;
begin
  asm
    eieio
  end;
  rw^.lock := 0;
end;
{$ENDIF}