shim_async.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_async.c
  17. *
  18. * This file contains functions to add asyncronous events triggered by timer.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_thread.h>
  23. #include <pal.h>
  24. #include <list.h>
  25. DEFINE_LIST(async_event);
  26. struct async_event {
  27. IDTYPE caller;
  28. LIST_TYPE(async_event) list;
  29. void (*callback) (IDTYPE caller, void * arg);
  30. void * arg;
  31. PAL_HANDLE object;
  32. unsigned long install_time;
  33. unsigned long expire_time;
  34. };
  35. DEFINE_LISTP(async_event);
  36. static LISTP_TYPE(async_event) async_list;
  37. /* This variable can be read without the async_helper_lock held, but is always
  38. * modified with it held. */
  39. static enum { HELPER_NOTALIVE, HELPER_ALIVE } async_helper_state;
  40. static struct shim_thread * async_helper_thread;
  41. static AEVENTTYPE async_helper_event;
  42. static LOCKTYPE async_helper_lock;
  43. /* Returns remaining usecs */
  44. uint64_t install_async_event (PAL_HANDLE object, unsigned long time,
  45. void (*callback) (IDTYPE caller, void * arg),
  46. void * arg)
  47. {
  48. struct async_event * event =
  49. malloc(sizeof(struct async_event));
  50. unsigned long install_time = DkSystemTimeQuery();
  51. uint64_t rv = 0;
  52. debug("install async event at %llu\n", install_time);
  53. event->callback = callback;
  54. event->arg = arg;
  55. event->caller = get_cur_tid();
  56. event->object = object;
  57. event->install_time = time ? install_time : 0;
  58. event->expire_time = time ? install_time + time : 0;
  59. lock(async_helper_lock);
  60. struct async_event * tmp;
  61. listp_for_each_entry(tmp, &async_list, list) {
  62. if (event->expire_time && tmp->expire_time > event->expire_time)
  63. break;
  64. }
  65. /*
  66. * man page of alarm system call :
  67. * DESCRIPTION
  68. * alarm() arranges for a SIGALRM signal to be delivered to the
  69. * calling process in seconds seconds.
  70. * If seconds is zero, any pending alarm is canceled.
  71. * In any event any previously set alarm() is canceled.
  72. */
  73. if (!listp_empty(&async_list)) {
  74. tmp = listp_first_entry(&async_list, struct async_event, list);
  75. tmp = tmp->list.prev;
  76. /*
  77. * any previously set alarm() is canceled.
  78. * There should be exactly only one timer pending
  79. */
  80. listp_del(tmp, &async_list, list);
  81. free(tmp);
  82. rv = tmp->expire_time - install_time;
  83. } else
  84. tmp = NULL;
  85. INIT_LIST_HEAD(event, list);
  86. if (!time) // If seconds is zero, any pending alarm is canceled.
  87. free(event);
  88. else
  89. listp_add_tail(event, &async_list, list);
  90. if (async_helper_state == HELPER_NOTALIVE)
  91. create_async_helper();
  92. unlock(async_helper_lock);
  93. set_event(&async_helper_event, 1);
  94. return rv;
  95. }
  96. int init_async (void)
  97. {
  98. /* This is early enough in init that we can write this variable without
  99. * the lock. */
  100. async_helper_state = HELPER_NOTALIVE;
  101. create_lock(async_helper_lock);
  102. create_event(&async_helper_event);
  103. return 0;
  104. }
  105. #define IDLE_SLEEP_TIME 1000
  106. #define MAX_IDLE_CYCLES 100
  107. static void shim_async_helper (void * arg)
  108. {
  109. struct shim_thread * self = (struct shim_thread *) arg;
  110. if (!arg)
  111. return;
  112. __libc_tcb_t tcb;
  113. allocate_tls(&tcb, false, self);
  114. debug_setbuf(&tcb.shim_tcb, true);
  115. debug("set tcb to %p\n", &tcb);
  116. lock(async_helper_lock);
  117. bool notme = (self != async_helper_thread);
  118. unlock(async_helper_lock);
  119. if (notme) {
  120. put_thread(self);
  121. DkThreadExit();
  122. return;
  123. }
  124. debug("async helper thread started\n");
  125. /* TSAI: we assume async helper thread will not drain the
  126. stack that PAL provides, so for efficiency, we don't
  127. swap any stack */
  128. unsigned long idle_cycles = 0;
  129. unsigned long latest_time;
  130. struct async_event * next_event = NULL;
  131. PAL_HANDLE async_event_handle = event_handle(&async_helper_event);
  132. int object_list_size = 32, object_num;
  133. PAL_HANDLE polled;
  134. PAL_HANDLE * local_objects =
  135. malloc(sizeof(PAL_HANDLE) * (1 + object_list_size));
  136. local_objects[0] = async_event_handle;
  137. goto update_status;
  138. /* This loop should be careful to use a barrier after sleeping
  139. * to ensure that the while breaks once async_helper_state changes.
  140. */
  141. while (async_helper_state == HELPER_ALIVE) {
  142. unsigned long sleep_time;
  143. if (next_event) {
  144. sleep_time = next_event->expire_time - latest_time;
  145. idle_cycles = 0;
  146. } else if (object_num) {
  147. sleep_time = NO_TIMEOUT;
  148. idle_cycles = 0;
  149. } else {
  150. sleep_time = IDLE_SLEEP_TIME;
  151. idle_cycles++;
  152. }
  153. polled = DkObjectsWaitAny(object_num + 1, local_objects, sleep_time);
  154. barrier();
  155. if (!polled) {
  156. if (next_event) {
  157. debug("async event trigger at %llu\n",
  158. next_event->expire_time);
  159. next_event->callback(next_event->caller, next_event->arg);
  160. lock(async_helper_lock);
  161. /* DEP: Events can only be on the async list */
  162. listp_del(next_event, &async_list, list);
  163. free(next_event);
  164. goto update_list;
  165. }
  166. continue;
  167. }
  168. if (polled == async_event_handle) {
  169. clear_event(&async_helper_event);
  170. update_status:
  171. latest_time = DkSystemTimeQuery();
  172. if (async_helper_state == HELPER_NOTALIVE) {
  173. break;
  174. } else {
  175. lock(async_helper_lock);
  176. goto update_list;
  177. }
  178. }
  179. struct async_event * tmp, * n;
  180. lock(async_helper_lock);
  181. listp_for_each_entry_safe(tmp, n, &async_list, list) {
  182. if (tmp->object == polled) {
  183. debug("async event trigger at %llu\n",
  184. latest_time);
  185. unlock(async_helper_lock);
  186. tmp->callback(tmp->caller, tmp->arg);
  187. lock(async_helper_lock);
  188. break;
  189. }
  190. }
  191. update_list:
  192. next_event = NULL;
  193. object_num = 0;
  194. if (!listp_empty(&async_list)) {
  195. struct async_event * tmp, * n;
  196. listp_for_each_entry_safe(tmp, n, &async_list, list) {
  197. if (tmp->object) {
  198. local_objects[object_num + 1] = tmp->object;
  199. object_num++;
  200. }
  201. if (!tmp->install_time)
  202. continue;
  203. if (tmp->expire_time > latest_time) {
  204. next_event = tmp;
  205. break;
  206. }
  207. debug("async event trigger at %llu (expire at %llu)\n",
  208. latest_time, tmp->expire_time);
  209. listp_del(tmp, &async_list, list);
  210. unlock(async_helper_lock);
  211. tmp->callback(tmp->caller, tmp->arg);
  212. free(tmp);
  213. lock(async_helper_lock);
  214. }
  215. idle_cycles = 0;
  216. }
  217. unlock(async_helper_lock);
  218. if (idle_cycles++ == MAX_IDLE_CYCLES) {
  219. debug("async helper thread reach helper cycle\n");
  220. /* walking away, if someone is issueing an event,
  221. they have to create another thread */
  222. break;
  223. }
  224. }
  225. lock(async_helper_lock);
  226. async_helper_state = HELPER_NOTALIVE;
  227. async_helper_thread = NULL;
  228. unlock(async_helper_lock);
  229. put_thread(self);
  230. debug("async helper thread terminated\n");
  231. DkThreadExit();
  232. }
  233. /* This should be called with the async_helper_lock held */
  234. int create_async_helper (void)
  235. {
  236. int ret = 0;
  237. if (async_helper_state == HELPER_ALIVE)
  238. return 0;
  239. enable_locking();
  240. struct shim_thread * new = get_new_internal_thread();
  241. if (!new)
  242. return -ENOMEM;
  243. PAL_HANDLE handle = thread_create(shim_async_helper, new, 0);
  244. if (!handle) {
  245. ret = -PAL_ERRNO;
  246. async_helper_thread = NULL;
  247. async_helper_state = HELPER_NOTALIVE;
  248. put_thread(new);
  249. return ret;
  250. }
  251. new->pal_handle = handle;
  252. /* Publish new and update the state once fully initialized */
  253. async_helper_thread = new;
  254. async_helper_state = HELPER_ALIVE;
  255. return 0;
  256. }
  257. int terminate_async_helper (void)
  258. {
  259. if (async_helper_state != HELPER_ALIVE)
  260. return 0;
  261. lock(async_helper_lock);
  262. async_helper_state = HELPER_NOTALIVE;
  263. unlock(async_helper_lock);
  264. set_event(&async_helper_event, 1);
  265. return 0;
  266. }