2018-02-25 11:14:43 +03:00
|
|
|
/* This file is part of ToaruOS and is released under the terms
|
|
|
|
* of the NCSA / University of Illinois License - see LICENSE.md
|
2018-05-01 11:12:56 +03:00
|
|
|
* Copyright (C) 2012-2018 K. Lange
|
2018-02-25 11:14:43 +03:00
|
|
|
*/
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <syscall.h>
|
2018-12-10 04:09:27 +03:00
|
|
|
#include <syscall_nums.h>
|
2018-02-25 11:14:43 +03:00
|
|
|
#include <signal.h>
|
2018-05-09 15:26:45 +03:00
|
|
|
#include <pthread.h>
|
2018-07-18 04:45:42 +03:00
|
|
|
#include <errno.h>
|
2021-05-31 04:47:02 +03:00
|
|
|
#include <string.h>
|
2022-05-26 02:58:49 +03:00
|
|
|
#include <stdio.h>
|
2018-02-25 11:14:43 +03:00
|
|
|
|
2018-12-19 08:38:08 +03:00
|
|
|
#include <sys/wait.h>
|
2021-02-16 12:44:31 +03:00
|
|
|
#include <sys/sysfunc.h>
|
2018-12-19 08:38:08 +03:00
|
|
|
|
2018-12-10 04:09:27 +03:00
|
|
|
DEFN_SYSCALL3(clone, SYS_CLONE, uintptr_t, uintptr_t, void *);
|
|
|
|
DEFN_SYSCALL0(gettid, SYS_GETTID);
|
|
|
|
|
2021-09-02 11:27:21 +03:00
|
|
|
extern int __libc_is_multicore;
|
|
|
|
static inline void _yield(void) {
|
|
|
|
if (!__libc_is_multicore) syscall_yield();
|
|
|
|
}
|
|
|
|
|
2018-02-25 11:14:43 +03:00
|
|
|
#define PTHREAD_STACK_SIZE 0x100000
|
|
|
|
|
|
|
|
int clone(uintptr_t a,uintptr_t b,void* c) {
|
2018-07-18 04:45:42 +03:00
|
|
|
__sets_errno(syscall_clone(a,b,c));
|
2018-02-25 11:14:43 +03:00
|
|
|
}
|
|
|
|
int gettid() {
|
2018-07-18 04:45:42 +03:00
|
|
|
return syscall_gettid(); /* never fails */
|
2018-02-25 11:14:43 +03:00
|
|
|
}
|
|
|
|
|
2021-02-16 12:44:31 +03:00
|
|
|
struct pthread {
|
|
|
|
void * (*entry)(void *);
|
|
|
|
void * arg;
|
|
|
|
};
|
|
|
|
|
2021-05-31 04:47:02 +03:00
|
|
|
void * __tls_get_addr(void* input) {
|
2022-05-26 02:58:49 +03:00
|
|
|
#ifdef __x86_64__
|
|
|
|
struct tls_index {
|
|
|
|
uintptr_t module;
|
|
|
|
uintptr_t offset;
|
|
|
|
};
|
|
|
|
struct tls_index * index = input;
|
|
|
|
/* We only support initial-exec stuff, so this must be %fs:offset */
|
|
|
|
uintptr_t threadbase;
|
|
|
|
asm ("mov %%fs:0, %0" :"=r"(threadbase));
|
|
|
|
return (void*)(threadbase + index->offset);
|
|
|
|
#else
|
2021-02-16 12:44:31 +03:00
|
|
|
return NULL;
|
2022-05-26 02:58:49 +03:00
|
|
|
#endif
|
2021-02-16 12:44:31 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void __make_tls(void) {
|
2021-05-31 04:47:02 +03:00
|
|
|
char * tlsSpace = valloc(4096);
|
|
|
|
memset(tlsSpace, 0x0, 4096);
|
2022-01-23 04:36:46 +03:00
|
|
|
/* self-pointer start? */
|
|
|
|
char ** tlsSelf = (char **)(tlsSpace);
|
2021-02-16 12:44:31 +03:00
|
|
|
*tlsSelf = (char*)tlsSelf;
|
|
|
|
sysfunc(TOARU_SYS_FUNC_SETGSBASE, (char*[]){(char*)tlsSelf});
|
|
|
|
}
|
|
|
|
|
2022-03-12 12:04:54 +03:00
|
|
|
void pthread_exit(void * value) {
|
|
|
|
syscall_exit(0);
|
|
|
|
__builtin_unreachable();
|
|
|
|
}
|
|
|
|
|
2021-02-16 12:44:31 +03:00
|
|
|
void * __thread_start(void * thread) {
|
|
|
|
__make_tls();
|
|
|
|
struct pthread * me = ((pthread_t *)thread)->ret_val;
|
|
|
|
((pthread_t *)thread)->ret_val = 0;
|
2022-03-12 12:04:54 +03:00
|
|
|
pthread_exit(me->entry(me->arg));
|
|
|
|
return NULL;
|
2021-02-16 12:44:31 +03:00
|
|
|
}
|
|
|
|
|
2018-02-25 11:14:43 +03:00
|
|
|
int pthread_create(pthread_t * thread, pthread_attr_t * attr, void *(*start_routine)(void *), void * arg) {
|
2022-01-23 04:36:46 +03:00
|
|
|
char * stack = valloc(PTHREAD_STACK_SIZE);
|
2018-02-25 11:14:43 +03:00
|
|
|
uintptr_t stack_top = (uintptr_t)stack + PTHREAD_STACK_SIZE;
|
2021-02-16 12:44:31 +03:00
|
|
|
|
2018-02-25 11:14:43 +03:00
|
|
|
thread->stack = stack;
|
2021-02-16 12:44:31 +03:00
|
|
|
struct pthread * data = malloc(sizeof(struct pthread));
|
|
|
|
data->entry = start_routine;
|
|
|
|
data->arg = arg;
|
|
|
|
thread->ret_val = data;
|
|
|
|
thread->id = clone(stack_top, (uintptr_t)__thread_start, thread);
|
2018-02-25 11:14:43 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_kill(pthread_t thread, int sig) {
|
2018-07-18 04:45:42 +03:00
|
|
|
__sets_errno(kill(thread.id, sig));
|
2018-02-25 11:14:43 +03:00
|
|
|
}
|
|
|
|
|
2018-12-19 08:38:08 +03:00
|
|
|
void pthread_cleanup_push(void (*routine)(void *), void *arg) {
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
void pthread_cleanup_pop(int execute) {
|
|
|
|
/* do nothing */
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_lock(pthread_mutex_t *mutex) {
|
|
|
|
while (__sync_lock_test_and_set(mutex, 0x01)) {
|
2021-09-02 11:27:21 +03:00
|
|
|
_yield();
|
2018-12-19 08:38:08 +03:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_trylock(pthread_mutex_t *mutex) {
|
|
|
|
if (__sync_lock_test_and_set(mutex, 0x01)) {
|
|
|
|
return EBUSY;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_unlock(pthread_mutex_t *mutex) {
|
|
|
|
__sync_lock_release(mutex);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr) {
|
|
|
|
*mutex = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_mutex_destroy(pthread_mutex_t *mutex) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_attr_init(pthread_attr_t *attr) {
|
|
|
|
*attr = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_attr_destroy(pthread_attr_t *attr) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int pthread_join(pthread_t thread, void **retval) {
|
|
|
|
int status;
|
|
|
|
int result = waitpid(thread.id, &status, 0);
|
|
|
|
if (retval) {
|
2021-05-31 04:47:02 +03:00
|
|
|
*retval = (void*)(uintptr_t)status;
|
2018-12-19 08:38:08 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|