Portable standard facades for byte text, environment, timers, randomness, mapped files, streams, worker pools, directory trees and threads; full ASCII lowercase in std/unicode; examples move to examples/ with generated site pages; Go output handles mixed-width slice bounds.
72 lines
2.7 KiB
Ziran
72 lines
2.7 KiB
Ziran
#import "thread"
|
|
#import "timer_linux"
|
|
|
|
first: Thread;
|
|
second: Thread;
|
|
sums: [2]s64;
|
|
Notice :: struct { thread: *Thread; value: *s64; calls: s32; valid: bool }
|
|
notices: [2]Notice;
|
|
|
|
Notify :: (argument: *void) {
|
|
notice := cast(*Notice)argument
|
|
notice.valid = notice.thread.finished && notice.value.* == 5000050000
|
|
notice.calls += 1
|
|
}
|
|
|
|
Sum :: (argument: *void) {
|
|
slot := cast(*s64)argument
|
|
total: s64 = 0
|
|
index: s64 = 1
|
|
while index <= 100000 {
|
|
total += index
|
|
index += 1
|
|
}
|
|
slot.* = total
|
|
}
|
|
|
|
// Two threads run at once; each result is visible once its thread reports
|
|
// finished, and a finished record starts again.
|
|
#program_export
|
|
SelfTest :: () -> s32 {
|
|
procedure: ThreadProcedure = Sum
|
|
if !ThreadStart(*first, procedure, cast(*void)*sums[0]) { return 1 }
|
|
if ThreadStart(*first, procedure, cast(*void)*sums[0]) { return 2 }
|
|
if !ThreadStart(*second, procedure, cast(*void)*sums[1]) { return 3 }
|
|
waited: s64 = 0
|
|
while !ThreadFinished(*first) {
|
|
if !SleepMilliseconds(1) || waited > 10000 { return 4 }
|
|
waited += 1
|
|
}
|
|
if ThreadRunning(*first) || ThreadFinished(*first) { return 5 }
|
|
if sums[0] != 5000050000 { return 6 }
|
|
ThreadWait(*second)
|
|
if ThreadRunning(*second) || sums[1] != 5000050000 { return 7 }
|
|
sums[0] = 0
|
|
if !ThreadStart(*first, procedure, cast(*void)*sums[0]) { return 8 }
|
|
ThreadWait(*first)
|
|
if sums[0] != 5000050000 { return 9 }
|
|
notify: ThreadProcedure = Notify
|
|
notifications: [2]ThreadNotification
|
|
notifications[0] = .{notify, cast(*void)*notices[0]}
|
|
notifications[1] = .{notify, cast(*void)*notices[1]}
|
|
notices[0].thread = *first; notices[0].value = *sums[0]
|
|
notices[1].thread = *second; notices[1].value = *sums[1]
|
|
// Each restart owns its notification arguments until joined. A rejected
|
|
// start sends no extra notice; a later unnotified start keeps none.
|
|
for iteration: 0..15 {
|
|
sums[0] = 0; sums[1] = 0
|
|
if !ThreadStart(*first, procedure, cast(*void)*sums[0], notifications[0]) { return 10 }
|
|
if ThreadStart(*first, procedure, cast(*void)*sums[0], notifications[0]) { return 11 }
|
|
if !ThreadStart(*second, procedure, cast(*void)*sums[1], notifications[1]) { return 12 }
|
|
ThreadWait(*first); ThreadWait(*second)
|
|
if !notices[0].valid || !notices[1].valid || notices[0].calls != iteration + 1 || notices[1].calls != iteration + 1 { return 13 }
|
|
}
|
|
if !ThreadStart(*first, procedure, cast(*void)*sums[0]) { return 14 }
|
|
ThreadWait(*first)
|
|
if notices[0].calls != 16 { return 15 }
|
|
missing: ThreadNotification
|
|
if ThreadStart(*first, missing.procedure, null, notifications[0]) { return 16 }
|
|
if notices[0].calls != 16 { return 17 }
|
|
return 42
|
|
}
|