Debian Codegraph @

external/bdwgc/tests/initsecondarythread.c

raw
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
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 "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
64 # ifdef LINT2
65 t = pthread_self(); /* explicitly initialize to some value */
66 # endif
67 # else
68 HANDLE t;
69 DWORD thread_id;
70 # endif
71 # if !(defined(BEOS) || defined(MSWIN32) || defined(MSWINCE) \
72 || defined(CYGWIN32) || defined(GC_OPENBSD_UTHREADS) \
73 || (defined(DARWIN) && !defined(NO_PTHREAD_GET_STACKADDR_NP)) \
74 || ((defined(FREEBSD) || defined(LINUX) || defined(NETBSD) \
75 || defined(HOST_ANDROID)) && !defined(NO_PTHREAD_GETATTR_NP) \
76 && !defined(NO_PTHREAD_ATTR_GET_NP)) \
77 || (defined(GC_SOLARIS_THREADS) && !defined(_STRICT_STDC)) \
78 || (!defined(STACKBOTTOM) && (defined(HEURISTIC1) \
79 || (!defined(LINUX_STACKBOTTOM) && !defined(FREEBSD_STACKBOTTOM)))))
80 /* GC_INIT() must be called from main thread only. */
81 GC_INIT();
82 # endif
83 (void)GC_get_parallel(); /* linking fails if no threads support */
84 # ifdef GC_PTHREADS
85 if ((code = pthread_create (&t, NULL, thread, NULL)) != 0) {
86 fprintf(stderr, "Thread creation failed %d\n", code);
87 return 1;
88 }
89 if ((code = pthread_join (t, NULL)) != 0) {
90 fprintf(stderr, "Thread join failed %d\n", code);
91 return 1;
92 }
93 # else
94 t = CreateThread(NULL, 0, thread, 0, 0, &thread_id);
95 if (t == NULL) {
96 fprintf(stderr, "Thread creation failed %d\n", (int)GetLastError());
97 return 1;
98 }
99 if (WaitForSingleObject(t, INFINITE) != WAIT_OBJECT_0) {
100 fprintf(stderr, "Thread join failed %d\n", (int)GetLastError());
101 CloseHandle(t);
102 return 1;
103 }
104 CloseHandle(t);
105 # endif
106 return 0;
107 }