timer.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. };
  58. static void timer_dump( struct object *obj, int verbose );
  59. static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
  60. static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
  61. static void timer_destroy( struct object *obj );
  62. static const struct object_ops timer_ops =
  63. {
  64. sizeof(struct timer), /* size */
  65. &timer_type, /* type */
  66. timer_dump, /* dump */
  67. add_queue, /* add_queue */
  68. remove_queue, /* remove_queue */
  69. timer_signaled, /* signaled */
  70. timer_satisfied, /* satisfied */
  71. no_signal, /* signal */
  72. no_get_fd, /* get_fd */
  73. default_map_access, /* map_access */
  74. default_get_sd, /* get_sd */
  75. default_set_sd, /* set_sd */
  76. default_get_full_name, /* get_full_name */
  77. no_lookup_name, /* lookup_name */
  78. directory_link_name, /* link_name */
  79. default_unlink_name, /* unlink_name */
  80. no_open_file, /* open_file */
  81. no_kernel_obj_list, /* get_kernel_obj_list */
  82. no_close_handle, /* close_handle */
  83. timer_destroy /* destroy */
  84. };
  85. /* create a timer object */
  86. static struct timer *create_timer( struct object *root, const struct unicode_str *name,
  87. unsigned int attr, int manual, const struct security_descriptor *sd )
  88. {
  89. struct timer *timer;
  90. if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
  91. {
  92. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  93. {
  94. /* initialize it if it didn't already exist */
  95. timer->manual = manual;
  96. timer->signaled = 0;
  97. timer->when = 0;
  98. timer->period = 0;
  99. timer->timeout = NULL;
  100. timer->thread = NULL;
  101. }
  102. }
  103. return timer;
  104. }
  105. /* callback on timer expiration */
  106. static void timer_callback( void *private )
  107. {
  108. struct timer *timer = (struct timer *)private;
  109. /* queue an APC */
  110. if (timer->thread)
  111. {
  112. apc_call_t data;
  113. assert (timer->callback);
  114. memset( &data, 0, sizeof(data) );
  115. data.type = APC_USER;
  116. data.user.func = timer->callback;
  117. data.user.args[0] = timer->arg;
  118. data.user.args[1] = (unsigned int)timer->when;
  119. data.user.args[2] = timer->when >> 32;
  120. if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
  121. {
  122. release_object( timer->thread );
  123. timer->thread = NULL;
  124. }
  125. }
  126. if (timer->period) /* schedule the next expiration */
  127. {
  128. if (timer->when > 0) timer->when = -monotonic_time;
  129. timer->when -= (abstime_t)timer->period * 10000;
  130. timer->timeout = add_timeout_user( abstime_to_timeout(timer->when), timer_callback, timer );
  131. }
  132. else timer->timeout = NULL;
  133. /* wake up waiters */
  134. timer->signaled = 1;
  135. wake_up( &timer->obj, 0 );
  136. }
  137. /* cancel a running timer */
  138. static int cancel_timer( struct timer *timer )
  139. {
  140. int signaled = timer->signaled;
  141. if (timer->timeout)
  142. {
  143. remove_timeout_user( timer->timeout );
  144. timer->timeout = NULL;
  145. }
  146. if (timer->thread)
  147. {
  148. thread_cancel_apc( timer->thread, &timer->obj, APC_USER );
  149. release_object( timer->thread );
  150. timer->thread = NULL;
  151. }
  152. return signaled;
  153. }
  154. /* set the timer expiration and period */
  155. static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
  156. client_ptr_t callback, client_ptr_t arg )
  157. {
  158. int signaled = cancel_timer( timer );
  159. if (timer->manual)
  160. {
  161. period = 0; /* period doesn't make any sense for a manual timer */
  162. timer->signaled = 0;
  163. }
  164. timer->when = (expire <= 0) ? expire - monotonic_time : max( expire, current_time );
  165. timer->period = period;
  166. timer->callback = callback;
  167. timer->arg = arg;
  168. if (callback) timer->thread = (struct thread *)grab_object( current );
  169. if (expire != TIMEOUT_INFINITE)
  170. timer->timeout = add_timeout_user( expire, timer_callback, timer );
  171. return signaled;
  172. }
  173. static void timer_dump( struct object *obj, int verbose )
  174. {
  175. struct timer *timer = (struct timer *)obj;
  176. timeout_t timeout = abstime_to_timeout( timer->when );
  177. assert( obj->ops == &timer_ops );
  178. fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
  179. timer->manual, get_timeout_str(timeout), timer->period );
  180. }
  181. static int timer_signaled( struct object *obj, struct wait_queue_entry *entry )
  182. {
  183. struct timer *timer = (struct timer *)obj;
  184. assert( obj->ops == &timer_ops );
  185. return timer->signaled;
  186. }
  187. static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
  188. {
  189. struct timer *timer = (struct timer *)obj;
  190. assert( obj->ops == &timer_ops );
  191. if (!timer->manual) timer->signaled = 0;
  192. }
  193. static void timer_destroy( struct object *obj )
  194. {
  195. struct timer *timer = (struct timer *)obj;
  196. assert( obj->ops == &timer_ops );
  197. if (timer->timeout) remove_timeout_user( timer->timeout );
  198. if (timer->thread) release_object( timer->thread );
  199. }
  200. /* create a timer */
  201. DECL_HANDLER(create_timer)
  202. {
  203. struct timer *timer;
  204. struct unicode_str name;
  205. struct object *root;
  206. const struct security_descriptor *sd;
  207. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  208. if (!objattr) return;
  209. if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
  210. {
  211. reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
  212. release_object( timer );
  213. }
  214. if (root) release_object( root );
  215. }
  216. /* open a handle to a timer */
  217. DECL_HANDLER(open_timer)
  218. {
  219. struct unicode_str name = get_req_unicode_str();
  220. reply->handle = open_object( current->process, req->rootdir, req->access,
  221. &timer_ops, &name, req->attributes );
  222. }
  223. /* set a waitable timer */
  224. DECL_HANDLER(set_timer)
  225. {
  226. struct timer *timer;
  227. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  228. TIMER_MODIFY_STATE, &timer_ops )))
  229. {
  230. reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
  231. release_object( timer );
  232. }
  233. }
  234. /* cancel a waitable timer */
  235. DECL_HANDLER(cancel_timer)
  236. {
  237. struct timer *timer;
  238. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  239. TIMER_MODIFY_STATE, &timer_ops )))
  240. {
  241. reply->signaled = cancel_timer( timer );
  242. release_object( timer );
  243. }
  244. }
  245. /* Get information on a waitable timer */
  246. DECL_HANDLER(get_timer_info)
  247. {
  248. struct timer *timer;
  249. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  250. TIMER_QUERY_STATE, &timer_ops )))
  251. {
  252. reply->when = timer->when;
  253. reply->signaled = timer->signaled;
  254. release_object( timer );
  255. }
  256. }