summaryrefslogtreecommitdiff
path: root/tests/subthread_create.c
blob: 63b501c279c75c4c0420fcc0551dac1ec5dda5d1 (plain)
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153

#ifdef HAVE_CONFIG_H
  /* For PARALLEL_MARK */
# include "config.h"
#endif

#ifndef GC_THREADS
# define GC_THREADS
#endif
#include "gc.h"

#ifdef PARALLEL_MARK
# define AO_REQUIRE_CAS
#endif
#include "atomic_ops.h"

#include <stdio.h>

#ifdef AO_HAVE_fetch_and_add

#ifdef GC_PTHREADS
# include <pthread.h>
#else
# include <windows.h>
#endif

#include <stdlib.h>
#include <string.h>

#ifndef MAX_SUBTHREAD_DEPTH
# define INITIAL_THREAD_COUNT 31
# define MAX_ALIVE_THREAD_COUNT 55
# define MAX_SUBTHREAD_DEPTH 7
# define MAX_SUBTHREAD_COUNT 200
#endif

#ifndef DECAY_NUMER
# define DECAY_NUMER 15
# define DECAY_DENOM 16
#endif

volatile AO_t thread_created_cnt = 0;
volatile AO_t thread_ended_cnt = 0;

#ifdef GC_PTHREADS
  void *entry(void *arg)
#else
  DWORD WINAPI entry(LPVOID arg)
#endif
{
    int thread_num = AO_fetch_and_add(&thread_created_cnt, 1);
    GC_word my_depth = (GC_word)arg + 1;

    if (my_depth <= MAX_SUBTHREAD_DEPTH
            && thread_num < MAX_SUBTHREAD_COUNT
            && (thread_num % DECAY_DENOM) < DECAY_NUMER
            && (int)(thread_num - AO_load(&thread_ended_cnt))
                <= MAX_ALIVE_THREAD_COUNT) {
# ifdef GC_PTHREADS
        int err;
        pthread_t th;

        err = pthread_create(&th, NULL, entry, (void *)my_depth);
        if (err != 0) {
            fprintf(stderr, "Thread #%d creation failed: %s\n", thread_num,
                    strerror(err));
            exit(2);
        }
        err = pthread_detach(th);
        if (err != 0) {
            fprintf(stderr, "Thread #%d detach failed: %s\n", thread_num,
                    strerror(err));
            exit(2);
        }
# else
        HANDLE th;
        DWORD thread_id;

        th = CreateThread(NULL, 0, entry, (LPVOID)my_depth, 0, &thread_id);
        if (th == NULL) {
            fprintf(stderr, "Thread #%d creation failed: %d\n", thread_num,
                   (int)GetLastError());
            exit(2);
        }
        CloseHandle(th);
# endif
    }

    AO_fetch_and_add(&thread_ended_cnt, 1);
    return 0;
}

int main(void)
{
    int i;
# ifdef GC_PTHREADS
    int err;
    pthread_t th[INITIAL_THREAD_COUNT];
# else
    HANDLE th[INITIAL_THREAD_COUNT];
# endif

    GC_INIT();
    for (i = 0; i < INITIAL_THREAD_COUNT; ++i) {
#     ifdef GC_PTHREADS
        err = pthread_create(&th[i], NULL, entry, 0);
        if (err) {
            fprintf(stderr, "Thread creation failed: %s\n", strerror(err));
            exit(1);
        }
#     else
        DWORD thread_id;
        th[i] = CreateThread(NULL, 0, entry, 0, 0, &thread_id);
        if (th[i] == NULL) {
            fprintf(stderr, "Thread creation failed: %d\n",
                    (int)GetLastError());
            exit(1);
        }
#     endif
    }

    for (i = 0; i < INITIAL_THREAD_COUNT; ++i) {
#     ifdef GC_PTHREADS
        void *res;
        err = pthread_join(th[i], &res);
        if (err) {
            fprintf(stderr, "Failed to join thread: %s\n", strerror(err));
            exit(1);
        }
#     else
        if (WaitForSingleObject(th[i], INFINITE) != WAIT_OBJECT_0) {
            fprintf(stderr, "Failed to join thread: %d\n",
                    (int)GetLastError());
            CloseHandle(th[i]);
            exit(1);
        }
        CloseHandle(th[i]);
#     endif
    }
  printf("subthread_create: created %d threads (%d ended)\n",
         (int)AO_load(&thread_created_cnt), (int)AO_load(&thread_ended_cnt));
  return 0;
}

#else

int main(void)
{
  printf("subthread_create test skipped\n");
  return 0;
}

#endif /* !AO_HAVE_fetch_and_add */