timer.c 9.2 KB

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