shithub: mc

ref: 164646bf80884c20db57978c56134fb875c9e337
dir: /lib/thread/test/mutex.myr/

View raw version
use std
use thread

use "test/util.use"

const Nherd = 20

var val : uint64 = 0
var done : uint32 = 0
var mtx : thread.mutex

const main = {
	done = 0
	val = 0

	mtx = thread.mkmtx()
	mkherd(Nherd, incvar)
	while thread.xget(&done) != Nherd
		/* nothing */
	;;
	if val != 10_000 * 20
		std.fatal("mutexes are broken, got {}\n", val)
	;;
}

const incvar = {
	for var i = 0; i < 10_000; i++
		thread.mtxlock(&mtx)
		val++
		thread.mtxunlock(&mtx)
	;;
	thread.xadd(&done, 1)
}