diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-11-16 12:19:58 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-11-16 12:23:36 +0100 |
commit | 66a2b582c36e3b0f7af80defd5507c01f7040b56 (patch) | |
tree | 896435087b0d72fe7ab50cbba94328943df54995 /Userland/tt.cpp | |
parent | 73d6a69b3fec21052c8d1059f350394b30e440b6 (diff) | |
download | serenity-66a2b582c36e3b0f7af80defd5507c01f7040b56.zip |
LibPthread: Implement a basic first pthread mutex
This patch adds these API's:
- pthread_mutex_init()
- pthread_mutex_lock()
- pthread_mutex_unlock()
No mutex attributes are supported yet, so we only do the simplest mutex
wihout recursive locking.
Diffstat (limited to 'Userland/tt.cpp')
-rw-r--r-- | Userland/tt.cpp | 59 |
1 files changed, 53 insertions, 6 deletions
diff --git a/Userland/tt.cpp b/Userland/tt.cpp index e8e56f8c8d..2823a19103 100644 --- a/Userland/tt.cpp +++ b/Userland/tt.cpp @@ -1,15 +1,24 @@ #include <pthread.h> #include <stdio.h> +#include <unistd.h> +#include <stdlib.h> -int main(int, char**) +static int mutex_test(); + +int main(int argc, char** argv) { + if (argc == 2 && *argv[1] == 'm') + return mutex_test(); + printf("Hello from the first thread!\n"); pthread_t thread_id; - int rc = pthread_create(&thread_id, nullptr, [](void*) -> void* { - printf("Hi there, from the second thread!\n"); - pthread_exit((void*)0xDEADBEEF); - return nullptr; - }, nullptr); + int rc = pthread_create( + &thread_id, nullptr, [](void*) -> void* { + printf("Hi there, from the second thread!\n"); + pthread_exit((void*)0xDEADBEEF); + return nullptr; + }, + nullptr); if (rc < 0) { perror("pthread_create"); return 1; @@ -23,3 +32,41 @@ int main(int, char**) printf("Okay, joined and got retval=%p\n", retval); return 0; } + +static pthread_mutex_t mutex; + +int mutex_test() +{ + int rc = pthread_mutex_init(&mutex, nullptr); + if (rc < 0) { + perror("pthread_mutex_init"); + return 1; + } + pthread_t thread_id; + rc = pthread_create( + &thread_id, nullptr, [](void*) -> void* { + printf("I'm the secondary thread :^)\n"); + for (;;) { + pthread_mutex_lock(&mutex); + printf("Second thread stole mutex\n"); + sleep(1); + printf("Second thread giving back mutex\n"); + pthread_mutex_unlock(&mutex); + sleep(1); + } + pthread_exit((void*)0xDEADBEEF); + return nullptr; + }, + nullptr); + if (rc < 0) { + perror("pthread_create"); + return 1; + } + for (;;) { + pthread_mutex_lock(&mutex); + printf("Obnoxious spam!\n"); + pthread_mutex_unlock(&mutex); + usleep(10000); + } + return 0; +} |