POSIX API

What libcobalt provides

libcobalt implements a subset of the POSIX.1-2024 interface. Like the native API, it is built on the libevl intrinsics, the basic services which send requests to the core. An application gets real-time behavior from familiar calls: threads, mutexes, condition variables, semaphores, clocks and timers, message queues, signals, scheduling, and output to the standard streams and syslog. The EVL core serves these calls, while the rest of the application keeps using the C library as usual.

These pages do not repeat the standard. Each entry links to the description of the call in POSIX.1-2024, or in the Linux manual for the few calls which are specific to Linux, and only lists where libcobalt departs from it. When an entry lists no departure, the reference description applies as is. The calls which libcobalt adds to the standard interface are documented in full.

Calling the POSIX API

An application calls the services of libcobalt explicitly, by wrapping their names in the __EVL() marker:

#include <cobalt/pthread.h>

static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;

void update(void)
{
	__EVL(pthread_mutex_lock)(&lock);
	/* ... */
	__EVL(pthread_mutex_unlock)(&lock);
}

__EVL(pthread_mutex_lock) names cobalt_pthread_mutex_lock(), the implementation libcobalt provides. A plain call to pthread_mutex_lock() still goes to the C library. This way, the source code shows which calls are served by the core, which is where the time-critical part of the application lives, and which ones are regular in-band services.

The libcobalt headers include the standard ones they extend:

Header Services
<cobalt/pthread.h> threads, mutexes, condition variables, scheduling
<cobalt/semaphore.h> unnamed semaphores
<cobalt/time.h> clocks, sleeping, timers
<cobalt/sys/timerfd.h> timers delivering expirations through a file descriptor
<cobalt/mqueue.h> message queues
<cobalt/signal.h> signals
<cobalt/stdio.h> output to the standard streams
<cobalt/syslog.h> logging to syslog

Threads attached to the core

The core serves the threads attached to it. Threads created by __EVL(pthread_create)() are attached on start. Any other thread, including the main thread of the application, attaches itself by calling evl_attach_self().

A thread which is not attached to the core may still issue requests which do not wait, such as reading a clock, or sending a message to a queue open in non-blocking mode. Calls which may wait, and calls for which the core has to know the caller, such as locking a mutex, fail with EPERM instead. The function index of each page tells which calls are available to such threads.

Process-shared objects

Mutexes, condition variables and semaphores may be shared between processes, by setting the PTHREAD_PROCESS_SHARED attribute or the pshared argument as the standard describes. The object must live in memory which all these processes share.

Statically initialized objects

Objects initialized with a static initializer, such as PTHREAD_MUTEX_INITIALIZER, are created in the core on first use. The thread using them first is switched to the in-band stage once for this purpose.

Clocks

The core serves CLOCK_REALTIME, CLOCK_MONOTONIC and CLOCK_MONOTONIC_RAW. Requests for any other clock go to the C library. By default, mutexes, condition variables and semaphores time their waits on CLOCK_REALTIME, as the standard requires. The extended attributes below can select another clock.

Extended attributes

Some services come with an extended variant, whose name starts with an e. It takes extended attributes, which add EVL settings to the standard ones, such as the clock timing the waits on an object, or the name of the object in the EVL device file hierarchy. For instance, pthread_emutex_init() initializes a mutex from a pthread_emutexattr_t, whose std member holds the standard pthread_mutexattr_t attributes.

Building an application

An application which uses the POSIX API must also be linked against libcobalt.so, which libevl provides as a separate library (e.g. -L$prefix/lib -lcobalt -levl). libcobalt comes with its own pkgconfig meta-data, which pulls in libevl, so asking for the cobalt package is enough:

​
project('a_posix_foo_system', [ 'c' ], version : '0.0.0')

pthread_dep = dependency('threads')
libcobalt_dep = dependency('cobalt', method : 'pkg-config')

executable('foo',
    'foo.c',
    install: true,
    dependencies : [ libcobalt_dep, pthread_dep ],
)