Files
haiku-beta6/src/kernel/apps/tls_test.c
T
Axel Dörfler f9afe354bf Added some calls to find_thread(NULL) which does now work as expected
(as it uses TLS, have a look at OS.h).


git-svn-id: file:///srv/svn/repos/haiku/trunk/current@2383 a95241bf-73f2-0310-859d-f6bbb57e9c96
2003-01-07 09:56:33 +00:00

98 lines
2.0 KiB
C

/* tests TLS (thread locale storage) functionality */
/*
** Copyright 2002, Axel Dörfler, [email protected]. All rights reserved.
** Distributed under the terms of the OpenBeOS License.
*/
#include <OS.h>
#include <TLS.h>
#include <stdio.h>
#include <string.h>
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, thread id = %ld\n", &data, find_thread(NULL));
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, thread id = %ld\n", &data, find_thread(NULL));
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;
status_t returnCode;
gWait = create_sem(0, "waiter");
gSlot = tls_allocate();
printf("got slot %ld, stack at %p, thread id = %ld\n", gSlot, &thread1, find_thread(NULL));
#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);
wait_for_thread(thread1, &returnCode);
wait_for_thread(thread2, &returnCode);
return 0;
}