diff --git a/src/kernel/apps/Jamfile b/src/kernel/apps/Jamfile index 45926944b6..418f3ed69b 100644 --- a/src/kernel/apps/Jamfile +++ b/src/kernel/apps/Jamfile @@ -7,6 +7,7 @@ KernelObjects true_main.c sig_test.c select_test.c + tls_test.c ; SubInclude OBOS_TOP src kernel apps cpuinfo ; diff --git a/src/kernel/apps/tls_test.c b/src/kernel/apps/tls_test.c new file mode 100644 index 0000000000..017c11be62 --- /dev/null +++ b/src/kernel/apps/tls_test.c @@ -0,0 +1,93 @@ +/* tests TLS (thread locale storage) functionality */ + +/* +** Copyright 2002, Axel Dörfler, axeld@pinc-software.de. All rights reserved. +** Distributed under the terms of the OpenBeOS License. +*/ + + +#include +#include + +#include +#include + + +sem_id gWait; +int32 gSlot; + + +#if __INTEL__ +static void * +read_fs_register(void) +{ + void *fs; + + asm("movl %%fs, %0" : "=r" (fs)); + return fs; +} +#endif + + +static int32 +thread1_func(void *data) +{ + printf("1. stack at: %p\n", &data); + printf("1. TLS address = %p\n", tls_address(gSlot)); + tls_set(gSlot, (void *)"thread 1"); + printf("1. TLS get = %s (should be \"thread 1\")\n", (char *)tls_get(gSlot)); + + acquire_sem(gWait); + + printf("1. TLS get = %s (should be \"thread 1\")\n", (char *)tls_get(gSlot)); + + return 0; +} + + +static int32 +thread2_func(void *data) +{ + printf("2. stack at: %p\n", &data); + printf("2. TLS address = %p\n", tls_address(gSlot)); + printf("2. TLS get = %s (should be NULL)\n", (char *)tls_get(gSlot)); + + acquire_sem(gWait); + + printf("2. TLS get = %s (should be NULL)\n", (char *)tls_get(gSlot)); + + return 0; +} + + +int +main(int argc, char **argv) +{ + thread_id thread1, thread2; + + gWait = create_sem(0, "waiter"); + gSlot = tls_allocate(); + + printf("got slot %ld : %p\n", gSlot, &thread1); + +#if __INTEL__ + printf("FS register is %p\n", read_fs_register()); +#endif + printf("TLS address in main = %p\n", tls_address(gSlot)); + + tls_set(gSlot, (void *)"main thread"); + + thread1 = spawn_thread(thread1_func, "Thread 1" , B_NORMAL_PRIORITY, NULL); + resume_thread(thread1); + thread2 = spawn_thread(thread2_func, "Thread 2" , B_NORMAL_PRIORITY, NULL); + resume_thread(thread2); + + printf("TLS in main = %s (should be \"main thread\")\n", (char *)tls_get(gSlot)); + + // shut down + snooze(100000); + delete_sem(gWait); + + return 0; +} +