require '_h2ph_pre.ph';

no warnings 'redefine';

unless(defined(&_AMD64_LOCK_H_)) {
    eval 'sub _AMD64_LOCK_H_ () {1;}' unless defined(&_AMD64_LOCK_H_);
    eval 'sub __SIMPLELOCK_LOCKED () {1;}' unless defined(&__SIMPLELOCK_LOCKED);
    eval 'sub __SIMPLELOCK_UNLOCKED () {0;}' unless defined(&__SIMPLELOCK_UNLOCKED);
    eval 'sub __lockbarrier () {
        eval q( &__asm  &__volatile(\\"\\": : :\\"memory\\"));
    }' unless defined(&__lockbarrier);
    require 'machine/atomic.ph';
    if(defined(&LOCKDEBUG)) {
    } else {
	eval 'sub __cpu_simple_lock_init {
	    my($lockp) = @_;
    	    eval q({ *$lockp =  &__SIMPLELOCK_UNLOCKED;  &__lockbarrier(); });
	}' unless defined(&__cpu_simple_lock_init);
	eval 'sub __cpu_simple_lock {
	    my($lockp) = @_;
    	    eval q({  &while ( &x86_atomic_testset_i($lockp,  &__SIMPLELOCK_LOCKED) ==  &__SIMPLELOCK_LOCKED) {  &continue; }  &__lockbarrier(); });
	}' unless defined(&__cpu_simple_lock);
	eval 'sub __cpu_simple_lock_try {
	    my($lockp) = @_;
    	    eval q({ \'int\'  &r = ( &x86_atomic_testset_i($lockp,  &__SIMPLELOCK_LOCKED) ==  &__SIMPLELOCK_UNLOCKED);  &__lockbarrier(); ( &r); });
	}' unless defined(&__cpu_simple_lock_try);
	eval 'sub __cpu_simple_unlock {
	    my($lockp) = @_;
    	    eval q({  &__lockbarrier(); *$lockp =  &__SIMPLELOCK_UNLOCKED; });
	}' unless defined(&__cpu_simple_unlock);
    }
    eval 'sub rw_cas {
        my($p, $o, $n) = @_;
	    eval q(( &x86_atomic_cas_ul($p, $o, $n) != $o));
    }' unless defined(&rw_cas);
}
1;
