Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.programming.threads > #1172

Implementing barrier in linux assembly

From Melzzzzz <mel@zzzzz.com>
Newsgroups alt.lang.asm, comp.programming.threads
Subject Implementing barrier in linux assembly
Date 2012-10-04 02:03 +0200
Organization albasani.net
Message-ID <k4ijno$k6l$2@news.albasani.net> (permalink)

Cross-posted to 2 groups.

Show all headers | View raw


I have implemented barrier in Linux, with simple
approach.
Struct barrier has num threads, count thread entered
in barrier and thread mutex to protect barrier operation.
When thread enters it first acquires mutex then
decreases count . If count is zero it resets counter ,goes to
wake all threads that wait, then releases mutex.
In other case releases mutex and goes to wait.

This works ok except that if several threads
enter barrier, it can happen that waking thread
wakes before some thread(s) enter syscall.
Then, of course, program hangs.
I have solved this with nanosleep syscall,
so that waking thread sleeps for one microsecond
before issuing futex wake syscall.
Is there simpler solution without sleeping?
Eg futex does not waits if edx variable does not
match variable that rdi points to.
But this I haven't tried as don't know
when to reset variable.
So for now I use sleeping method.

Thanks

struc barrier num{
	.count dd num
	.num dd num
	.mutex dd 0
}

virtual at 0
	oBarrier barrier 0
end virtual

macro futex_barrier barrier{
	local .L0, .L1
	mov r15,barrier
	acquire r15 + oBarrier.mutex
	dec dword[r15 + oBarrier.count]
	jz .L0
	release r15 + oBarrier.mutex
	mov eax, 202
	lea rdi, [r15 + oBarrier.num]
	mov rsi, FUTEX_WAIT or FUTEX_PRIVATE_FLAG
	mov edx, [r15 + oBarrier.num]
	xor r10,r10
	syscall
	jmp .L1
.L0:
	sys_nanosleep myspec
;	mov rdi,msgwake
;	xor eax,eax
;	call printf
	mov eax,[r15 + oBarrier.num]
	mov [r15 + oBarrier.count],eax
	mov edx,eax
	mov eax,202
	lea rdi, [r15 + oBarrier.num]
	mov rsi, FUTEX_WAKE or FUTEX_PRIVATE_FLAG
	syscall
	release r15 + oBarrier.mutex
.L1:
}

macro acquire sema{
	local .L0
	mov ebx,1
.L0:
	xor eax,eax
	lock cmpxchg [sema],ebx
	test eax,eax
	jnz .L0
}

macro release sema{
	lock and dword[sema],0
}

Back to comp.programming.threads | Previous | Next | Find similar


Thread

Implementing barrier in linux assembly Melzzzzz <mel@zzzzz.com> - 2012-10-04 02:03 +0200

csiph-web