2
0

apple_thread.m 1.2 KB

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