thread-test.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * source URL:
  3. * http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=67838
  4. */
  5. /*
  6. * Author: Petter Reinholdtsen <pere@hungry.com>
  7. * Date: 2002-02-13
  8. * Lisence: GPL
  9. *
  10. * Small test program to demonstrate efence crashing on multithreaded
  11. * programs.
  12. * Link it like this, and it crashes with segfault.
  13. * cc -o efence-thread-test efence-thread-test.c -lpthread -lefence
  14. * Remove the -lefence, and it work as it should.
  15. * The output from gdb when it fails:
  16. *
  17. * Program received signal SIGSEGV, Segmentation fault.
  18. * [Switching to Thread 1026 (LWP 1189)]
  19. * 0x400382c7 in memalign () from /usr/lib/libefence.so.0
  20. * (gdb) bt
  21. * #0 0x400382c7 in memalign () from /usr/lib/libefence.so.0
  22. * #1 0x4003873c in malloc () from /usr/lib/libefence.so.0
  23. * #2 0x400a0811 in fopen () from /lib/libc.so.6
  24. * #3 0x080488e8 in thread_func ()
  25. * #4 0x40027f0a in pthread_start_thread () from /lib/libpthread.so.0
  26. * #5 0x40027f51 in pthread_start_thread_event () from /lib/libpthread.so.0
  27. */
  28. /* check for pthread library */
  29. #if (!defined(WIN32) || defined(__CYGWIN__))
  30. #define HAVE_PTHREADS 1
  31. #else
  32. #define HAVE_PTHREADS 0
  33. #endif
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #if HAVE_PTHREADS
  37. #include <pthread.h>
  38. #include "../duma.h"
  39. static pthread_mutex_t mutex;
  40. static volatile int threads_left = 2;
  41. static void* thread_func(void *arg)
  42. {
  43. int i = 2000;
  44. char *name = (char*)arg;
  45. while (--i)
  46. {
  47. if (pthread_mutex_lock(&mutex))
  48. {
  49. fprintf(stderr, "error: %s failed to lock mutex.\n", name);
  50. exit(1);
  51. }
  52. printf ("%s : %d\n", name, i);
  53. if (pthread_mutex_unlock(&mutex))
  54. {
  55. fprintf(stderr, "error: %s failed to unlock mutex.\n", name);
  56. exit(1);
  57. }
  58. {
  59. /* Try to trigger efence error */
  60. FILE *fp = fopen("/etc/resolv.conf", "r");
  61. if (NULL != fp)
  62. {
  63. char buf[1024];
  64. fread(buf, sizeof(buf), 1, fp);
  65. fclose(fp);
  66. }
  67. }
  68. }
  69. --threads_left;
  70. return NULL;
  71. }
  72. static void*
  73. idle_func(void* arg)
  74. {
  75. (void)arg;
  76. while (threads_left) ;
  77. /* NOTREACHED */
  78. return NULL;
  79. }
  80. int main(int argc, char **argv)
  81. {
  82. pthread_t hello_thread, goodbye_thread;
  83. (void)argc;
  84. (void)argv;
  85. pthread_mutex_init(&mutex, NULL);
  86. if (pthread_create(&hello_thread, NULL, thread_func, (void*)"hello"))
  87. {
  88. fprintf(stderr, "Failed to create hello thread\n");
  89. exit(1);
  90. }
  91. if (pthread_create(&goodbye_thread, NULL, thread_func, (void*)"goodbye"))
  92. {
  93. fprintf(stderr, "Failed to create goodbye thread\n");
  94. exit(1);
  95. }
  96. idle_func(NULL);
  97. pthread_mutex_destroy(&mutex);
  98. /* NOTREACHED */
  99. return 0;
  100. }
  101. #else
  102. int main(int argc, char **argv)
  103. {
  104. (void)argc;
  105. (void)argv;
  106. printf("Test not implemented for Win32 and Mingw\n");
  107. return 0;
  108. }
  109. #endif