Google 그룹스는 더 이상 새로운 유즈넷 게시물 또는 구독을 지원하지 않습니다. 과거의 콘텐츠는 계속 볼 수 있습니다.

experimental i686 Peterson's algorithm impl...

조회수 454회
읽지 않은 첫 메시지로 건너뛰기

Chris Thomasson

읽지 않음,
2005. 4. 7. 오후 3:19:5805. 4. 7.
받는사람
This mutex is faster than a traditional spinlock because it does not have to
use the LOCK prefix and it doesn't use atomic operations ( cmpxchg, xchg,
ect... ). However, it does have a single (store/load) for the locking
functions. This is necessary to prevent x86 from reordering
load-after-store. Also, it is only good for two processes or threads.


This is experimental for now, but the code should work just fine:


typedef signed int ac_i686_intword_t;


/* must be three adjacent words and aligned on 128 boundary */
typedef struct
__attribute__( (packed) )
__attribute__( (aligned( 128 )) )
ac_i686_mutex_
{
ac_i686_intword_t m1;
ac_i686_intword_t m2;
ac_i686_intword_t t;

} ac_i686_mutex_t;


#define ac_i686_mutex_init( ac_macro_this ) \
(ac_macro_this)->m1 = 0; \
(ac_macro_this)->m2 = 0; \
(ac_macro_this)->t = 1


extern void ac_i686_mutex_lock_1( ac_i686_mutex_t* );
extern void ac_i686_mutex_unlock_1( ac_i686_mutex_t* );
extern void ac_i686_mutex_lock_2( ac_i686_mutex_t* );
extern void ac_i686_mutex_unlock_2( ac_i686_mutex_t* );


align 16
ac_i686_mutex_lock_1 PROC
mov eax, [esp + 4]
mov ecx, 1
mov [eax], ecx
mov ecx, 2
mov [eax + 8], ecx
mfence ; load-after-store

ac_i686_mutex_lock_1_retry:
mov ecx, 0
cmp ecx, [eax + 4]
je ac_i686_mutex_lock_1_done
pause
mov ecx, 1
cmp ecx, [eax + 8]
je ac_i686_mutex_lock_1_done
pause
jmp ac_i686_mutex_lock_1_retry

ac_i686_mutex_lock_1_done:
ret
ac_i686_mutex_lock_1 ENDP


align 16
ac_i686_mutex_unlock_1 PROC
mov ecx, [esp + 4]
mov eax, 0
mov [ecx], eax
ret
ac_i686_mutex_unlock_1 ENDP


align 16
ac_i686_mutex_lock_2 PROC
mov eax, [esp + 4]
mov ecx, 1
mov [eax + 4], ecx
mov ecx, 1
mov [eax + 8], ecx
mfence ; load-after-store

ac_i686_mutex_lock_2_retry:
mov ecx, 0
cmp ecx, [eax]
je ac_i686_mutex_lock_2_done
pause
mov ecx, 2
cmp ecx, [eax + 8]
je ac_i686_mutex_lock_2_done
pause
jmp ac_i686_mutex_lock_2_retry

ac_i686_mutex_lock_2_done:
ret
ac_i686_mutex_lock_2 ENDP


align 16
ac_i686_mutex_unlock_2 PROC
mov eax, [esp + 4]
mov ecx, 0
mov [eax + 4], ecx
ret
ac_i686_mutex_unlock_2 ENDP


--
http://appcore.home.comcast.net/
(portable lock-free data-structures)


Chris Thomasson

읽지 않음,
2005. 4. 7. 오후 3:25:5905. 4. 7.
받는사람
> align 16
> ac_i686_mutex_lock_2 PROC
> mov eax, [esp + 4]
> mov ecx, 1
> mov [eax + 4], ecx

this is redundant, you can remove it.

새 메시지 0개