exception3.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /*
  2. * File: exception3.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Test Synopsis: Test running of user supplied terminate() function.
  37. *
  38. * Test Method (Validation or Falsification):
  39. * -
  40. *
  41. * Requirements Tested:
  42. * -
  43. *
  44. * Features Tested:
  45. * -
  46. *
  47. * Cases Tested:
  48. * -
  49. *
  50. * Description:
  51. * -
  52. *
  53. * Environment:
  54. * -
  55. *
  56. * Input:
  57. * - None.
  58. *
  59. * Output:
  60. * - File name, Line number, and failed expression on failure.
  61. * - No output on success.
  62. *
  63. * Assumptions:
  64. * - have working pthread_create, pthread_self, pthread_mutex_lock/unlock
  65. * pthread_testcancel, pthread_cancel
  66. *
  67. * Pass Criteria:
  68. * - Process returns zero exit status.
  69. *
  70. * Fail Criteria:
  71. * - Process returns non-zero exit status.
  72. */
  73. #include "test.h"
  74. #if defined(__cplusplus)
  75. #if defined(_MSC_VER)
  76. # include <eh.h>
  77. #else
  78. # if defined(__GNUC__) && __GNUC__ < 3
  79. # include <new.h>
  80. # else
  81. # include <new>
  82. using std::set_terminate;
  83. # endif
  84. #endif
  85. /*
  86. * Create NUMTHREADS threads in addition to the Main thread.
  87. */
  88. enum {
  89. NUMTHREADS = 1
  90. };
  91. int caught = 0;
  92. pthread_mutex_t caughtLock;
  93. void
  94. terminateFunction ()
  95. {
  96. assert(pthread_mutex_lock(&caughtLock) == 0);
  97. caught++;
  98. #if 0
  99. {
  100. FILE * fp = fopen("pthread.log", "a");
  101. fprintf(fp, "Caught = %d\n", caught);
  102. fclose(fp);
  103. }
  104. #endif
  105. assert(pthread_mutex_unlock(&caughtLock) == 0);
  106. /*
  107. * Notes from the MSVC++ manual:
  108. * 1) A term_func() should call exit(), otherwise
  109. * abort() will be called on return to the caller.
  110. * abort() raises SIGABRT. The default signal handler
  111. * for all signals terminates the calling program with
  112. * exit code 3.
  113. * 2) A term_func() must not throw an exception. Dev: Therefore
  114. * term_func() should not call pthread_exit() if an
  115. * exception-using version of pthreads-win32 library
  116. * is being used (i.e. either pthreadVCE or pthreadVSE).
  117. */
  118. exit(0);
  119. }
  120. void *
  121. exceptionedThread(void * arg)
  122. {
  123. int dummy = 0x1;
  124. (void) set_terminate(&terminateFunction);
  125. throw dummy;
  126. return (void *) 0;
  127. }
  128. int
  129. main()
  130. {
  131. int i;
  132. pthread_t mt;
  133. pthread_t et[NUMTHREADS];
  134. pthread_mutexattr_t ma;
  135. assert((mt = pthread_self()).p != NULL);
  136. printf("See the notes inside of exception3.c re term_funcs.\n");
  137. assert(pthread_mutexattr_init(&ma) == 0);
  138. assert(pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_ERRORCHECK) == 0);
  139. assert(pthread_mutex_init(&caughtLock, &ma) == 0);
  140. assert(pthread_mutexattr_destroy(&ma) == 0);
  141. for (i = 0; i < NUMTHREADS; i++)
  142. {
  143. assert(pthread_create(&et[i], NULL, exceptionedThread, NULL) == 0);
  144. }
  145. Sleep(NUMTHREADS * 100);
  146. assert(caught == NUMTHREADS);
  147. /*
  148. * Success.
  149. */
  150. return 0;
  151. }
  152. #else /* defined(__cplusplus) */
  153. #include <stdio.h>
  154. int
  155. main()
  156. {
  157. fprintf(stderr, "Test N/A for this compiler environment.\n");
  158. return 0;
  159. }
  160. #endif /* defined(__cplusplus) */