From d017b5de9d07b3c2eaa5a190d61440e97c53e807 Mon Sep 17 00:00:00 2001 From: KTRosenberg Date: Thu, 2 Jan 2020 16:25:48 -0500 Subject: [PATCH 1/3] replaced pthread_yield with ssched_yield, fixed semaphore post:q --- core/sync/sync_darwin.odin | 8 +++++--- core/sync/sync_linux.odin | 5 ++++- core/sys/unix/pthread_linux.odin | 2 ++ core/sys/unix/pthread_unix.odin | 3 ++- core/thread/thread_unix.odin | 2 +- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/core/sync/sync_darwin.odin b/core/sync/sync_darwin.odin index 2c86e21db..8153b4b37 100644 --- a/core/sync/sync_darwin.odin +++ b/core/sync/sync_darwin.odin @@ -28,9 +28,11 @@ semaphore_destroy :: proc(s: ^Semaphore) { } semaphore_post :: proc(s: ^Semaphore, count := 1) { - assert(count == 1); - res := darwin.semaphore_signal(s.handle); - assert(res == 0); + // NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop. + for in 0..count-1 { + res := darwin.semaphore_signal(s.handle); + assert(res == 0); + } } semaphore_wait_for :: proc(s: ^Semaphore) { diff --git a/core/sync/sync_linux.odin b/core/sync/sync_linux.odin index dc761f6aa..74f2b1e87 100644 --- a/core/sync/sync_linux.odin +++ b/core/sync/sync_linux.odin @@ -20,7 +20,10 @@ semaphore_destroy :: proc(s: ^Semaphore) { } semaphore_post :: proc(s: ^Semaphore, count := 1) { - assert(unix.sem_post(&s.handle) == 0); + // NOTE: SPEED: If there's one syscall to do this, we should use it instead of the loop. + for in 0..count-1 { + assert(unix.sem_post(&s.handle) == 0); + } } semaphore_wait_for :: proc(s: ^Semaphore) { diff --git a/core/sys/unix/pthread_linux.odin b/core/sys/unix/pthread_linux.odin index 18ef09a69..16137e471 100644 --- a/core/sys/unix/pthread_linux.odin +++ b/core/sys/unix/pthread_linux.odin @@ -103,4 +103,6 @@ foreign pthread { sem_wait :: proc(sem: ^sem_t) -> c.int ---; sem_trywait :: proc(sem: ^sem_t) -> c.int ---; // sem_timedwait :: proc(sem: ^sem_t, timeout: time.TimeSpec) -> c.int ---; + + pthread_yield :: proc() -> c.int ---; } diff --git a/core/sys/unix/pthread_unix.odin b/core/sys/unix/pthread_unix.odin index cd3b5c198..267bd28bc 100644 --- a/core/sys/unix/pthread_unix.odin +++ b/core/sys/unix/pthread_unix.odin @@ -52,7 +52,8 @@ foreign pthread { pthread_attr_setstack :: proc(attrs: ^pthread_attr_t, stack_ptr: rawptr, stack_size: u64) -> c.int ---; pthread_attr_getstack :: proc(attrs: ^pthread_attr_t, stack_ptr: ^rawptr, stack_size: ^u64) -> c.int ---; - pthread_yield :: proc() -> c.int ---; + sched_yield :: proc() -> c.int ---; + } @(default_calling_convention="c") diff --git a/core/thread/thread_unix.odin b/core/thread/thread_unix.odin index dd8230ade..53b8790d7 100644 --- a/core/thread/thread_unix.odin +++ b/core/thread/thread_unix.odin @@ -158,5 +158,5 @@ destroy :: proc(t: ^Thread) { yield :: proc() { - unix.pthread_yield(); + unix.sched_yield(); } From 5d1c9583cb42c43dd2b8ab320433de171d7d17a5 Mon Sep 17 00:00:00 2001 From: KTRosenberg Date: Thu, 2 Jan 2020 16:42:34 -0500 Subject: [PATCH 2/3] added the demo to the gitignore --- .gitignore | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index b80ebb2dc..20e3578ca 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ bld/ .vs/ # Uncomment if you have tasks that create the project's static files in wwwroot #wwwroot/ +demo # MSTest test Results [Tt]est[Rr]esult*/ @@ -272,4 +273,4 @@ shared/ *.bc *.ll -*.sublime-workspace \ No newline at end of file +*.sublime-workspace From 673879d1d2d87440195458474cc221d8dc2ef78c Mon Sep 17 00:00:00 2001 From: KTRosenberg Date: Thu, 2 Jan 2020 16:44:30 -0500 Subject: [PATCH 3/3] added note about pthread_yield --- core/sys/unix/pthread_linux.odin | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/sys/unix/pthread_linux.odin b/core/sys/unix/pthread_linux.odin index 16137e471..09d27329c 100644 --- a/core/sys/unix/pthread_linux.odin +++ b/core/sys/unix/pthread_linux.odin @@ -104,5 +104,7 @@ foreign pthread { sem_trywait :: proc(sem: ^sem_t) -> c.int ---; // sem_timedwait :: proc(sem: ^sem_t, timeout: time.TimeSpec) -> c.int ---; + // NOTE: unclear whether pthread_yield is well-supported on Linux systems, + // see https://linux.die.net/man/3/pthread_yield pthread_yield :: proc() -> c.int ---; }