initsecondarythread.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. * Copyright (C) 2011 Ludovic Courtes
  3. *
  4. * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
  6. *
  7. * Permission is hereby granted to use or copy this program
  8. * for any purpose, provided the above notices are retained on all copies.
  9. * Permission to modify the code and to distribute modified code is granted,
  10. * provided the above notices are retained, and a notice that the code was
  11. * modified is included with the above copyright notice.
  12. */
  13. /* Make sure 'GC_INIT' can be called from threads other than the initial
  14. * thread.
  15. */
  16. #ifdef HAVE_CONFIG_H
  17. # include "config.h"
  18. #endif
  19. #ifndef GC_THREADS
  20. # define GC_THREADS
  21. #endif
  22. #define GC_NO_THREAD_REDIRECTS 1
  23. /* Do not redirect thread creation and join calls. */
  24. #include "gc.h"
  25. #ifdef GC_PTHREADS
  26. # include <pthread.h>
  27. #else
  28. # include <windows.h>
  29. #endif
  30. #include <stdlib.h>
  31. #include <stdio.h>
  32. #ifdef GC_PTHREADS
  33. static void *thread(void *arg)
  34. #else
  35. static DWORD WINAPI thread(LPVOID arg)
  36. #endif
  37. {
  38. GC_INIT();
  39. (void)GC_MALLOC(123);
  40. (void)GC_MALLOC(12345);
  41. # ifdef GC_PTHREADS
  42. return arg;
  43. # else
  44. return (DWORD)(GC_word)arg;
  45. # endif
  46. }
  47. #include "private/gcconfig.h"
  48. int main(void)
  49. {
  50. # ifdef GC_PTHREADS
  51. int code;
  52. pthread_t t;
  53. # ifdef LINT2
  54. t = pthread_self(); /* explicitly initialize to some value */
  55. # endif
  56. # else
  57. HANDLE t;
  58. DWORD thread_id;
  59. # endif
  60. # if !(defined(BEOS) || defined(MSWIN32) || defined(MSWINCE) \
  61. || defined(CYGWIN32) || defined(GC_OPENBSD_UTHREADS) \
  62. || (defined(DARWIN) && !defined(NO_PTHREAD_GET_STACKADDR_NP)) \
  63. || ((defined(FREEBSD) || defined(LINUX) || defined(NETBSD) \
  64. || defined(HOST_ANDROID)) && !defined(NO_PTHREAD_GETATTR_NP) \
  65. && !defined(NO_PTHREAD_ATTR_GET_NP)) \
  66. || (defined(GC_SOLARIS_THREADS) && !defined(_STRICT_STDC)) \
  67. || (!defined(STACKBOTTOM) && (defined(HEURISTIC1) \
  68. || (!defined(LINUX_STACKBOTTOM) && !defined(FREEBSD_STACKBOTTOM)))))
  69. /* GC_INIT() must be called from main thread only. */
  70. GC_INIT();
  71. # endif
  72. (void)GC_get_parallel(); /* linking fails if no threads support */
  73. # ifdef GC_PTHREADS
  74. if ((code = pthread_create (&t, NULL, thread, NULL)) != 0) {
  75. fprintf(stderr, "Thread creation failed %d\n", code);
  76. return 1;
  77. }
  78. if ((code = pthread_join (t, NULL)) != 0) {
  79. fprintf(stderr, "Thread join failed %d\n", code);
  80. return 1;
  81. }
  82. # else
  83. t = CreateThread(NULL, 0, thread, 0, 0, &thread_id);
  84. if (t == NULL) {
  85. fprintf(stderr, "Thread creation failed %d\n", (int)GetLastError());
  86. return 1;
  87. }
  88. if (WaitForSingleObject(t, INFINITE) != WAIT_OBJECT_0) {
  89. fprintf(stderr, "Thread join failed %d\n", (int)GetLastError());
  90. CloseHandle(t);
  91. return 1;
  92. }
  93. CloseHandle(t);
  94. # endif
  95. return 0;
  96. }