1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-14 09:34:59 +00:00

LibPthread+Kernel: Add pthread_kill() and the thread_kill syscall

This commit is contained in:
Ali Mohammad Pur 2021-07-09 06:26:29 +04:30 committed by Andreas Kling
parent ca71d99c66
commit e37f9fa7db
5 changed files with 70 additions and 2 deletions

View file

@ -7,6 +7,7 @@
#include <LibCore/ArgsParser.h>
#include <errno.h>
#include <pthread.h>
#include <signal_numbers.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -19,6 +20,7 @@ static int priority_test();
static int stack_size_test();
static int staying_alive_test();
static int set_stack_test();
static int kill_test();
int main(int argc, char** argv)
{
@ -28,7 +30,7 @@ int main(int argc, char** argv)
args_parser.set_general_help(
"Exercise error-handling and edge-case paths of the execution environment "
"(i.e., Kernel or UE) by doing unusual thread-related things.");
args_parser.add_positional_argument(test_name, "Test to run (m = mutex, d = detached, p = priority, s = stack size, t = simple thread test, x = set stack, nothing = join race)", "test-name", Core::ArgsParser::Required::No);
args_parser.add_positional_argument(test_name, "Test to run (m = mutex, d = detached, p = priority, s = stack size, t = simple thread test, x = set stack, k = kill, nothing = join race)", "test-name", Core::ArgsParser::Required::No);
args_parser.parse(argc, argv);
if (*test_name == 'm')
@ -43,6 +45,8 @@ int main(int argc, char** argv)
return staying_alive_test();
if (*test_name == 'x')
return set_stack_test();
if (*test_name == 'k')
return kill_test();
if (*test_name != 'n') {
args_parser.print_usage(stdout, argv[0]);
return 1;
@ -372,3 +376,38 @@ int set_stack_test()
return 0;
}
int kill_test()
{
pthread_t thread_id;
int rc = pthread_create(
&thread_id, nullptr, [](void*) -> void* {
outln("I'm the secondary thread :^)");
sleep(100);
outln("Secondary thread is still alive :^(");
pthread_exit((void*)0xDEADBEEF);
return nullptr;
},
nullptr);
if (rc < 0) {
perror("pthread_create");
return 1;
}
int result = 0;
sleep(1);
outln("I'm the main thread :^)");
if (pthread_kill(thread_id, 0) != 0) {
perror("pthread_kill");
result = 1;
}
if (pthread_kill(thread_id, SIGKILL) != 0) {
perror("pthread_kill(SIGKILL)");
result = 1;
}
outln("Main thread exiting");
return result;
}