test.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * test.h
  3. *
  4. * Useful definitions and declarations for tests.
  5. *
  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. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  35. *
  36. */
  37. #ifndef _PTHREAD_TEST_H_
  38. #define _PTHREAD_TEST_H_
  39. #include "pthread.h"
  40. #include "sched.h"
  41. #include "semaphore.h"
  42. #include <windows.h>
  43. #include <stdio.h>
  44. #define PTW32_THREAD_NULL_ID {NULL,0}
  45. /*
  46. * Some non-thread POSIX API substitutes
  47. */
  48. #define rand_r( _seed ) \
  49. ( _seed == _seed? rand() : rand() )
  50. #if defined(__MINGW32__)
  51. #include <stdint.h>
  52. #elif defined(__BORLANDC__)
  53. #define int64_t ULONGLONG
  54. #else
  55. #define int64_t _int64
  56. #endif
  57. #if defined(_MSC_VER) && _MSC_VER >= 1400
  58. # define PTW32_FTIME(x) _ftime64_s(x)
  59. # define PTW32_STRUCT_TIMEB struct __timeb64
  60. #elif ( defined(_MSC_VER) && _MSC_VER >= 1300 ) || \
  61. ( defined(__MINGW32__) && __MSVCRT_VERSION__ >= 0x0601 )
  62. # define PTW32_FTIME(x) _ftime64(x)
  63. # define PTW32_STRUCT_TIMEB struct __timeb64
  64. #else
  65. # define PTW32_FTIME(x) _ftime(x)
  66. # define PTW32_STRUCT_TIMEB struct _timeb
  67. #endif
  68. const char * error_string[] = {
  69. "ZERO_or_EOK",
  70. "EPERM",
  71. "ENOFILE_or_ENOENT",
  72. "ESRCH",
  73. "EINTR",
  74. "EIO",
  75. "ENXIO",
  76. "E2BIG",
  77. "ENOEXEC",
  78. "EBADF",
  79. "ECHILD",
  80. "EAGAIN",
  81. "ENOMEM",
  82. "EACCES",
  83. "EFAULT",
  84. "UNKNOWN_15",
  85. "EBUSY",
  86. "EEXIST",
  87. "EXDEV",
  88. "ENODEV",
  89. "ENOTDIR",
  90. "EISDIR",
  91. "EINVAL",
  92. "ENFILE",
  93. "EMFILE",
  94. "ENOTTY",
  95. "UNKNOWN_26",
  96. "EFBIG",
  97. "ENOSPC",
  98. "ESPIPE",
  99. "EROFS",
  100. "EMLINK",
  101. "EPIPE",
  102. "EDOM",
  103. "ERANGE",
  104. "UNKNOWN_35",
  105. "EDEADLOCK_or_EDEADLK",
  106. "UNKNOWN_37",
  107. "ENAMETOOLONG",
  108. "ENOLCK",
  109. "ENOSYS",
  110. "ENOTEMPTY",
  111. "EILSEQ",
  112. "EOWNERDEAD",
  113. "ENOTRECOVERABLE"
  114. };
  115. /*
  116. * The Mingw32 assert macro calls the CRTDLL _assert function
  117. * which pops up a dialog. We want to run in batch mode so
  118. * we define our own assert macro.
  119. */
  120. #ifdef assert
  121. # undef assert
  122. #endif
  123. #ifndef ASSERT_TRACE
  124. # define ASSERT_TRACE 0
  125. #else
  126. # undef ASSERT_TRACE
  127. # define ASSERT_TRACE 1
  128. #endif
  129. # define assert(e) \
  130. ((e) ? ((ASSERT_TRACE) ? fprintf(stderr, \
  131. "Assertion succeeded: (%s), file %s, line %d\n", \
  132. #e, __FILE__, (int) __LINE__), \
  133. fflush(stderr) : \
  134. 0) : \
  135. (fprintf(stderr, "Assertion failed: (%s), file %s, line %d\n", \
  136. #e, __FILE__, (int) __LINE__), exit(1), 0))
  137. int assertE;
  138. # define assert_e(e, o, r) \
  139. (((assertE = e) o (r)) ? ((ASSERT_TRACE) ? fprintf(stderr, \
  140. "Assertion succeeded: (%s), file %s, line %d\n", \
  141. #e, __FILE__, (int) __LINE__), \
  142. fflush(stderr) : \
  143. 0) : \
  144. (fprintf(stderr, "Assertion failed: (%s %s %s), file %s, line %d, error %s\n", \
  145. #e,#o,#r, __FILE__, (int) __LINE__, error_string[assertE]), exit(1), 0))
  146. #endif
  147. # define BEGIN_MUTEX_STALLED_ROBUST(mxAttr) \
  148. for(;;) \
  149. { \
  150. static int _i=0; \
  151. static int _robust; \
  152. pthread_mutexattr_getrobust(&(mxAttr), &_robust);
  153. # define END_MUTEX_STALLED_ROBUST(mxAttr) \
  154. printf("Pass %s\n", _robust==PTHREAD_MUTEX_ROBUST?"Robust":"Non-robust"); \
  155. if (++_i > 1) \
  156. break; \
  157. else \
  158. { \
  159. pthread_mutexattr_t *pma, *pmaEnd; \
  160. for(pma = &(mxAttr), pmaEnd = pma + sizeof(mxAttr)/sizeof(pthread_mutexattr_t); \
  161. pma < pmaEnd; \
  162. pthread_mutexattr_setrobust(pma++, PTHREAD_MUTEX_ROBUST)); \
  163. } \
  164. }
  165. # define IS_ROBUST (_robust==PTHREAD_MUTEX_ROBUST)