aboutsummaryrefslogtreecommitdiff
path: root/test/validation/api/thread/thread.c
blob: f279dd16f97403c1e8c99fdae1aebd82a321d4b4 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/* Copyright (c) 2015-2018, Linaro Limited
 * Copyright (c) 2022, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_api.h>
#include <odp/helper/odph_api.h>
#include <odp_cunit_common.h>
#include <mask_common.h>

#define GLOBAL_SHM_NAME		"GlobalThreadTest"

typedef struct {
	/* Test thread entry and exit synchronization barriers */
	odp_barrier_t bar_entry;
	odp_barrier_t bar_exit;
} global_shared_mem_t;

static global_shared_mem_t *global_mem;

static int thread_global_init(odp_instance_t *inst)
{
	odp_shm_t global_shm;
	odp_init_t init_param;
	odph_helper_options_t helper_options;

	if (odph_options(&helper_options)) {
		fprintf(stderr, "error: odph_options() failed.\n");
		return -1;
	}

	odp_init_param_init(&init_param);
	init_param.mem_model = helper_options.mem_model;

	if (0 != odp_init_global(inst, &init_param, NULL)) {
		fprintf(stderr, "error: odp_init_global() failed.\n");
		return -1;
	}
	if (0 != odp_init_local(*inst, ODP_THREAD_CONTROL)) {
		fprintf(stderr, "error: odp_init_local() failed.\n");
		return -1;
	}

	global_shm = odp_shm_reserve(GLOBAL_SHM_NAME,
				     sizeof(global_shared_mem_t),
				     ODP_CACHE_LINE_SIZE, 0);
	if (global_shm == ODP_SHM_INVALID) {
		fprintf(stderr, "Unable reserve memory for global_shm\n");
		return -1;
	}

	global_mem = odp_shm_addr(global_shm);
	memset(global_mem, 0, sizeof(global_shared_mem_t));

	return 0;
}

static int thread_global_term(odp_instance_t inst)
{
	odp_shm_t shm;

	shm = odp_shm_lookup(GLOBAL_SHM_NAME);
	if (0 != odp_shm_free(shm)) {
		fprintf(stderr, "error: odp_shm_free() failed.\n");
		return -1;
	}

	if (0 != odp_term_local()) {
		fprintf(stderr, "error: odp_term_local() failed.\n");
		return -1;
	}

	if (0 != odp_term_global(inst)) {
		fprintf(stderr, "error: odp_term_global() failed.\n");
		return -1;
	}

	return 0;
}

static void thread_test_odp_cpu_id(void)
{
	CU_ASSERT(odp_cpu_id() >= 0);
}

static void thread_test_odp_thread_id(void)
{
	int id = odp_thread_id();

	CU_ASSERT(id >= 0);
	CU_ASSERT(id < odp_thread_count_max());
	CU_ASSERT(id < ODP_THREAD_COUNT_MAX);
}

static void thread_test_odp_thread_count(void)
{
	int count = odp_thread_count();

	/* One thread running */
	CU_ASSERT(count == 1);

	CU_ASSERT(count >= 1);
	CU_ASSERT(count <= odp_thread_count_max());
	CU_ASSERT(count <= ODP_THREAD_COUNT_MAX);
	CU_ASSERT(odp_thread_count_max() <= ODP_THREAD_COUNT_MAX);
}

static int thread_func(void *arg ODP_UNUSED)
{
	/* indicate that thread has started */
	odp_barrier_wait(&global_mem->bar_entry);

	CU_ASSERT(odp_thread_type() == ODP_THREAD_WORKER);

	/* wait for indication that we can exit */
	odp_barrier_wait(&global_mem->bar_exit);

	return CU_get_number_of_failures();
}

static void thread_test_odp_thrmask_worker(void)
{
	odp_thrmask_t mask;
	int ret;
	int num = 1;

	CU_ASSERT_FATAL(odp_thread_type() == ODP_THREAD_CONTROL);

	odp_barrier_init(&global_mem->bar_entry, num + 1);
	odp_barrier_init(&global_mem->bar_exit,  num + 1);

	/* should start out with 0 worker threads */
	ret = odp_thrmask_worker(&mask);
	CU_ASSERT(ret == odp_thrmask_count(&mask));
	CU_ASSERT(ret == 0);

	/* start the test thread(s) */
	ret = odp_cunit_thread_create(num, thread_func, NULL, 0);
	CU_ASSERT(ret == num);

	if (ret != num)
		return;

	/* wait for thread(s) to start */
	odp_barrier_wait(&global_mem->bar_entry);

	ret = odp_thrmask_worker(&mask);
	CU_ASSERT(ret == odp_thrmask_count(&mask));
	CU_ASSERT(ret == num);
	CU_ASSERT(ret <= odp_thread_count_max());

	/* allow thread(s) to exit */
	odp_barrier_wait(&global_mem->bar_exit);

	odp_cunit_thread_join(num);
}

static void thread_test_odp_thrmask_control(void)
{
	odp_thrmask_t mask;
	int ret;

	CU_ASSERT(odp_thread_type() == ODP_THREAD_CONTROL);

	/* should start out with 1 worker thread */
	ret = odp_thrmask_control(&mask);
	CU_ASSERT(ret == odp_thrmask_count(&mask));
	CU_ASSERT(ret == 1);
}

odp_testinfo_t thread_suite[] = {
	ODP_TEST_INFO(thread_test_odp_cpu_id),
	ODP_TEST_INFO(thread_test_odp_thread_id),
	ODP_TEST_INFO(thread_test_odp_thread_count),
	ODP_TEST_INFO(thread_test_odp_thrmask_to_from_str),
	ODP_TEST_INFO(thread_test_odp_thrmask_equal),
	ODP_TEST_INFO(thread_test_odp_thrmask_zero),
	ODP_TEST_INFO(thread_test_odp_thrmask_set),
	ODP_TEST_INFO(thread_test_odp_thrmask_clr),
	ODP_TEST_INFO(thread_test_odp_thrmask_isset),
	ODP_TEST_INFO(thread_test_odp_thrmask_count),
	ODP_TEST_INFO(thread_test_odp_thrmask_and),
	ODP_TEST_INFO(thread_test_odp_thrmask_or),
	ODP_TEST_INFO(thread_test_odp_thrmask_xor),
	ODP_TEST_INFO(thread_test_odp_thrmask_copy),
	ODP_TEST_INFO(thread_test_odp_thrmask_first),
	ODP_TEST_INFO(thread_test_odp_thrmask_last),
	ODP_TEST_INFO(thread_test_odp_thrmask_next),
	ODP_TEST_INFO(thread_test_odp_thrmask_worker),
	ODP_TEST_INFO(thread_test_odp_thrmask_control),
	ODP_TEST_INFO_NULL,
};

odp_suiteinfo_t thread_suites[] = {
	{"thread", NULL, NULL, thread_suite},
	ODP_SUITE_INFO_NULL,
};

int main(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	odp_cunit_register_global_init(thread_global_init);
	odp_cunit_register_global_term(thread_global_term);

	ret = odp_cunit_register(thread_suites);

	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}