thread.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /* MegaZeux
  2. *
  3. * Copyright (C) 2008 Alan Williams <mralert@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 __MUTEX_WII_H
  20. #define __MUTEX_WII_H
  21. #include "../../src/compat.h"
  22. __M_BEGIN_DECLS
  23. #include <time.h>
  24. #define BOOL _BOOL
  25. #include <gctypes.h>
  26. #include <ogc/mutex.h>
  27. #include <ogc/cond.h>
  28. #include <ogc/lwp.h>
  29. #include <ogc/lwp_watchdog.h>
  30. #undef BOOL
  31. #define THREAD_RES void *
  32. #define THREAD_RETURN do { return NULL; } while(0)
  33. typedef cond_t platform_cond;
  34. typedef mutex_t platform_mutex;
  35. typedef lwp_t platform_thread;
  36. typedef THREAD_RES (*platform_thread_fn)(void *);
  37. static inline void platform_mutex_init(platform_mutex *mutex)
  38. {
  39. // FIXME: Okay to ignore retval?
  40. LWP_MutexInit(mutex, false);
  41. }
  42. static inline void platform_mutex_destroy(platform_mutex *mutex)
  43. {
  44. LWP_MutexDestroy(*mutex);
  45. }
  46. static inline boolean platform_mutex_lock(platform_mutex *mutex)
  47. {
  48. if(LWP_MutexLock(*mutex))
  49. return false;
  50. return true;
  51. }
  52. static inline boolean platform_mutex_unlock(platform_mutex *mutex)
  53. {
  54. if(LWP_MutexUnlock(*mutex))
  55. return false;
  56. return true;
  57. }
  58. static inline void platform_cond_init(platform_cond *cond)
  59. {
  60. LWP_CondInit(cond);
  61. }
  62. static inline void platform_cond_destroy(platform_cond *cond)
  63. {
  64. LWP_CondDestroy(*cond);
  65. }
  66. static inline boolean platform_cond_wait(platform_cond *cond,
  67. platform_mutex *mutex)
  68. {
  69. if(LWP_CondWait(*cond, *mutex))
  70. return false;
  71. return true;
  72. }
  73. static inline boolean platform_cond_timedwait(platform_cond *cond,
  74. platform_mutex *mutex, unsigned int timeout_ms)
  75. {
  76. struct timespec timeout;
  77. u64 ticks;
  78. // Use LWP watchdog to get a usable absolute time
  79. ticks = gettime() + millisecs_to_ticks(timeout_ms);
  80. timeout.tv_sec = ticks_to_secs(ticks);
  81. timeout.tv_nsec = ticks_to_nanosecs(ticks) % 1000000000;
  82. if(LWP_CondTimedWait(*cond, *mutex, &timeout))
  83. return false;
  84. return true;
  85. }
  86. static inline boolean platform_cond_signal(platform_cond *cond)
  87. {
  88. if(LWP_CondSignal(*cond))
  89. return false;
  90. return true;
  91. }
  92. static inline boolean platform_cond_broadcast(platform_cond *cond)
  93. {
  94. if(LWP_CondBroadcast(*cond))
  95. return false;
  96. return true;
  97. }
  98. static inline int platform_thread_create(platform_thread *thread,
  99. platform_thread_fn start_function, void *data)
  100. {
  101. return LWP_CreateThread(thread, start_function, data, NULL, 0, 64);
  102. }
  103. static inline void platform_thread_join(platform_thread *thread)
  104. {
  105. LWP_JoinThread(*thread, NULL);
  106. }
  107. static inline void platform_yield(void)
  108. {
  109. LWP_YieldThread();
  110. }
  111. __M_END_DECLS
  112. #endif // __MUTEX_WII_H