2017-06-16 00:36:39 +02:00
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
;; ;;
|
2022-02-12 18:27:41 +01:00
|
|
|
;; Copyright (C) KolibriOS team 2004-2022. All rights reserved. ;;
|
2017-06-16 00:36:39 +02:00
|
|
|
;; Distributed under terms of the GNU General Public License. ;;
|
|
|
|
;; ;;
|
|
|
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
|
|
|
2020-11-03 17:03:39 +01:00
|
|
|
$Revision$
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
align 4
|
|
|
|
;struct futex* __fastcall create_futex(int *ptr)
|
|
|
|
create_futex:
|
|
|
|
push ecx
|
|
|
|
mov ecx, sizeof.FUTEX
|
|
|
|
call create_object
|
|
|
|
pop ecx
|
|
|
|
test eax, eax
|
|
|
|
jz .fail
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [eax + FUTEX.magic], 'FUTX'
|
|
|
|
mov [eax + FUTEX.destroy], 0
|
|
|
|
mov [eax + FUTEX.pointer], ecx
|
|
|
|
lea ecx, [eax + FUTEX.wait_list]
|
2017-06-16 00:36:39 +02:00
|
|
|
list_init ecx
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [eax + FUTEX.flags], 0
|
2017-06-16 00:36:39 +02:00
|
|
|
.fail:
|
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
;int __fastcall destroy_futex(struct futex *futex)
|
|
|
|
destroy_futex:
|
|
|
|
push esi
|
|
|
|
mov esi, [current_process]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov edx, [ecx + FUTEX.handle]
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea eax, [ecx + FUTEX.wait_list]
|
|
|
|
cmp eax, [eax + LHEAD.next]
|
2017-06-16 00:36:39 +02:00
|
|
|
jne .fail
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [esi + PROC.ht_next]
|
|
|
|
mov [esi + PROC.htab + edx*4], eax
|
|
|
|
mov [esi + PROC.ht_next], edx
|
|
|
|
inc [esi + PROC.ht_free]
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
popfd
|
|
|
|
pop esi
|
|
|
|
|
|
|
|
mov eax, ecx
|
|
|
|
call free
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.fail:
|
|
|
|
popfd
|
|
|
|
pop esi
|
|
|
|
mov eax, -1
|
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
sys_futex:
|
|
|
|
cmp ecx, STDERR_FILENO
|
|
|
|
jbe .fail
|
|
|
|
cmp ecx, (PROC.pdt_0 - PROC.htab)/4
|
|
|
|
jae .fail
|
|
|
|
|
|
|
|
mov edi, [current_process]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ebp, [edi + PROC.htab + ecx*4]
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp [ebp + FUTEX.magic], 'FUTX'
|
2017-06-16 00:36:39 +02:00
|
|
|
jne .fail
|
2022-02-12 18:27:41 +01:00
|
|
|
cmp [ebp + FUTEX.handle], ecx
|
2017-06-16 00:36:39 +02:00
|
|
|
jne .fail
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
jmp dword [sys_futex_call + ebx*4-4]
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
.fail:
|
|
|
|
.requeue:
|
|
|
|
.cmp_requeue:
|
|
|
|
.wait_bitset:
|
|
|
|
.wake_bitset:
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], -1
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
.init:
|
|
|
|
call create_futex
|
|
|
|
test eax, eax
|
|
|
|
jz @F
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [eax + FUTEX.handle]
|
2017-06-16 00:36:39 +02:00
|
|
|
@@:
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], eax
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
;ecx futex handle
|
|
|
|
;edi current process
|
|
|
|
;ebp futex object
|
|
|
|
.destroy:
|
|
|
|
mov ecx, ebp
|
|
|
|
call destroy_futex
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], eax
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
;ecx futex handle
|
|
|
|
;edx control value
|
|
|
|
;esi timeout
|
|
|
|
;edi current process
|
|
|
|
;ebp futex object
|
|
|
|
.wait:
|
|
|
|
test esi, esi
|
|
|
|
jnz .wait_timeout
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [ebp + FUTEX.pointer]
|
2017-06-16 00:36:39 +02:00
|
|
|
mov eax, edx
|
|
|
|
lock cmpxchg [ecx], edx
|
|
|
|
je .wait_slow
|
|
|
|
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], -2
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.wait_slow:
|
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
|
|
|
sub esp, sizeof.MUTEX_WAITER
|
2022-02-09 18:07:51 +01:00
|
|
|
mov ebx, [current_slot]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + MUTEX_WAITER.task], ebx
|
|
|
|
lea esi, [ebp + FUTEX.wait_list]
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
list_add_tail esp, esi ;esp= new waiter, esi= list head
|
|
|
|
mov eax, edx
|
|
|
|
.again:
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [ebx + APPDATA.state], TSTATE_RUN_SUSPENDED
|
2017-06-16 00:36:39 +02:00
|
|
|
call change_task
|
|
|
|
|
|
|
|
lock cmpxchg [ecx], edx
|
|
|
|
je .again
|
|
|
|
|
|
|
|
list_del esp
|
|
|
|
add esp, sizeof.MUTEX_WAITER
|
|
|
|
|
|
|
|
popfd
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], 0
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
;ecx futex handle
|
|
|
|
;edx control value
|
|
|
|
;esi timeout
|
|
|
|
;edi current process
|
|
|
|
;ebp futex object
|
|
|
|
|
|
|
|
.wait_timeout:
|
2022-02-12 18:27:41 +01:00
|
|
|
mov ecx, [ebp + FUTEX.pointer]
|
2017-06-16 00:36:39 +02:00
|
|
|
mov eax, edx
|
|
|
|
lock cmpxchg [ecx], edx ;wait until old_value == new_value
|
|
|
|
je .wait_slow_timeout
|
|
|
|
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], -2
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
align 4
|
|
|
|
.wait_test:
|
|
|
|
xor eax, eax
|
|
|
|
ret
|
|
|
|
|
|
|
|
.wait_slow_timeout:
|
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
|
|
|
sub esp, sizeof.MUTEX_WAITER
|
|
|
|
|
|
|
|
mov ebx, [current_slot]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ebx + APPDATA.wait_test], sys_futex.wait_test
|
|
|
|
mov [ebx + APPDATA.wait_timeout], esi
|
|
|
|
mov [ebx + APPDATA.wait_param], ebp
|
2017-06-16 00:36:39 +02:00
|
|
|
mov eax, [timer_ticks]
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [ebx + APPDATA.wait_begin], eax
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [ebx + APPDATA.state], TSTATE_WAITING
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov [esp + MUTEX_WAITER.task], ebx
|
|
|
|
lea esi, [ebp + FUTEX.wait_list]
|
2017-06-16 00:36:39 +02:00
|
|
|
|
|
|
|
list_add_tail esp, esi ;esp= new waiter, esi= list head
|
|
|
|
|
|
|
|
.again_timeout:
|
|
|
|
call change_task
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [ebx + APPDATA.wait_param]
|
2017-06-16 00:36:39 +02:00
|
|
|
test eax, eax
|
|
|
|
jz .timeout
|
|
|
|
|
|
|
|
mov eax, edx
|
|
|
|
lock cmpxchg [ecx], edx
|
|
|
|
jz .again_timeout
|
|
|
|
@@:
|
|
|
|
list_del esp
|
|
|
|
add esp, sizeof.MUTEX_WAITER
|
|
|
|
|
|
|
|
popfd
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], 0
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
.timeout:
|
|
|
|
list_del esp
|
|
|
|
add esp, sizeof.MUTEX_WAITER
|
|
|
|
|
|
|
|
popfd
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], -1
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|
|
|
|
|
|
|
|
align 4
|
|
|
|
;ecx futex handle
|
|
|
|
;edx number of threads
|
|
|
|
;edi current process
|
|
|
|
;ebp futex object
|
|
|
|
.wake:
|
|
|
|
|
|
|
|
xor ecx, ecx
|
|
|
|
|
|
|
|
pushfd
|
|
|
|
cli
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
lea ebx, [ebp + FUTEX.wait_list]
|
|
|
|
mov esi, [ebx + LHEAD.next]
|
2017-06-16 00:36:39 +02:00
|
|
|
.again_wake:
|
|
|
|
cmp esi, ebx
|
|
|
|
je .done
|
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov eax, [esi + MUTEX_WAITER.task]
|
2022-02-09 18:07:51 +01:00
|
|
|
mov [eax + APPDATA.state], TSTATE_RUNNING
|
2017-06-16 00:36:39 +02:00
|
|
|
|
2022-02-12 18:27:41 +01:00
|
|
|
mov esi, [esi + MUTEX_WAITER.list.next]
|
2017-06-16 00:36:39 +02:00
|
|
|
inc ecx
|
|
|
|
cmp ecx, edx
|
|
|
|
jb .again_wake
|
|
|
|
.done:
|
|
|
|
popfd
|
2022-06-05 16:41:51 +02:00
|
|
|
mov [esp + SYSCALL_STACK.eax], ecx
|
2017-06-16 00:36:39 +02:00
|
|
|
ret
|
|
|
|
|