aboutsummaryrefslogtreecommitdiff
path: root/platform/linux-dpdk/odp_thread.c
blob: 4c5950cb6f23babddc14bd241cae4cdaa2db606d (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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* Copyright (c) 2013, Linaro Limited
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include <sched.h>

#include <odp/thread.h>
#include <odp/thrmask.h>
#include <odp_internal.h>
#include <odp/spinlock.h>
#include <odp/config.h>
#include <odp_debug_internal.h>
#include <odp/shared_memory.h>
#include <odp/align.h>
#include <odp/cpu.h>
#include <rte_lcore.h>

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

#define MASK_SIZE_16 ((ODP_THREAD_COUNT_MAX+15)/16)

typedef struct {
	int thr;
	int cpu;
	odp_thread_type_t type;
} thread_state_t;


typedef struct {
	thread_state_t thr[ODP_THREAD_COUNT_MAX];
	union {
		/* struct order must be kept in sync with schedule_types.h */
		struct {
			odp_thrmask_t  all;
			odp_thrmask_t  worker;
			odp_thrmask_t  control;
		};
		odp_thrmask_t sched_grp_mask[ODP_CONFIG_SCHED_GRPS];
	};
	uint32_t       num;
	uint32_t       num_worker;
	uint32_t       num_control;
	odp_spinlock_t lock;
} thread_globals_t;


/* Globals */
static thread_globals_t *thread_globals;


/* Thread local */
static __thread thread_state_t *this_thread;


int odp_thread_init_global(void)
{
	odp_shm_t shm;
	int i;

	shm = odp_shm_reserve("odp_thread_globals",
			      sizeof(thread_globals_t),
			      ODP_CACHE_LINE_SIZE, 0);

	thread_globals = odp_shm_addr(shm);

	if (thread_globals == NULL)
		return -1;

	memset(thread_globals, 0, sizeof(thread_globals_t));
	odp_spinlock_init(&thread_globals->lock);

	for (i = 0; i < ODP_CONFIG_SCHED_GRPS; i++)
		odp_thrmask_zero(&thread_globals->sched_grp_mask[i]);

	return 0;
}

odp_thrmask_t *thread_sched_grp_mask(int index);
odp_thrmask_t *thread_sched_grp_mask(int index)
{
	return &thread_globals->sched_grp_mask[index];
}

int odp_thread_term_global(void)
{
	int ret;

	ret = odp_shm_free(odp_shm_lookup("odp_thread_globals"));
	if (ret < 0)
		ODP_ERR("shm free failed for odp_thread_globals");

	return ret;
}

static int alloc_id(odp_thread_type_t type)
{
	int thr;
	odp_thrmask_t *all = &thread_globals->all;

	if (thread_globals->num >= ODP_THREAD_COUNT_MAX)
		return -1;

	for (thr = 0; thr < ODP_THREAD_COUNT_MAX; thr++) {
		if (odp_thrmask_isset(all, thr) == 0) {
			odp_thrmask_set(all, thr);

			if (type == ODP_THREAD_WORKER) {
				odp_thrmask_set(&thread_globals->worker, thr);
				thread_globals->num_worker++;
			} else {
				odp_thrmask_set(&thread_globals->control, thr);
				thread_globals->num_control++;
			}

			thread_globals->num++;
			return thr;
		}
	}

	return -2;
}

static int free_id(int thr)
{
	odp_thrmask_t *all = &thread_globals->all;

	if (thr < 0 || thr >= ODP_THREAD_COUNT_MAX)
		return -1;

	if (odp_thrmask_isset(all, thr) == 0)
		return -1;

	odp_thrmask_clr(all, thr);

	if (thread_globals->thr[thr].type == ODP_THREAD_WORKER) {
		odp_thrmask_clr(&thread_globals->worker, thr);
		thread_globals->num_worker--;
	} else {
		odp_thrmask_clr(&thread_globals->control, thr);
		thread_globals->num_control--;
	}

	thread_globals->num--;
	return thread_globals->num;
}

int odp_thread_init_local(odp_thread_type_t type)
{
	int id;
	int cpu;
	struct rte_config *cfg = rte_eal_get_configuration();

	odp_spinlock_lock(&thread_globals->lock);
	id = alloc_id(type);
	odp_spinlock_unlock(&thread_globals->lock);

	if (id < 0) {
		ODP_ERR("Too many threads\n");
		return -1;
	}

	cpu = sched_getcpu();

	if (cpu < 0) {
		ODP_ERR("getcpu failed\n");
		return -1;
	}

	thread_globals->thr[id].thr  = id;
	thread_globals->thr[id].cpu  = cpu;
	thread_globals->thr[id].type = type;
	RTE_PER_LCORE(_lcore_id) = cpu;
	if (cfg->lcore_role[cpu] == ROLE_RTE)
		ODP_ERR("There is a thread already running on core %d\n", cpu);
	cfg->lcore_role[cpu] = ROLE_RTE;

	this_thread = &thread_globals->thr[id];
	return 0;
}

int odp_thread_term_local(void)
{
	int num;
	int id = this_thread->thr;

	odp_spinlock_lock(&thread_globals->lock);
	num = free_id(id);
	odp_spinlock_unlock(&thread_globals->lock);

	if (num < 0) {
		ODP_ERR("failed to free thread id %i", id);
		return -1;
	}

	return num; /* return a number of threads left */
}

int odp_thread_id(void)
{
	return this_thread->thr;
}

int odp_thread_count(void)
{
	return thread_globals->num;
}

int odp_thread_count_max(void)
{
	return ODP_THREAD_COUNT_MAX;
}

odp_thread_type_t odp_thread_type(void)
{
	return this_thread->type;
}

int odp_cpu_id(void)
{
	return this_thread->cpu;
}

int odp_thrmask_worker(odp_thrmask_t *mask)
{
	odp_thrmask_copy(mask, &thread_globals->worker);
	return thread_globals->num_worker;
}

int odp_thrmask_control(odp_thrmask_t *mask)
{
	odp_thrmask_copy(mask, &thread_globals->control);
	return thread_globals->num_control;
}