sync_openbsd.odin 1.0 KB

123456789101112131415161718192021222324252627282930313233343536
  1. package sync
  2. import "core:sys/unix"
  3. import "core:os"
  4. current_thread_id :: proc "contextless" () -> int {
  5. return os.current_thread_id()
  6. }
  7. // The Darwin docs say it best:
  8. // A semaphore is much like a lock, except that a finite number of threads can hold it simultaneously.
  9. // Semaphores can be thought of as being much like piles of tokens; multiple threads can take these tokens,
  10. // but when there are none left, a thread must wait until another thread returns one.
  11. Semaphore :: struct #align 16 {
  12. handle: unix.sem_t,
  13. }
  14. semaphore_init :: proc(s: ^Semaphore, initial_count := 0) {
  15. assert(unix.sem_init(&s.handle, 0, u32(initial_count)) == 0)
  16. }
  17. semaphore_destroy :: proc(s: ^Semaphore) {
  18. assert(unix.sem_destroy(&s.handle) == 0)
  19. s.handle = {}
  20. }
  21. semaphore_post :: proc(s: ^Semaphore, count := 1) {
  22. // NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop.
  23. for in 0..<count {
  24. assert(unix.sem_post(&s.handle) == 0)
  25. }
  26. }
  27. semaphore_wait_for :: proc(s: ^Semaphore) {
  28. assert(unix.sem_wait(&s.handle) == 0)
  29. }