Debian Codegraph @

gc/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 # ifndef WIN32_LEAN_AND_MEAN
35 # define WIN32_LEAN_AND_MEAN 1
36 # endif
37 # define NOSERVICE
38 # include <windows.h>
39 #endif /* !GC_PTHREADS */
40
41 #include <stdlib.h>
42 #include <stdio.h>
43
44 #define CHECK_OUT_OF_MEMORY(p) \
45 do { \
46 if (NULL == (p)) { \
47 fprintf(stderr, "Out of memory\n"); \
48 exit(69); \
49 } \
50 } while (0)
51
52 #ifdef GC_PTHREADS
53 static void *thread(void *arg)
54 #else
55 static DWORD WINAPI thread(LPVOID arg)
56 #endif
57 {
58 GC_INIT();
59 CHECK_OUT_OF_MEMORY(GC_MALLOC(123));
60 CHECK_OUT_OF_MEMORY(GC_MALLOC(12345));
61 # ifdef GC_PTHREADS
62 return arg;
63 # else
64 return (DWORD)(GC_word)arg;
65 # endif
66 }
67
68 #include "private/gcconfig.h"
69
70 int main(void)
71 {
72 # ifdef GC_PTHREADS
73 int code;
74 pthread_t t;
75
76 # ifdef LINT2
77 t = pthread_self(); /* explicitly initialize to some value */
78 # endif
79 # else
80 HANDLE t;
81 DWORD thread_id;
82 # endif
83 # if !(defined(BEOS) || defined(MSWIN32) || defined(MSWINCE) \
84 || defined(CYGWIN32) || defined(GC_OPENBSD_UTHREADS) \
85 || (defined(DARWIN) && !defined(NO_PTHREAD_GET_STACKADDR_NP)) \
86 || ((defined(FREEBSD) || defined(LINUX) || defined(NETBSD) \
87 || defined(HOST_ANDROID)) && !defined(NO_PTHREAD_GETATTR_NP) \
88 && !defined(NO_PTHREAD_ATTR_GET_NP)) \
89 || (defined(GC_SOLARIS_THREADS) && !defined(_STRICT_STDC)) \
90 || (!defined(STACKBOTTOM) && (defined(HEURISTIC1) \
91 || (!defined(LINUX_STACKBOTTOM) && !defined(FREEBSD_STACKBOTTOM)))))
92 /* GC_INIT() must be called from main thread only. */
93 GC_INIT();
94 # endif
95 (void)GC_get_parallel(); /* linking fails if no threads support */
96 if (GC_get_find_leak())
97 printf("This test program is not designed for leak detection mode\n");
98 # ifdef GC_PTHREADS
99 if ((code = pthread_create (&t, NULL, thread, NULL)) != 0) {
100 fprintf(stderr, "Thread creation failed, errno= %d\n", code);
101 return 1;
102 }
103 if ((code = pthread_join (t, NULL)) != 0) {
104 fprintf(stderr, "Thread join failed, errno= %d\n", code);
105 return 1;
106 }
107 # else
108 t = CreateThread(NULL, 0, thread, 0, 0, &thread_id);
109 if (t == NULL) {
110 fprintf(stderr, "Thread creation failed, errcode= %d\n",
111 (int)GetLastError());
112 return 1;
113 }
114 if (WaitForSingleObject(t, INFINITE) != WAIT_OBJECT_0) {
115 fprintf(stderr, "Thread join failed, errcode= %d\n",
116 (int)GetLastError());
117 CloseHandle(t);
118 return 1;
119 }
120 CloseHandle(t);
121 # endif
122 return 0;
123 }