aboutsummaryrefslogtreecommitdiffstats
path: root/examples/performance-thread/pthread_shim/pthread_shim.h
blob: bba8ed00e4f4fb01a5989181d79e67b2f3a847a8 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/* SPDX-License-Identifier: BSD-3-Clause
 * Copyright(c) 2015 Intel Corporation
 */

#ifndef _PTHREAD_SHIM_H_
#define _PTHREAD_SHIM_H_

#include <rte_lcore.h>

/*
 * This pthread shim is an example that demonstrates how legacy code
 * that makes use of POSIX pthread services can make use of lthreads
 * with reduced porting effort.
 *
 * N.B. The example is not a complete implementation, only a subset of
 * pthread APIs sufficient to demonstrate the principle of operation
 * are implemented.
 *
 * In general pthread attribute objects do not have equivalent functions
 * in lthreads, and are ignored.
 *
 * There is one exception and that is the use of attr to specify a
 * core affinity in calls to pthread_create.
 *
 * The shim operates as follows:-
 *
 * On initialisation a constructor function uses dlsym to obtain and
 * save the loaded address of the full set of pthread APIs that will
 * be overridden.
 *
 * For each function there is a stub provided that will invoke either
 * the genuine pthread library function saved saved by the constructor,
 * or else the corresponding equivalent lthread function.
 *
 * The stub functions are implemented in pthread_shim.c
 *
 * The stub will take care of adapting parameters, and any police
 * any constraints where lthread functionality differs.
 *
 * The initial thread must always be a pure lthread.
 *
 * The decision whether to invoke the real library function or the lthread
 * function is controlled by a per pthread flag that can be switched
 * on of off by the pthread_override_set() API described below. Typcially
 * this should be done as the first action of the initial lthread.
 *
 * N.B In general it would be poor practice to revert to invoke a real
 * pthread function when running as an lthread, since these may block and
 * effectively stall the lthread scheduler.
 *
 */


/*
 * An exiting lthread must not terminate the pthread it is running in
 * since this would mean terminating the lthread scheduler.
 * We override pthread_exit() with a macro because it is typically declared with
 * __attribute__((noreturn))
 */
void pthread_exit_override(void *v);

#define pthread_exit(v) do { \
	pthread_exit_override((v));	\
	return NULL;	\
} while (0)

/*
 * Enable/Disable pthread override
 * state
 * 0 disable
 * 1 enable
 */
void pthread_override_set(int state);


/*
 * Return pthread override state
 * return
 * 0 disable
 * 1 enable
 */
int pthread_override_get(void);


#endif /* _PTHREAD_SHIM_H_ */