thread.c 12.5 KB
Newer Older
Michael Schmid committed
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
* Copyright (c) 2014-2016, Siemens AG. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
Michael Schmid committed
26 27 28 29 30 31 32 33 34 35

#include <embb/base/c/thread.h>
#include <embb/base/c/internal/thread_index.h>
#include <embb/base/c/internal/cmake_config.h>
#include <embb/base/c/memory_allocation.h>
#include <embb/base/c/log.h>
#include <stdlib.h>
#include <assert.h>

unsigned int embb_thread_get_max_count() {
36
	return (unsigned int)(embb_internal_thread_index_max());
Michael Schmid committed
37 38 39
}

void embb_thread_set_max_count(unsigned int max) {
40
	embb_internal_thread_index_set_max(max);
Michael Schmid committed
41 42 43 44 45
}

#ifdef EMBB_PLATFORM_THREADING_WINTHREADS

/**
46 47 48
* Used to wrap client thread start function and argument when calling internal
* thread start function embb_internal_thread_start.
*/
Michael Schmid committed
49
typedef struct embb_internal_thread_arg_t {
50 51
	embb_thread_start_t func;
	void* arg;
Michael Schmid committed
52 53 54
} embb_internal_thread_arg_t;

/**
55 56 57 58 59
* Used to offer a consistent thread start function signature. Windows threads
* have a different signature than Pthreads and C11. This internal start
* function for Windows threads just calls the client start function with the
* given argument.
*/
Michael Schmid committed
60
DWORD WINAPI embb_internal_thread_start(LPVOID internalArg) {
61 62
	int result = ((embb_internal_thread_arg_t*)internalArg)->func(
	((embb_internal_thread_arg_t*)internalArg)->arg);
Michael Schmid committed
63
#if !defined(__cplusplus)
64
	ExitThread((DWORD)result); /* In C, returning the result code doesn't work */
Michael Schmid committed
65
#else
66
	return (DWORD)result;
Michael Schmid committed
67 68 69 70
#endif
}

embb_thread_t embb_thread_current() {
71 72 73 74
	embb_thread_t thread;
	thread.embb_internal_handle = GetCurrentThread();
	thread.embb_internal_arg = NULL;
	return thread;
Michael Schmid committed
75 76 77
}

void embb_thread_yield() {
78
	SwitchToThread();
Michael Schmid committed
79 80 81
}

int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
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
embb_thread_start_t func, void *arg) {
	if (thread == NULL) {
		return EMBB_ERROR;
	}
	thread->embb_internal_arg = (embb_internal_thread_arg_t*)
	embb_alloc(sizeof(embb_internal_thread_arg_t));
	if (thread->embb_internal_arg == NULL) {
		thread->embb_internal_handle = NULL;
		return EMBB_NOMEM;
	}
	thread->embb_internal_arg->func = func;
	thread->embb_internal_arg->arg = arg;

	thread->embb_internal_handle = CreateThread(
	0,                                  /* no security */
	0,                                  /* default stack size */
	embb_internal_thread_start,         /* entry function */
	(LPVOID)thread->embb_internal_arg,  /* parameters */
	0,                                  /* no creation arguments */
	0);                                 /* no system thread ID */
	if (thread->embb_internal_handle == NULL) {
		embb_free(thread->embb_internal_arg);
		thread->embb_internal_arg = NULL;
		return EMBB_ERROR;
	}

	if (core_set != NULL) { /* Set thread affinity, if a core set is given */
		DWORD_PTR core_mask = 0;
		DWORD bit_mask = 1;
		assert(embb_core_count_available() < 64);
		for (unsigned int i = 0; i < embb_core_count_available(); i++) {
			if (embb_core_set_contains(core_set, i)) {
				core_mask |= bit_mask;
			}
			bit_mask <<= 1;
		}
		if (SetThreadAffinityMask(thread->embb_internal_handle, core_mask)
				== (DWORD_PTR)NULL) {
			return EMBB_ERROR;
		}
	}

	return EMBB_SUCCESS;
Michael Schmid committed
125 126 127
}

int embb_thread_join(embb_thread_t* thread, int* result_code) {
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
	if (thread == NULL) {
		return EMBB_ERROR;
	}
	BOOL success;
	DWORD result;
	result = WaitForSingleObject(thread->embb_internal_handle, INFINITE);
	embb_free(thread->embb_internal_arg);
	if (result != WAIT_OBJECT_0) {
		/* WAIT_OBJECT_0 indicates successful waiting */
		return EMBB_ERROR;
	}
	if (result_code != NULL) { /* != NULL means the client wants a result code */
		if (GetExitCodeThread(thread->embb_internal_handle, &result) != 0) {
			*result_code = (int)result;
		} else {
			*result_code = 0; /* Error on obtaining result code */
			return EMBB_ERROR;
		}
	}
	success = CloseHandle(thread->embb_internal_handle);
	if (success == FALSE) {
		return EMBB_ERROR;
	}
	/*return embb_internal_thread_counter_try_decrement();*/
	return EMBB_SUCCESS;
Michael Schmid committed
153 154 155
}

int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) {
156 157 158 159 160 161 162 163 164
	if (lhs == NULL || rhs == NULL) {
		return 0;
	}
	embb_thread_id_t idLhs = GetThreadId(lhs->embb_internal_handle);
	embb_thread_id_t idRhs = GetThreadId(rhs->embb_internal_handle);
	if (idLhs == idRhs) {
		return 1;
	}
	return 0;
Michael Schmid committed
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

#endif /* EMBB_PLATFORM_THREADING_WINTHREADS */

#ifdef EMBB_PLATFORM_THREADING_POSIXTHREADS

#ifdef EMBB_PLATFORM_HAS_GLIB_CPU
#include <sched.h>
#elif defined EMBB_PLATFORM_HAS_HEADER_CPUSET
#include <pthread_np.h>
#include <sys/param.h>
#include <sys/cpuset.h>
#endif

#ifdef EMBB_PLATFORM_HAS_HEADER_SYSINFO
#include <sys/sysinfo.h> /* Used to get number of processors */
#endif /* EMBB_PLATFORM_HAS_HEADER_SYSINFO */

/**
184 185 186
* Used to wrap client thread start function and argument when calling internal
* thread start function embb_internal_thread_start.
*/
Michael Schmid committed
187
typedef struct embb_internal_thread_arg_t {
188 189 190
	embb_thread_start_t func;
	void* arg;
	int result;
Michael Schmid committed
191 192 193
} embb_internal_thread_arg_t;

/**
194 195 196 197 198
* Used to offer a consistent thread start function signature. POSIX threads
* have a different signature than C11 threads. This internal start function
* for POSIX threads just calls the client start function with the given
* argument.
*/
Michael Schmid committed
199
void* embb_internal_thread_start(void* internalArg) {
200 201 202 203
	((embb_internal_thread_arg_t*)internalArg)->result =
	((embb_internal_thread_arg_t*)internalArg)->func(
	((struct embb_internal_thread_arg_t*)internalArg)->arg);
	return NULL;
Michael Schmid committed
204 205 206
}

embb_thread_t embb_thread_current() {
207 208 209 210
	embb_thread_t thread;
	thread.embb_internal_handle = pthread_self();
	thread.embb_internal_arg = NULL;
	return thread;
Michael Schmid committed
211 212 213
}

void embb_thread_yield() {
214
	pthread_yield();
Michael Schmid committed
215 216 217
}

int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
218 219 220 221 222 223 224 225
embb_thread_start_t func, void* arg) {
	if (thread == NULL) {
		return EMBB_ERROR;
	}
	pthread_attr_t attr; /* Used to set thread affinities */
	int status = pthread_attr_init(&attr);
	if (status != 0) return EMBB_ERROR;
	if (core_set != NULL) {
Michael Schmid committed
226
#if defined(EMBB_PLATFORM_HAS_GLIB_CPU) || \
227 228 229
			defined(EMBB_PLATFORM_HAS_HEADER_CPUSET)
		assert(embb_core_count_available() < CPU_SETSIZE &&
		"Core sets are only supported up to CPU_SETSIZE processors!");
Michael Schmid committed
230
#ifdef EMBB_PLATFORM_HAS_GLIB_CPU
231
		cpu_set_t cpuset;
Michael Schmid committed
232
#else
233
		cpuset_t cpuset;
Michael Schmid committed
234
#endif
235 236 237 238 239 240 241 242 243 244 245 246
		CPU_ZERO(&cpuset); /* Disable all processors */
		for (unsigned int i = 0; i < embb_core_count_available(); i++) {
			if (embb_core_set_contains(core_set, i)) {
				CPU_SET(i, &cpuset);
			}
		}
		status = pthread_attr_setaffinity_np(&attr, sizeof(cpuset), &cpuset);
		if (status != 0) {
			thread->embb_internal_arg = NULL;
			thread->embb_internal_handle = 0;
			return EMBB_ERROR;
		}
Michael Schmid committed
247
#else
248 249
		embb_log_write("base_c", EMBB_LOG_LEVEL_WARNING, "Could not set thread "
		"affinity, since no implementation available!\n");
Michael Schmid committed
250
#endif
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
	}

	/* Dynamic allocation of thread arguments. Freed on call of join. */
	thread->embb_internal_arg = (embb_internal_thread_arg_t*)
	embb_alloc(sizeof(embb_internal_thread_arg_t));
	if (thread->embb_internal_arg == NULL) {
		thread->embb_internal_handle = 0;
		pthread_attr_destroy(&attr);
		return EMBB_NOMEM;
	}
	thread->embb_internal_arg->func = func;
	thread->embb_internal_arg->arg = arg;

	status = pthread_create(
			&(thread->embb_internal_handle),     /* pthread handle */
			&attr,                               /* additional attributes,
													e.g., affinities */
			embb_internal_thread_start,          /* thread start function */
			(void*)(thread->embb_internal_arg)); /* arguments to thread start func. */
	if (status != 0) return EMBB_ERROR;

	status = pthread_attr_destroy(&attr);
	if (status != 0) return EMBB_ERROR;
	return EMBB_SUCCESS;
Michael Schmid committed
275 276 277
}

int embb_thread_join(embb_thread_t* thread, int *result_code) {
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
	if (thread == NULL) {
		return EMBB_ERROR;
	}
	int status = 0;
	status = pthread_join(thread->embb_internal_handle, NULL);
	if (thread->embb_internal_arg != NULL) {
		if (result_code != NULL) {
			*result_code = thread->embb_internal_arg->result;
		}
		embb_free(thread->embb_internal_arg);
	}
	if (status != 0) {
		return EMBB_ERROR;
	}
	return EMBB_SUCCESS;
Michael Schmid committed
293 294 295
}

int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) {
296 297 298 299
	if (lhs == NULL || rhs == NULL) {
		return 0;
	}
	return pthread_equal(lhs->embb_internal_handle, rhs->embb_internal_handle);
Michael Schmid committed
300 301 302
}

#endif /* EMBB_PLATFORM_THREADING_POSIXTHREADS */
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

#ifdef EMBB_PLATFORM_THREADING_RTOSTASKS

#include <FreeRTOS/task.h>

/**
* Used to wrap client thread start function and argument when calling internal
* thread start function embb_internal_thread_start.
*/
typedef struct embb_internal_thread_arg_t {
	embb_thread_start_t func;
	void* arg;
	int result;
} embb_internal_thread_arg_t;


/**
* Used to offer a consistent thread start function signature. POSIX threads
* have a different signature than C11 threads. This internal start function
* for POSIX threads just calls the client start function with the given
* argument.
*/
void* embb_internal_thread_start(void* internalArg) {
	for(;;) {
		((embb_internal_thread_arg_t*)internalArg)->result =
			((embb_internal_thread_arg_t*)internalArg)->func(
			((struct embb_internal_thread_arg_t*)internalArg)->arg);
			
		vTaskDelete(NULL);
	}
}

embb_thread_t embb_thread_current() {
	embb_thread_t thread;
	thread.embb_internal_handle = xTaskGetCurrentTaskHandle();
	thread.embb_internal_arg = NULL;
	return thread;
}

void embb_thread_yield() {
	taskYIELD();
}


int embb_thread_create(embb_thread_t* thread, const embb_core_set_t* core_set,
embb_thread_start_t func, void *arg) {
	int status;
	if (thread == NULL) {
		return EMBB_ERROR;
	}
	thread->embb_internal_arg = (embb_internal_thread_arg_t*)
	embb_alloc(sizeof(embb_internal_thread_arg_t));
	if (thread->embb_internal_arg == NULL) {
		thread->embb_internal_handle = NULL;
		return EMBB_NOMEM;
	}

	thread->embb_internal_arg->func = func;
	thread->embb_internal_arg->arg = arg;	  
	
363
	status = xTaskCreate( (TaskFunction_t) embb_internal_thread_start, 			// entry function
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
						0, 										// task name (not needed)
						128, 									// stack size
						thread->embb_internal_arg, 				// parameters
						1,  									// priority
						thread->embb_internal_handle);			// thread handle
	
	
	if (status != 1) {
		embb_free(thread->embb_internal_arg);
		thread->embb_internal_arg = NULL;
		return EMBB_ERROR;
	}

	/*if (core_set != NULL) {   						// scm34681: implement core affinity
	DWORD_PTR core_mask = 0;
	DWORD bit_mask = 1;
	assert(embb_core_count_available() < 64);
	for (unsigned int i = 0; i < embb_core_count_available(); i++) {
	if (embb_core_set_contains(core_set, i)) {
		core_mask |= bit_mask;
	}
	bit_mask <<= 1;
	}
	if (SetThreadAffinityMask(thread->embb_internal_handle, core_mask)
		== (DWORD_PTR)NULL) {
	return EMBB_ERROR;
	}
}*/

	return EMBB_SUCCESS;
}

int embb_thread_join(embb_thread_t* thread, int *result_code) {
	if (thread == NULL) {
		return EMBB_ERROR;
	}
	int status = 0;
	while(thread->embb_internal_handle != NULL && status++ < 10000);				 // scm34681: rework
	
	if (thread->embb_internal_arg != NULL) {
		if (result_code != NULL) {
			*result_code = thread->embb_internal_arg->result;
		}
		embb_free(thread->embb_internal_arg);
	}
	
	
	return EMBB_SUCCESS;
}

int embb_thread_equal(const embb_thread_t* lhs, const embb_thread_t* rhs) {
	if (lhs == NULL || rhs == NULL) {
		return 0;
	}
	TaskStatus_t lhs_info;
	TaskStatus_t rhs_info;

	vTaskGetInfo(lhs->embb_internal_handle, &lhs_info, 0, 0);
	vTaskGetInfo(lhs->embb_internal_handle, &rhs_info, 0, 0);

	return lhs_info.xTaskNumber == rhs_info.xTaskNumber;
}

#endif /* EMBB_PLATFORM_THREADING_RTOSTASKS */