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