shithub: mc

ref: 16b894430a5b71f4956ee305229bbbffd13605d0
dir: /lib/thread/test/atomic.myr/

View raw version
use std
use thread

use thrtestutil

const Nherd = 20

var val : uint64 = 0
var done : uint32 = 0

const main = {
	done = 0
	val = 0
	thrtestutil.mkherd(Nherd, incvar)
	while thread.xget(&done) != Nherd
		/* nothing */
	;;
	std.assert(val == 2_000_000, "atomics are broken\n")

	testintptr()
}

const incvar = {
	var i

	for i = 0; i < 100_000; i++
		thread.xadd(&val, 1)
	;;
	thread.xadd(&done, 1)
}

const testintptr = {
	var i = 123
	var j = 456
	var p = &i

	std.assert(std.get(thread.xgetptr(&p))# == 123, "xgetptr is broken\n")
	thread.xsetptr(&p, `std.Some &j)
	std.assert(p# == 456, "xsetptr is broken\n")
	std.assert(std.get(thread.xcasptr(&p, `std.Some &j, `std.Some &i)) == &j, "xcasptr is broken\n")
	std.assert(p# == 123, "xcasptr is broken\n")
	std.assert(std.get(thread.xchgptr(&p, `std.None)) == &i, "xchgptr is broken\n")
	std.assert((p : std.intptr) == 0, "xchgptr is broken\n")
}