Austin Schuh | 745610d | 2015-09-06 18:19:50 -0700 | [diff] [blame] | 1 | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
| 2 | // Copyright (c) 2005, Google Inc. |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
| 8 | // |
| 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | // --- |
| 32 | // Author: Paul Menage <opensource@google.com> |
| 33 | // |
| 34 | // Some wrappers for pthread functions so that we can be LD_PRELOADed |
| 35 | // against non-pthreads apps. |
| 36 | // |
| 37 | // This module will behave very strangely if some pthreads functions |
| 38 | // exist and others don't. |
| 39 | |
| 40 | #include "config.h" |
| 41 | #include <assert.h> |
| 42 | #include <string.h> // for memcmp |
| 43 | #include <stdio.h> // for __isthreaded on FreeBSD |
| 44 | // We don't actually need strings. But including this header seems to |
| 45 | // stop the compiler trying to short-circuit our pthreads existence |
| 46 | // tests and claiming that the address of a function is always |
| 47 | // non-zero. I have no idea why ... |
| 48 | #include <string> |
| 49 | #include "maybe_threads.h" |
| 50 | #include "base/basictypes.h" |
| 51 | |
| 52 | // __THROW is defined in glibc systems. It means, counter-intuitively, |
| 53 | // "This function will never throw an exception." It's an optional |
| 54 | // optimization tool, but we may need to use it to match glibc prototypes. |
| 55 | #ifndef __THROW // I guess we're not on a glibc system |
| 56 | # define __THROW // __THROW is just an optimization, so ok to make it "" |
| 57 | #endif |
| 58 | |
| 59 | // These are the methods we're going to conditionally include. |
| 60 | extern "C" { |
| 61 | int pthread_key_create (pthread_key_t*, void (*)(void*)) |
| 62 | __THROW ATTRIBUTE_WEAK; |
| 63 | int pthread_key_delete (pthread_key_t) |
| 64 | __THROW ATTRIBUTE_WEAK; |
| 65 | void *pthread_getspecific(pthread_key_t) |
| 66 | __THROW ATTRIBUTE_WEAK; |
| 67 | int pthread_setspecific(pthread_key_t, const void*) |
| 68 | __THROW ATTRIBUTE_WEAK; |
| 69 | int pthread_once(pthread_once_t *, void (*)(void)) |
| 70 | ATTRIBUTE_WEAK; |
| 71 | } |
| 72 | |
| 73 | #define MAX_PERTHREAD_VALS 16 |
| 74 | static void *perftools_pthread_specific_vals[MAX_PERTHREAD_VALS]; |
| 75 | static int next_key; |
| 76 | |
| 77 | // NOTE: it's similar to bitcast defined in basic_types.h with |
| 78 | // exception of ignoring sizes mismatch |
| 79 | template <typename T1, typename T2> |
| 80 | static T2 memcpy_cast(const T1 &input) { |
| 81 | T2 output; |
| 82 | size_t s = sizeof(input); |
| 83 | if (sizeof(output) < s) { |
| 84 | s = sizeof(output); |
| 85 | } |
| 86 | memcpy(&output, &input, s); |
| 87 | return output; |
| 88 | } |
| 89 | |
| 90 | int perftools_pthread_key_create(pthread_key_t *key, |
| 91 | void (*destr_function) (void *)) { |
| 92 | if (pthread_key_create) { |
| 93 | return pthread_key_create(key, destr_function); |
| 94 | } else { |
| 95 | assert(next_key < MAX_PERTHREAD_VALS); |
| 96 | *key = memcpy_cast<int, pthread_key_t>(next_key++); |
| 97 | return 0; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | int perftools_pthread_key_delete(pthread_key_t key) { |
| 102 | if (pthread_key_delete) { |
| 103 | return pthread_key_delete(key); |
| 104 | } else { |
| 105 | return 0; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void *perftools_pthread_getspecific(pthread_key_t key) { |
| 110 | if (pthread_getspecific) { |
| 111 | return pthread_getspecific(key); |
| 112 | } else { |
| 113 | return perftools_pthread_specific_vals[memcpy_cast<pthread_key_t, int>(key)]; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | int perftools_pthread_setspecific(pthread_key_t key, void *val) { |
| 118 | if (pthread_setspecific) { |
| 119 | return pthread_setspecific(key, val); |
| 120 | } else { |
| 121 | perftools_pthread_specific_vals[memcpy_cast<pthread_key_t, int>(key)] = val; |
| 122 | return 0; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | static pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT; |
| 128 | int perftools_pthread_once(pthread_once_t *ctl, |
| 129 | void (*init_routine) (void)) { |
| 130 | #ifdef __FreeBSD__ |
| 131 | // On __FreeBSD__, calling pthread_once on a system that is not |
| 132 | // linked with -pthread is silently a noop. :-( Luckily, we have a |
| 133 | // workaround: FreeBSD exposes __isthreaded in <stdio.h>, which is |
| 134 | // set to 1 when the first thread is spawned. So on those systems, |
| 135 | // we can use our own separate pthreads-once mechanism, which is |
| 136 | // used until __isthreaded is 1 (which will never be true if the app |
| 137 | // is not linked with -pthread). |
| 138 | static bool pthread_once_ran_before_threads = false; |
| 139 | if (pthread_once_ran_before_threads) { |
| 140 | return 0; |
| 141 | } |
| 142 | if (!__isthreaded) { |
| 143 | init_routine(); |
| 144 | pthread_once_ran_before_threads = true; |
| 145 | return 0; |
| 146 | } |
| 147 | #endif |
| 148 | if (pthread_once) { |
| 149 | return pthread_once(ctl, init_routine); |
| 150 | } else { |
| 151 | if (memcmp(ctl, &pthread_once_init, sizeof(*ctl)) == 0) { |
| 152 | init_routine(); |
| 153 | ++*(char*)(ctl); // make it so it's no longer equal to init |
| 154 | } |
| 155 | return 0; |
| 156 | } |
| 157 | } |