apple_thread.m 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "posix_thread.c"
  2. #include <Foundation/Foundation.h>
  3. #include <iron_thread.h>
  4. #include <pthread.h>
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <wchar.h>
  8. static void *ThreadProc(void *arg) {
  9. @autoreleasepool {
  10. iron_thread_t *t = (iron_thread_t *)arg;
  11. t->impl.thread(t->impl.param);
  12. pthread_exit(NULL);
  13. return NULL;
  14. }
  15. }
  16. void iron_thread_init(iron_thread_t *t, void (*thread)(void *param), void *param) {
  17. t->impl.param = param;
  18. t->impl.thread = thread;
  19. pthread_attr_t attr;
  20. pthread_attr_init(&attr);
  21. // pthread_attr_setstacksize(&attr, 1024 * 64);
  22. struct sched_param sp;
  23. memset(&sp, 0, sizeof(sp));
  24. sp.sched_priority = 0;
  25. pthread_attr_setschedparam(&attr, &sp);
  26. pthread_create(&t->impl.pthread, &attr, &ThreadProc, t);
  27. pthread_attr_destroy(&attr);
  28. }
  29. void iron_thread_wait_and_destroy(iron_thread_t *thread) {
  30. int ret;
  31. do {
  32. ret = pthread_join(thread->impl.pthread, NULL);
  33. } while (ret != 0);
  34. }
  35. bool iron_thread_try_to_destroy(iron_thread_t *thread) {
  36. return pthread_join(thread->impl.pthread, NULL) == 0;
  37. }
  38. void iron_threads_init(void) {}
  39. void iron_threads_quit(void) {}
  40. int iron_hardware_threads(void) {
  41. return (int)[[NSProcessInfo processInfo] processorCount];
  42. }