lwp_futex.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021/01/02 bernard the first version
  9. */
  10. #include <rtthread.h>
  11. #include <lwp.h>
  12. #ifdef ARCH_MM_MMU
  13. #include <lwp_user_mm.h>
  14. #endif
  15. #include "sys/time.h"
  16. struct rt_futex
  17. {
  18. int *uaddr;
  19. rt_list_t waiting_thread;
  20. struct lwp_avl_struct node;
  21. struct rt_object *custom_obj;
  22. };
  23. static struct rt_mutex _futex_lock;
  24. static int futex_system_init(void)
  25. {
  26. rt_mutex_init(&_futex_lock, "futexList", RT_IPC_FLAG_FIFO);
  27. return 0;
  28. }
  29. INIT_PREV_EXPORT(futex_system_init);
  30. rt_err_t futex_destory(void *data)
  31. {
  32. rt_err_t ret = -1;
  33. rt_base_t level;
  34. struct rt_futex *futex = (struct rt_futex *)data;
  35. if (futex)
  36. {
  37. level = rt_hw_interrupt_disable();
  38. /* remove futex from futext avl */
  39. lwp_avl_remove(&futex->node, (struct lwp_avl_struct **)futex->node.data);
  40. rt_hw_interrupt_enable(level);
  41. /* release object */
  42. rt_free(futex);
  43. ret = 0;
  44. }
  45. return ret;
  46. }
  47. struct rt_futex *futex_create(int *uaddr, struct rt_lwp *lwp)
  48. {
  49. struct rt_futex *futex = RT_NULL;
  50. struct rt_object *obj = RT_NULL;
  51. if (!lwp)
  52. {
  53. return RT_NULL;
  54. }
  55. futex = (struct rt_futex *)rt_malloc(sizeof(struct rt_futex));
  56. if (!futex)
  57. {
  58. return RT_NULL;
  59. }
  60. obj = rt_custom_object_create("futex", (void *)futex, futex_destory);
  61. if (!obj)
  62. {
  63. rt_free(futex);
  64. return RT_NULL;
  65. }
  66. futex->uaddr = uaddr;
  67. futex->node.avl_key = (avl_key_t)uaddr;
  68. futex->node.data = &lwp->address_search_head;
  69. futex->custom_obj = obj;
  70. rt_list_init(&(futex->waiting_thread));
  71. /* insert into futex head */
  72. lwp_avl_insert(&futex->node, &lwp->address_search_head);
  73. return futex;
  74. }
  75. static struct rt_futex *futex_get(void *uaddr, struct rt_lwp *lwp)
  76. {
  77. struct rt_futex *futex = RT_NULL;
  78. struct lwp_avl_struct *node = RT_NULL;
  79. node = lwp_avl_find((avl_key_t)uaddr, lwp->address_search_head);
  80. if (!node)
  81. {
  82. return RT_NULL;
  83. }
  84. futex = rt_container_of(node, struct rt_futex, node);
  85. return futex;
  86. }
  87. int futex_wait(struct rt_futex *futex, int value, const struct timespec *timeout)
  88. {
  89. rt_base_t level = 0;
  90. rt_err_t ret = -RT_EINTR;
  91. if (*(futex->uaddr) == value)
  92. {
  93. rt_thread_t thread = rt_thread_self();
  94. level = rt_hw_interrupt_disable();
  95. ret = rt_thread_suspend_with_flag(thread, RT_INTERRUPTIBLE);
  96. if (ret < 0)
  97. {
  98. rt_mutex_release(&_futex_lock);
  99. rt_hw_interrupt_enable(level);
  100. rt_set_errno(EINTR);
  101. return ret;
  102. }
  103. /* add into waiting thread list */
  104. rt_list_insert_before(&(futex->waiting_thread), &(thread->tlist));
  105. /* with timeout */
  106. if (timeout)
  107. {
  108. rt_int32_t time = rt_timespec_to_tick(timeout);
  109. /* start the timer of thread */
  110. rt_timer_control(&(thread->thread_timer),
  111. RT_TIMER_CTRL_SET_TIME,
  112. &time);
  113. rt_timer_start(&(thread->thread_timer));
  114. }
  115. rt_mutex_release(&_futex_lock);
  116. rt_hw_interrupt_enable(level);
  117. /* do schedule */
  118. rt_schedule();
  119. ret = thread->error;
  120. /* check errno */
  121. }
  122. else
  123. {
  124. rt_mutex_release(&_futex_lock);
  125. rt_set_errno(EAGAIN);
  126. }
  127. return ret;
  128. }
  129. void futex_wake(struct rt_futex *futex, int number)
  130. {
  131. rt_base_t level = rt_hw_interrupt_disable();
  132. while (!rt_list_isempty(&(futex->waiting_thread)) && number)
  133. {
  134. rt_thread_t thread;
  135. thread = rt_list_entry(futex->waiting_thread.next, struct rt_thread, tlist);
  136. /* remove from waiting list */
  137. rt_list_remove(&(thread->tlist));
  138. thread->error = RT_EOK;
  139. /* resume the suspended thread */
  140. rt_thread_resume(thread);
  141. number--;
  142. }
  143. rt_mutex_release(&_futex_lock);
  144. rt_hw_interrupt_enable(level);
  145. /* do schedule */
  146. rt_schedule();
  147. }
  148. int sys_futex(int *uaddr, int op, int val, const struct timespec *timeout,
  149. int *uaddr2, int val3)
  150. {
  151. struct rt_lwp *lwp = RT_NULL;
  152. struct rt_futex *futex = RT_NULL;
  153. int ret = 0;
  154. rt_err_t lock_ret = 0;
  155. if (!lwp_user_accessable(uaddr, sizeof(int)))
  156. {
  157. rt_set_errno(EINVAL);
  158. return -RT_EINVAL;
  159. }
  160. /**
  161. * if (op & (FUTEX_WAKE|FUTEX_FD|FUTEX_WAKE_BITSET|FUTEX_TRYLOCK_PI|FUTEX_UNLOCK_PI)) was TRUE
  162. * `timeout` should be ignored by implementation, according to POSIX futex(2) manual.
  163. * since only FUTEX_WAKE is implemented in rt-smart, only FUTEX_WAKE was omitted currently
  164. */
  165. if (timeout && !(op & (FUTEX_WAKE)))
  166. {
  167. if (!lwp_user_accessable((void *)timeout, sizeof(struct timespec)))
  168. {
  169. rt_set_errno(EINVAL);
  170. return -RT_EINVAL;
  171. }
  172. }
  173. lock_ret = rt_mutex_take_interruptible(&_futex_lock, RT_WAITING_FOREVER);
  174. if (lock_ret != RT_EOK)
  175. {
  176. rt_set_errno(EAGAIN);
  177. return -RT_EINTR;
  178. }
  179. lwp = lwp_self();
  180. futex = futex_get(uaddr, lwp);
  181. if (futex == RT_NULL)
  182. {
  183. /* create a futex according to this uaddr */
  184. futex = futex_create(uaddr, lwp);
  185. if (futex == RT_NULL)
  186. {
  187. rt_mutex_release(&_futex_lock);
  188. rt_set_errno(ENOMEM);
  189. return -RT_ENOMEM;
  190. }
  191. if (lwp_user_object_add(lwp, futex->custom_obj) != 0)
  192. {
  193. rt_custom_object_destroy(futex->custom_obj);
  194. rt_mutex_release(&_futex_lock);
  195. rt_set_errno(ENOMEM);
  196. return -RT_ENOMEM;
  197. }
  198. }
  199. switch (op)
  200. {
  201. case FUTEX_WAIT:
  202. ret = futex_wait(futex, val, timeout);
  203. /* _futex_lock is released by futex_wait */
  204. break;
  205. case FUTEX_WAKE:
  206. futex_wake(futex, val);
  207. /* _futex_lock is released by futex_wake */
  208. break;
  209. default:
  210. rt_mutex_release(&_futex_lock);
  211. rt_set_errno(ENOSYS);
  212. ret = -ENOSYS;
  213. break;
  214. }
  215. return ret;
  216. }