BThreadSignal.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @file BThreadSignal.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <errno.h>
  30. #include <unistd.h>
  31. #include <misc/debug.h>
  32. #include <misc/nonblocking.h>
  33. #include <base/BLog.h>
  34. #include "BThreadSignal.h"
  35. #include <generated/blog_channel_BThreadSignal.h>
  36. static void bfd_handler (void *user, int events)
  37. {
  38. BThreadSignal *o = user;
  39. DebugObject_Access(&o->d_obj);
  40. char byte;
  41. ssize_t res = read(o->pipe[0], &byte, sizeof(byte));
  42. if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  43. return;
  44. }
  45. if (res < 0) {
  46. BLog(BLOG_ERROR, "read failed");
  47. return;
  48. }
  49. if (res != sizeof(byte)) {
  50. BLog(BLOG_ERROR, "bad read");
  51. return;
  52. }
  53. o->handler(o);
  54. }
  55. int BThreadSignal_Init (BThreadSignal *o, BReactor *reactor, BThreadSignal_handler handler)
  56. {
  57. o->reactor = reactor;
  58. o->handler = handler;
  59. if (pipe(o->pipe) < 0) {
  60. BLog(BLOG_ERROR, "pipe failed");
  61. goto fail0;
  62. }
  63. if (!badvpn_set_nonblocking(o->pipe[0]) || !badvpn_set_nonblocking(o->pipe[1])) {
  64. BLog(BLOG_ERROR, "badvpn_set_nonblocking failed");
  65. goto fail1;
  66. }
  67. BFileDescriptor_Init(&o->bfd, o->pipe[0], bfd_handler, o);
  68. if (!BReactor_AddFileDescriptor(o->reactor, &o->bfd)) {
  69. BLog(BLOG_ERROR, "BReactor_AddFileDescriptor failed");
  70. goto fail1;
  71. }
  72. BReactor_SetFileDescriptorEvents(o->reactor, &o->bfd, BREACTOR_READ);
  73. DebugObject_Init(&o->d_obj);
  74. return 1;
  75. fail1:
  76. if (close(o->pipe[1]) < 0) {
  77. BLog(BLOG_ERROR, "close failed");
  78. }
  79. if (close(o->pipe[0]) < 0) {
  80. BLog(BLOG_ERROR, "close failed");
  81. }
  82. fail0:
  83. return 0;
  84. }
  85. void BThreadSignal_Free (BThreadSignal *o)
  86. {
  87. DebugObject_Free(&o->d_obj);
  88. BReactor_RemoveFileDescriptor(o->reactor, &o->bfd);
  89. if (close(o->pipe[1]) < 0) {
  90. BLog(BLOG_ERROR, "close failed");
  91. }
  92. if (close(o->pipe[0]) < 0) {
  93. BLog(BLOG_ERROR, "close failed");
  94. }
  95. }
  96. int BThreadSignal_Thread_Signal (BThreadSignal *o)
  97. {
  98. DebugObject_Access(&o->d_obj);
  99. char byte = 0;
  100. ssize_t res = write(o->pipe[1], &byte, sizeof(byte));
  101. if (res < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
  102. return 0;
  103. }
  104. if (res < 0) {
  105. BLog(BLOG_ERROR, "write failed");
  106. } else if (res != sizeof(byte)) {
  107. BLog(BLOG_ERROR, "bad write");
  108. }
  109. return 1;
  110. }