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 | 67f1d58 | 2002-05-24 02:11:32 +0000 | [diff] [blame] | 1255 | extern |
| 1256 | int __libc_msync(const void *start, size_t length, int flags); |
| 1257 | __attribute__((weak)) |
| 1258 | int msync(const void *start, size_t length, int flags) |
| 1259 | { |
| 1260 | return __libc_msync(start, length, flags); |
| 1261 | } |
| 1262 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1263 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1264 | /* --------------------------------------------------------------------- |
| 1265 | Nonblocking implementations of select() and poll(). This stuff will |
| 1266 | surely rot your mind. |
| 1267 | ------------------------------------------------------------------ */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1268 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1269 | /*--------------------------------------------------*/ |
| 1270 | |
| 1271 | #include "vg_kerneliface.h" |
| 1272 | |
| 1273 | static |
| 1274 | __inline__ |
| 1275 | int is_kerror ( int res ) |
| 1276 | { |
| 1277 | if (res >= -4095 && res <= -1) |
| 1278 | return 1; |
| 1279 | else |
| 1280 | return 0; |
| 1281 | } |
| 1282 | |
| 1283 | |
| 1284 | static |
| 1285 | int my_do_syscall1 ( int syscallno, int arg1 ) |
| 1286 | { |
| 1287 | int __res; |
| 1288 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1289 | : "=a" (__res) |
| 1290 | : "0" (syscallno), |
| 1291 | "d" (arg1) ); |
| 1292 | return __res; |
| 1293 | } |
| 1294 | |
| 1295 | static |
| 1296 | int my_do_syscall2 ( int syscallno, |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1297 | int arg1, int arg2 ) |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1298 | { |
| 1299 | int __res; |
| 1300 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1301 | : "=a" (__res) |
| 1302 | : "0" (syscallno), |
| 1303 | "d" (arg1), |
| 1304 | "c" (arg2) ); |
| 1305 | return __res; |
| 1306 | } |
| 1307 | |
| 1308 | static |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1309 | int my_do_syscall3 ( int syscallno, |
| 1310 | int arg1, int arg2, int arg3 ) |
| 1311 | { |
| 1312 | int __res; |
| 1313 | __asm__ volatile ("pushl %%ebx; movl %%esi,%%ebx ; int $0x80 ; popl %%ebx" |
| 1314 | : "=a" (__res) |
| 1315 | : "0" (syscallno), |
| 1316 | "S" (arg1), |
| 1317 | "c" (arg2), |
| 1318 | "d" (arg3) ); |
| 1319 | return __res; |
| 1320 | } |
| 1321 | |
| 1322 | static |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1323 | int do_syscall_select( int n, |
| 1324 | vki_fd_set* readfds, |
| 1325 | vki_fd_set* writefds, |
| 1326 | vki_fd_set* exceptfds, |
| 1327 | struct vki_timeval * timeout ) |
| 1328 | { |
| 1329 | int res; |
| 1330 | int args[5]; |
| 1331 | args[0] = n; |
| 1332 | args[1] = (int)readfds; |
| 1333 | args[2] = (int)writefds; |
| 1334 | args[3] = (int)exceptfds; |
| 1335 | args[4] = (int)timeout; |
| 1336 | res = my_do_syscall1(__NR_select, (int)(&(args[0])) ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1337 | return res; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | /* This is a wrapper round select(), which makes it thread-safe, |
| 1342 | meaning that only this thread will block, rather than the entire |
| 1343 | process. This wrapper in turn depends on nanosleep() not to block |
| 1344 | the entire process, but I think (hope? suspect?) that POSIX |
| 1345 | pthreads guarantees that to be the case. |
| 1346 | |
| 1347 | Basic idea is: modify the timeout parameter to select so that it |
| 1348 | returns immediately. Poll like this until select returns non-zero, |
| 1349 | indicating something interesting happened, or until our time is up. |
| 1350 | Space out the polls with nanosleeps of say 20 milliseconds, which |
| 1351 | is required to be nonblocking; this allows other threads to run. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1352 | |
| 1353 | Assumes: |
| 1354 | * (checked via assert) types fd_set and vki_fd_set are identical. |
| 1355 | * (checked via assert) types timeval and vki_timeval are identical. |
| 1356 | * (unchecked) libc error numbers (EINTR etc) are the negation of the |
| 1357 | kernel's error numbers (VKI_EINTR etc). |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1358 | */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1359 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1360 | /* __attribute__((weak)) */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1361 | int select ( int n, |
| 1362 | fd_set *rfds, |
| 1363 | fd_set *wfds, |
| 1364 | fd_set *xfds, |
| 1365 | struct timeval *timeout ) |
| 1366 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1367 | unsigned int ms_now, ms_end; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1368 | int res; |
| 1369 | fd_set rfds_copy; |
| 1370 | fd_set wfds_copy; |
| 1371 | fd_set xfds_copy; |
| 1372 | struct vki_timeval t_now; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1373 | struct vki_timeval zero_timeout; |
| 1374 | struct vki_timespec nanosleep_interval; |
| 1375 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1376 | /* gcc's complains about ms_end being used uninitialised -- classic |
| 1377 | case it can't understand, where ms_end is both defined and used |
| 1378 | only if timeout != NULL. Hence ... */ |
| 1379 | ms_end = 0; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1380 | |
| 1381 | /* We assume that the kernel and libc data layouts are identical |
| 1382 | for the following types. These asserts provide a crude |
| 1383 | check. */ |
| 1384 | if (sizeof(fd_set) != sizeof(vki_fd_set) |
| 1385 | || sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1386 | barf("valgrind's hacky non-blocking select(): data sizes error"); |
| 1387 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1388 | /* Detect the current time and simultaneously find out if we are |
| 1389 | running on Valgrind. */ |
| 1390 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1391 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1392 | 0, 0, 0, 0); |
| 1393 | |
| 1394 | /* If a zero timeout specified, this call is harmless. Also go |
| 1395 | this route if we're not running on Valgrind, for whatever |
| 1396 | reason. */ |
| 1397 | if ( (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) |
| 1398 | || (ms_now == 0xFFFFFFFF) ) { |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1399 | res = do_syscall_select( n, (vki_fd_set*)rfds, |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1400 | (vki_fd_set*)wfds, |
| 1401 | (vki_fd_set*)xfds, |
| 1402 | (struct vki_timeval*)timeout); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1403 | if (is_kerror(res)) { |
| 1404 | * (__errno_location()) = -res; |
| 1405 | return -1; |
| 1406 | } else { |
| 1407 | return res; |
| 1408 | } |
| 1409 | } |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1410 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1411 | /* If a timeout was specified, set ms_end to be the end millisecond |
| 1412 | counter [wallclock] time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1413 | if (timeout) { |
| 1414 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
| 1415 | assert(res == 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1416 | ms_end = ms_now; |
| 1417 | ms_end += (timeout->tv_usec / 1000); |
| 1418 | ms_end += (timeout->tv_sec * 1000); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1419 | /* Stay sane ... */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1420 | assert (ms_end >= ms_now); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* fprintf(stderr, "MY_SELECT: before loop\n"); */ |
| 1424 | |
| 1425 | /* Either timeout == NULL, meaning wait indefinitely, or timeout != |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1426 | NULL, in which case ms_end holds the end time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1427 | while (1) { |
| 1428 | if (timeout) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1429 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1430 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1431 | 0, 0, 0, 0); |
| 1432 | assert(ms_now != 0xFFFFFFFF); |
| 1433 | if (ms_now >= ms_end) { |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1434 | /* timeout; nothing interesting happened. */ |
| 1435 | if (rfds) FD_ZERO(rfds); |
| 1436 | if (wfds) FD_ZERO(wfds); |
| 1437 | if (xfds) FD_ZERO(xfds); |
| 1438 | return 0; |
| 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /* These could be trashed each time round the loop, so restore |
| 1443 | them each time. */ |
| 1444 | if (rfds) rfds_copy = *rfds; |
| 1445 | if (wfds) wfds_copy = *wfds; |
| 1446 | if (xfds) xfds_copy = *xfds; |
| 1447 | |
| 1448 | zero_timeout.tv_sec = zero_timeout.tv_usec = 0; |
| 1449 | |
| 1450 | res = do_syscall_select( n, |
| 1451 | rfds ? (vki_fd_set*)(&rfds_copy) : NULL, |
| 1452 | wfds ? (vki_fd_set*)(&wfds_copy) : NULL, |
| 1453 | xfds ? (vki_fd_set*)(&xfds_copy) : NULL, |
| 1454 | & zero_timeout ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1455 | if (is_kerror(res)) { |
| 1456 | /* Some kind of error (including EINTR). Set errno and |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1457 | return. The sets are unspecified in this case. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1458 | * (__errno_location()) = -res; |
| 1459 | return -1; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1460 | } |
| 1461 | if (res > 0) { |
| 1462 | /* one or more fds is ready. Copy out resulting sets and |
| 1463 | return. */ |
| 1464 | if (rfds) *rfds = rfds_copy; |
| 1465 | if (wfds) *wfds = wfds_copy; |
| 1466 | if (xfds) *xfds = xfds_copy; |
| 1467 | return res; |
| 1468 | } |
| 1469 | /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ |
| 1470 | /* nanosleep and go round again */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1471 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1472 | nanosleep_interval.tv_nsec = 50 * 1000 * 1000; /* 50 milliseconds */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1473 | /* It's critical here that valgrind's nanosleep implementation |
| 1474 | is nonblocking. */ |
| 1475 | (void)my_do_syscall2(__NR_nanosleep, |
| 1476 | (int)(&nanosleep_interval), (int)NULL); |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | |
| 1482 | |
| 1483 | #include <sys/poll.h> |
| 1484 | |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 1485 | #ifdef GLIBC_2_1 |
| 1486 | typedef unsigned long int nfds_t; |
| 1487 | #endif |
| 1488 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1489 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1490 | /* __attribute__((weak)) */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1491 | int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) |
| 1492 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1493 | unsigned int ms_now, ms_end; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1494 | int res, i; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1495 | struct vki_timespec nanosleep_interval; |
| 1496 | |
| 1497 | ensure_valgrind("poll"); |
| 1498 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1499 | /* Detect the current time and simultaneously find out if we are |
| 1500 | running on Valgrind. */ |
| 1501 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1502 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1503 | 0, 0, 0, 0); |
| 1504 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1505 | if (/* CHECK SIZES FOR struct pollfd */ |
| 1506 | sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1507 | barf("valgrind's hacky non-blocking poll(): data sizes error"); |
| 1508 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1509 | /* dummy initialisation to keep gcc -Wall happy */ |
| 1510 | ms_end = 0; |
| 1511 | |
| 1512 | /* If a zero timeout specified, this call is harmless. Also do |
| 1513 | this if not running on Valgrind. */ |
| 1514 | if (__timeout == 0 || ms_now == 0xFFFFFFFF) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1515 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, __timeout); |
| 1516 | if (is_kerror(res)) { |
| 1517 | * (__errno_location()) = -res; |
| 1518 | return -1; |
| 1519 | } else { |
| 1520 | return res; |
| 1521 | } |
| 1522 | } |
| 1523 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1524 | /* If a timeout was specified, set ms_end to be the end wallclock |
| 1525 | time. Easy considering that __timeout is in milliseconds. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1526 | if (__timeout > 0) { |
sewardj | 650ac00 | 2002-04-29 12:20:34 +0000 | [diff] [blame] | 1527 | ms_end = ms_now + (unsigned int)__timeout; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1528 | } |
| 1529 | |
| 1530 | /* fprintf(stderr, "MY_POLL: before loop\n"); */ |
| 1531 | |
| 1532 | /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, |
| 1533 | in which case t_end holds the end time. */ |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1534 | assert(__timeout != 0); |
| 1535 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1536 | while (1) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1537 | if (__timeout > 0) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1538 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1539 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1540 | 0, 0, 0, 0); |
| 1541 | assert(ms_now != 0xFFFFFFFF); |
| 1542 | if (ms_now >= ms_end) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1543 | /* timeout; nothing interesting happened. */ |
| 1544 | for (i = 0; i < __nfds; i++) |
| 1545 | __fds[i].revents = 0; |
| 1546 | return 0; |
| 1547 | } |
| 1548 | } |
| 1549 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1550 | /* Do a return-immediately poll. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1551 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); |
| 1552 | if (is_kerror(res)) { |
| 1553 | /* Some kind of error. Set errno and return. */ |
| 1554 | * (__errno_location()) = -res; |
| 1555 | return -1; |
| 1556 | } |
| 1557 | if (res > 0) { |
| 1558 | /* One or more fds is ready. Return now. */ |
| 1559 | return res; |
| 1560 | } |
| 1561 | /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ |
| 1562 | /* nanosleep and go round again */ |
| 1563 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1564 | nanosleep_interval.tv_nsec = 51 * 1000 * 1000; /* 51 milliseconds */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1565 | /* It's critical here that valgrind's nanosleep implementation |
| 1566 | is nonblocking. */ |
| 1567 | (void)my_do_syscall2(__NR_nanosleep, |
| 1568 | (int)(&nanosleep_interval), (int)NULL); |
| 1569 | } |
| 1570 | } |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1571 | |
| 1572 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1573 | /* Helper function used to make accept() non-blocking. Idea is to use |
| 1574 | the above nonblocking poll() to make this thread ONLY wait for the |
| 1575 | specified fd to become ready, and then return. */ |
| 1576 | static void wait_for_fd_to_be_readable_or_erring ( int fd ) |
| 1577 | { |
| 1578 | struct pollfd pfd; |
sewardj | 6e6cbaa | 2002-05-24 02:12:52 +0000 | [diff] [blame^] | 1579 | /* fprintf(stderr, "wait_for_fd_to_be_readable_or_erring %d\n", fd); */ |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1580 | pfd.fd = fd; |
| 1581 | pfd.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL; |
| 1582 | /* ... but not POLLOUT, you may notice. */ |
| 1583 | pfd.revents = 0; |
| 1584 | (void)poll(&pfd, 1, -1 /* forever */); |
| 1585 | } |
| 1586 | |
| 1587 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1588 | /* --------------------------------------------------------------------- |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 1589 | Hacky implementation of semaphores. |
| 1590 | ------------------------------------------------------------------ */ |
| 1591 | |
| 1592 | #include <semaphore.h> |
| 1593 | |
| 1594 | /* This is a terrible way to do the remapping. Plan is to import an |
| 1595 | AVL tree at some point. */ |
| 1596 | #define VG_N_SEMAPHORES 50 |
| 1597 | |
| 1598 | typedef |
| 1599 | struct { |
| 1600 | pthread_mutex_t se_mx; |
| 1601 | pthread_cond_t se_cv; |
| 1602 | int count; |
| 1603 | } |
| 1604 | vg_sem_t; |
| 1605 | |
| 1606 | static pthread_mutex_t se_remap_mx = PTHREAD_MUTEX_INITIALIZER; |
| 1607 | |
| 1608 | static int se_remap_used = 0; |
| 1609 | static sem_t* se_remap_orig[VG_N_SEMAPHORES]; |
| 1610 | static vg_sem_t se_remap_new[VG_N_SEMAPHORES]; |
| 1611 | |
| 1612 | static vg_sem_t* se_remap ( sem_t* orig ) |
| 1613 | { |
| 1614 | int res, i; |
| 1615 | res = __pthread_mutex_lock(&se_remap_mx); |
| 1616 | assert(res == 0); |
| 1617 | |
| 1618 | for (i = 0; i < se_remap_used; i++) { |
| 1619 | if (se_remap_orig[i] == orig) |
| 1620 | break; |
| 1621 | } |
| 1622 | if (i == se_remap_used) { |
| 1623 | if (se_remap_used == VG_N_SEMAPHORES) { |
| 1624 | res = pthread_mutex_unlock(&se_remap_mx); |
| 1625 | assert(res == 0); |
| 1626 | barf("N_SEMAPHORES is too low. Increase and recompile."); |
| 1627 | } |
| 1628 | se_remap_used++; |
| 1629 | se_remap_orig[i] = orig; |
| 1630 | /* printf("allocated semaphore %d\n", i); */ |
| 1631 | } |
| 1632 | res = __pthread_mutex_unlock(&se_remap_mx); |
| 1633 | assert(res == 0); |
| 1634 | return &se_remap_new[i]; |
| 1635 | } |
| 1636 | |
| 1637 | |
| 1638 | int sem_init(sem_t *sem, int pshared, unsigned int value) |
| 1639 | { |
| 1640 | int res; |
| 1641 | vg_sem_t* vg_sem; |
| 1642 | ensure_valgrind("sem_init"); |
| 1643 | if (pshared != 0) { |
| 1644 | errno = ENOSYS; |
| 1645 | return -1; |
| 1646 | } |
| 1647 | vg_sem = se_remap(sem); |
| 1648 | res = pthread_mutex_init(&vg_sem->se_mx, NULL); |
| 1649 | assert(res == 0); |
| 1650 | res = pthread_cond_init(&vg_sem->se_cv, NULL); |
| 1651 | assert(res == 0); |
| 1652 | vg_sem->count = value; |
| 1653 | return 0; |
| 1654 | } |
| 1655 | |
| 1656 | |
| 1657 | int sem_wait ( sem_t* sem ) |
| 1658 | { |
| 1659 | int res; |
| 1660 | vg_sem_t* vg_sem; |
| 1661 | ensure_valgrind("sem_wait"); |
| 1662 | vg_sem = se_remap(sem); |
| 1663 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
| 1664 | assert(res == 0); |
| 1665 | while (vg_sem->count == 0) { |
| 1666 | res = pthread_cond_wait(&vg_sem->se_cv, &vg_sem->se_mx); |
| 1667 | assert(res == 0); |
| 1668 | } |
| 1669 | vg_sem->count--; |
| 1670 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 1671 | assert(res == 0); |
| 1672 | return 0; |
| 1673 | } |
| 1674 | |
| 1675 | int sem_post ( sem_t* sem ) |
| 1676 | { |
| 1677 | int res; |
| 1678 | vg_sem_t* vg_sem; |
| 1679 | ensure_valgrind("sem_post"); |
| 1680 | vg_sem = se_remap(sem); |
| 1681 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
| 1682 | assert(res == 0); |
| 1683 | if (vg_sem->count == 0) { |
| 1684 | vg_sem->count++; |
| 1685 | res = pthread_cond_broadcast(&vg_sem->se_cv); |
| 1686 | assert(res == 0); |
| 1687 | } else { |
| 1688 | vg_sem->count++; |
| 1689 | } |
| 1690 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 1691 | assert(res == 0); |
| 1692 | return 0; |
| 1693 | } |
| 1694 | |
| 1695 | |
| 1696 | int sem_trywait ( sem_t* sem ) |
| 1697 | { |
| 1698 | int ret, res; |
| 1699 | vg_sem_t* vg_sem; |
| 1700 | ensure_valgrind("sem_trywait"); |
| 1701 | vg_sem = se_remap(sem); |
| 1702 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
| 1703 | assert(res == 0); |
| 1704 | if (vg_sem->count > 0) { |
| 1705 | vg_sem->count--; |
| 1706 | ret = 0; |
| 1707 | } else { |
| 1708 | ret = -1; |
| 1709 | errno = EAGAIN; |
| 1710 | } |
| 1711 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 1712 | assert(res == 0); |
| 1713 | return ret; |
| 1714 | } |
| 1715 | |
| 1716 | |
| 1717 | int sem_getvalue(sem_t* sem, int * sval) |
| 1718 | { |
| 1719 | vg_sem_t* vg_sem; |
| 1720 | ensure_valgrind("sem_trywait"); |
| 1721 | vg_sem = se_remap(sem); |
| 1722 | *sval = vg_sem->count; |
| 1723 | return 0; |
| 1724 | } |
| 1725 | |
| 1726 | |
| 1727 | int sem_destroy(sem_t * sem) |
| 1728 | { |
| 1729 | kludged("sem_destroy"); |
| 1730 | /* if someone waiting on this semaphore, errno = EBUSY, return -1 */ |
| 1731 | return 0; |
| 1732 | } |
| 1733 | |
| 1734 | |
| 1735 | /* --------------------------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1736 | B'stard. |
| 1737 | ------------------------------------------------------------------ */ |
| 1738 | |
| 1739 | # define strong_alias(name, aliasname) \ |
| 1740 | extern __typeof (name) aliasname __attribute__ ((alias (#name))); |
| 1741 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1742 | # define weak_alias(name, aliasname) \ |
| 1743 | extern __typeof (name) aliasname __attribute__ ((weak, alias (#name))); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1744 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1745 | strong_alias(__pthread_mutex_lock, pthread_mutex_lock) |
| 1746 | strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock) |
| 1747 | strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock) |
| 1748 | strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init) |
| 1749 | weak_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype) |
| 1750 | strong_alias(__pthread_mutex_init, pthread_mutex_init) |
| 1751 | strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy) |
| 1752 | strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy) |
| 1753 | strong_alias(__pthread_once, pthread_once) |
| 1754 | strong_alias(__pthread_atfork, pthread_atfork) |
| 1755 | strong_alias(__pthread_key_create, pthread_key_create) |
| 1756 | strong_alias(__pthread_getspecific, pthread_getspecific) |
| 1757 | strong_alias(__pthread_setspecific, pthread_setspecific) |
| 1758 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1759 | #ifndef GLIBC_2_1 |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1760 | strong_alias(sigaction, __sigaction) |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1761 | #endif |
| 1762 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1763 | strong_alias(close, __close) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1764 | strong_alias(fcntl, __fcntl) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1765 | strong_alias(lseek, __lseek) |
| 1766 | strong_alias(open, __open) |
| 1767 | strong_alias(open64, __open64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1768 | strong_alias(read, __read) |
| 1769 | strong_alias(wait, __wait) |
| 1770 | strong_alias(write, __write) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1771 | strong_alias(connect, __connect) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1772 | strong_alias(send, __send) |
| 1773 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1774 | weak_alias (__pread64, pread64) |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 1775 | weak_alias (__pwrite64, pwrite64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1776 | weak_alias(__fork, fork) |
sewardj | 7f6456d | 2002-05-21 00:51:21 +0000 | [diff] [blame] | 1777 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1778 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1779 | |
| 1780 | /*--------------------------------------------------*/ |
| 1781 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1782 | int |
| 1783 | pthread_rwlock_rdlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1784 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1785 | static int moans = N_MOANS; |
| 1786 | if (moans-- > 0) |
| 1787 | kludged("pthread_rwlock_rdlock"); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1788 | return 0; |
| 1789 | } |
| 1790 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1791 | weak_alias(pthread_rwlock_rdlock, __pthread_rwlock_rdlock) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1792 | |
| 1793 | |
| 1794 | int |
| 1795 | pthread_rwlock_unlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1796 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1797 | static int moans = N_MOANS; |
| 1798 | if (moans-- > 0) |
| 1799 | kludged("pthread_rwlock_unlock"); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1800 | return 0; |
| 1801 | } |
| 1802 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1803 | weak_alias(pthread_rwlock_unlock, __pthread_rwlock_unlock) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1804 | |
| 1805 | |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 1806 | int |
| 1807 | pthread_rwlock_wrlock (void* /* pthread_rwlock_t* */ rwlock) |
| 1808 | { |
| 1809 | static int moans = N_MOANS; |
| 1810 | if (moans-- > 0) |
| 1811 | kludged("pthread_rwlock_wrlock"); |
| 1812 | return 0; |
| 1813 | } |
| 1814 | |
sewardj | 262b029 | 2002-05-01 00:03:16 +0000 | [diff] [blame] | 1815 | weak_alias(pthread_rwlock_wrlock, __pthread_rwlock_wrlock) |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 1816 | |
| 1817 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1818 | /* I've no idea what these are, but they get called quite a lot. |
| 1819 | Anybody know? */ |
| 1820 | |
| 1821 | #undef _IO_flockfile |
| 1822 | void _IO_flockfile ( _IO_FILE * file ) |
| 1823 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1824 | pthread_mutex_lock(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_flockfile, flockfile); |
| 1827 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1828 | |
| 1829 | #undef _IO_funlockfile |
| 1830 | void _IO_funlockfile ( _IO_FILE * file ) |
| 1831 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1832 | pthread_mutex_unlock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1833 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1834 | weak_alias(_IO_funlockfile, funlockfile); |
| 1835 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1836 | |
| 1837 | void _pthread_cleanup_push_defer ( void ) |
| 1838 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1839 | static int moans = N_MOANS; |
| 1840 | if (moans-- > 0) |
| 1841 | ignored("_pthread_cleanup_push_defer"); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1842 | } |
| 1843 | |
| 1844 | void _pthread_cleanup_pop_restore ( void ) |
| 1845 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1846 | static int moans = N_MOANS; |
| 1847 | if (moans-- > 0) |
| 1848 | ignored("_pthread_cleanup_pop_restore"); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1849 | } |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 1850 | |
sewardj | 60e3842 | 2002-05-08 14:08:22 +0000 | [diff] [blame] | 1851 | /*--------*/ |
| 1852 | void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, |
| 1853 | void (*__routine) (void *), |
| 1854 | void *__arg) |
| 1855 | { |
| 1856 | static int moans = N_MOANS; |
| 1857 | if (moans-- > 0) |
| 1858 | ignored("_pthread_cleanup_push"); |
| 1859 | } |
| 1860 | |
| 1861 | void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, |
| 1862 | int __execute) |
| 1863 | { |
| 1864 | static int moans = N_MOANS; |
| 1865 | if (moans-- > 0) { |
| 1866 | if (__execute) |
| 1867 | ignored("_pthread_cleanup_pop-EXECUTE"); |
| 1868 | else |
| 1869 | ignored("_pthread_cleanup_pop-NO-EXECUTE"); |
| 1870 | } |
| 1871 | } |
| 1872 | |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 1873 | |
| 1874 | /* This doesn't seem to be needed to simulate libpthread.so's external |
| 1875 | interface, but many people complain about its absence. */ |
| 1876 | |
| 1877 | strong_alias(__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np) |
| 1878 | weak_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np) |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1879 | |
| 1880 | |
| 1881 | /*--------------------------------------------------------------------*/ |
| 1882 | /*--- end vg_libpthread.c ---*/ |
| 1883 | /*--------------------------------------------------------------------*/ |