libcosmos
Linux C++ System Programming Library
All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
thread.hxx File Reference
#include <cosmos/dso_export.h>
#include <cosmos/proc/types.hxx>
#include <cosmos/utils.hxx>

Go to the source code of this file.

Enumerations

enum class  ThreadID : pid_t { SELF = 0 }
 

Functions

ProcessID cosmos::as_pid (const ThreadID id)
 Return the ProcessID representation of the given thread ID.
 
ThreadID cosmos::thread::get_tid ()
 Returns the Linux low-level thread ID of the caller.
 
bool cosmos::thread::is_main_thread ()
 Returns whether the calling thread is this process's main thread.
 

Detailed Description

This header contains low level Linux thread functionality (i.e. not pthread related).

Definition in file thread.hxx.

Enumeration Type Documentation

◆ ThreadID

enum class cosmos::ThreadID : pid_t
strong

Definition at line 17 of file thread.hxx.

17 : pid_t {
18 SELF = 0
19};
@ SELF
In a number of system calls zero refers to the calling thread.

Function Documentation

◆ as_pid()

ProcessID cosmos::as_pid ( const ThreadID id)
inline

Return the ProcessID representation of the given thread ID.

Thread IDs are treated very similar to process IDs on Linux. For extra type safety and for being explicit libcosmos uses a strong unique type ThreadID, which can be explicitly casted into a ProcessID, if required.

Definition at line 27 of file thread.hxx.

27 {
28 return ProcessID{to_integral(id)};
29}
ProcessID
Definition types.hxx:25

◆ get_tid()

ThreadID COSMOS_API cosmos::thread::get_tid ( )

Returns the Linux low-level thread ID of the caller.

Definition at line 11 of file thread.cxx.

11 {
12 // glibc doesn't come with a wrapper for this
13 return static_cast<ThreadID>(syscall(SYS_gettid));
14}

◆ is_main_thread()

bool COSMOS_API cosmos::thread::is_main_thread ( )

Returns whether the calling thread is this process's main thread.

Definition at line 16 of file thread.cxx.

16 {
17 return as_pid(get_tid()) == proc::get_own_pid();
18}
ProcessID as_pid(const ThreadID id)
Return the ProcessID representation of the given thread ID.
Definition thread.hxx:27