pthread.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* This file is part of ToaruOS and is released under the terms
  2. * of the NCSA / University of Illinois License - see LICENSE.md
  3. * Copyright (C) 2012-2018 K. Lange
  4. */
  5. #include <stdlib.h>
  6. #include <stdint.h>
  7. #include <syscall.h>
  8. #include <signal.h>
  9. #include <pthread.h>
  10. #include <errno.h>
  11. #define PTHREAD_STACK_SIZE 0x100000
  12. int clone(uintptr_t a,uintptr_t b,void* c) {
  13. __sets_errno(syscall_clone(a,b,c));
  14. }
  15. int gettid() {
  16. return syscall_gettid(); /* never fails */
  17. }
  18. int pthread_create(pthread_t * thread, pthread_attr_t * attr, void *(*start_routine)(void *), void * arg) {
  19. char * stack = malloc(PTHREAD_STACK_SIZE);
  20. uintptr_t stack_top = (uintptr_t)stack + PTHREAD_STACK_SIZE;
  21. thread->stack = stack;
  22. thread->id = clone(stack_top, (uintptr_t)start_routine, arg);
  23. return 0;
  24. }
  25. int pthread_kill(pthread_t thread, int sig) {
  26. __sets_errno(kill(thread.id, sig));
  27. }
  28. void pthread_exit(void * value) {
  29. /* Perform nice cleanup */
  30. #if 0
  31. /* XXX: LOCK */
  32. free(stack);
  33. /* XXX: Return value!? */
  34. #endif
  35. uintptr_t magic_exit_target = 0xFFFFB00F;
  36. void (*magic_exit_func)(void) = (void *)magic_exit_target;
  37. magic_exit_func();
  38. }