nx_block.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /****************************************************************************
  2. * libs/libnx/nxmu/nx_block.c
  3. *
  4. * Copyright (C) 2012-2013 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <errno.h>
  40. #include <debug.h>
  41. #include <nuttx/nx/nx.h>
  42. #include <nuttx/nx/nxbe.h>
  43. #include <nuttx/nx/nxmu.h>
  44. /****************************************************************************
  45. * Public Functions
  46. ****************************************************************************/
  47. /****************************************************************************
  48. * Name: nx_block
  49. *
  50. * Description:
  51. * This is callback will do to things: (1) any queue a 'blocked' callback
  52. * to the window and then (2) block any further window messaging.
  53. *
  54. * The 'blocked' callback is the response from nx_block (or nxtk_block).
  55. * Those blocking interfaces are used to assure that no further messages are
  56. * are directed to the window. Receipt of the blocked callback signifies
  57. * that (1) there are no further pending callbacks and (2) that the
  58. * window is now 'defunct' and will receive no further callbacks.
  59. *
  60. * This callback supports coordinated destruction of a window in multi-
  61. * user mode. In multi-use mode, the client window logic must stay
  62. * intact until all of the queued callbacks are processed. Then the
  63. * window may be safely closed. Closing the window prior with pending
  64. * callbacks can lead to bad behavior when the callback is executed.
  65. *
  66. * Input Parameters:
  67. * wnd - The window to be blocked
  68. * arg - An argument that will accompany the block messages (This is arg2
  69. * in the blocked callback).
  70. *
  71. * Returned Value:
  72. * OK on success; ERROR on failure with errno set appropriately
  73. *
  74. ****************************************************************************/
  75. int nx_block(NXWINDOW hwnd, FAR void *arg)
  76. {
  77. FAR struct nxbe_window_s *wnd = (FAR struct nxbe_window_s *)hwnd;
  78. struct nxsvrmsg_blocked_s outmsg;
  79. int ret = OK;
  80. #ifdef CONFIG_DEBUG_FEATURES
  81. if (!hwnd)
  82. {
  83. set_errno(EINVAL);
  84. return ERROR;
  85. }
  86. #endif
  87. /* Ignore additional attempts to block messages (no errors reported) */
  88. if (!NXBE_ISBLOCKED(wnd))
  89. {
  90. /* Mark the window as blocked. This will stop all messages to the window
  91. * (EXCEPT the NX_SVRMSG_BLOCKED). Blocking the messages before sending the
  92. * blocked message is awkward but assures that no other messages sneak into
  93. * the message queue before we can set the blocked state.
  94. */
  95. NXBE_SETBLOCKED(wnd);
  96. /* Send the message inicating that the window is blocked (and because of
  97. * queue also that there are no additional queue messages for the window)
  98. */
  99. outmsg.msgid = NX_SVRMSG_BLOCKED;
  100. outmsg.wnd = wnd;
  101. outmsg.arg = arg;
  102. /* Send the window message via nxmu_sendserver (vs. nxmu_sendwindow) so
  103. * that it will not be blocked.
  104. */
  105. ret = nxmu_sendserver(wnd->conn, &outmsg, sizeof(struct nxsvrmsg_blocked_s));
  106. }
  107. return ret;
  108. }