1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2015 Intel Corporation
5 #ifndef _PTHREAD_SHIM_H_
6 #define _PTHREAD_SHIM_H_
11 * This pthread shim is an example that demonstrates how legacy code
12 * that makes use of POSIX pthread services can make use of lthreads
13 * with reduced porting effort.
15 * N.B. The example is not a complete implementation, only a subset of
16 * pthread APIs sufficient to demonstrate the principle of operation
19 * In general pthread attribute objects do not have equivalent functions
20 * in lthreads, and are ignored.
22 * There is one exception and that is the use of attr to specify a
23 * core affinity in calls to pthread_create.
25 * The shim operates as follows:-
27 * On initialisation a constructor function uses dlsym to obtain and
28 * save the loaded address of the full set of pthread APIs that will
31 * For each function there is a stub provided that will invoke either
32 * the genuine pthread library function saved saved by the constructor,
33 * or else the corresponding equivalent lthread function.
35 * The stub functions are implemented in pthread_shim.c
37 * The stub will take care of adapting parameters, and any police
38 * any constraints where lthread functionality differs.
40 * The initial thread must always be a pure lthread.
42 * The decision whether to invoke the real library function or the lthread
43 * function is controlled by a per pthread flag that can be switched
44 * on of off by the pthread_override_set() API described below. Typcially
45 * this should be done as the first action of the initial lthread.
47 * N.B In general it would be poor practice to revert to invoke a real
48 * pthread function when running as an lthread, since these may block and
49 * effectively stall the lthread scheduler.
55 * An exiting lthread must not terminate the pthread it is running in
56 * since this would mean terminating the lthread scheduler.
57 * We override pthread_exit() with a macro because it is typically declared with
58 * __attribute__((noreturn))
60 void pthread_exit_override(void *v);
62 #define pthread_exit(v) do { \
63 pthread_exit_override((v)); \
68 * Enable/Disable pthread override
73 void pthread_override_set(int state);
77 * Return pthread override state
82 int pthread_override_get(void);
85 #endif /* _PTHREAD_SHIM_H_ */