Import boehm-gc snapshot, taken from
[official-gcc.git] / boehm-gc / tests / initsecondarythread.c
blob4a13ca95f55b927ef77b73191e47d2921c95f501
1 /*
2 * Copyright (C) 2011 Ludovic Courtes
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
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.
14 /* Make sure 'GC_INIT' can be called from threads other than the initial
15 * thread.
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
22 #ifndef GC_THREADS
23 # define GC_THREADS
24 #endif
26 #define GC_NO_THREAD_REDIRECTS 1
27 /* Do not redirect thread creation and join calls. */
29 #include "gc.h"
31 #ifdef GC_PTHREADS
32 # include <pthread.h>
33 #else
34 # include <windows.h>
35 #endif
37 #include <stdlib.h>
38 #include <stdio.h>
40 #ifdef GC_PTHREADS
41 static void *thread(void *arg)
42 #else
43 static DWORD WINAPI thread(LPVOID arg)
44 #endif
46 GC_INIT();
47 (void)GC_MALLOC(123);
48 (void)GC_MALLOC(12345);
49 # ifdef GC_PTHREADS
50 return arg;
51 # else
52 return (DWORD)(GC_word)arg;
53 # endif
56 #include "private/gcconfig.h"
58 int main(void)
60 # ifdef GC_PTHREADS
61 int code;
62 pthread_t t;
63 # else
64 HANDLE t;
65 DWORD thread_id;
66 # endif
67 # if !(defined(BEOS) || defined(MSWIN32) || defined(MSWINCE) \
68 || defined(CYGWIN32) || defined(GC_OPENBSD_THREADS) \
69 || (defined(DARWIN) && !defined(NO_PTHREAD_GET_STACKADDR_NP)) \
70 || (defined(LINUX) && !defined(NACL)) \
71 || (defined(GC_SOLARIS_THREADS) && !defined(_STRICT_STDC)) \
72 || (!defined(STACKBOTTOM) && (defined(HEURISTIC1) \
73 || (!defined(LINUX_STACKBOTTOM) && !defined(FREEBSD_STACKBOTTOM)))))
74 /* GC_INIT() must be called from main thread only. */
75 GC_INIT();
76 # endif
77 # ifdef GC_PTHREADS
78 if ((code = pthread_create (&t, NULL, thread, NULL)) != 0) {
79 fprintf(stderr, "Thread creation failed %d\n", code);
80 return 1;
82 if ((code = pthread_join (t, NULL)) != 0) {
83 fprintf(stderr, "Thread join failed %d\n", code);
84 return 1;
86 # else
87 t = CreateThread(NULL, 0, thread, 0, 0, &thread_id);
88 if (t == NULL) {
89 fprintf(stderr, "Thread creation failed %d\n", (int)GetLastError());
90 return 1;
92 if (WaitForSingleObject(t, INFINITE) != WAIT_OBJECT_0) {
93 fprintf(stderr, "Thread join failed %d\n", (int)GetLastError());
94 CloseHandle(t);
95 return 1;
97 CloseHandle(t);
98 # endif
99 return 0;