thread.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2016, 2018 Adrian Siekierka <asiekierka@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifndef __THREAD_3DS_H
  20. #define __THREAD_3DS_H
  21. #include "../../src/compat.h"
  22. #define STACK_SIZE_CTR 8096
  23. __M_BEGIN_DECLS
  24. #include <3ds.h>
  25. #define THREAD_RES void
  26. #define THREAD_RETURN do { return; } while(0)
  27. typedef LightLock platform_mutex;
  28. typedef Thread platform_thread;
  29. typedef ThreadFunc platform_thread_fn;
  30. static inline void platform_mutex_init(platform_mutex *mutex)
  31. {
  32. LightLock_Init(mutex);
  33. }
  34. static inline boolean platform_mutex_lock(platform_mutex *mutex)
  35. {
  36. if(LightLock_TryLock(mutex))
  37. return false;
  38. return true;
  39. }
  40. static inline boolean platform_mutex_unlock(platform_mutex *mutex)
  41. {
  42. LightLock_Unlock(mutex);
  43. return true;
  44. }
  45. static inline boolean platform_mutex_destroy(platform_mutex *mutex)
  46. {
  47. return true;
  48. }
  49. static inline int platform_thread_create(platform_thread *thread,
  50. platform_thread_fn start_function, void *data)
  51. {
  52. s32 priority;
  53. svcGetThreadPriority(&priority, CUR_THREAD_HANDLE);
  54. *thread = threadCreate(start_function, data, STACK_SIZE_CTR, priority-1, -1,
  55. true);
  56. return (*thread == NULL) ? 1 : 0;
  57. }
  58. static inline void platform_thread_join(platform_thread *thread)
  59. {
  60. threadJoin(*thread, U64_MAX);
  61. }
  62. __M_END_DECLS
  63. #endif // __THREAD_3DS_H