OLD | NEW |
1 // Copyright (c) 2005, Google Inc. | 1 // Copyright (c) 2005, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 21 matching lines...) Expand all Loading... |
32 // | 32 // |
33 // Some wrappers for pthread functions so that we can be LD_PRELOADed | 33 // Some wrappers for pthread functions so that we can be LD_PRELOADed |
34 // against non-pthreads apps. | 34 // against non-pthreads apps. |
35 // | 35 // |
36 // This module will behave very strangely if some pthreads functions | 36 // This module will behave very strangely if some pthreads functions |
37 // exist and others don't. | 37 // exist and others don't. |
38 | 38 |
39 #include "config.h" | 39 #include "config.h" |
40 #include <assert.h> | 40 #include <assert.h> |
41 #include <string.h> // for memcmp | 41 #include <string.h> // for memcmp |
42 #include <stdio.h> // for __isthreaded on FreeBSD | |
43 // We don't actually need strings. But including this header seems to | 42 // We don't actually need strings. But including this header seems to |
44 // stop the compiler trying to short-circuit our pthreads existence | 43 // stop the compiler trying to short-circuit our pthreads existence |
45 // tests and claiming that the address of a function is always | 44 // tests and claiming that the address of a function is always |
46 // non-zero. I have no idea why ... | 45 // non-zero. I have no idea why ... |
47 #include <string> | 46 #include <string> |
48 #include "maybe_threads.h" | 47 #include "maybe_threads.h" |
49 #include "base/basictypes.h" | 48 #include "base/basictypes.h" |
50 | 49 |
51 // __THROW is defined in glibc systems. It means, counter-intuitively, | 50 // __THROW is defined in glibc systems. It means, counter-intuitively, |
52 // "This function will never throw an exception." It's an optional | 51 // "This function will never throw an exception." It's an optional |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 91 |
93 int perftools_pthread_setspecific(pthread_key_t key, void *val) { | 92 int perftools_pthread_setspecific(pthread_key_t key, void *val) { |
94 if (pthread_setspecific) { | 93 if (pthread_setspecific) { |
95 return pthread_setspecific(key, val); | 94 return pthread_setspecific(key, val); |
96 } else { | 95 } else { |
97 perftools_pthread_specific_vals[(int)key] = val; | 96 perftools_pthread_specific_vals[(int)key] = val; |
98 return 0; | 97 return 0; |
99 } | 98 } |
100 } | 99 } |
101 | 100 |
102 | |
103 static pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT; | 101 static pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT; |
104 int perftools_pthread_once(pthread_once_t *ctl, | 102 int perftools_pthread_once(pthread_once_t *ctl, |
105 void (*init_routine) (void)) { | 103 void (*init_routine) (void)) { |
106 #ifdef __FreeBSD__ | |
107 // On __FreeBSD__, calling pthread_once on a system that is not | |
108 // linked with -pthread is silently a noop. :-( Luckily, we have a | |
109 // workaround: FreeBSD exposes __isthreaded in <stdio.h>, which is | |
110 // set to 1 when the first thread is spawned. So on those systems, | |
111 // we can use our own separate pthreads-once mechanism, which is | |
112 // used until __isthreaded is 1 (which will never be true if the app | |
113 // is not linked with -pthread). | |
114 static bool pthread_once_ran_before_threads = false; | |
115 if (pthread_once_ran_before_threads) { | |
116 return 0; | |
117 } | |
118 if (!__isthreaded) { | |
119 init_routine(); | |
120 pthread_once_ran_before_threads = true; | |
121 return 0; | |
122 } | |
123 #endif | |
124 if (pthread_once) { | 104 if (pthread_once) { |
125 return pthread_once(ctl, init_routine); | 105 return pthread_once(ctl, init_routine); |
126 } else { | 106 } else { |
127 if (memcmp(ctl, &pthread_once_init, sizeof(*ctl)) == 0) { | 107 if (memcmp(ctl, &pthread_once_init, sizeof(*ctl)) == 0) { |
128 init_routine(); | 108 init_routine(); |
129 ++*(char*)(ctl); // make it so it's no longer equal to init | 109 ++*(char*)(ctl); // make it so it's no longer equal to init |
130 } | 110 } |
131 return 0; | 111 return 0; |
132 } | 112 } |
133 } | 113 } |
OLD | NEW |