timer.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Waitable timers management
  3. *
  4. * Copyright (C) 1999 Alexandre Julliard
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include "config.h"
  21. #include <assert.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <sys/time.h>
  25. #include <sys/types.h>
  26. #include <stdarg.h>
  27. #include "ntstatus.h"
  28. #define WIN32_NO_STATUS
  29. #include "windef.h"
  30. #include "winternl.h"
  31. #include "file.h"
  32. #include "handle.h"
  33. #include "request.h"
  34. static const WCHAR timer_name[] = {'T','i','m','e','r'};
  35. struct type_descr timer_type =
  36. {
  37. { timer_name, sizeof(timer_name) }, /* name */
  38. TIMER_ALL_ACCESS, /* valid_access */
  39. { /* mapping */
  40. STANDARD_RIGHTS_READ | TIMER_QUERY_STATE,
  41. STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE,
  42. STANDARD_RIGHTS_EXECUTE | SYNCHRONIZE,
  43. TIMER_ALL_ACCESS
  44. },
  45. };
  46. struct timer
  47. {
  48. struct object obj; /* object header */
  49. int manual; /* manual reset */
  50. int signaled; /* current signaled state */
  51. unsigned int period; /* timer period in ms */
  52. abstime_t when; /* next expiration */
  53. struct timeout_user *timeout; /* timeout user */
  54. struct thread *thread; /* thread that set the APC function */
  55. client_ptr_t callback; /* callback APC function */
  56. client_ptr_t arg; /* callback argument */
  57. struct fast_sync *fast_sync; /* fast synchronization object */
  58. };
  59. static void timer_dump( struct object *obj, int verbose );
  60. static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
  61. static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
  62. static struct fast_sync *timer_get_fast_sync( struct object *obj );
  63. static void timer_destroy( struct object *obj );
  64. static const struct object_ops timer_ops =
  65. {
  66. sizeof(struct timer), /* size */
  67. &timer_type, /* type */
  68. timer_dump, /* dump */
  69. add_queue, /* add_queue */
  70. remove_queue, /* remove_queue */
  71. timer_signaled, /* signaled */
  72. timer_satisfied, /* satisfied */
  73. no_signal, /* signal */
  74. no_get_fd, /* get_fd */
  75. default_map_access, /* map_access */
  76. default_get_sd, /* get_sd */
  77. default_set_sd, /* set_sd */
  78. default_get_full_name, /* get_full_name */
  79. no_lookup_name, /* lookup_name */
  80. directory_link_name, /* link_name */
  81. default_unlink_name, /* unlink_name */
  82. no_open_file, /* open_file */
  83. no_kernel_obj_list, /* get_kernel_obj_list */
  84. timer_get_fast_sync, /* get_fast_sync */
  85. no_close_handle, /* close_handle */
  86. timer_destroy /* destroy */
  87. };
  88. /* create a timer object */
  89. static struct timer *create_timer( struct object *root, const struct unicode_str *name,
  90. unsigned int attr, int manual, const struct security_descriptor *sd )
  91. {
  92. struct timer *timer;
  93. if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
  94. {
  95. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  96. {
  97. /* initialize it if it didn't already exist */
  98. timer->manual = manual;
  99. timer->signaled = 0;
  100. timer->when = 0;
  101. timer->period = 0;
  102. timer->timeout = NULL;
  103. timer->thread = NULL;
  104. timer->fast_sync = NULL;
  105. }
  106. }
  107. return timer;
  108. }
  109. /* callback on timer expiration */
  110. static void timer_callback( void *private )
  111. {
  112. struct timer *timer = (struct timer *)private;
  113. /* queue an APC */
  114. if (timer->thread)
  115. {
  116. apc_call_t data;
  117. assert (timer->callback);
  118. memset( &data, 0, sizeof(data) );
  119. data.type = APC_USER;
  120. data.user.func = timer->callback;
  121. data.user.args[0] = timer->arg;
  122. data.user.args[1] = (unsigned int)timer->when;
  123. data.user.args[2] = timer->when >> 32;
  124. if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
  125. {
  126. release_object( timer->thread );
  127. timer->thread = NULL;
  128. }
  129. }
  130. if (timer->period) /* schedule the next expiration */
  131. {
  132. if (timer->when > 0) timer->when = -monotonic_time;
  133. timer->when -= (abstime_t)timer->period * 10000;
  134. timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
  135. }
  136. else timer->timeout = NULL;
  137. /* wake up waiters */
  138. timer->signaled = 1;
  139. wake_up( &timer->obj, 0 );
  140. fast_set_event( timer->fast_sync );
  141. }
  142. /* cancel a running timer */
  143. static int cancel_timer( struct timer *timer )
  144. {
  145. int signaled = timer->signaled;
  146. if (timer->timeout)
  147. {
  148. remove_timeout_user( timer->timeout );
  149. timer->timeout = NULL;
  150. }
  151. if (timer->thread)
  152. {
  153. thread_cancel_apc( timer->thread, &timer->obj, APC_USER );
  154. release_object( timer->thread );
  155. timer->thread = NULL;
  156. }
  157. return signaled;
  158. }
  159. /* set the timer expiration and period */
  160. static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
  161. client_ptr_t callback, client_ptr_t arg )
  162. {
  163. int signaled = cancel_timer( timer );
  164. if (timer->manual)
  165. {
  166. period = 0; /* period doesn't make any sense for a manual timer */
  167. timer->signaled = 0;
  168. fast_reset_event( timer->fast_sync );
  169. }
  170. timer->when = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
  171. timer->period = period;
  172. timer->callback = callback;
  173. timer->arg = arg;
  174. if (callback) timer->thread = (struct thread *)grab_object( current );
  175. if (expire != TIMEOUT_INFINITE)
  176. timer->timeout = add_timeout_user( expire, timer_callback, timer );
  177. return signaled;
  178. }
  179. static void timer_dump( struct object *obj, int verbose )
  180. {
  181. struct timer *timer = (struct timer *)obj;
  182. timeout_t timeout = abstime_to_timeout( timer->when );
  183. assert( obj->ops == &timer_ops );
  184. fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
  185. timer->manual, get_timeout_str(timeout), timer->period );
  186. }
  187. static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
  188. {
  189. struct timer *timer = (struct timer *)obj;
  190. assert( obj->ops == &timer_ops );
  191. return timer->signaled;
  192. }
  193. static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
  194. {
  195. struct timer *timer = (struct timer *)obj;
  196. assert( obj->ops == &timer_ops );
  197. if (!timer->manual) timer->signaled = 0;
  198. }
  199. static struct fast_sync *timer_get_fast_sync( struct object *obj )
  200. {
  201. struct timer *timer = (struct timer *)obj;
  202. if (!timer->fast_sync)
  203. {
  204. enum fast_sync_type type = timer->manual ? FAST_SYNC_MANUAL_SERVER : FAST_SYNC_AUTO_SERVER;
  205. timer->fast_sync = fast_create_event( type, timer->signaled );
  206. }
  207. if (timer->fast_sync) grab_object( timer->fast_sync );
  208. return timer->fast_sync;
  209. }
  210. static void timer_destroy( struct object *obj )
  211. {
  212. struct timer *timer = (struct timer *)obj;
  213. assert( obj->ops == &timer_ops );
  214. if (timer->timeout) remove_timeout_user( timer->timeout );
  215. if (timer->thread) release_object( timer->thread );
  216. if (timer->fast_sync) release_object( timer->fast_sync );
  217. }
  218. /* create a timer */
  219. DECL_HANDLER(create_timer)
  220. {
  221. struct timer *timer;
  222. struct unicode_str name;
  223. struct object *root;
  224. const struct security_descriptor *sd;
  225. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  226. if (!objattr) return;
  227. if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
  228. {
  229. reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
  230. release_object( timer );
  231. }
  232. if (root) release_object( root );
  233. }
  234. /* open a handle to a timer */
  235. DECL_HANDLER(open_timer)
  236. {
  237. struct unicode_str name = get_req_unicode_str();
  238. reply->handle = open_object( current->process, req->rootdir, req->access,
  239. &timer_ops, &name, req->attributes );
  240. }
  241. /* set a waitable timer */
  242. DECL_HANDLER(set_timer)
  243. {
  244. struct timer *timer;
  245. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  246. TIMER_MODIFY_STATE, &timer_ops )))
  247. {
  248. reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
  249. release_object( timer );
  250. }
  251. }
  252. /* cancel a waitable timer */
  253. DECL_HANDLER(cancel_timer)
  254. {
  255. struct timer *timer;
  256. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  257. TIMER_MODIFY_STATE, &timer_ops )))
  258. {
  259. reply->signaled = cancel_timer( timer );
  260. release_object( timer );
  261. }
  262. }
  263. /* Get information on a waitable timer */
  264. DECL_HANDLER(get_timer_info)
  265. {
  266. struct timer *timer;
  267. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  268. TIMER_QUERY_STATE, &timer_ops )))
  269. {
  270. reply->when = timer->when;
  271. reply->signaled = timer->signaled;
  272. release_object( timer );
  273. }
  274. }