Cthulhu  0.2.10
Cthulhu compiler collection
thread.c
Go to the documentation of this file.
1 // SPDX-License-Identifier: LGPL-3.0-only
2 #include "os_common.h"
3 
4 #include "base/panic.h"
5 
6 static void *thread_fn(void *arg)
7 {
8  os_thread_t *thread = arg;
9 
10  return (void *)(uintptr_t)thread->fn(thread->arg);
11 }
12 
14 os_error_t os_thread_init(
15  os_thread_t *thread,
16  const char *name,
17  os_thread_fn_t fn,
18  void *arg)
19 {
20  CTASSERT(thread != NULL);
21  CTASSERT(name != NULL);
22  CTASSERT(fn != NULL);
23 
24  thread->name = name;
25  thread->fn = fn;
26  thread->arg = arg;
27 
28  if (pthread_create(&thread->impl, NULL, thread_fn, thread) != 0)
29  {
30  return errno;
31  }
32 
33  thread->id = thread->impl;
34 
35  return eOsSuccess;
36 }
37 
39 os_error_t os_thread_join(os_thread_t *thread, os_status_t *status)
40 {
41  CTASSERT(thread != NULL);
42  CTASSERT(status != NULL);
43 
44  void *result = NULL;
45 
46  if (pthread_join(thread->impl, &result) != 0)
47  {
48  return errno;
49  }
50 
51  *status = (os_status_t)(uintptr_t)result;
52 
53  return eOsSuccess;
54 }
55 
57 {
58  return pthread_self();
59 }
#define STA_DECL
sal2 annotation on function implementations to copy annotations from the declaration
os_thread_id_t os_get_thread_id(void)
get the current thread id
Definition: thread.c:56
unsigned os_status_t
thread return code
Definition: core.h:71
size_t os_thread_id_t
thread id
Definition: core.h:75
os_exitcode_t(* os_thread_fn_t)(void *arg)
Definition: os.h:73
#define CTASSERT(expr)
assert a condition, prints the condition as a message
Definition: panic.h:130
STA_DECL os_error_t os_thread_join(os_thread_t *thread, os_status_t *status)
Definition: thread.c:39
STA_DECL os_error_t os_thread_init(os_thread_t *thread, const char *name, os_thread_fn_t fn, void *arg)
Definition: thread.c:14
@ eOsSuccess
Definition: posix.h:24
a thread handle
Definition: os.h:79
os_thread_id_t id
Definition: os.h:88
os_thread_fn_t fn
Definition: os.h:83
const char * name
Definition: os.h:81
void * arg
Definition: os.h:84
os_thread_impl_t impl
Definition: os.h:87