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 "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. #include "esync.h"
  36. struct timer
  37. {
  38. struct object obj; /* object header */
  39. int manual; /* manual reset */
  40. int signaled; /* current signaled state */
  41. unsigned int period; /* timer period in ms */
  42. timeout_t when; /* next expiration */
  43. struct timeout_user *timeout; /* timeout user */
  44. struct thread *thread; /* thread that set the APC function */
  45. client_ptr_t callback; /* callback APC function */
  46. client_ptr_t arg; /* callback argument */
  47. int esync_fd; /* esync file descriptor */
  48. };
  49. static void timer_dump( struct object *obj, int verbose );
  50. static struct object_type *timer_get_type( struct object *obj );
  51. static int timer_signaled( struct object *obj, struct wait_queue_entry *entry );
  52. static int timer_get_esync_fd( struct object *obj, enum esync_type *type );
  53. static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry );
  54. static unsigned int timer_map_access( struct object *obj, unsigned int access );
  55. static void timer_destroy( struct object *obj );
  56. static const struct object_ops timer_ops =
  57. {
  58. sizeof(struct timer), /* size */
  59. timer_dump, /* dump */
  60. timer_get_type, /* get_type */
  61. add_queue, /* add_queue */
  62. remove_queue, /* remove_queue */
  63. timer_signaled, /* signaled */
  64. timer_get_esync_fd, /* get_esync_fd */
  65. timer_satisfied, /* satisfied */
  66. no_signal, /* signal */
  67. no_get_fd, /* get_fd */
  68. timer_map_access, /* map_access */
  69. default_get_sd, /* get_sd */
  70. default_set_sd, /* set_sd */
  71. no_lookup_name, /* lookup_name */
  72. directory_link_name, /* link_name */
  73. default_unlink_name, /* unlink_name */
  74. no_open_file, /* open_file */
  75. no_kernel_obj_list, /* get_kernel_obj_list */
  76. no_alloc_handle, /* alloc_handle */
  77. no_close_handle, /* close_handle */
  78. timer_destroy /* destroy */
  79. };
  80. /* create a timer object */
  81. static struct timer *create_timer( struct object *root, const struct unicode_str *name,
  82. unsigned int attr, int manual, const struct security_descriptor *sd )
  83. {
  84. struct timer *timer;
  85. if ((timer = create_named_object( root, &timer_ops, name, attr, sd )))
  86. {
  87. if (get_error() != STATUS_OBJECT_NAME_EXISTS)
  88. {
  89. /* initialize it if it didn't already exist */
  90. timer->manual = manual;
  91. timer->signaled = 0;
  92. timer->when = 0;
  93. timer->period = 0;
  94. timer->timeout = NULL;
  95. timer->thread = NULL;
  96. if (do_esync())
  97. timer->esync_fd = esync_create_fd( 0, 0 );
  98. }
  99. }
  100. return timer;
  101. }
  102. /* callback on timer expiration */
  103. static void timer_callback( void *private )
  104. {
  105. struct timer *timer = (struct timer *)private;
  106. /* queue an APC */
  107. if (timer->thread)
  108. {
  109. apc_call_t data;
  110. memset( &data, 0, sizeof(data) );
  111. if (timer->callback)
  112. {
  113. data.type = APC_TIMER;
  114. data.timer.func = timer->callback;
  115. data.timer.time = timer->when;
  116. data.timer.arg = timer->arg;
  117. }
  118. else data.type = APC_NONE; /* wake up only */
  119. if (!thread_queue_apc( NULL, timer->thread, &timer->obj, &data ))
  120. {
  121. release_object( timer->thread );
  122. timer->thread = NULL;
  123. }
  124. }
  125. if (timer->period) /* schedule the next expiration */
  126. {
  127. timer->when += (timeout_t)timer->period * 10000;
  128. timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
  129. }
  130. else timer->timeout = NULL;
  131. /* wake up waiters */
  132. timer->signaled = 1;
  133. wake_up( &timer->obj, 0 );
  134. }
  135. /* cancel a running timer */
  136. static int cancel_timer( struct timer *timer )
  137. {
  138. int signaled = timer->signaled;
  139. if (timer->timeout)
  140. {
  141. remove_timeout_user( timer->timeout );
  142. timer->timeout = NULL;
  143. }
  144. if (timer->thread)
  145. {
  146. thread_cancel_apc( timer->thread, &timer->obj, APC_TIMER );
  147. release_object( timer->thread );
  148. timer->thread = NULL;
  149. }
  150. return signaled;
  151. }
  152. /* set the timer expiration and period */
  153. static int set_timer( struct timer *timer, timeout_t expire, unsigned int period,
  154. client_ptr_t callback, client_ptr_t arg )
  155. {
  156. int signaled = cancel_timer( timer );
  157. if (timer->manual)
  158. {
  159. period = 0; /* period doesn't make any sense for a manual timer */
  160. timer->signaled = 0;
  161. if (do_esync())
  162. esync_clear( timer->esync_fd );
  163. }
  164. timer->when = (expire <= 0) ? current_time - expire : 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. timer->timeout = add_timeout_user( timer->when, timer_callback, timer );
  170. return signaled;
  171. }
  172. static void timer_dump( struct object *obj, int verbose )
  173. {
  174. struct timer *timer = (struct timer *)obj;
  175. assert( obj->ops == &timer_ops );
  176. fprintf( stderr, "Timer manual=%d when=%s period=%u\n",
  177. timer->manual, get_timeout_str(timer->when), timer->period );
  178. }
  179. static struct object_type *timer_get_type( struct object *obj )
  180. {
  181. static const struct unicode_str str = { type_Timer, sizeof(type_Timer) };
  182. return get_object_type( &str );
  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 int timer_get_esync_fd( struct object *obj, enum esync_type *type )
  191. {
  192. struct timer *timer = (struct timer *)obj;
  193. *type = timer->manual ? ESYNC_MANUAL_SERVER : ESYNC_AUTO_SERVER;
  194. return timer->esync_fd;
  195. }
  196. static void timer_satisfied( struct object *obj, struct wait_queue_entry *entry )
  197. {
  198. struct timer *timer = (struct timer *)obj;
  199. assert( obj->ops == &timer_ops );
  200. if (!timer->manual) timer->signaled = 0;
  201. }
  202. static unsigned int timer_map_access( struct object *obj, unsigned int access )
  203. {
  204. if (access & GENERIC_READ) access |= STANDARD_RIGHTS_READ | SYNCHRONIZE | TIMER_QUERY_STATE;
  205. if (access & GENERIC_WRITE) access |= STANDARD_RIGHTS_WRITE | TIMER_MODIFY_STATE;
  206. if (access & GENERIC_EXECUTE) access |= STANDARD_RIGHTS_EXECUTE;
  207. if (access & GENERIC_ALL) access |= TIMER_ALL_ACCESS;
  208. return access & ~(GENERIC_READ | GENERIC_WRITE | GENERIC_EXECUTE | GENERIC_ALL);
  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. }
  217. /* create a timer */
  218. DECL_HANDLER(create_timer)
  219. {
  220. struct timer *timer;
  221. struct unicode_str name;
  222. struct object *root;
  223. const struct security_descriptor *sd;
  224. const struct object_attributes *objattr = get_req_object_attributes( &sd, &name, &root );
  225. if (!objattr) return;
  226. if ((timer = create_timer( root, &name, objattr->attributes, req->manual, sd )))
  227. {
  228. reply->handle = alloc_handle( current->process, timer, req->access, objattr->attributes );
  229. release_object( timer );
  230. }
  231. if (root) release_object( root );
  232. }
  233. /* open a handle to a timer */
  234. DECL_HANDLER(open_timer)
  235. {
  236. struct unicode_str name = get_req_unicode_str();
  237. reply->handle = open_object( current->process, req->rootdir, req->access,
  238. &timer_ops, &name, req->attributes );
  239. }
  240. /* set a waitable timer */
  241. DECL_HANDLER(set_timer)
  242. {
  243. struct timer *timer;
  244. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  245. TIMER_MODIFY_STATE, &timer_ops )))
  246. {
  247. reply->signaled = set_timer( timer, req->expire, req->period, req->callback, req->arg );
  248. release_object( timer );
  249. }
  250. }
  251. /* cancel a waitable timer */
  252. DECL_HANDLER(cancel_timer)
  253. {
  254. struct timer *timer;
  255. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  256. TIMER_MODIFY_STATE, &timer_ops )))
  257. {
  258. reply->signaled = cancel_timer( timer );
  259. release_object( timer );
  260. }
  261. }
  262. /* Get information on a waitable timer */
  263. DECL_HANDLER(get_timer_info)
  264. {
  265. struct timer *timer;
  266. if ((timer = (struct timer *)get_handle_obj( current->process, req->handle,
  267. TIMER_QUERY_STATE, &timer_ops )))
  268. {
  269. reply->when = timer->when;
  270. reply->signaled = timer->signaled;
  271. release_object( timer );
  272. }
  273. }