File: initsecondarythread.c

package info (click to toggle)
libgc 1%3A8.2.8-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 5,128 kB
  • sloc: ansic: 40,056; sh: 4,449; cpp: 1,066; makefile: 200; asm: 77
file content (123 lines) | stat: -rw-r--r-- 3,361 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * Copyright (C) 2011 Ludovic Courtes
 *
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
 *
 * Permission is hereby granted to use or copy this program
 * for any purpose, provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 */

/* Make sure 'GC_INIT' can be called from threads other than the initial
 * thread.
 */

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#ifndef GC_THREADS
# define GC_THREADS
#endif

#define GC_NO_THREAD_REDIRECTS 1
                /* Do not redirect thread creation and join calls.      */

#include "gc.h"

#ifdef GC_PTHREADS
# include <pthread.h>
#else
# ifndef WIN32_LEAN_AND_MEAN
#   define WIN32_LEAN_AND_MEAN 1
# endif
# define NOSERVICE
# include <windows.h>
#endif /* !GC_PTHREADS */

#include <stdlib.h>
#include <stdio.h>

#define CHECK_OUT_OF_MEMORY(p) \
    do { \
        if (NULL == (p)) { \
            fprintf(stderr, "Out of memory\n"); \
            exit(69); \
        } \
    } while (0)

#ifdef GC_PTHREADS
  static void *thread(void *arg)
#else
  static DWORD WINAPI thread(LPVOID arg)
#endif
{
  GC_INIT();
  CHECK_OUT_OF_MEMORY(GC_MALLOC(123));
  CHECK_OUT_OF_MEMORY(GC_MALLOC(12345));
# ifdef GC_PTHREADS
    return arg;
# else
    return (DWORD)(GC_word)arg;
# endif
}

#include "private/gcconfig.h"

int main(void)
{
# ifdef GC_PTHREADS
    int code;
    pthread_t t;

#   ifdef LINT2
      t = pthread_self(); /* explicitly initialize to some value */
#   endif
# else
    HANDLE t;
    DWORD thread_id;
# endif
# if !(defined(BEOS) || defined(MSWIN32) || defined(MSWINCE) \
       || defined(CYGWIN32) || defined(GC_OPENBSD_UTHREADS) \
       || (defined(DARWIN) && !defined(NO_PTHREAD_GET_STACKADDR_NP)) \
       || ((defined(FREEBSD) || defined(LINUX) || defined(NETBSD) \
            || defined(HOST_ANDROID)) && !defined(NO_PTHREAD_GETATTR_NP) \
           && !defined(NO_PTHREAD_ATTR_GET_NP)) \
       || (defined(GC_SOLARIS_THREADS) && !defined(_STRICT_STDC)) \
       || (!defined(STACKBOTTOM) && (defined(HEURISTIC1) \
          || (!defined(LINUX_STACKBOTTOM) && !defined(FREEBSD_STACKBOTTOM)))))
    /* GC_INIT() must be called from main thread only. */
    GC_INIT();
# endif
  (void)GC_get_parallel(); /* linking fails if no threads support */
  if (GC_get_find_leak())
    printf("This test program is not designed for leak detection mode\n");
# ifdef GC_PTHREADS
    if ((code = pthread_create (&t, NULL, thread, NULL)) != 0) {
      fprintf(stderr, "Thread creation failed, errno= %d\n", code);
      return 1;
    }
    if ((code = pthread_join (t, NULL)) != 0) {
      fprintf(stderr, "Thread join failed, errno= %d\n", code);
      return 1;
    }
# else
    t = CreateThread(NULL, 0, thread, 0, 0, &thread_id);
    if (t == NULL) {
      fprintf(stderr, "Thread creation failed, errcode= %d\n",
              (int)GetLastError());
      return 1;
    }
    if (WaitForSingleObject(t, INFINITE) != WAIT_OBJECT_0) {
      fprintf(stderr, "Thread join failed, errcode= %d\n",
              (int)GetLastError());
      CloseHandle(t);
      return 1;
    }
    CloseHandle(t);
# endif
  return 0;
}