sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 2 | /*--------------------------------------------------------------------*/ |
| 3 | /*--- A replacement for the standard libpthread.so. ---*/ |
| 4 | /*--- vg_libpthread.c ---*/ |
| 5 | /*--------------------------------------------------------------------*/ |
| 6 | |
| 7 | /* |
| 8 | This file is part of Valgrind, an x86 protected-mode emulator |
| 9 | designed for debugging and profiling binaries on x86-Unixes. |
| 10 | |
| 11 | Copyright (C) 2000-2002 Julian Seward |
| 12 | jseward@acm.org |
| 13 | |
| 14 | This program is free software; you can redistribute it and/or |
| 15 | modify it under the terms of the GNU General Public License as |
| 16 | published by the Free Software Foundation; either version 2 of the |
| 17 | License, or (at your option) any later version. |
| 18 | |
| 19 | This program is distributed in the hope that it will be useful, but |
| 20 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 22 | General Public License for more details. |
| 23 | |
| 24 | You should have received a copy of the GNU General Public License |
| 25 | along with this program; if not, write to the Free Software |
| 26 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 27 | 02111-1307, USA. |
| 28 | |
| 29 | The GNU General Public License is contained in the file LICENSE. |
| 30 | */ |
| 31 | |
| 32 | /* ALL THIS CODE RUNS ON THE SIMULATED CPU. |
| 33 | |
| 34 | This is a replacement for the standard libpthread.so. It is loaded |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 35 | as part of the client's image (if required) and directs pthread |
| 36 | calls through to Valgrind's request mechanism. |
| 37 | |
| 38 | A couple of caveats. |
| 39 | |
| 40 | 1. Since it's a binary-compatible replacement for an existing library, |
| 41 | we must take care to used exactly the same data layouts, etc, as |
| 42 | the standard pthread.so does. |
| 43 | |
| 44 | 2. Since this runs as part of the client, there are no specific |
| 45 | restrictions on what headers etc we can include, so long as |
| 46 | this libpthread.so does not end up having dependencies on .so's |
| 47 | which the real one doesn't. |
| 48 | |
| 49 | Later ... it appears we cannot call file-related stuff in libc here, |
| 50 | perhaps fair enough. Be careful what you call from here. Even exit() |
| 51 | doesn't work (gives infinite recursion and then stack overflow); hence |
| 52 | myexit(). Also fprintf doesn't seem safe. |
| 53 | */ |
| 54 | |
| 55 | #include "valgrind.h" /* For the request-passing mechanism */ |
| 56 | #include "vg_include.h" /* For the VG_USERREQ__* constants */ |
| 57 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 58 | #include <unistd.h> |
| 59 | #include <string.h> |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 60 | #ifdef GLIBC_2_1 |
| 61 | #include <sys/time.h> |
| 62 | #endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 63 | |
| 64 | /* --------------------------------------------------------------------- |
| 65 | Helpers. We have to be pretty self-sufficient. |
| 66 | ------------------------------------------------------------------ */ |
| 67 | |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 68 | /* Number of times any given error message is printed. */ |
| 69 | #define N_MOANS 3 |
| 70 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 71 | /* Extract from Valgrind the value of VG_(clo_trace_pthread_level). |
| 72 | Returns 0 (none) if not running on Valgrind. */ |
| 73 | static |
| 74 | int get_pt_trace_level ( void ) |
| 75 | { |
| 76 | int res; |
| 77 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 78 | VG_USERREQ__GET_PTHREAD_TRACE_LEVEL, |
| 79 | 0, 0, 0, 0); |
| 80 | return res; |
| 81 | } |
| 82 | |
| 83 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 84 | static |
| 85 | void myexit ( int arg ) |
| 86 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 87 | int __res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 88 | __asm__ volatile ("movl %%ecx, %%ebx ; int $0x80" |
| 89 | : "=a" (__res) |
| 90 | : "0" (__NR_exit), |
| 91 | "c" (arg) ); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 92 | /* We don't bother to mention the fact that this asm trashes %ebx, |
| 93 | since it won't return. If you ever do let it return ... fix |
| 94 | this! */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 98 | /* We need this guy -- it's in valgrind.so. */ |
| 99 | extern void VG_(startup) ( void ); |
| 100 | |
| 101 | |
| 102 | /* Just start up Valgrind if it's not already going. VG_(startup)() |
| 103 | detects and ignores second and subsequent calls. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 104 | static __inline__ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 105 | void ensure_valgrind ( char* caller ) |
| 106 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 107 | VG_(startup)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 108 | } |
| 109 | |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 110 | /* While we're at it ... hook our own startup function into this |
| 111 | game. */ |
| 112 | __asm__ ( |
| 113 | ".section .init\n" |
| 114 | "\tcall vgPlain_startup" |
| 115 | ); |
| 116 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 117 | |
| 118 | static |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 119 | __attribute__((noreturn)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 120 | void barf ( char* str ) |
| 121 | { |
| 122 | char buf[100]; |
| 123 | buf[0] = 0; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 124 | strcat(buf, "\nvalgrind's libpthread.so: "); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 125 | strcat(buf, str); |
| 126 | strcat(buf, "\n\n"); |
| 127 | write(2, buf, strlen(buf)); |
| 128 | myexit(1); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 129 | /* We have to persuade gcc into believing this doesn't return. */ |
| 130 | while (1) { }; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 134 | static void ignored ( char* msg ) |
| 135 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 136 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 137 | char* ig = "valgrind's libpthread.so: IGNORED call to: "; |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 138 | write(2, ig, strlen(ig)); |
| 139 | write(2, msg, strlen(msg)); |
| 140 | ig = "\n"; |
| 141 | write(2, ig, strlen(ig)); |
| 142 | } |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 143 | } |
| 144 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 145 | static void kludged ( char* msg ) |
| 146 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 147 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 148 | char* ig = "valgrind's libpthread.so: KLUDGED call to: "; |
| 149 | write(2, ig, strlen(ig)); |
| 150 | write(2, msg, strlen(msg)); |
| 151 | ig = "\n"; |
| 152 | write(2, ig, strlen(ig)); |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | static void not_inside ( char* msg ) |
| 157 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 158 | VG_(startup)(); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 159 | } |
| 160 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 161 | void vgPlain_unimp ( char* what ) |
| 162 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 163 | char* ig = "valgrind's libpthread.so: UNIMPLEMENTED FUNCTION: "; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 164 | write(2, ig, strlen(ig)); |
| 165 | write(2, what, strlen(what)); |
| 166 | ig = "\n"; |
| 167 | write(2, ig, strlen(ig)); |
| 168 | barf("Please report this bug to me at: jseward@acm.org"); |
| 169 | } |
| 170 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 171 | |
| 172 | /* --------------------------------------------------------------------- |
| 173 | Pass pthread_ calls to Valgrind's request mechanism. |
| 174 | ------------------------------------------------------------------ */ |
| 175 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 176 | #include <pthread.h> |
| 177 | #include <stdio.h> |
| 178 | #include <errno.h> |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 179 | #include <assert.h> |
| 180 | #include <sys/time.h> /* gettimeofday */ |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 181 | |
| 182 | /* --------------------------------------------------- |
| 183 | THREAD ATTRIBUTES |
| 184 | ------------------------------------------------ */ |
| 185 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 186 | int pthread_attr_init(pthread_attr_t *attr) |
| 187 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 188 | static int moans = N_MOANS; |
| 189 | if (moans-- > 0) |
| 190 | ignored("pthread_attr_init"); |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) |
| 195 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 196 | static int moans = N_MOANS; |
| 197 | if (moans-- > 0) |
| 198 | ignored("pthread_attr_setdetachstate"); |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 199 | return 0; |
| 200 | } |
| 201 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 202 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit) |
| 203 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 204 | static int moans = N_MOANS; |
| 205 | if (moans-- > 0) |
| 206 | ignored("pthread_attr_setinheritsched"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 207 | return 0; |
| 208 | } |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 209 | |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 210 | __attribute__((weak)) |
| 211 | int pthread_attr_setstacksize (pthread_attr_t *__attr, |
| 212 | size_t __stacksize) |
| 213 | { |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 214 | size_t limit; |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 215 | ensure_valgrind("pthread_attr_setstacksize"); |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 216 | limit = VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 217 | - 1000; /* paranoia */ |
| 218 | if (__stacksize < limit) |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 219 | return 0; |
| 220 | barf("pthread_attr_setstacksize: " |
| 221 | "requested size >= VG_PTHREAD_STACK_SIZE\n " |
| 222 | "edit vg_include.h and rebuild."); |
| 223 | } |
| 224 | |
| 225 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 226 | /* This is completely bogus. */ |
| 227 | int pthread_attr_getschedparam(const pthread_attr_t *attr, |
| 228 | struct sched_param *param) |
| 229 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 230 | static int moans = N_MOANS; |
| 231 | if (moans-- > 0) |
| 232 | kludged("pthread_attr_getschedparam"); |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 233 | # ifdef GLIBC_2_1 |
| 234 | if (param) param->sched_priority = 0; /* who knows */ |
| 235 | # else |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 236 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 237 | # endif |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | int pthread_attr_setschedparam(pthread_attr_t *attr, |
| 242 | const struct sched_param *param) |
| 243 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 244 | static int moans = N_MOANS; |
| 245 | if (moans-- > 0) |
| 246 | ignored("pthread_attr_setschedparam"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int pthread_attr_destroy(pthread_attr_t *attr) |
| 251 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 252 | static int moans = N_MOANS; |
| 253 | if (moans-- > 0) |
| 254 | ignored("pthread_attr_destroy"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 257 | |
| 258 | /* --------------------------------------------------- |
| 259 | THREADs |
| 260 | ------------------------------------------------ */ |
| 261 | |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame^] | 262 | __attribute__((weak)) |
| 263 | int pthread_yield ( void ) |
| 264 | { |
| 265 | int res; |
| 266 | ensure_valgrind("pthread_yield"); |
| 267 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 268 | VG_USERREQ__PTHREAD_YIELD, 0, 0, 0, 0); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 273 | int pthread_equal(pthread_t thread1, pthread_t thread2) |
| 274 | { |
| 275 | return thread1 == thread2 ? 1 : 0; |
| 276 | } |
| 277 | |
| 278 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 279 | int |
| 280 | pthread_create (pthread_t *__restrict __thread, |
| 281 | __const pthread_attr_t *__restrict __attr, |
| 282 | void *(*__start_routine) (void *), |
| 283 | void *__restrict __arg) |
| 284 | { |
| 285 | int res; |
| 286 | ensure_valgrind("pthread_create"); |
| 287 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 288 | VG_USERREQ__PTHREAD_CREATE, |
| 289 | __thread, __attr, __start_routine, __arg); |
| 290 | return res; |
| 291 | } |
| 292 | |
| 293 | |
| 294 | |
| 295 | int |
| 296 | pthread_join (pthread_t __th, void **__thread_return) |
| 297 | { |
| 298 | int res; |
| 299 | ensure_valgrind("pthread_join"); |
| 300 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 301 | VG_USERREQ__PTHREAD_JOIN, |
| 302 | __th, __thread_return, 0, 0); |
| 303 | return res; |
| 304 | } |
| 305 | |
| 306 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 307 | void pthread_exit(void *retval) |
| 308 | { |
| 309 | int res; |
| 310 | ensure_valgrind("pthread_exit"); |
| 311 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 312 | VG_USERREQ__PTHREAD_EXIT, |
| 313 | retval, 0, 0, 0); |
| 314 | /* Doesn't return! */ |
| 315 | /* However, we have to fool gcc into knowing that. */ |
| 316 | barf("pthread_exit: still alive after request?!"); |
| 317 | } |
| 318 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 319 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 320 | pthread_t pthread_self(void) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 321 | { |
| 322 | int tid; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 323 | ensure_valgrind("pthread_self"); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 324 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 325 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 326 | 0, 0, 0, 0); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 327 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 328 | barf("pthread_self: invalid ThreadId"); |
| 329 | return tid; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 333 | int pthread_detach(pthread_t th) |
| 334 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 335 | static int moans = N_MOANS; |
| 336 | if (moans-- > 0) |
| 337 | ignored("pthread_detach"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 342 | /* --------------------------------------------------- |
| 343 | MUTEX ATTRIBUTES |
| 344 | ------------------------------------------------ */ |
| 345 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 346 | int __pthread_mutexattr_init(pthread_mutexattr_t *attr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 347 | { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 348 | attr->__mutexkind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 349 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 350 | } |
| 351 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 352 | int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 353 | { |
| 354 | switch (type) { |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 355 | # ifndef GLIBC_2_1 |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 356 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 357 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 358 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 359 | # ifdef GLIBC_2_1 |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 360 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 361 | # endif |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 362 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 363 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 364 | attr->__mutexkind = type; |
| 365 | return 0; |
| 366 | default: |
| 367 | return EINVAL; |
| 368 | } |
| 369 | } |
| 370 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 371 | int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 372 | { |
| 373 | return 0; |
| 374 | } |
| 375 | |
| 376 | |
| 377 | /* --------------------------------------------------- |
| 378 | MUTEXes |
| 379 | ------------------------------------------------ */ |
| 380 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 381 | int __pthread_mutex_init(pthread_mutex_t *mutex, |
| 382 | const pthread_mutexattr_t *mutexattr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 383 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 384 | mutex->__m_count = 0; |
| 385 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 386 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
| 387 | if (mutexattr) |
| 388 | mutex->__m_kind = mutexattr->__mutexkind; |
| 389 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 390 | } |
| 391 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 392 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 393 | int __pthread_mutex_lock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 394 | { |
| 395 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 396 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 397 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 398 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 399 | VG_USERREQ__PTHREAD_MUTEX_LOCK, |
| 400 | mutex, 0, 0, 0); |
| 401 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 402 | } else { |
| 403 | if (moans-- > 0) |
| 404 | not_inside("pthread_mutex_lock"); |
| 405 | return 0; /* success */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 406 | } |
| 407 | } |
| 408 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 409 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 410 | int __pthread_mutex_trylock(pthread_mutex_t *mutex) |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 411 | { |
| 412 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 413 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 414 | if (RUNNING_ON_VALGRIND) { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 415 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 416 | VG_USERREQ__PTHREAD_MUTEX_TRYLOCK, |
| 417 | mutex, 0, 0, 0); |
| 418 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 419 | } else { |
| 420 | if (moans-- > 0) |
| 421 | not_inside("pthread_mutex_trylock"); |
| 422 | return 0; |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 423 | } |
| 424 | } |
| 425 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 426 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 427 | int __pthread_mutex_unlock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 428 | { |
| 429 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 430 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 431 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 432 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 433 | VG_USERREQ__PTHREAD_MUTEX_UNLOCK, |
| 434 | mutex, 0, 0, 0); |
| 435 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 436 | } else { |
| 437 | if (moans-- > 0) |
| 438 | not_inside("pthread_mutex_unlock"); |
| 439 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 443 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 444 | int __pthread_mutex_destroy(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 445 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 446 | /* Valgrind doesn't hold any resources on behalf of the mutex, so no |
| 447 | need to involve it. */ |
| 448 | if (mutex->__m_count > 0) |
| 449 | return EBUSY; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 450 | mutex->__m_count = 0; |
| 451 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 452 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 453 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 457 | /* --------------------------------------------------- |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 458 | CONDITION VARIABLES |
| 459 | ------------------------------------------------ */ |
| 460 | |
| 461 | /* LinuxThreads supports no attributes for conditions. Hence ... */ |
| 462 | |
| 463 | int pthread_condattr_init(pthread_condattr_t *attr) |
| 464 | { |
| 465 | return 0; |
| 466 | } |
| 467 | |
sewardj | 0738a59 | 2002-04-20 13:59:33 +0000 | [diff] [blame] | 468 | int pthread_condattr_destroy(pthread_condattr_t *attr) |
| 469 | { |
| 470 | return 0; |
| 471 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 472 | |
| 473 | int pthread_cond_init( pthread_cond_t *cond, |
| 474 | const pthread_condattr_t *cond_attr) |
| 475 | { |
| 476 | cond->__c_waiting = (_pthread_descr)VG_INVALID_THREADID; |
| 477 | return 0; |
| 478 | } |
| 479 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 480 | int pthread_cond_destroy(pthread_cond_t *cond) |
| 481 | { |
| 482 | /* should check that no threads are waiting on this CV */ |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 483 | static int moans = N_MOANS; |
| 484 | if (moans-- > 0) |
| 485 | kludged("pthread_cond_destroy"); |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 488 | |
| 489 | /* --------------------------------------------------- |
| 490 | SCHEDULING |
| 491 | ------------------------------------------------ */ |
| 492 | |
| 493 | /* This is completely bogus. */ |
| 494 | int pthread_getschedparam(pthread_t target_thread, |
| 495 | int *policy, |
| 496 | struct sched_param *param) |
| 497 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 498 | static int moans = N_MOANS; |
| 499 | if (moans-- > 0) |
| 500 | kludged("pthread_getschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 501 | if (policy) *policy = SCHED_OTHER; |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 502 | # ifdef GLIBC_2_1 |
| 503 | if (param) param->sched_priority = 0; /* who knows */ |
| 504 | # else |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 505 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 506 | # endif |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | int pthread_setschedparam(pthread_t target_thread, |
| 511 | int policy, |
| 512 | const struct sched_param *param) |
| 513 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 514 | static int moans = N_MOANS; |
| 515 | if (moans-- > 0) |
| 516 | ignored("pthread_setschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 520 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 521 | { |
| 522 | int res; |
| 523 | ensure_valgrind("pthread_cond_wait"); |
| 524 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 525 | VG_USERREQ__PTHREAD_COND_WAIT, |
| 526 | cond, mutex, 0, 0); |
| 527 | return res; |
| 528 | } |
| 529 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 530 | int pthread_cond_timedwait ( pthread_cond_t *cond, |
| 531 | pthread_mutex_t *mutex, |
| 532 | const struct timespec *abstime ) |
| 533 | { |
| 534 | int res; |
| 535 | unsigned int ms_now, ms_end; |
| 536 | struct timeval timeval_now; |
| 537 | unsigned long long int ull_ms_now_after_1970; |
| 538 | unsigned long long int ull_ms_end_after_1970; |
| 539 | |
| 540 | ensure_valgrind("pthread_cond_timedwait"); |
| 541 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 542 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 543 | 0, 0, 0, 0); |
| 544 | assert(ms_now != 0xFFFFFFFF); |
| 545 | res = gettimeofday(&timeval_now, NULL); |
| 546 | assert(res == 0); |
| 547 | |
| 548 | ull_ms_now_after_1970 |
| 549 | = 1000ULL * ((unsigned long long int)(timeval_now.tv_sec)) |
| 550 | + ((unsigned long long int)(timeval_now.tv_usec / 1000000)); |
| 551 | ull_ms_end_after_1970 |
| 552 | = 1000ULL * ((unsigned long long int)(abstime->tv_sec)) |
| 553 | + ((unsigned long long int)(abstime->tv_nsec / 1000000)); |
| 554 | assert(ull_ms_end_after_1970 >= ull_ms_now_after_1970); |
| 555 | ms_end |
| 556 | = ms_now + (unsigned int)(ull_ms_end_after_1970 - ull_ms_now_after_1970); |
| 557 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 558 | VG_USERREQ__PTHREAD_COND_TIMEDWAIT, |
| 559 | cond, mutex, ms_end, 0); |
| 560 | return res; |
| 561 | } |
| 562 | |
| 563 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 564 | int pthread_cond_signal(pthread_cond_t *cond) |
| 565 | { |
| 566 | int res; |
| 567 | ensure_valgrind("pthread_cond_signal"); |
| 568 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 569 | VG_USERREQ__PTHREAD_COND_SIGNAL, |
| 570 | cond, 0, 0, 0); |
| 571 | return res; |
| 572 | } |
| 573 | |
| 574 | int pthread_cond_broadcast(pthread_cond_t *cond) |
| 575 | { |
| 576 | int res; |
| 577 | ensure_valgrind("pthread_cond_broadcast"); |
| 578 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 579 | VG_USERREQ__PTHREAD_COND_BROADCAST, |
| 580 | cond, 0, 0, 0); |
| 581 | return res; |
| 582 | } |
| 583 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 584 | |
| 585 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 586 | CANCELLATION |
| 587 | ------------------------------------------------ */ |
| 588 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 589 | int pthread_setcancelstate(int state, int *oldstate) |
| 590 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 591 | static int moans = N_MOANS; |
| 592 | if (moans-- > 0) |
| 593 | ignored("pthread_setcancelstate"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 594 | return 0; |
| 595 | } |
| 596 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 597 | int pthread_setcanceltype(int type, int *oldtype) |
| 598 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 599 | static int moans = N_MOANS; |
| 600 | if (moans-- > 0) |
| 601 | ignored("pthread_setcanceltype"); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 602 | return 0; |
| 603 | } |
| 604 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 605 | int pthread_cancel(pthread_t thread) |
| 606 | { |
| 607 | int res; |
| 608 | ensure_valgrind("pthread_cancel"); |
| 609 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 610 | VG_USERREQ__PTHREAD_CANCEL, |
| 611 | thread, 0, 0, 0); |
| 612 | return res; |
| 613 | } |
| 614 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 615 | void pthread_testcancel(void) |
| 616 | { |
| 617 | } |
| 618 | |
| 619 | /*-------------------*/ |
| 620 | static pthread_mutex_t massacre_mx = PTHREAD_MUTEX_INITIALIZER; |
| 621 | |
| 622 | void __pthread_kill_other_threads_np ( void ) |
| 623 | { |
| 624 | int i, res, me; |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 625 | __pthread_mutex_lock(&massacre_mx); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 626 | me = pthread_self(); |
| 627 | for (i = 1; i < VG_N_THREADS; i++) { |
| 628 | if (i == me) continue; |
| 629 | res = pthread_cancel(i); |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 630 | if (0 && res == 0) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 631 | printf("----------- NUKED %d\n", i); |
| 632 | } |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 633 | __pthread_mutex_unlock(&massacre_mx); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 634 | } |
| 635 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 636 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 637 | /* --------------------------------------------------- |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 638 | SIGNALS |
| 639 | ------------------------------------------------ */ |
| 640 | |
| 641 | #include <signal.h> |
| 642 | |
| 643 | int pthread_sigmask(int how, const sigset_t *newmask, |
| 644 | sigset_t *oldmask) |
| 645 | { |
| 646 | int res; |
| 647 | |
| 648 | /* A bit subtle, because the scheduler expects newmask and oldmask |
| 649 | to be vki_sigset_t* rather than sigset_t*, and the two are |
| 650 | different. Fortunately the first 64 bits of a sigset_t are |
| 651 | exactly a vki_sigset_t, so we just pass the pointers through |
| 652 | unmodified. Haaaack! |
| 653 | |
| 654 | Also mash the how value so that the SIG_ constants from glibc |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 655 | constants to VKI_ constants, so that the former do not have to |
| 656 | be included into vg_scheduler.c. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 657 | |
| 658 | ensure_valgrind("pthread_sigmask"); |
| 659 | |
| 660 | switch (how) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 661 | case SIG_SETMASK: how = VKI_SIG_SETMASK; break; |
| 662 | case SIG_BLOCK: how = VKI_SIG_BLOCK; break; |
| 663 | case SIG_UNBLOCK: how = VKI_SIG_UNBLOCK; break; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 664 | default: return EINVAL; |
| 665 | } |
| 666 | |
| 667 | /* Crude check */ |
| 668 | if (newmask == NULL) |
| 669 | return EFAULT; |
| 670 | |
| 671 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 672 | VG_USERREQ__PTHREAD_SIGMASK, |
| 673 | how, newmask, oldmask, 0); |
| 674 | |
| 675 | /* The scheduler tells us of any memory violations. */ |
| 676 | return res == 0 ? 0 : EFAULT; |
| 677 | } |
| 678 | |
| 679 | |
| 680 | int sigwait ( const sigset_t* set, int* sig ) |
| 681 | { |
| 682 | int res; |
| 683 | ensure_valgrind("sigwait"); |
| 684 | /* As with pthread_sigmask we deliberately confuse sigset_t with |
| 685 | vki_ksigset_t. */ |
| 686 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 687 | VG_USERREQ__SIGWAIT, |
| 688 | set, sig, 0, 0); |
| 689 | return res; |
| 690 | } |
| 691 | |
| 692 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 693 | int pthread_kill(pthread_t thread, int signo) |
| 694 | { |
| 695 | int res; |
| 696 | ensure_valgrind("pthread_kill"); |
| 697 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 698 | VG_USERREQ__PTHREAD_KILL, |
| 699 | thread, signo, 0, 0); |
| 700 | return res; |
| 701 | } |
| 702 | |
| 703 | |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 704 | /* Copied verbatim from Linuxthreads */ |
| 705 | /* Redefine raise() to send signal to calling thread only, |
| 706 | as per POSIX 1003.1c */ |
| 707 | int raise (int sig) |
| 708 | { |
| 709 | int retcode = pthread_kill(pthread_self(), sig); |
| 710 | if (retcode == 0) |
| 711 | return 0; |
| 712 | else { |
| 713 | errno = retcode; |
| 714 | return -1; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 719 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 720 | THREAD-SPECIFICs |
| 721 | ------------------------------------------------ */ |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 722 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 723 | int __pthread_key_create(pthread_key_t *key, |
| 724 | void (*destr_function) (void *)) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 725 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 726 | int res; |
| 727 | ensure_valgrind("pthread_key_create"); |
| 728 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 729 | VG_USERREQ__PTHREAD_KEY_CREATE, |
| 730 | key, destr_function, 0, 0); |
| 731 | return res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | int pthread_key_delete(pthread_key_t key) |
| 735 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 736 | static int moans = N_MOANS; |
| 737 | if (moans-- > 0) |
| 738 | ignored("pthread_key_delete"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 739 | return 0; |
| 740 | } |
| 741 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 742 | int __pthread_setspecific(pthread_key_t key, const void *pointer) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 743 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 744 | int res; |
| 745 | ensure_valgrind("pthread_setspecific"); |
| 746 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 747 | VG_USERREQ__PTHREAD_SETSPECIFIC, |
| 748 | key, pointer, 0, 0); |
| 749 | return res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 750 | } |
| 751 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 752 | void * __pthread_getspecific(pthread_key_t key) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 753 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 754 | int res; |
| 755 | ensure_valgrind("pthread_getspecific"); |
| 756 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 757 | VG_USERREQ__PTHREAD_GETSPECIFIC, |
| 758 | key, 0 , 0, 0); |
| 759 | return (void*)res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 760 | } |
| 761 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 762 | |
| 763 | /* --------------------------------------------------- |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 764 | ONCEry |
| 765 | ------------------------------------------------ */ |
| 766 | |
| 767 | static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER; |
| 768 | |
| 769 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 770 | int __pthread_once ( pthread_once_t *once_control, |
| 771 | void (*init_routine) (void) ) |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 772 | { |
| 773 | int res; |
| 774 | ensure_valgrind("pthread_once"); |
| 775 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 776 | res = __pthread_mutex_lock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 777 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 778 | if (res != 0) { |
| 779 | printf("res = %d\n",res); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 780 | barf("pthread_once: Looks like your program's " |
| 781 | "init routine calls back to pthread_once() ?!"); |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 782 | } |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 783 | |
| 784 | if (*once_control == 0) { |
| 785 | *once_control = 1; |
| 786 | init_routine(); |
| 787 | } |
| 788 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 789 | __pthread_mutex_unlock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | |
| 795 | /* --------------------------------------------------- |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 796 | MISC |
| 797 | ------------------------------------------------ */ |
| 798 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 799 | int __pthread_atfork ( void (*prepare)(void), |
| 800 | void (*parent)(void), |
| 801 | void (*child)(void) ) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 802 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 803 | static int moans = N_MOANS; |
| 804 | if (moans-- > 0) |
| 805 | ignored("pthread_atfork"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 810 | __attribute__((weak)) |
| 811 | void __pthread_initialize ( void ) |
| 812 | { |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 813 | ensure_valgrind("__pthread_initialize"); |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 814 | } |
| 815 | |
| 816 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 817 | /* --------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 818 | LIBRARY-PRIVATE THREAD SPECIFIC STATE |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 819 | ------------------------------------------------ */ |
| 820 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 821 | #include <resolv.h> |
| 822 | static int thread_specific_errno[VG_N_THREADS]; |
| 823 | static int thread_specific_h_errno[VG_N_THREADS]; |
| 824 | static struct __res_state |
| 825 | thread_specific_res_state[VG_N_THREADS]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 826 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 827 | int* __errno_location ( void ) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 828 | { |
| 829 | int tid; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 830 | /* ensure_valgrind("__errno_location"); */ |
| 831 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 832 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 833 | 0, 0, 0, 0); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 834 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 835 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 836 | barf("__errno_location: invalid ThreadId"); |
| 837 | return & thread_specific_errno[tid]; |
| 838 | } |
| 839 | |
| 840 | int* __h_errno_location ( void ) |
| 841 | { |
| 842 | int tid; |
| 843 | /* ensure_valgrind("__h_errno_location"); */ |
| 844 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 845 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 846 | 0, 0, 0, 0); |
| 847 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 848 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 849 | barf("__h_errno_location: invalid ThreadId"); |
| 850 | return & thread_specific_h_errno[tid]; |
| 851 | } |
| 852 | |
| 853 | struct __res_state* __res_state ( void ) |
| 854 | { |
| 855 | int tid; |
| 856 | /* ensure_valgrind("__res_state"); */ |
| 857 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 858 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 859 | 0, 0, 0, 0); |
| 860 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 861 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 862 | barf("__res_state: invalid ThreadId"); |
| 863 | return & thread_specific_res_state[tid]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 867 | /* --------------------------------------------------- |
| 868 | LIBC-PRIVATE SPECIFIC DATA |
| 869 | ------------------------------------------------ */ |
| 870 | |
| 871 | /* Relies on assumption that initial private data is NULL. This |
| 872 | should be fixed somehow. */ |
| 873 | |
| 874 | /* The allowable keys (indices) (all 2 of them). |
| 875 | From sysdeps/pthread/bits/libc-tsd.h |
| 876 | */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 877 | #define N_LIBC_TSD_EXTRA_KEYS 1 |
| 878 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 879 | enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0, |
| 880 | _LIBC_TSD_KEY_DL_ERROR, |
| 881 | _LIBC_TSD_KEY_N }; |
| 882 | |
| 883 | /* Auto-initialising subsystem. libc_specifics_inited is set |
| 884 | after initialisation. libc_specifics_inited_mx guards it. */ |
| 885 | static int libc_specifics_inited = 0; |
| 886 | static pthread_mutex_t libc_specifics_inited_mx = PTHREAD_MUTEX_INITIALIZER; |
| 887 | |
| 888 | /* These are the keys we must initialise the first time. */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 889 | static pthread_key_t libc_specifics_keys[_LIBC_TSD_KEY_N |
| 890 | + N_LIBC_TSD_EXTRA_KEYS]; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 891 | |
| 892 | /* Initialise the keys, if they are not already initialise. */ |
| 893 | static |
| 894 | void init_libc_tsd_keys ( void ) |
| 895 | { |
| 896 | int res, i; |
| 897 | pthread_key_t k; |
| 898 | |
| 899 | res = pthread_mutex_lock(&libc_specifics_inited_mx); |
| 900 | if (res != 0) barf("init_libc_tsd_keys: lock"); |
| 901 | |
| 902 | if (libc_specifics_inited == 0) { |
| 903 | /* printf("INIT libc specifics\n"); */ |
| 904 | libc_specifics_inited = 1; |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 905 | for (i = 0; i < _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS; i++) { |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 906 | res = pthread_key_create(&k, NULL); |
| 907 | if (res != 0) barf("init_libc_tsd_keys: create"); |
| 908 | libc_specifics_keys[i] = k; |
| 909 | } |
| 910 | } |
| 911 | |
| 912 | res = pthread_mutex_unlock(&libc_specifics_inited_mx); |
| 913 | if (res != 0) barf("init_libc_tsd_keys: unlock"); |
| 914 | } |
| 915 | |
| 916 | |
| 917 | static int |
| 918 | libc_internal_tsd_set ( enum __libc_tsd_key_t key, |
| 919 | const void * pointer ) |
| 920 | { |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 921 | int res; |
| 922 | static int moans = N_MOANS; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 923 | /* printf("SET SET SET key %d ptr %p\n", key, pointer); */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 924 | if (key < _LIBC_TSD_KEY_MALLOC |
| 925 | || key >= _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 926 | barf("libc_internal_tsd_set: invalid key"); |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 927 | if (key >= _LIBC_TSD_KEY_N && moans-- > 0) |
| 928 | fprintf(stderr, |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 929 | "valgrind's libpthread.so: libc_internal_tsd_set: " |
| 930 | "dubious key %d\n", key); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 931 | init_libc_tsd_keys(); |
| 932 | res = pthread_setspecific(libc_specifics_keys[key], pointer); |
| 933 | if (res != 0) barf("libc_internal_tsd_set: setspecific failed"); |
| 934 | return 0; |
| 935 | } |
| 936 | |
| 937 | static void * |
| 938 | libc_internal_tsd_get ( enum __libc_tsd_key_t key ) |
| 939 | { |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 940 | void* v; |
| 941 | static int moans = N_MOANS; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 942 | /* printf("GET GET GET key %d\n", key); */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 943 | if (key < _LIBC_TSD_KEY_MALLOC |
| 944 | || key >= _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 945 | barf("libc_internal_tsd_get: invalid key"); |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 946 | if (key >= _LIBC_TSD_KEY_N && moans-- > 0) |
| 947 | fprintf(stderr, |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 948 | "valgrind's libpthread.so: libc_internal_tsd_get: " |
| 949 | "dubious key %d\n", key); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 950 | init_libc_tsd_keys(); |
| 951 | v = pthread_getspecific(libc_specifics_keys[key]); |
| 952 | /* if (v == NULL) barf("libc_internal_tsd_set: getspecific failed"); */ |
| 953 | return v; |
| 954 | } |
| 955 | |
| 956 | |
| 957 | |
| 958 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 959 | int (*__libc_internal_tsd_set) |
| 960 | (enum __libc_tsd_key_t key, const void * pointer) |
| 961 | = libc_internal_tsd_set; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 962 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 963 | void* (*__libc_internal_tsd_get) |
| 964 | (enum __libc_tsd_key_t key) |
| 965 | = libc_internal_tsd_get; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 966 | |
| 967 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 968 | /* --------------------------------------------------------------------- |
| 969 | These are here (I think) because they are deemed cancellation |
| 970 | points by POSIX. For the moment we'll simply pass the call along |
| 971 | to the corresponding thread-unaware (?) libc routine. |
| 972 | ------------------------------------------------------------------ */ |
| 973 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 974 | #include <stdlib.h> |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 975 | #include <sys/types.h> |
| 976 | #include <sys/socket.h> |
| 977 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 978 | #ifdef GLIBC_2_1 |
| 979 | extern |
| 980 | int __sigaction |
| 981 | (int signum, |
| 982 | const struct sigaction *act, |
| 983 | struct sigaction *oldact); |
| 984 | #else |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 985 | extern |
| 986 | int __libc_sigaction |
| 987 | (int signum, |
| 988 | const struct sigaction *act, |
| 989 | struct sigaction *oldact); |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 990 | #endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 991 | int sigaction(int signum, |
| 992 | const struct sigaction *act, |
| 993 | struct sigaction *oldact) |
| 994 | { |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 995 | # ifdef GLIBC_2_1 |
| 996 | return __sigaction(signum, act, oldact); |
| 997 | # else |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 998 | return __libc_sigaction(signum, act, oldact); |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 999 | # endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | |
| 1003 | extern |
| 1004 | int __libc_connect(int sockfd, |
| 1005 | const struct sockaddr *serv_addr, |
| 1006 | socklen_t addrlen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1007 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1008 | int connect(int sockfd, |
| 1009 | const struct sockaddr *serv_addr, |
| 1010 | socklen_t addrlen) |
| 1011 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1012 | return __libc_connect(sockfd, serv_addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
| 1015 | |
| 1016 | extern |
| 1017 | int __libc_fcntl(int fd, int cmd, long arg); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1018 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1019 | int fcntl(int fd, int cmd, long arg) |
| 1020 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1021 | return __libc_fcntl(fd, cmd, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
| 1024 | |
| 1025 | extern |
| 1026 | ssize_t __libc_write(int fd, const void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1027 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1028 | ssize_t write(int fd, const void *buf, size_t count) |
| 1029 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1030 | return __libc_write(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
| 1033 | |
| 1034 | extern |
| 1035 | ssize_t __libc_read(int fd, void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1036 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1037 | ssize_t read(int fd, void *buf, size_t count) |
| 1038 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1039 | return __libc_read(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1040 | } |
| 1041 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1042 | |
| 1043 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1044 | int __libc_open64(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1045 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1046 | int open64(const char *pathname, int flags, mode_t mode) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1047 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1048 | return __libc_open64(pathname, flags, mode); |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1049 | } |
| 1050 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1051 | |
| 1052 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1053 | int __libc_open(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1054 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1055 | int open(const char *pathname, int flags, mode_t mode) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1056 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1057 | return __libc_open(pathname, flags, mode); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1058 | } |
| 1059 | |
| 1060 | |
| 1061 | extern |
| 1062 | int __libc_close(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1063 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1064 | int close(int fd) |
| 1065 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1066 | return __libc_close(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | |
| 1070 | extern |
| 1071 | int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1072 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1073 | int accept(int s, struct sockaddr *addr, socklen_t *addrlen) |
| 1074 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1075 | return __libc_accept(s, addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | |
| 1079 | extern |
| 1080 | pid_t __libc_fork(void); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1081 | pid_t __fork(void) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1082 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1083 | return __libc_fork(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | |
| 1087 | extern |
| 1088 | pid_t __libc_waitpid(pid_t pid, int *status, int options); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1089 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1090 | pid_t waitpid(pid_t pid, int *status, int options) |
| 1091 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1092 | return __libc_waitpid(pid, status, options); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
| 1095 | |
| 1096 | extern |
| 1097 | int __libc_nanosleep(const struct timespec *req, struct timespec *rem); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1098 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1099 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 1100 | { |
| 1101 | return __libc_nanosleep(req, rem); |
| 1102 | } |
| 1103 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1104 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1105 | extern |
| 1106 | int __libc_fsync(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1107 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1108 | int fsync(int fd) |
| 1109 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1110 | return __libc_fsync(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1111 | } |
| 1112 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1113 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1114 | extern |
| 1115 | off_t __libc_lseek(int fildes, off_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1116 | __attribute__((weak)) |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1117 | off_t lseek(int fildes, off_t offset, int whence) |
| 1118 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1119 | return __libc_lseek(fildes, offset, whence); |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1122 | |
| 1123 | extern |
| 1124 | __off64_t __libc_lseek64(int fildes, __off64_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1125 | __attribute__((weak)) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1126 | __off64_t lseek64(int fildes, __off64_t offset, int whence) |
| 1127 | { |
| 1128 | return __libc_lseek64(fildes, offset, whence); |
| 1129 | } |
| 1130 | |
| 1131 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1132 | extern |
| 1133 | ssize_t __libc_pread64 (int __fd, void *__buf, size_t __nbytes, |
| 1134 | __off64_t __offset); |
| 1135 | ssize_t __pread64 (int __fd, void *__buf, size_t __nbytes, |
| 1136 | __off64_t __offset) |
| 1137 | { |
| 1138 | return __libc_pread64(__fd, __buf, __nbytes, __offset); |
| 1139 | } |
| 1140 | |
| 1141 | |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 1142 | extern |
| 1143 | ssize_t __libc_pwrite64 (int __fd, const void *__buf, size_t __nbytes, |
| 1144 | __off64_t __offset); |
| 1145 | ssize_t __pwrite64 (int __fd, const void *__buf, size_t __nbytes, |
| 1146 | __off64_t __offset) |
| 1147 | { |
| 1148 | return __libc_pwrite64(__fd, __buf, __nbytes, __offset); |
| 1149 | } |
| 1150 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1151 | |
sewardj | 39b93b1 | 2002-05-18 10:56:27 +0000 | [diff] [blame] | 1152 | extern |
| 1153 | ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset); |
| 1154 | __attribute__((weak)) |
| 1155 | ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) |
| 1156 | { |
| 1157 | return __libc_pwrite(fd, buf, count, offset); |
| 1158 | } |
| 1159 | |
| 1160 | |
| 1161 | extern |
| 1162 | ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset); |
| 1163 | __attribute__((weak)) |
| 1164 | ssize_t pread(int fd, void *buf, size_t count, off_t offset) |
| 1165 | { |
| 1166 | return __libc_pread(fd, buf, count, offset); |
| 1167 | } |
| 1168 | |
| 1169 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1170 | extern |
| 1171 | void __libc_longjmp(jmp_buf env, int val) __attribute((noreturn)); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1172 | /* not weak: __attribute__((weak)) */ |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1173 | void longjmp(jmp_buf env, int val) |
| 1174 | { |
| 1175 | __libc_longjmp(env, val); |
| 1176 | } |
| 1177 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1178 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1179 | extern |
| 1180 | int __libc_send(int s, const void *msg, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1181 | __attribute__((weak)) |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1182 | int send(int s, const void *msg, size_t len, int flags) |
| 1183 | { |
| 1184 | return __libc_send(s, msg, len, flags); |
| 1185 | } |
| 1186 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1187 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1188 | extern |
| 1189 | int __libc_recv(int s, void *buf, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1190 | __attribute__((weak)) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1191 | int recv(int s, void *buf, size_t len, int flags) |
| 1192 | { |
| 1193 | return __libc_recv(s, buf, len, flags); |
| 1194 | } |
| 1195 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1196 | |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1197 | extern |
| 1198 | int __libc_sendmsg(int s, const struct msghdr *msg, int flags); |
| 1199 | __attribute__((weak)) |
| 1200 | int sendmsg(int s, const struct msghdr *msg, int flags) |
| 1201 | { |
| 1202 | return __libc_sendmsg(s, msg, flags); |
| 1203 | } |
| 1204 | |
| 1205 | |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1206 | extern |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1207 | int __libc_recvfrom(int s, void *buf, size_t len, int flags, |
| 1208 | struct sockaddr *from, socklen_t *fromlen); |
| 1209 | __attribute__((weak)) |
| 1210 | int recvfrom(int s, void *buf, size_t len, int flags, |
| 1211 | struct sockaddr *from, socklen_t *fromlen) |
| 1212 | { |
| 1213 | return __libc_recvfrom(s, buf, len, flags, from, fromlen); |
| 1214 | } |
| 1215 | |
| 1216 | |
| 1217 | extern |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1218 | int __libc_sendto(int s, const void *msg, size_t len, int flags, |
| 1219 | const struct sockaddr *to, socklen_t tolen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1220 | __attribute__((weak)) |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1221 | int sendto(int s, const void *msg, size_t len, int flags, |
| 1222 | const struct sockaddr *to, socklen_t tolen) |
| 1223 | { |
| 1224 | return __libc_sendto(s, msg, len, flags, to, tolen); |
| 1225 | } |
| 1226 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1227 | |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 1228 | extern |
| 1229 | int __libc_system(const char* str); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1230 | __attribute__((weak)) |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 1231 | int system(const char* str) |
| 1232 | { |
| 1233 | return __libc_system(str); |
| 1234 | } |
| 1235 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1236 | |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 1237 | extern |
| 1238 | pid_t __libc_wait(int *status); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1239 | __attribute__((weak)) |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 1240 | pid_t wait(int *status) |
| 1241 | { |
| 1242 | return __libc_wait(status); |
| 1243 | } |
| 1244 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1245 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1246 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1247 | /* --------------------------------------------------------------------- |
| 1248 | Nonblocking implementations of select() and poll(). This stuff will |
| 1249 | surely rot your mind. |
| 1250 | ------------------------------------------------------------------ */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1251 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1252 | /*--------------------------------------------------*/ |
| 1253 | |
| 1254 | #include "vg_kerneliface.h" |
| 1255 | |
| 1256 | static |
| 1257 | __inline__ |
| 1258 | int is_kerror ( int res ) |
| 1259 | { |
| 1260 | if (res >= -4095 && res <= -1) |
| 1261 | return 1; |
| 1262 | else |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | |
| 1267 | static |
| 1268 | int my_do_syscall1 ( int syscallno, int arg1 ) |
| 1269 | { |
| 1270 | int __res; |
| 1271 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1272 | : "=a" (__res) |
| 1273 | : "0" (syscallno), |
| 1274 | "d" (arg1) ); |
| 1275 | return __res; |
| 1276 | } |
| 1277 | |
| 1278 | static |
| 1279 | int my_do_syscall2 ( int syscallno, |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1280 | int arg1, int arg2 ) |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1281 | { |
| 1282 | int __res; |
| 1283 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1284 | : "=a" (__res) |
| 1285 | : "0" (syscallno), |
| 1286 | "d" (arg1), |
| 1287 | "c" (arg2) ); |
| 1288 | return __res; |
| 1289 | } |
| 1290 | |
| 1291 | static |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1292 | int my_do_syscall3 ( int syscallno, |
| 1293 | int arg1, int arg2, int arg3 ) |
| 1294 | { |
| 1295 | int __res; |
| 1296 | __asm__ volatile ("pushl %%ebx; movl %%esi,%%ebx ; int $0x80 ; popl %%ebx" |
| 1297 | : "=a" (__res) |
| 1298 | : "0" (syscallno), |
| 1299 | "S" (arg1), |
| 1300 | "c" (arg2), |
| 1301 | "d" (arg3) ); |
| 1302 | return __res; |
| 1303 | } |
| 1304 | |
| 1305 | static |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1306 | int do_syscall_select( int n, |
| 1307 | vki_fd_set* readfds, |
| 1308 | vki_fd_set* writefds, |
| 1309 | vki_fd_set* exceptfds, |
| 1310 | struct vki_timeval * timeout ) |
| 1311 | { |
| 1312 | int res; |
| 1313 | int args[5]; |
| 1314 | args[0] = n; |
| 1315 | args[1] = (int)readfds; |
| 1316 | args[2] = (int)writefds; |
| 1317 | args[3] = (int)exceptfds; |
| 1318 | args[4] = (int)timeout; |
| 1319 | res = my_do_syscall1(__NR_select, (int)(&(args[0])) ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1320 | return res; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
| 1323 | |
| 1324 | /* This is a wrapper round select(), which makes it thread-safe, |
| 1325 | meaning that only this thread will block, rather than the entire |
| 1326 | process. This wrapper in turn depends on nanosleep() not to block |
| 1327 | the entire process, but I think (hope? suspect?) that POSIX |
| 1328 | pthreads guarantees that to be the case. |
| 1329 | |
| 1330 | Basic idea is: modify the timeout parameter to select so that it |
| 1331 | returns immediately. Poll like this until select returns non-zero, |
| 1332 | indicating something interesting happened, or until our time is up. |
| 1333 | Space out the polls with nanosleeps of say 20 milliseconds, which |
| 1334 | is required to be nonblocking; this allows other threads to run. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1335 | |
| 1336 | Assumes: |
| 1337 | * (checked via assert) types fd_set and vki_fd_set are identical. |
| 1338 | * (checked via assert) types timeval and vki_timeval are identical. |
| 1339 | * (unchecked) libc error numbers (EINTR etc) are the negation of the |
| 1340 | kernel's error numbers (VKI_EINTR etc). |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1341 | */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1342 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1343 | /* __attribute__((weak)) */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1344 | int select ( int n, |
| 1345 | fd_set *rfds, |
| 1346 | fd_set *wfds, |
| 1347 | fd_set *xfds, |
| 1348 | struct timeval *timeout ) |
| 1349 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1350 | unsigned int ms_now, ms_end; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1351 | int res; |
| 1352 | fd_set rfds_copy; |
| 1353 | fd_set wfds_copy; |
| 1354 | fd_set xfds_copy; |
| 1355 | struct vki_timeval t_now; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1356 | struct vki_timeval zero_timeout; |
| 1357 | struct vki_timespec nanosleep_interval; |
| 1358 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1359 | /* gcc's complains about ms_end being used uninitialised -- classic |
| 1360 | case it can't understand, where ms_end is both defined and used |
| 1361 | only if timeout != NULL. Hence ... */ |
| 1362 | ms_end = 0; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1363 | |
| 1364 | /* We assume that the kernel and libc data layouts are identical |
| 1365 | for the following types. These asserts provide a crude |
| 1366 | check. */ |
| 1367 | if (sizeof(fd_set) != sizeof(vki_fd_set) |
| 1368 | || sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1369 | barf("valgrind's hacky non-blocking select(): data sizes error"); |
| 1370 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1371 | /* Detect the current time and simultaneously find out if we are |
| 1372 | running on Valgrind. */ |
| 1373 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1374 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1375 | 0, 0, 0, 0); |
| 1376 | |
| 1377 | /* If a zero timeout specified, this call is harmless. Also go |
| 1378 | this route if we're not running on Valgrind, for whatever |
| 1379 | reason. */ |
| 1380 | if ( (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) |
| 1381 | || (ms_now == 0xFFFFFFFF) ) { |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1382 | res = do_syscall_select( n, (vki_fd_set*)rfds, |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1383 | (vki_fd_set*)wfds, |
| 1384 | (vki_fd_set*)xfds, |
| 1385 | (struct vki_timeval*)timeout); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1386 | if (is_kerror(res)) { |
| 1387 | * (__errno_location()) = -res; |
| 1388 | return -1; |
| 1389 | } else { |
| 1390 | return res; |
| 1391 | } |
| 1392 | } |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1393 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1394 | /* If a timeout was specified, set ms_end to be the end millisecond |
| 1395 | counter [wallclock] time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1396 | if (timeout) { |
| 1397 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
| 1398 | assert(res == 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1399 | ms_end = ms_now; |
| 1400 | ms_end += (timeout->tv_usec / 1000); |
| 1401 | ms_end += (timeout->tv_sec * 1000); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1402 | /* Stay sane ... */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1403 | assert (ms_end >= ms_now); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
| 1406 | /* fprintf(stderr, "MY_SELECT: before loop\n"); */ |
| 1407 | |
| 1408 | /* Either timeout == NULL, meaning wait indefinitely, or timeout != |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1409 | NULL, in which case ms_end holds the end time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1410 | while (1) { |
| 1411 | if (timeout) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1412 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1413 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1414 | 0, 0, 0, 0); |
| 1415 | assert(ms_now != 0xFFFFFFFF); |
| 1416 | if (ms_now >= ms_end) { |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1417 | /* timeout; nothing interesting happened. */ |
| 1418 | if (rfds) FD_ZERO(rfds); |
| 1419 | if (wfds) FD_ZERO(wfds); |
| 1420 | if (xfds) FD_ZERO(xfds); |
| 1421 | return 0; |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | /* These could be trashed each time round the loop, so restore |
| 1426 | them each time. */ |
| 1427 | if (rfds) rfds_copy = *rfds; |
| 1428 | if (wfds) wfds_copy = *wfds; |
| 1429 | if (xfds) xfds_copy = *xfds; |
| 1430 | |
| 1431 | zero_timeout.tv_sec = zero_timeout.tv_usec = 0; |
| 1432 | |
| 1433 | res = do_syscall_select( n, |
| 1434 | rfds ? (vki_fd_set*)(&rfds_copy) : NULL, |
| 1435 | wfds ? (vki_fd_set*)(&wfds_copy) : NULL, |
| 1436 | xfds ? (vki_fd_set*)(&xfds_copy) : NULL, |
| 1437 | & zero_timeout ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1438 | if (is_kerror(res)) { |
| 1439 | /* Some kind of error (including EINTR). Set errno and |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1440 | return. The sets are unspecified in this case. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1441 | * (__errno_location()) = -res; |
| 1442 | return -1; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1443 | } |
| 1444 | if (res > 0) { |
| 1445 | /* one or more fds is ready. Copy out resulting sets and |
| 1446 | return. */ |
| 1447 | if (rfds) *rfds = rfds_copy; |
| 1448 | if (wfds) *wfds = wfds_copy; |
| 1449 | if (xfds) *xfds = xfds_copy; |
| 1450 | return res; |
| 1451 | } |
| 1452 | /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ |
| 1453 | /* nanosleep and go round again */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1454 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1455 | nanosleep_interval.tv_nsec = 50 * 1000 * 1000; /* 50 milliseconds */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1456 | /* It's critical here that valgrind's nanosleep implementation |
| 1457 | is nonblocking. */ |
| 1458 | (void)my_do_syscall2(__NR_nanosleep, |
| 1459 | (int)(&nanosleep_interval), (int)NULL); |
| 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | |
| 1464 | |
| 1465 | |
| 1466 | #include <sys/poll.h> |
| 1467 | |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 1468 | #ifdef GLIBC_2_1 |
| 1469 | typedef unsigned long int nfds_t; |
| 1470 | #endif |
| 1471 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1472 | /* __attribute__((weak)) */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1473 | int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) |
| 1474 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1475 | unsigned int ms_now, ms_end; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1476 | int res, i; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1477 | struct vki_timespec nanosleep_interval; |
| 1478 | |
| 1479 | ensure_valgrind("poll"); |
| 1480 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1481 | /* Detect the current time and simultaneously find out if we are |
| 1482 | running on Valgrind. */ |
| 1483 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1484 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1485 | 0, 0, 0, 0); |
| 1486 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1487 | if (/* CHECK SIZES FOR struct pollfd */ |
| 1488 | sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1489 | barf("valgrind's hacky non-blocking poll(): data sizes error"); |
| 1490 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1491 | /* dummy initialisation to keep gcc -Wall happy */ |
| 1492 | ms_end = 0; |
| 1493 | |
| 1494 | /* If a zero timeout specified, this call is harmless. Also do |
| 1495 | this if not running on Valgrind. */ |
| 1496 | if (__timeout == 0 || ms_now == 0xFFFFFFFF) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1497 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, __timeout); |
| 1498 | if (is_kerror(res)) { |
| 1499 | * (__errno_location()) = -res; |
| 1500 | return -1; |
| 1501 | } else { |
| 1502 | return res; |
| 1503 | } |
| 1504 | } |
| 1505 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1506 | /* If a timeout was specified, set ms_end to be the end wallclock |
| 1507 | time. Easy considering that __timeout is in milliseconds. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1508 | if (__timeout > 0) { |
sewardj | 650ac00 | 2002-04-29 12:20:34 +0000 | [diff] [blame] | 1509 | ms_end = ms_now + (unsigned int)__timeout; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
| 1512 | /* fprintf(stderr, "MY_POLL: before loop\n"); */ |
| 1513 | |
| 1514 | /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, |
| 1515 | in which case t_end holds the end time. */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1516 | assert(__timeout != 0); |
| 1517 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1518 | while (1) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1519 | if (__timeout > 0) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1520 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1521 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1522 | 0, 0, 0, 0); |
| 1523 | assert(ms_now != 0xFFFFFFFF); |
| 1524 | if (ms_now >= ms_end) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1525 | /* timeout; nothing interesting happened. */ |
| 1526 | for (i = 0; i < __nfds; i++) |
| 1527 | __fds[i].revents = 0; |
| 1528 | return 0; |
| 1529 | } |
| 1530 | } |
| 1531 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1532 | /* Do a return-immediately poll. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1533 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); |
| 1534 | if (is_kerror(res)) { |
| 1535 | /* Some kind of error. Set errno and return. */ |
| 1536 | * (__errno_location()) = -res; |
| 1537 | return -1; |
| 1538 | } |
| 1539 | if (res > 0) { |
| 1540 | /* One or more fds is ready. Return now. */ |
| 1541 | return res; |
| 1542 | } |
| 1543 | /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ |
| 1544 | /* nanosleep and go round again */ |
| 1545 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1546 | nanosleep_interval.tv_nsec = 51 * 1000 * 1000; /* 51 milliseconds */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1547 | /* It's critical here that valgrind's nanosleep implementation |
| 1548 | is nonblocking. */ |
| 1549 | (void)my_do_syscall2(__NR_nanosleep, |
| 1550 | (int)(&nanosleep_interval), (int)NULL); |
| 1551 | } |
| 1552 | } |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1553 | |
| 1554 | |
| 1555 | /* --------------------------------------------------------------------- |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 1556 | Hacky implementation of semaphores. |
| 1557 | ------------------------------------------------------------------ */ |
| 1558 | |
| 1559 | #include <semaphore.h> |
| 1560 | |
| 1561 | /* This is a terrible way to do the remapping. Plan is to import an |
| 1562 | AVL tree at some point. */ |
| 1563 | #define VG_N_SEMAPHORES 50 |
| 1564 | |
| 1565 | typedef |
| 1566 | struct { |
| 1567 | pthread_mutex_t se_mx; |
| 1568 | pthread_cond_t se_cv; |
| 1569 | int count; |
| 1570 | } |
| 1571 | vg_sem_t; |
| 1572 | |
| 1573 | static pthread_mutex_t se_remap_mx = PTHREAD_MUTEX_INITIALIZER; |
| 1574 | |
| 1575 | static int se_remap_used = 0; |
| 1576 | static sem_t* se_remap_orig[VG_N_SEMAPHORES]; |
| 1577 | static vg_sem_t se_remap_new[VG_N_SEMAPHORES]; |
| 1578 | |
| 1579 | static vg_sem_t* se_remap ( sem_t* orig ) |
| 1580 | { |
| 1581 | int res, i; |
| 1582 | res = __pthread_mutex_lock(&se_remap_mx); |
| 1583 | assert(res == 0); |
| 1584 | |
| 1585 | for (i = 0; i < se_remap_used; i++) { |
| 1586 | if (se_remap_orig[i] == orig) |
| 1587 | break; |
| 1588 | } |
| 1589 | if (i == se_remap_used) { |
| 1590 | if (se_remap_used == VG_N_SEMAPHORES) { |
| 1591 | res = pthread_mutex_unlock(&se_remap_mx); |
| 1592 | assert(res == 0); |
| 1593 | barf("N_SEMAPHORES is too low. Increase and recompile."); |
| 1594 | } |
| 1595 | se_remap_used++; |
| 1596 | se_remap_orig[i] = orig; |
| 1597 | /* printf("allocated semaphore %d\n", i); */ |
| 1598 | } |
| 1599 | res = __pthread_mutex_unlock(&se_remap_mx); |
| 1600 | assert(res == 0); |
| 1601 | return &se_remap_new[i]; |
| 1602 | } |
| 1603 | |
| 1604 | |
| 1605 | int sem_init(sem_t *sem, int pshared, unsigned int value) |
| 1606 | { |
| 1607 | int res; |
| 1608 | vg_sem_t* vg_sem; |
| 1609 | ensure_valgrind("sem_init"); |
| 1610 | if (pshared != 0) { |
| 1611 | errno = ENOSYS; |
| 1612 | return -1; |
| 1613 | } |
| 1614 | vg_sem = se_remap(sem); |
| 1615 | res = pthread_mutex_init(&vg_sem->se_mx, NULL); |
| 1616 | assert(res == 0); |
| 1617 | res = pthread_cond_init(&vg_sem->se_cv, NULL); |
| 1618 | assert(res == 0); |
| 1619 | vg_sem->count = value; |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | |
| 1624 | int sem_wait ( sem_t* sem ) |
| 1625 | { |
| 1626 | int res; |
| 1627 | vg_sem_t* vg_sem; |
| 1628 | ensure_valgrind("sem_wait"); |
| 1629 | vg_sem = se_remap(sem); |
| 1630 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
| 1631 | assert(res == 0); |
| 1632 | while (vg_sem->count == 0) { |
| 1633 | res = pthread_cond_wait(&vg_sem->se_cv, &vg_sem->se_mx); |
| 1634 | assert(res == 0); |
| 1635 | } |
| 1636 | vg_sem->count--; |
| 1637 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 1638 | assert(res == 0); |
| 1639 | return 0; |
| 1640 | } |
| 1641 | |
| 1642 | int sem_post ( sem_t* sem ) |
| 1643 | { |
| 1644 | int res; |
| 1645 | vg_sem_t* vg_sem; |
| 1646 | ensure_valgrind("sem_post"); |
| 1647 | vg_sem = se_remap(sem); |
| 1648 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
| 1649 | assert(res == 0); |
| 1650 | if (vg_sem->count == 0) { |
| 1651 | vg_sem->count++; |
| 1652 | res = pthread_cond_broadcast(&vg_sem->se_cv); |
| 1653 | assert(res == 0); |
| 1654 | } else { |
| 1655 | vg_sem->count++; |
| 1656 | } |
| 1657 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 1658 | assert(res == 0); |
| 1659 | return 0; |
| 1660 | } |
| 1661 | |
| 1662 | |
| 1663 | int sem_trywait ( sem_t* sem ) |
| 1664 | { |
| 1665 | int ret, res; |
| 1666 | vg_sem_t* vg_sem; |
| 1667 | ensure_valgrind("sem_trywait"); |
| 1668 | vg_sem = se_remap(sem); |
| 1669 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
| 1670 | assert(res == 0); |
| 1671 | if (vg_sem->count > 0) { |
| 1672 | vg_sem->count--; |
| 1673 | ret = 0; |
| 1674 | } else { |
| 1675 | ret = -1; |
| 1676 | errno = EAGAIN; |
| 1677 | } |
| 1678 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 1679 | assert(res == 0); |
| 1680 | return ret; |
| 1681 | } |
| 1682 | |
| 1683 | |
| 1684 | int sem_getvalue(sem_t* sem, int * sval) |
| 1685 | { |
| 1686 | vg_sem_t* vg_sem; |
| 1687 | ensure_valgrind("sem_trywait"); |
| 1688 | vg_sem = se_remap(sem); |
| 1689 | *sval = vg_sem->count; |
| 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | |
| 1694 | int sem_destroy(sem_t * sem) |
| 1695 | { |
| 1696 | kludged("sem_destroy"); |
| 1697 | /* if someone waiting on this semaphore, errno = EBUSY, return -1 */ |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | |
| 1702 | /* --------------------------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1703 | B'stard. |
| 1704 | ------------------------------------------------------------------ */ |
| 1705 | |
| 1706 | # define strong_alias(name, aliasname) \ |
| 1707 | extern __typeof (name) aliasname __attribute__ ((alias (#name))); |
| 1708 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1709 | # define weak_alias(name, aliasname) \ |
| 1710 | extern __typeof (name) aliasname __attribute__ ((weak, alias (#name))); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1711 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1712 | strong_alias(__pthread_mutex_lock, pthread_mutex_lock) |
| 1713 | strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock) |
| 1714 | strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock) |
| 1715 | strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init) |
| 1716 | weak_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype) |
| 1717 | strong_alias(__pthread_mutex_init, pthread_mutex_init) |
| 1718 | strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy) |
| 1719 | strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy) |
| 1720 | strong_alias(__pthread_once, pthread_once) |
| 1721 | strong_alias(__pthread_atfork, pthread_atfork) |
| 1722 | strong_alias(__pthread_key_create, pthread_key_create) |
| 1723 | strong_alias(__pthread_getspecific, pthread_getspecific) |
| 1724 | strong_alias(__pthread_setspecific, pthread_setspecific) |
| 1725 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1726 | #ifndef GLIBC_2_1 |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1727 | strong_alias(sigaction, __sigaction) |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1728 | #endif |
| 1729 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1730 | strong_alias(close, __close) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1731 | strong_alias(fcntl, __fcntl) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1732 | strong_alias(lseek, __lseek) |
| 1733 | strong_alias(open, __open) |
| 1734 | strong_alias(open64, __open64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1735 | strong_alias(read, __read) |
| 1736 | strong_alias(wait, __wait) |
| 1737 | strong_alias(write, __write) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1738 | strong_alias(connect, __connect) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1739 | strong_alias(send, __send) |
| 1740 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1741 | weak_alias (__pread64, pread64) |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 1742 | weak_alias (__pwrite64, pwrite64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1743 | weak_alias(__fork, fork) |
sewardj | 7f6456d | 2002-05-21 00:51:21 +0000 | [diff] [blame] | 1744 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1745 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1746 | |
| 1747 | /*--------------------------------------------------*/ |
| 1748 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1749 | int |
| 1750 | pthread_rwlock_rdlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1751 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1752 | static int moans = N_MOANS; |
| 1753 | if (moans-- > 0) |
| 1754 | kludged("pthread_rwlock_rdlock"); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1755 | return 0; |
| 1756 | } |
| 1757 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1758 | weak_alias(pthread_rwlock_rdlock, __pthread_rwlock_rdlock) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1759 | |
| 1760 | |
| 1761 | int |
| 1762 | pthread_rwlock_unlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1763 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1764 | static int moans = N_MOANS; |
| 1765 | if (moans-- > 0) |
| 1766 | kludged("pthread_rwlock_unlock"); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1767 | return 0; |
| 1768 | } |
| 1769 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1770 | weak_alias(pthread_rwlock_unlock, __pthread_rwlock_unlock) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1771 | |
| 1772 | |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 1773 | int |
| 1774 | pthread_rwlock_wrlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1775 | { |
| 1776 | static int moans = N_MOANS; |
| 1777 | if (moans-- > 0) |
| 1778 | kludged("pthread_rwlock_wrlock"); |
| 1779 | return 0; |
| 1780 | } |
| 1781 | |
sewardj | 262b029 | 2002-05-01 00:03:16 +0000 | [diff] [blame] | 1782 | weak_alias(pthread_rwlock_wrlock, __pthread_rwlock_wrlock) |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 1783 | |
| 1784 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1785 | /* I've no idea what these are, but they get called quite a lot. |
| 1786 | Anybody know? */ |
| 1787 | |
| 1788 | #undef _IO_flockfile |
| 1789 | void _IO_flockfile ( _IO_FILE * file ) |
| 1790 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1791 | pthread_mutex_lock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1792 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1793 | weak_alias(_IO_flockfile, flockfile); |
| 1794 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1795 | |
| 1796 | #undef _IO_funlockfile |
| 1797 | void _IO_funlockfile ( _IO_FILE * file ) |
| 1798 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1799 | pthread_mutex_unlock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1800 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1801 | weak_alias(_IO_funlockfile, funlockfile); |
| 1802 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1803 | |
| 1804 | void _pthread_cleanup_push_defer ( void ) |
| 1805 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1806 | static int moans = N_MOANS; |
| 1807 | if (moans-- > 0) |
| 1808 | ignored("_pthread_cleanup_push_defer"); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | void _pthread_cleanup_pop_restore ( void ) |
| 1812 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1813 | static int moans = N_MOANS; |
| 1814 | if (moans-- > 0) |
| 1815 | ignored("_pthread_cleanup_pop_restore"); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1816 | } |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 1817 | |
sewardj | 60e3842 | 2002-05-08 14:08:22 +0000 | [diff] [blame] | 1818 | /*--------*/ |
| 1819 | void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, |
| 1820 | void (*__routine) (void *), |
| 1821 | void *__arg) |
| 1822 | { |
| 1823 | static int moans = N_MOANS; |
| 1824 | if (moans-- > 0) |
| 1825 | ignored("_pthread_cleanup_push"); |
| 1826 | } |
| 1827 | |
| 1828 | void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, |
| 1829 | int __execute) |
| 1830 | { |
| 1831 | static int moans = N_MOANS; |
| 1832 | if (moans-- > 0) { |
| 1833 | if (__execute) |
| 1834 | ignored("_pthread_cleanup_pop-EXECUTE"); |
| 1835 | else |
| 1836 | ignored("_pthread_cleanup_pop-NO-EXECUTE"); |
| 1837 | } |
| 1838 | } |
| 1839 | |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 1840 | |
| 1841 | /* This doesn't seem to be needed to simulate libpthread.so's external |
| 1842 | interface, but many people complain about its absence. */ |
| 1843 | |
| 1844 | strong_alias(__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np) |
| 1845 | weak_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np) |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1846 | |
| 1847 | |
| 1848 | /*--------------------------------------------------------------------*/ |
| 1849 | /*--- end vg_libpthread.c ---*/ |
| 1850 | /*--------------------------------------------------------------------*/ |