mirror of
https://github.com/jart/cosmopolitan.git
synced 2025-06-27 14:58:30 +00:00
Make _Thread_local work across platforms
We now rewrite the binary image at runtime on Windows and XNU to change mov %fs:0,%reg instructions to use %gs instead. There's also simpler threading API introduced by this change and it's called _spawn() and _join(), which has replaced most clone() usage.
This commit is contained in:
parent
e4d6e263d4
commit
5f4f6b0e69
51 changed files with 808 additions and 1043 deletions
|
@ -1,70 +0,0 @@
|
|||
#if 0
|
||||
/*─────────────────────────────────────────────────────────────────╗
|
||||
│ To the extent possible under law, Justine Tunney has waived │
|
||||
│ all copyright and related or neighboring rights to this file, │
|
||||
│ as it is written in the following disclaimers: │
|
||||
│ • http://unlicense.org/ │
|
||||
│ • http://creativecommons.org/publicdomain/zero/1.0/ │
|
||||
╚─────────────────────────────────────────────────────────────────*/
|
||||
#endif
|
||||
#include "libc/calls/calls.h"
|
||||
#include "libc/dce.h"
|
||||
#include "libc/intrin/kprintf.h"
|
||||
#include "libc/log/log.h"
|
||||
#include "libc/runtime/runtime.h"
|
||||
#include "libc/stdio/stdio.h"
|
||||
#include "libc/thread/thread.h"
|
||||
#include "libc/time/time.h"
|
||||
|
||||
cthread_sem_t semaphore;
|
||||
_Thread_local int test_tls = 0x12345678;
|
||||
|
||||
static void *worker(void *arg) {
|
||||
int tid;
|
||||
cthread_t self;
|
||||
cthread_sem_signal(&semaphore);
|
||||
self = cthread_self();
|
||||
tid = self->tid;
|
||||
printf("[%p] %d -> %#x\n", self, tid, test_tls);
|
||||
if (test_tls != 0x12345678) {
|
||||
printf(".tdata test #2 failed\n");
|
||||
}
|
||||
return (void *)4;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int rc, tid;
|
||||
void *exitcode;
|
||||
cthread_t self, thread;
|
||||
|
||||
if (IsWindows() || IsXnu()) {
|
||||
fprintf(stderr,
|
||||
"error: can't run example\n"
|
||||
"_Thread_local only works on Linux/FreeBSD/NetBSD/OpenBSD\n");
|
||||
return 1;
|
||||
}
|
||||
|
||||
self = cthread_self();
|
||||
tid = self->tid;
|
||||
printf("[%p] %d -> %#x\n", self, tid, test_tls);
|
||||
if (test_tls != 0x12345678) {
|
||||
printf(".tdata test #1 failed\n");
|
||||
}
|
||||
cthread_sem_init(&semaphore, 0);
|
||||
rc = cthread_create(&thread, NULL, &worker, NULL);
|
||||
if (rc == 0) {
|
||||
cthread_sem_wait(&semaphore, 0, NULL);
|
||||
printf("thread created: %p\n", thread);
|
||||
#if 1
|
||||
cthread_join(thread, &exitcode);
|
||||
#else
|
||||
exitcode = cthread_detach(thread);
|
||||
#endif
|
||||
cthread_sem_signal(&semaphore);
|
||||
cthread_sem_wait(&semaphore, 0, NULL);
|
||||
printf("thread joined: %p -> %p\n", thread, exitcode);
|
||||
} else {
|
||||
fprintf(stderr, "ERROR: thread could not be started: %d\n", rc);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue