aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test/erl_drv_thread_SUITE_data/tsd.c
blob: 3809c915e0b3d8d63c80e46d279fe071c2a0620a (plain) (blame)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* ``The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved via the world wide web at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
 * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
 * AB. All Rights Reserved.''
 * 
 *     $Id$
 */

#include "testcase_driver.h"
#include <stdio.h>

#define NO_OF_THREADS 17
#define NO_OF_KEYS 4711

struct {
    int alive;
    ErlDrvTid tid;
} test_thr[NO_OF_THREADS] = {0};

struct {
    int used;
    ErlDrvTSDKey key;
} test_key[NO_OF_KEYS] = {0};

typedef struct {
    int n;
} thr_arg_t;

static void *tf(void *vta)
{
    int i;
    int thr_val = (((thr_arg_t *) vta)->n << 16);
    
    for (i = 0; i < NO_OF_KEYS; i++)
	erl_drv_tsd_set(test_key[i].key, (void *) (thr_val | i));

    for (i = 0; i < NO_OF_KEYS; i++)
	if (erl_drv_tsd_get(test_key[i].key) != ((void *) (thr_val | i)))
	    return (void *) 1;

    for (i = 0; i < NO_OF_KEYS; i++)
	erl_drv_tsd_set(test_key[i].key, NULL);

    for (i = 0; i < NO_OF_KEYS; i++)
	if (erl_drv_tsd_get(test_key[i].key) != NULL)
	    return (void *) 2;

    return (void *) 3;
}

void
thr_key_clnup(void)
{
    int i;
    for (i = 0; i < NO_OF_KEYS; i++)
	erl_drv_tsd_set(test_key[i].key, NULL);
}

void
testcase_run(TestCaseState_t *tcs)
{
    int i, r;
    thr_arg_t ta[NO_OF_THREADS];
    ErlDrvSysInfo sinfo;

    testcase_printf(tcs, "Initializing\n");

    driver_system_info(&sinfo, sizeof(ErlDrvSysInfo));

    for (i = 0; i < NO_OF_KEYS; i++) {
	char name[100];
	sprintf(name, "key %d", i);
	r = erl_drv_tsd_key_create(name, &test_key[i].key);
	ASSERT(tcs, r == 0);
	test_key[i].used = 1;
    }

    for (i = 0; i < NO_OF_KEYS; i++)
	erl_drv_tsd_set(test_key[i].key,
			    (void *) (((NO_OF_THREADS+1) << 16) | i));

    if (!sinfo.thread_support) 
	testcase_printf(tcs, "No thread support; testing tsd in one thread\n");
    else {
	testcase_printf(tcs, "Creating %d threads\n", NO_OF_THREADS);

	/* Create the threads */
	for (i = 0; i < NO_OF_THREADS; i++) {
	    char name[100];
	    ta[i].n = 0;
	    sprintf(name, "thread %d", i);
	    r = erl_drv_thread_create(name,
				      &test_thr[i].tid,
				      tf,
				      (void *) &ta[i],
				      NULL);
	    ASSERT_CLNUP(tcs, r == 0, thr_key_clnup());
	    test_thr[i].alive = 1;
	}
    }

    testcase_printf(tcs, "Testing tsd\n");

    for (i = 0; i < NO_OF_KEYS; i++)
	ASSERT_CLNUP(tcs,
		     ((void *) (((NO_OF_THREADS+1) << 16) | i)
		      == erl_drv_tsd_get(test_key[i].key)),
		     thr_key_clnup());

    testcase_printf(tcs, "Joining threads\n");

    if (sinfo.thread_support) {
	/* Join the threads */
	for (i = 0; i < NO_OF_THREADS; i++) {
	    void *res;
	    r = erl_drv_thread_join(test_thr[i].tid, &res);
	    test_thr[i].alive = 0;
	    ASSERT_CLNUP(tcs, r == 0, thr_key_clnup());
	    ASSERT_CLNUP(tcs, res == ((void *) 3), thr_key_clnup());
	}
    }

    thr_key_clnup();

    for (i = 0; i < NO_OF_KEYS; i++)
	ASSERT(tcs, NULL == erl_drv_tsd_get(test_key[i].key));

    testcase_printf(tcs, "Destroying keys\n");

    for (i = 0; i < NO_OF_KEYS; i++)
	if (test_key[i].used) {
	    test_key[i].used = 0;
	    erl_drv_tsd_key_destroy(test_key[i].key);
	}

    testcase_printf(tcs, "done\n");

    if (!sinfo.thread_support)
	testcase_succeeded(tcs, "No thread support; only one thread tested");
}

char *
testcase_name(void)
{
    return "tsd";
}

void
testcase_cleanup(TestCaseState_t *tcs)
{
    int i;
    for (i = 0; i < NO_OF_THREADS; i++)
	if (test_thr[i].alive) {
	    test_thr[i].alive = 0;
	    erl_drv_thread_join(test_thr[i].tid, NULL);
	}
    
    for (i = 0; i < NO_OF_KEYS; i++)
	if (test_key[i].used) {
	    test_key[i].used = 0;
	    erl_drv_tsd_key_destroy(test_key[i].key);
	}
}