signal.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * signal.c
  3. *
  4. * Description:
  5. * Thread-aware signal functions.
  6. *
  7. * --------------------------------------------------------------------------
  8. *
  9. * Pthreads-win32 - POSIX Threads Library for Win32
  10. * Copyright(C) 1998 John E. Bossom
  11. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  12. *
  13. * Contact Email: [email protected]
  14. *
  15. * The current list of contributors is contained
  16. * in the file CONTRIBUTORS included with the source
  17. * code distribution. The list can also be seen at the
  18. * following World Wide Web location:
  19. * http://sources.redhat.com/pthreads-win32/contributors.html
  20. *
  21. * This library is free software; you can redistribute it and/or
  22. * modify it under the terms of the GNU Lesser General Public
  23. * License as published by the Free Software Foundation; either
  24. * version 2 of the License, or (at your option) any later version.
  25. *
  26. * This library is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  29. * Lesser General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Lesser General Public
  32. * License along with this library in the file COPYING.LIB;
  33. * if not, write to the Free Software Foundation, Inc.,
  34. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  35. */
  36. /*
  37. * Possible future strategy for implementing pthread_kill()
  38. * ========================================================
  39. *
  40. * Win32 does not implement signals.
  41. * Signals are simply software interrupts.
  42. * pthread_kill() asks the system to deliver a specified
  43. * signal (interrupt) to a specified thread in the same
  44. * process.
  45. * Signals are always asynchronous (no deferred signals).
  46. * Pthread-win32 has an async cancelation mechanism.
  47. * A similar system can be written to deliver signals
  48. * within the same process (on ix86 processors at least).
  49. *
  50. * Each thread maintains information about which
  51. * signals it will respond to. Handler routines
  52. * are set on a per-process basis - not per-thread.
  53. * When signalled, a thread will check it's sigmask
  54. * and, if the signal is not being ignored, call the
  55. * handler routine associated with the signal. The
  56. * thread must then (except for some signals) return to
  57. * the point where it was interrupted.
  58. *
  59. * Ideally the system itself would check the target thread's
  60. * mask before possibly needlessly bothering the thread
  61. * itself. This could be done by pthread_kill(), that is,
  62. * in the signaling thread since it has access to
  63. * all pthread_t structures. It could also retrieve
  64. * the handler routine address to minimise the target
  65. * threads response overhead. This may also simplify
  66. * serialisation of the access to the per-thread signal
  67. * structures.
  68. *
  69. * pthread_kill() eventually calls a routine similar to
  70. * ptw32_cancel_thread() which manipulates the target
  71. * threads processor context to cause the thread to
  72. * run the handler launcher routine. pthread_kill() must
  73. * save the target threads current context so that the
  74. * handler launcher routine can restore the context after
  75. * the signal handler has returned. Some handlers will not
  76. * return, eg. the default SIGKILL handler may simply
  77. * call pthread_exit().
  78. *
  79. * The current context is saved in the target threads
  80. * pthread_t structure.
  81. */
  82. #include "pthread.h"
  83. #include "implement.h"
  84. #if defined(HAVE_SIGSET_T)
  85. static void
  86. ptw32_signal_thread ()
  87. {
  88. }
  89. static void
  90. ptw32_signal_callhandler ()
  91. {
  92. }
  93. int
  94. pthread_sigmask (int how, sigset_t const *set, sigset_t * oset)
  95. {
  96. pthread_t thread = pthread_self ();
  97. if (thread.p == NULL)
  98. {
  99. return ENOENT;
  100. }
  101. /* Validate the `how' argument. */
  102. if (set != NULL)
  103. {
  104. switch (how)
  105. {
  106. case SIG_BLOCK:
  107. break;
  108. case SIG_UNBLOCK:
  109. break;
  110. case SIG_SETMASK:
  111. break;
  112. default:
  113. /* Invalid `how' argument. */
  114. return EINVAL;
  115. }
  116. }
  117. /* Copy the old mask before modifying it. */
  118. if (oset != NULL)
  119. {
  120. memcpy (oset, &(thread.p->sigmask), sizeof (sigset_t));
  121. }
  122. if (set != NULL)
  123. {
  124. unsigned int i;
  125. /* FIXME: this code assumes that sigmask is an even multiple of
  126. the size of a long integer. */
  127. unsigned long *src = (unsigned long const *) set;
  128. unsigned long *dest = (unsigned long *) &(thread.p->sigmask);
  129. switch (how)
  130. {
  131. case SIG_BLOCK:
  132. for (i = 0; i < (sizeof (sigset_t) / sizeof (unsigned long)); i++)
  133. {
  134. /* OR the bit field longword-wise. */
  135. *dest++ |= *src++;
  136. }
  137. break;
  138. case SIG_UNBLOCK:
  139. for (i = 0; i < (sizeof (sigset_t) / sizeof (unsigned long)); i++)
  140. {
  141. /* XOR the bitfield longword-wise. */
  142. *dest++ ^= *src++;
  143. }
  144. case SIG_SETMASK:
  145. /* Replace the whole sigmask. */
  146. memcpy (&(thread.p->sigmask), set, sizeof (sigset_t));
  147. break;
  148. }
  149. }
  150. return 0;
  151. }
  152. int
  153. sigwait (const sigset_t * set, int *sig)
  154. {
  155. /* This routine is a cancellation point */
  156. pthread_test_cancel();
  157. }
  158. int
  159. sigaction (int signum, const struct sigaction *act, struct sigaction *oldact)
  160. {
  161. }
  162. #endif /* HAVE_SIGSET_T */