/src/win/async.c

http://github.com/joyent/libuv · C · 99 lines · 53 code · 23 blank · 23 comment · 9 complexity · d290f5b85a62edebe39050ccf47513a2 MD5 · raw file

  1. /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to
  5. * deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  7. * sell copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  18. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  19. * IN THE SOFTWARE.
  20. */
  21. #include <assert.h>
  22. #include "uv.h"
  23. #include "internal.h"
  24. #include "atomicops-inl.h"
  25. #include "handle-inl.h"
  26. #include "req-inl.h"
  27. void uv_async_endgame(uv_loop_t* loop, uv_async_t* handle) {
  28. if (handle->flags & UV__HANDLE_CLOSING &&
  29. !handle->async_sent) {
  30. assert(!(handle->flags & UV_HANDLE_CLOSED));
  31. uv__handle_close(handle);
  32. }
  33. }
  34. int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
  35. uv_req_t* req;
  36. uv__handle_init(loop, (uv_handle_t*) handle, UV_ASYNC);
  37. handle->async_sent = 0;
  38. handle->async_cb = async_cb;
  39. req = &handle->async_req;
  40. uv_req_init(loop, req);
  41. req->type = UV_WAKEUP;
  42. req->data = handle;
  43. uv__handle_start(handle);
  44. return 0;
  45. }
  46. void uv_async_close(uv_loop_t* loop, uv_async_t* handle) {
  47. if (!((uv_async_t*)handle)->async_sent) {
  48. uv_want_endgame(loop, (uv_handle_t*) handle);
  49. }
  50. uv__handle_closing(handle);
  51. }
  52. int uv_async_send(uv_async_t* handle) {
  53. uv_loop_t* loop = handle->loop;
  54. if (handle->type != UV_ASYNC) {
  55. /* Can't set errno because that's not thread-safe. */
  56. return -1;
  57. }
  58. /* The user should make sure never to call uv_async_send to a closing */
  59. /* or closed handle. */
  60. assert(!(handle->flags & UV__HANDLE_CLOSING));
  61. if (!uv__atomic_exchange_set(&handle->async_sent)) {
  62. POST_COMPLETION_FOR_REQ(loop, &handle->async_req);
  63. }
  64. return 0;
  65. }
  66. void uv_process_async_wakeup_req(uv_loop_t* loop, uv_async_t* handle,
  67. uv_req_t* req) {
  68. assert(handle->type == UV_ASYNC);
  69. assert(req->type == UV_WAKEUP);
  70. handle->async_sent = 0;
  71. if (!(handle->flags & UV__HANDLE_CLOSING)) {
  72. handle->async_cb((uv_async_t*) handle, 0);
  73. } else {
  74. uv_want_endgame(loop, (uv_handle_t*)handle);
  75. }
  76. }