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 #include "base/logging.h"
52
53 // __THROW is defined in glibc systems. It means, counter-intuitively,
54 // "This function will never throw an exception." It's an optional
55 // optimization tool, but we may need to use it to match glibc prototypes.
56 #ifndef __THROW // I guess we're not on a glibc system
57 # define __THROW // __THROW is just an optimization, so ok to make it ""
58 #endif
59
60 // These are the methods we're going to conditionally include.
61 extern "C" {
62 int pthread_key_create (pthread_key_t*, void (*)(void*))
63 __THROW ATTRIBUTE_WEAK;
64 int pthread_key_delete (pthread_key_t)
65 __THROW ATTRIBUTE_WEAK;
66 void *pthread_getspecific(pthread_key_t)
67 __THROW ATTRIBUTE_WEAK;
68 int pthread_setspecific(pthread_key_t, const void*)
69 __THROW ATTRIBUTE_WEAK;
70 int pthread_once(pthread_once_t *, void (*)(void))
71 ATTRIBUTE_WEAK;
72 #ifdef HAVE_FORK
73 int pthread_atfork(void (*__prepare) (void),
74 void (*__parent) (void),
75 void (*__child) (void))
76 __THROW ATTRIBUTE_WEAK;
77 #endif
78 }
79
80 #define MAX_PERTHREAD_VALS 16
81 static void *perftools_pthread_specific_vals[MAX_PERTHREAD_VALS];
82 static int next_key;
83
84 // NOTE: it's similar to bitcast defined in basic_types.h with
85 // exception of ignoring sizes mismatch
86 template <typename T1, typename T2>
memcpy_cast(const T1 & input)87 static T2 memcpy_cast(const T1 &input) {
88 T2 output;
89 size_t s = sizeof(input);
90 if (sizeof(output) < s) {
91 s = sizeof(output);
92 }
93 memcpy(&output, &input, s);
94 return output;
95 }
96
perftools_pthread_key_create(pthread_key_t * key,void (* destr_function)(void *))97 int perftools_pthread_key_create(pthread_key_t *key,
98 void (*destr_function) (void *)) {
99 if (pthread_key_create) {
100 return pthread_key_create(key, destr_function);
101 } else {
102 assert(next_key < MAX_PERTHREAD_VALS);
103 *key = memcpy_cast<int, pthread_key_t>(next_key++);
104 return 0;
105 }
106 }
107
perftools_pthread_key_delete(pthread_key_t key)108 int perftools_pthread_key_delete(pthread_key_t key) {
109 if (pthread_key_delete) {
110 return pthread_key_delete(key);
111 } else {
112 return 0;
113 }
114 }
115
perftools_pthread_getspecific(pthread_key_t key)116 void *perftools_pthread_getspecific(pthread_key_t key) {
117 if (pthread_getspecific) {
118 return pthread_getspecific(key);
119 } else {
120 return perftools_pthread_specific_vals[memcpy_cast<pthread_key_t, int>(key)];
121 }
122 }
123
perftools_pthread_setspecific(pthread_key_t key,void * val)124 int perftools_pthread_setspecific(pthread_key_t key, void *val) {
125 if (pthread_setspecific) {
126 return pthread_setspecific(key, val);
127 } else {
128 perftools_pthread_specific_vals[memcpy_cast<pthread_key_t, int>(key)] = val;
129 return 0;
130 }
131 }
132
133
134 static pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT;
perftools_pthread_once(pthread_once_t * ctl,void (* init_routine)(void))135 int perftools_pthread_once(pthread_once_t *ctl,
136 void (*init_routine) (void)) {
137 #ifdef __FreeBSD__
138 // On __FreeBSD__, calling pthread_once on a system that is not
139 // linked with -pthread is silently a noop. :-( Luckily, we have a
140 // workaround: FreeBSD exposes __isthreaded in <stdio.h>, which is
141 // set to 1 when the first thread is spawned. So on those systems,
142 // we can use our own separate pthreads-once mechanism, which is
143 // used until __isthreaded is 1 (which will never be true if the app
144 // is not linked with -pthread).
145 static bool pthread_once_ran_before_threads = false;
146 if (pthread_once_ran_before_threads) {
147 return 0;
148 }
149 if (!__isthreaded) {
150 init_routine();
151 pthread_once_ran_before_threads = true;
152 return 0;
153 }
154 #endif
155 if (pthread_once) {
156 return pthread_once(ctl, init_routine);
157 } else {
158 if (memcmp(ctl, &pthread_once_init, sizeof(*ctl)) == 0) {
159 init_routine();
160 ++*(char*)(ctl); // make it so it's no longer equal to init
161 }
162 return 0;
163 }
164 }
165
166 #ifdef HAVE_FORK
167
perftools_pthread_atfork(void (* before)(),void (* parent_after)(),void (* child_after)())168 void perftools_pthread_atfork(void (*before)(),
169 void (*parent_after)(),
170 void (*child_after)()) {
171 if (pthread_atfork) {
172 int rv = pthread_atfork(before, parent_after, child_after);
173 CHECK(rv == 0);
174 }
175 }
176
177 #endif
178