implemented Setup.hs to build boehm cpp libs and install them;
[hs-boehmgc.git] / gc-7.2 / tests / initsecondarythread.c
1 /*
2  * Copyright (C) 2011 Ludovic Courtes <ludo@gnu.org>
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
14 /* Make sure 'GC_INIT' can be called from threads other than the initial
15  * thread.
16  */
17
18 #ifdef HAVE_CONFIG_H
19 # include "private/config.h"
20 #endif
21
22 #ifndef GC_THREADS
23 # define GC_THREADS
24 #endif
25
26 #define GC_NO_THREAD_REDIRECTS 1
27                 /* Do not redirect thread creation and join calls.      */
28
29 #include "gc.h"
30
31 #ifdef GC_PTHREADS
32 # include <pthread.h>
33 #else
34 # include <windows.h>
35 #endif
36
37 #include <stdlib.h>
38 #include <stdio.h>
39
40 #ifdef GC_PTHREADS
41   static void *thread(void *arg)
42 #else
43   static DWORD WINAPI thread(LPVOID arg)
44 #endif
45 {
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
54 }
55
56 #include "private/gcconfig.h"
57
58 int main(void)
59 {
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       printf("Thread creation failed %d\n", code);
80       return 1;
81     }
82     if ((code = pthread_join (t, NULL)) != 0) {
83       printf("Thread join failed %d\n", code);
84       return 1;
85     }
86 # else
87     t = CreateThread(NULL, 0, thread, 0, 0, &thread_id);
88     if (t == NULL) {
89       printf("Thread creation failed %d\n", (int)GetLastError());
90       return 1;
91     }
92     if (WaitForSingleObject(t, INFINITE) != WAIT_OBJECT_0) {
93       printf("Thread join failed %d\n", (int)GetLastError());
94       CloseHandle(t);
95       return 1;
96     }
97     CloseHandle(t);
98 # endif
99   return 0;
100 }