4 * Copyright(c) 2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _PTHREAD_SHIM_H_
35 #define _PTHREAD_SHIM_H_
39 * This pthread shim is an example that demonstrates how legacy code
40 * that makes use of POSIX pthread services can make use of lthreads
41 * with reduced porting effort.
43 * N.B. The example is not a complete implementation, only a subset of
44 * pthread APIs sufficient to demonstrate the principle of operation
47 * In general pthread attribute objects do not have equivalent functions
48 * in lthreads, and are ignored.
50 * There is one exception and that is the use of attr to specify a
51 * core affinity in calls to pthread_create.
53 * The shim operates as follows:-
55 * On initialisation a constructor function uses dlsym to obtain and
56 * save the loaded address of the full set of pthread APIs that will
59 * For each function there is a stub provided that will invoke either
60 * the genuine pthread library function saved saved by the constructor,
61 * or else the corresponding equivalent lthread function.
63 * The stub functions are implemented in pthread_shim.c
65 * The stub will take care of adapting parameters, and any police
66 * any constraints where lthread functionality differs.
68 * The initial thread must always be a pure lthread.
70 * The decision whether to invoke the real library function or the lthread
71 * function is controlled by a per pthread flag that can be switched
72 * on of off by the pthread_override_set() API described below. Typcially
73 * this should be done as the first action of the initial lthread.
75 * N.B In general it would be poor practice to revert to invoke a real
76 * pthread function when running as an lthread, since these may block and
77 * effectively stall the lthread scheduler.
83 * An exiting lthread must not terminate the pthread it is running in
84 * since this would mean terminating the lthread scheduler.
85 * We override pthread_exit() with a macro because it is typically declared with
86 * __attribute__((noreturn))
88 void pthread_exit_override(void *v);
90 #define pthread_exit(v) do { \
91 pthread_exit_override((v)); \
96 * Enable/Disable pthread override
101 void pthread_override_set(int state);
105 * Return pthread override state
110 int pthread_override_get(void);
113 #endif /* _PTHREAD_SHIM_H_ */