Subversion Repositories Kolibri OS

Rev

Rev 6089 | Rev 8874 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 6089 Rev 6926
Line 5... Line 5...
5
;;                                                              ;;
5
;;                                                              ;;
6
;; Synhronization for MenuetOS.                                 ;;
6
;; Synhronization for MenuetOS.                                 ;;
7
;; Author: Halyavin Andrey, halyavin@land.ru                    ;;
7
;; Author: Halyavin Andrey, halyavin@land.ru                    ;;
8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Line 9... Line 9...
9
 
9
 
Line 10... Line -...
10
$Revision: 6089 $
-
 
11
 
-
 
12
align 4
-
 
13
;struct futex*  __fastcall create_futex(int *ptr)
-
 
14
create_futex:
-
 
15
        push    ecx
-
 
16
        mov     ecx, sizeof.FUTEX
-
 
17
        call    create_object
-
 
18
        pop     ecx
-
 
Line 19... Line -...
19
        test    eax, eax
-
 
20
        jz      .fail
-
 
21
 
-
 
22
        mov     [eax+FUTEX.magic], 'FUTX'
-
 
23
        mov     [eax+FUTEX.destroy], 0
-
 
24
        mov     [eax+FUTEX.pointer], ecx
-
 
25
        lea     ecx, [eax+FUTEX.wait_list]
-
 
26
        list_init ecx
-
 
27
        mov     [eax+FUTEX.flags], 0
-
 
28
.fail:
-
 
29
        ret
-
 
30
 
-
 
31
align 4
-
 
32
;int __fastcall destroy_futex(struct futex *futex)
-
 
33
destroy_futex:
-
 
34
        push    esi
-
 
35
        mov     esi, [current_process]
-
 
36
        mov     edx, [ecx+FUTEX.handle]
-
 
37
 
-
 
38
        pushfd
-
 
39
        cli
-
 
40
 
-
 
41
        lea     eax, [ecx+FUTEX.wait_list]
-
 
42
        cmp     eax, [eax+LHEAD.next]
-
 
43
        jne     .fail
-
 
44
 
-
 
45
        mov     eax, [esi+PROC.ht_next]
-
 
46
        mov     [esi+PROC.htab+edx*4], eax
-
 
47
        mov     [esi+PROC.ht_next], edx
-
 
48
        inc     [esi+PROC.ht_free]
-
 
49
 
-
 
50
        popfd
-
 
51
        pop     esi
-
 
52
 
-
 
53
        mov     eax, ecx
-
 
54
        call    free
-
 
55
        xor     eax, eax
-
 
56
        ret
-
 
57
 
-
 
58
.fail:
-
 
59
        popfd
-
 
60
        pop     esi
-
 
61
        mov     eax, -1
-
 
62
        ret
-
 
63
 
-
 
64
 
-
 
65
iglobal
-
 
66
align 4
-
 
67
f77call:
-
 
68
        dd f77.futex_init     ;0
-
 
69
        dd f77.futex_destroy  ;1
-
 
70
        dd f77.futex_wait     ;2
-
 
71
        dd f77.futex_wake     ;3
-
 
72
.end:
-
 
73
endg
-
 
74
 
-
 
75
align 4
-
 
76
sys_synchronization:
-
 
77
f77:
-
 
78
        test    ebx, ebx
-
 
79
        jz      .futex_init
-
 
80
 
-
 
81
        cmp     ebx, (f77call.end-f77call)/4
-
 
82
        jae     .fail
-
 
83
 
-
 
84
        cmp     ecx, STDERR_FILENO
-
 
85
        jbe     .fail
-
 
86
        cmp     ecx, (PROC.pdt_0 - PROC.htab)/4
-
 
87
        jae     .fail
-
 
88
 
-
 
89
        mov     edi, [current_process]
-
 
90
        mov     ebp, [edi+PROC.htab+ecx*4]
-
 
91
 
-
 
92
        cmp     [ebp+FUTEX.magic], 'FUTX'
-
 
93
        jne     .fail
-
 
94
        cmp     [ebp+FUTEX.handle], ecx
-
 
95
        jne     .fail
-
 
96
 
-
 
97
        jmp     dword [f77call+ebx*4]
-
 
98
 
-
 
99
.fail:
-
 
100
        mov     [esp+SYSCALL_STACK._eax], -1
-
 
101
        ret
-
 
102
 
-
 
103
align 4
-
 
104
.futex_init:
-
 
105
        call    create_futex
-
 
106
        test    eax, eax
-
 
107
        jz      @F
-
 
108
        mov     eax, [eax+FUTEX.handle]
-
 
109
@@:
-
 
110
        mov     [esp+SYSCALL_STACK._eax], eax
-
 
111
        ret
-
 
112
 
-
 
113
 
-
 
114
align 4
-
 
115
;ecx futex handle
-
 
116
;edi current process
-
 
117
;ebp futex object
-
 
118
.futex_destroy:
-
 
119
        mov     ecx, ebp
-
 
120
        call    destroy_futex
-
 
121
        mov     [esp+SYSCALL_STACK._eax], eax
-
 
122
        ret
-
 
123
 
-
 
124
align 4
-
 
125
;ecx futex handle
-
 
126
;edx control value
-
 
127
;esi timeout
-
 
128
;edi current process
-
 
129
;ebp futex object
-
 
130
.futex_wait:
-
 
131
        test    esi, esi
-
 
132
        jnz     .futex_wait_timeout
-
 
133
        mov     ecx, [ebp+FUTEX.pointer]
-
 
134
        mov     eax, edx
-
 
135
        lock cmpxchg [ecx], edx
-
 
136
        je      .wait_slow
-
 
137
 
-
 
138
        mov     [esp+SYSCALL_STACK._eax], -2
-
 
139
        ret
-
 
140
 
-
 
141
.wait_slow:
-
 
142
        pushfd
-
 
143
        cli
-
 
144
 
-
 
145
        sub     esp, sizeof.MUTEX_WAITER
-
 
146
        mov     ebx, [TASK_BASE]
-
 
147
        mov     [esp+MUTEX_WAITER.task], ebx
-
 
148
        lea     esi, [ebp+FUTEX.wait_list]
-
 
149
 
-
 
150
        list_add_tail esp, esi      ;esp= new waiter, esi= list head
-
 
151
        mov     eax, edx
-
 
152
.again:
-
 
153
        mov     [ebx+TASKDATA.state], 1
-
 
154
        call    change_task
-
 
155
 
-
 
156
        lock cmpxchg [ecx], edx
-
 
157
        je      .again
-
 
158
 
-
 
159
        list_del esp
-
 
160
        add     esp, sizeof.MUTEX_WAITER
-
 
161
 
-
 
162
        popfd
-
 
163
        mov     [esp+SYSCALL_STACK._eax], 0
-
 
164
        ret
-
 
165
 
-
 
166
align 4
-
 
167
;ecx futex handle
-
 
168
;edx control value
-
 
169
;esi timeout
-
 
170
;edi current process
-
 
171
;ebp futex object
-
 
172
 
-
 
173
.futex_wait_timeout:
-
 
174
        mov     ecx, [ebp+FUTEX.pointer]
-
 
175
        mov     eax, edx
-
 
176
        lock cmpxchg [ecx], edx         ;wait until old_value == new_value
-
 
177
        je      .wait_slow_timeout
-
 
178
 
-
 
179
        mov     [esp+SYSCALL_STACK._eax], -2
-
 
180
        ret
-
 
181
 
-
 
182
align 4
-
 
183
.wait_test:
-
 
184
        xor     eax, eax
-
 
185
        ret
-
 
186
 
-
 
187
.wait_slow_timeout:
-
 
188
        pushfd
-
 
189
        cli
-
 
190
 
-
 
191
        sub     esp, sizeof.MUTEX_WAITER
-
 
192
 
-
 
193
        mov     ebx, [current_slot]
-
 
194
        mov     [ebx+APPDATA.wait_test], f77.wait_test
-
 
195
        mov     [ebx+APPDATA.wait_timeout], esi
-
 
196
        mov     [ebx+APPDATA.wait_param], ebp
-
 
197
        mov     eax, [timer_ticks]
-
 
198
        mov     [ebx+APPDATA.wait_begin], eax
-
 
199
        mov     eax, [TASK_BASE]
-
 
200
        mov     [eax+TASKDATA.state], 5
-
 
201
 
-
 
202
        mov     [esp+MUTEX_WAITER.task], eax
-
 
203
        lea     esi, [ebp+FUTEX.wait_list]
-
 
204
 
-
 
205
        list_add_tail esp, esi      ;esp= new waiter, esi= list head
-
 
206
 
-
 
207
.again_timeout:
-
 
208
        call    change_task
-
 
209
        mov     eax, [ebx+APPDATA.wait_param]
-
 
210
        test    eax, eax
-
 
211
        jz      .timeout
-
 
212
 
-
 
213
        mov     eax, edx
-
 
214
        lock cmpxchg [ecx], edx
-
 
215
        jz      .again_timeout
-
 
216
@@:
-
 
217
        list_del esp
-
 
218
        add     esp, sizeof.MUTEX_WAITER
-
 
219
 
-
 
220
        popfd
-
 
221
        mov     [esp+SYSCALL_STACK._eax], 0
-
 
222
        ret
-
 
223
 
-
 
224
.timeout:
-
 
225
        list_del esp
-
 
226
        add     esp, sizeof.MUTEX_WAITER
-
 
227
 
-
 
228
        popfd
-
 
229
        mov     [esp+SYSCALL_STACK._eax], -1
-
 
230
        ret
-
 
231
 
-
 
232
 
-
 
233
align 4
-
 
234
;ecx futex handle
-
 
235
;edx number of threads
-
 
236
;edi current process
-
 
237
;ebp futex object
-
 
238
.futex_wake:
-
 
239
 
-
 
240
        xor     ecx, ecx
-
 
241
 
-
 
242
        pushfd
-
 
243
        cli
-
 
244
 
-
 
245
        lea     ebx, [ebp+FUTEX.wait_list]
-
 
246
        mov     esi, [ebx+LHEAD.next]
-
 
247
.wake:
-
 
248
        cmp     esi, ebx
-
 
249
        je      .done
-
 
250
 
-
 
251
        mov     eax, [esi+MUTEX_WAITER.task]
-
 
252
        mov     [eax+TASKDATA.state], 0
-
 
253
 
-
 
254
        mov     esi, [esi+MUTEX_WAITER.list.next]
-
 
255
        inc     ecx
-
 
256
        cmp     ecx, edx
-
 
257
        jb      .wake
-
 
258
.done:
-
 
Line 259... Line 10...
259
        popfd
10
$Revision: 6926 $
260
        mov     [esp+SYSCALL_STACK._eax], ecx
11
 
Line 261... Line 12...
261
        ret
12