aboutsummaryrefslogtreecommitdiff
path: root/core/sync/sync2/sema_internal.odin
blob: f4027e90889f7458cc97cf0f8ed3576205b6082f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
//+private
package sync2

import "core:time"


when #config(ODIN_SYNC_SEMA_USE_FUTEX, true) {
	_Sema :: struct {
		count: Futex,
	}

	_sema_post :: proc(s: ^Sema, count := 1) {
		atomic_add(&s.impl.count, Futex(count))
		if count == 1 {
			futex_signal(&s.impl.count)
		} else {
			futex_broadcast(&s.impl.count)
		}
	}

	_sema_wait :: proc(s: ^Sema) {
		for {
			original_count := atomic_load(&s.impl.count)
			for original_count == 0 {
				futex_wait(&s.impl.count, u32(original_count))
				original_count = s.impl.count
			}
			if original_count == atomic_compare_exchange_strong(&s.impl.count, original_count-1, original_count) {
				return
			}
		}
	}

	_sema_wait_with_timeout :: proc(s: ^Sema, duration: time.Duration) -> bool {
		if duration <= 0 {
			return false
		}
		for {
		
			original_count := atomic_load(&s.impl.count)
			for start := time.tick_now(); original_count == 0; /**/ {
				remaining := duration - time.tick_since(start)
				if remaining < 0 {
					return false
				}
				
				if !futex_wait_with_timeout(&s.impl.count, u32(original_count), remaining) {
					return false
				}
				original_count = s.impl.count
			}
			if original_count == atomic_compare_exchange_strong(&s.impl.count, original_count-1, original_count) {
				return true
			}
		}
	}
} else {
	_Sema :: struct {
		wg: Wait_Group,
	}

	_sema_post :: proc(s: ^Sema, count := 1) {
		wait_group_add(&s.impl.wg, count)
	}

	_sema_wait :: proc(s: ^Sema) {
		wait_group_wait(&s.impl.wg)
	}

	_sema_wait_with_timeout :: proc(s: ^Sema, duration: time.Duration) -> bool {
		return wait_group_wait_with_timeout(&s.impl.wg, duration)
	}
}