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 |
42 // We don't actually need strings. But including this header seems to | 43 // We don't actually need strings. But including this header seems to |
43 // stop the compiler trying to short-circuit our pthreads existence | 44 // stop the compiler trying to short-circuit our pthreads existence |
44 // tests and claiming that the address of a function is always | 45 // tests and claiming that the address of a function is always |
45 // non-zero. I have no idea why ... | 46 // non-zero. I have no idea why ... |
46 #include <string> | 47 #include <string> |
47 #include "maybe_threads.h" | 48 #include "maybe_threads.h" |
48 #include "base/basictypes.h" | 49 #include "base/basictypes.h" |
49 | 50 |
50 // __THROW is defined in glibc systems. It means, counter-intuitively, | 51 // __THROW is defined in glibc systems. It means, counter-intuitively, |
51 // "This function will never throw an exception." It's an optional | 52 // "This function will never throw an exception." It's an optional |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 | 92 |
92 int perftools_pthread_setspecific(pthread_key_t key, void *val) { | 93 int perftools_pthread_setspecific(pthread_key_t key, void *val) { |
93 if (pthread_setspecific) { | 94 if (pthread_setspecific) { |
94 return pthread_setspecific(key, val); | 95 return pthread_setspecific(key, val); |
95 } else { | 96 } else { |
96 perftools_pthread_specific_vals[(int)key] = val; | 97 perftools_pthread_specific_vals[(int)key] = val; |
97 return 0; | 98 return 0; |
98 } | 99 } |
99 } | 100 } |
100 | 101 |
| 102 |
101 static pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT; | 103 static pthread_once_t pthread_once_init = PTHREAD_ONCE_INIT; |
102 int perftools_pthread_once(pthread_once_t *ctl, | 104 int perftools_pthread_once(pthread_once_t *ctl, |
103 void (*init_routine) (void)) { | 105 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 |
104 if (pthread_once) { | 124 if (pthread_once) { |
105 return pthread_once(ctl, init_routine); | 125 return pthread_once(ctl, init_routine); |
106 } else { | 126 } else { |
107 if (memcmp(ctl, &pthread_once_init, sizeof(*ctl)) == 0) { | 127 if (memcmp(ctl, &pthread_once_init, sizeof(*ctl)) == 0) { |
108 init_routine(); | 128 init_routine(); |
109 ++*(char*)(ctl); // make it so it's no longer equal to init | 129 ++*(char*)(ctl); // make it so it's no longer equal to init |
110 } | 130 } |
111 return 0; | 131 return 0; |
112 } | 132 } |
113 } | 133 } |
OLD | NEW |