shithub: mc

ref: 0bb2378d7e2b714998b34394f6977b2ead023d6c
dir: /lib/thread/test/sem.myr/

View raw version
use std
use thread

const Cap = 4

var write : uint32 = 0, read : uint32 = 0
var lock
var len
var cap
var buf : int[Cap]
var done : uint32 = 0

const main = {
	lock = thread.mkmtx()
	len = thread.mksem(0)
	cap = thread.mksem(Cap)

	thread.spawn({
		for var i = 0; i < 100000; i++
			thread.semwait(&cap)
			thread.mtxlock(&lock)
			buf[write++ & (Cap - 1)] = i
			thread.mtxunlock(&lock)
			thread.sempost(&len)
		;;
	})

	thread.spawn({
		for var i = 0; i < 100000; i++
			thread.semwait(&len)
			thread.mtxlock(&lock)
			std.assert(i == buf[read++ & (Cap - 1)], "semaphores are broken\n")
			thread.mtxunlock(&lock)
			thread.sempost(&cap)
		;;
		thread.xset(&done, 1)
	})

	while thread.xget(&done) == 0
		std.nanosleep(1_000_000)
	;;
}