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 #include <pthread.h> 37 38 /* 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. 42 * 43 * N.B. The example is not a complete implementation, only a subset of 44 * pthread APIs sufficient to demonstrate the principle of operation 45 * are implemented. 46 * 47 * In general pthread attribute objects do not have equivalent functions 48 * in lthreads, and are ignored. 49 * 50 * There is one exception and that is the use of attr to specify a 51 * core affinity in calls to pthread_create. 52 * 53 * The shim operates as follows:- 54 * 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 57 * be overridden. 58 * 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. 62 * 63 * The stub functions are implemented in pthread_shim.c 64 * 65 * The stub will take care of adapting parameters, and any police 66 * any constraints where lthread functionality differs. 67 * 68 * The initial thread must always be a pure lthread. 69 * 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. 74 * 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. 78 * 79 */ 80 81 82 /* 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)) 87 */ 88 void pthread_exit_override(void *v); 89 90 #define pthread_exit(v) do { \ 91 pthread_exit_override((v)); \ 92 return NULL; \ 93 } while (0) 94 95 /* 96 * Enable/Disable pthread override 97 * state 98 * 0 disable 99 * 1 enable 100 */ 101 void pthread_override_set(int state); 102 103 104 /* 105 * Return pthread override state 106 * return 107 * 0 disable 108 * 1 enable 109 */ 110 int pthread_override_get(void); 111 112 113 #endif /* _PTHREAD_SHIM_H_ */ 114