10f8789407e8e066577efe3053fd15d0666a7b34
[deb_dpdk.git] / examples / performance-thread / pthread_shim / pthread_shim.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2015 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _PTHREAD_SHIM_H_
35 #define _PTHREAD_SHIM_H_
36
37 #include <rte_lcore.h>
38
39 /*
40  * This pthread shim is an example that demonstrates how legacy code
41  * that makes use of POSIX pthread services can make use of lthreads
42  * with reduced porting effort.
43  *
44  * N.B. The example is not a complete implementation, only a subset of
45  * pthread APIs sufficient to demonstrate the principle of operation
46  * are implemented.
47  *
48  * In general pthread attribute objects do not have equivalent functions
49  * in lthreads, and are ignored.
50  *
51  * There is one exception and that is the use of attr to specify a
52  * core affinity in calls to pthread_create.
53  *
54  * The shim operates as follows:-
55  *
56  * On initialisation a constructor function uses dlsym to obtain and
57  * save the loaded address of the full set of pthread APIs that will
58  * be overridden.
59  *
60  * For each function there is a stub provided that will invoke either
61  * the genuine pthread library function saved saved by the constructor,
62  * or else the corresponding equivalent lthread function.
63  *
64  * The stub functions are implemented in pthread_shim.c
65  *
66  * The stub will take care of adapting parameters, and any police
67  * any constraints where lthread functionality differs.
68  *
69  * The initial thread must always be a pure lthread.
70  *
71  * The decision whether to invoke the real library function or the lthread
72  * function is controlled by a per pthread flag that can be switched
73  * on of off by the pthread_override_set() API described below. Typcially
74  * this should be done as the first action of the initial lthread.
75  *
76  * N.B In general it would be poor practice to revert to invoke a real
77  * pthread function when running as an lthread, since these may block and
78  * effectively stall the lthread scheduler.
79  *
80  */
81
82
83 /*
84  * An exiting lthread must not terminate the pthread it is running in
85  * since this would mean terminating the lthread scheduler.
86  * We override pthread_exit() with a macro because it is typically declared with
87  * __attribute__((noreturn))
88  */
89 void pthread_exit_override(void *v);
90
91 #define pthread_exit(v) do { \
92         pthread_exit_override((v));     \
93         return NULL;    \
94 } while (0)
95
96 /*
97  * Enable/Disable pthread override
98  * state
99  * 0 disable
100  * 1 enable
101  */
102 void pthread_override_set(int state);
103
104
105 /*
106  * Return pthread override state
107  * return
108  * 0 disable
109  * 1 enable
110  */
111 int pthread_override_get(void);
112
113
114 #endif /* _PTHREAD_SHIM_H_ */