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 | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 58 | #define __USE_UNIX98 |
| 59 | #include <sys/types.h> |
| 60 | #include <pthread.h> |
| 61 | #undef __USE_UNIX98 |
| 62 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 63 | #include <unistd.h> |
| 64 | #include <string.h> |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 65 | #ifdef GLIBC_2_1 |
| 66 | #include <sys/time.h> |
| 67 | #endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 68 | |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 69 | #include <stdio.h> |
| 70 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 71 | |
| 72 | /* --------------------------------------------------------------------- |
| 73 | Forwardses. |
| 74 | ------------------------------------------------------------------ */ |
| 75 | |
| 76 | static void wait_for_fd_to_be_readable_or_erring ( int fd ); |
| 77 | |
| 78 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 79 | /* --------------------------------------------------------------------- |
| 80 | Helpers. We have to be pretty self-sufficient. |
| 81 | ------------------------------------------------------------------ */ |
| 82 | |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 83 | /* Number of times any given error message is printed. */ |
| 84 | #define N_MOANS 3 |
| 85 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 86 | /* Extract from Valgrind the value of VG_(clo_trace_pthread_level). |
| 87 | Returns 0 (none) if not running on Valgrind. */ |
| 88 | static |
| 89 | int get_pt_trace_level ( void ) |
| 90 | { |
| 91 | int res; |
| 92 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 93 | VG_USERREQ__GET_PTHREAD_TRACE_LEVEL, |
| 94 | 0, 0, 0, 0); |
| 95 | return res; |
| 96 | } |
| 97 | |
| 98 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 99 | static |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 100 | void my_exit ( int arg ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 101 | { |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 102 | int __res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 103 | __asm__ volatile ("movl %%ecx, %%ebx ; int $0x80" |
| 104 | : "=a" (__res) |
| 105 | : "0" (__NR_exit), |
| 106 | "c" (arg) ); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 107 | /* We don't bother to mention the fact that this asm trashes %ebx, |
| 108 | since it won't return. If you ever do let it return ... fix |
| 109 | this! */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 113 | /* We need this guy -- it's in valgrind.so. */ |
| 114 | extern void VG_(startup) ( void ); |
| 115 | |
| 116 | |
| 117 | /* Just start up Valgrind if it's not already going. VG_(startup)() |
| 118 | detects and ignores second and subsequent calls. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 119 | static __inline__ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 120 | void ensure_valgrind ( char* caller ) |
| 121 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 122 | VG_(startup)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 123 | } |
| 124 | |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 125 | /* While we're at it ... hook our own startup function into this |
| 126 | game. */ |
| 127 | __asm__ ( |
| 128 | ".section .init\n" |
| 129 | "\tcall vgPlain_startup" |
| 130 | ); |
| 131 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 132 | |
| 133 | static |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 134 | __attribute__((noreturn)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 135 | void barf ( char* str ) |
| 136 | { |
| 137 | char buf[100]; |
| 138 | buf[0] = 0; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 139 | strcat(buf, "\nvalgrind's libpthread.so: "); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 140 | strcat(buf, str); |
| 141 | strcat(buf, "\n\n"); |
| 142 | write(2, buf, strlen(buf)); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 143 | my_exit(1); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 144 | /* We have to persuade gcc into believing this doesn't return. */ |
| 145 | while (1) { }; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 149 | static void ignored ( char* msg ) |
| 150 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 151 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 152 | char* ig = "valgrind's libpthread.so: IGNORED call to: "; |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 153 | write(2, ig, strlen(ig)); |
| 154 | write(2, msg, strlen(msg)); |
| 155 | ig = "\n"; |
| 156 | write(2, ig, strlen(ig)); |
| 157 | } |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 158 | } |
| 159 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 160 | static void kludged ( char* msg ) |
| 161 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 162 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 163 | char* ig = "valgrind's libpthread.so: KLUDGED call to: "; |
| 164 | write(2, ig, strlen(ig)); |
| 165 | write(2, msg, strlen(msg)); |
| 166 | ig = "\n"; |
| 167 | write(2, ig, strlen(ig)); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | static void not_inside ( char* msg ) |
| 172 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 173 | VG_(startup)(); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 174 | } |
| 175 | |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 176 | __attribute__((noreturn)) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 177 | void vgPlain_unimp ( char* what ) |
| 178 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 179 | char* ig = "valgrind's libpthread.so: UNIMPLEMENTED FUNCTION: "; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 180 | write(2, ig, strlen(ig)); |
| 181 | write(2, what, strlen(what)); |
| 182 | ig = "\n"; |
| 183 | write(2, ig, strlen(ig)); |
| 184 | barf("Please report this bug to me at: jseward@acm.org"); |
| 185 | } |
| 186 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 187 | |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 188 | void my_assert_fail ( Char* expr, Char* file, Int line, Char* fn ) |
| 189 | { |
| 190 | static Bool entered = False; |
| 191 | if (entered) |
| 192 | my_exit(2); |
| 193 | entered = True; |
| 194 | fprintf(stderr, "\n%s: %s:%d (%s): Assertion `%s' failed.\n", |
| 195 | "valgrind", file, line, fn, expr ); |
| 196 | fprintf(stderr, "Please report this bug to me at: %s\n\n", |
| 197 | VG_EMAIL_ADDR); |
| 198 | my_exit(1); |
| 199 | } |
| 200 | |
| 201 | #define MY__STRING(__str) #__str |
| 202 | |
| 203 | #define my_assert(expr) \ |
| 204 | ((void) ((expr) ? 0 : \ |
| 205 | (my_assert_fail (MY__STRING(expr), \ |
| 206 | __FILE__, __LINE__, \ |
| 207 | __PRETTY_FUNCTION__), 0))) |
| 208 | |
| 209 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 210 | /* --------------------------------------------------------------------- |
| 211 | Pass pthread_ calls to Valgrind's request mechanism. |
| 212 | ------------------------------------------------------------------ */ |
| 213 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 214 | #include <errno.h> |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 215 | #include <sys/time.h> /* gettimeofday */ |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 216 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 217 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 218 | /* --------------------------------------------------- |
| 219 | THREAD ATTRIBUTES |
| 220 | ------------------------------------------------ */ |
| 221 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 222 | int pthread_attr_init(pthread_attr_t *attr) |
| 223 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 224 | /* Just initialise the fields which we might look at. */ |
| 225 | attr->__detachstate = PTHREAD_CREATE_JOINABLE; |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) |
| 230 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 231 | if (detachstate != PTHREAD_CREATE_JOINABLE |
| 232 | && detachstate != PTHREAD_CREATE_DETACHED) |
| 233 | return EINVAL; |
| 234 | attr->__detachstate = detachstate; |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 235 | return 0; |
| 236 | } |
| 237 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 238 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit) |
| 239 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 240 | static int moans = N_MOANS; |
| 241 | if (moans-- > 0) |
| 242 | ignored("pthread_attr_setinheritsched"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 243 | return 0; |
| 244 | } |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 245 | |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 246 | __attribute__((weak)) |
| 247 | int pthread_attr_setstacksize (pthread_attr_t *__attr, |
| 248 | size_t __stacksize) |
| 249 | { |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 250 | size_t limit; |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 251 | ensure_valgrind("pthread_attr_setstacksize"); |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 252 | limit = VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 253 | - 1000; /* paranoia */ |
| 254 | if (__stacksize < limit) |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 255 | return 0; |
| 256 | barf("pthread_attr_setstacksize: " |
| 257 | "requested size >= VG_PTHREAD_STACK_SIZE\n " |
| 258 | "edit vg_include.h and rebuild."); |
| 259 | } |
| 260 | |
| 261 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 262 | /* This is completely bogus. */ |
| 263 | int pthread_attr_getschedparam(const pthread_attr_t *attr, |
| 264 | struct sched_param *param) |
| 265 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 266 | static int moans = N_MOANS; |
| 267 | if (moans-- > 0) |
| 268 | kludged("pthread_attr_getschedparam"); |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 269 | # ifdef GLIBC_2_1 |
| 270 | if (param) param->sched_priority = 0; /* who knows */ |
| 271 | # else |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 272 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 273 | # endif |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | int pthread_attr_setschedparam(pthread_attr_t *attr, |
| 278 | const struct sched_param *param) |
| 279 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 280 | static int moans = N_MOANS; |
| 281 | if (moans-- > 0) |
| 282 | ignored("pthread_attr_setschedparam"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | int pthread_attr_destroy(pthread_attr_t *attr) |
| 287 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 288 | static int moans = N_MOANS; |
| 289 | if (moans-- > 0) |
| 290 | ignored("pthread_attr_destroy"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 291 | return 0; |
| 292 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 293 | |
sewardj | 0d84423 | 2002-06-02 09:29:31 +0000 | [diff] [blame] | 294 | /* These are no-ops, as with LinuxThreads. */ |
| 295 | int pthread_attr_setscope ( pthread_attr_t *attr, int scope ) |
| 296 | { |
| 297 | ensure_valgrind("pthread_attr_setscope"); |
| 298 | if (scope == PTHREAD_SCOPE_SYSTEM) |
| 299 | return 0; |
| 300 | if (scope == PTHREAD_SCOPE_PROCESS) |
| 301 | return ENOTSUP; |
| 302 | return EINVAL; |
| 303 | } |
| 304 | |
| 305 | int pthread_attr_getscope ( const pthread_attr_t *attr, int *scope ) |
| 306 | { |
| 307 | ensure_valgrind("pthread_attr_setscope"); |
| 308 | if (scope) |
| 309 | *scope = PTHREAD_SCOPE_SYSTEM; |
| 310 | return 0; |
| 311 | } |
| 312 | |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 313 | |
| 314 | /* Pretty bogus. Avoid if possible. */ |
| 315 | int pthread_getattr_np (pthread_t thread, pthread_attr_t *attr) |
| 316 | { |
| 317 | int detached; |
| 318 | size_t limit; |
| 319 | ensure_valgrind("pthread_getattr_np"); |
| 320 | kludged("pthread_getattr_np"); |
| 321 | limit = VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 322 | - 1000; /* paranoia */ |
| 323 | attr->__detachstate = PTHREAD_CREATE_JOINABLE; |
| 324 | attr->__schedpolicy = SCHED_OTHER; |
| 325 | attr->__schedparam.sched_priority = 0; |
| 326 | attr->__inheritsched = PTHREAD_EXPLICIT_SCHED; |
| 327 | attr->__scope = PTHREAD_SCOPE_SYSTEM; |
| 328 | attr->__guardsize = VKI_BYTES_PER_PAGE; |
| 329 | attr->__stackaddr = NULL; |
| 330 | attr->__stackaddr_set = 0; |
| 331 | attr->__stacksize = limit; |
| 332 | VALGRIND_MAGIC_SEQUENCE(detached, (-1) /* default */, |
| 333 | VG_USERREQ__SET_OR_GET_DETACH, |
| 334 | 2 /* get */, thread, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 335 | my_assert(detached == 0 || detached == 1); |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 336 | if (detached) |
| 337 | attr->__detachstate = PTHREAD_CREATE_DETACHED; |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | |
| 342 | /* Bogus ... */ |
| 343 | int pthread_attr_getstackaddr ( const pthread_attr_t * attr, |
| 344 | void ** stackaddr ) |
| 345 | { |
| 346 | ensure_valgrind("pthread_attr_getstackaddr"); |
| 347 | kludged("pthread_attr_getstackaddr"); |
| 348 | if (stackaddr) |
| 349 | *stackaddr = NULL; |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* Not bogus (!) */ |
| 354 | int pthread_attr_getstacksize ( const pthread_attr_t * _attr, |
| 355 | size_t * __stacksize ) |
| 356 | { |
| 357 | size_t limit; |
| 358 | ensure_valgrind("pthread_attr_getstacksize"); |
| 359 | limit = VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 360 | - 1000; /* paranoia */ |
| 361 | if (__stacksize) |
| 362 | *__stacksize = limit; |
| 363 | return 0; |
| 364 | } |
| 365 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 366 | /* --------------------------------------------------- |
| 367 | Helper functions for running a thread |
| 368 | and for clearing up afterwards. |
| 369 | ------------------------------------------------ */ |
| 370 | |
| 371 | /* All exiting threads eventually pass through here, bearing the |
| 372 | return value, or PTHREAD_CANCELED, in ret_val. */ |
| 373 | static |
| 374 | __attribute__((noreturn)) |
| 375 | void thread_exit_wrapper ( void* ret_val ) |
| 376 | { |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 377 | int detached, res; |
| 378 | CleanupEntry cu; |
| 379 | pthread_key_t key; |
| 380 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 381 | /* Run this thread's cleanup handlers. */ |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 382 | while (1) { |
| 383 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 384 | VG_USERREQ__CLEANUP_POP, |
| 385 | &cu, 0, 0, 0); |
| 386 | if (res == -1) break; /* stack empty */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 387 | my_assert(res == 0); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 388 | if (0) printf("running exit cleanup handler"); |
| 389 | cu.fn ( cu.arg ); |
| 390 | } |
| 391 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 392 | /* Run this thread's key finalizers. Really this should be run |
| 393 | PTHREAD_DESTRUCTOR_ITERATIONS times. */ |
| 394 | for (key = 0; key < VG_N_THREAD_KEYS; key++) { |
| 395 | VALGRIND_MAGIC_SEQUENCE(res, (-2) /* default */, |
| 396 | VG_USERREQ__GET_KEY_D_AND_S, |
| 397 | key, &cu, 0, 0 ); |
| 398 | if (res == 0) { |
| 399 | /* valid key */ |
| 400 | if (cu.fn && cu.arg) |
| 401 | cu.fn /* destructor for key */ |
| 402 | ( cu.arg /* specific for key for this thread */ ); |
| 403 | continue; |
| 404 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 405 | my_assert(res == -1); |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 406 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 407 | |
| 408 | /* Decide on my final disposition. */ |
| 409 | VALGRIND_MAGIC_SEQUENCE(detached, (-1) /* default */, |
| 410 | VG_USERREQ__SET_OR_GET_DETACH, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 411 | 2 /* get */, pthread_self(), 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 412 | my_assert(detached == 0 || detached == 1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 413 | |
| 414 | if (detached) { |
| 415 | /* Detached; I just quit right now. */ |
| 416 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 417 | VG_USERREQ__QUIT, 0, 0, 0, 0); |
| 418 | } else { |
| 419 | /* Not detached; so I wait for a joiner. */ |
| 420 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 421 | VG_USERREQ__WAIT_JOINER, ret_val, 0, 0, 0); |
| 422 | } |
| 423 | /* NOTREACHED */ |
| 424 | barf("thread_exit_wrapper: still alive?!"); |
| 425 | } |
| 426 | |
| 427 | |
| 428 | /* This function is a wrapper function for running a thread. It runs |
| 429 | the root function specified in pthread_create, and then, should the |
| 430 | root function return a value, it arranges to run the thread's |
| 431 | cleanup handlers and exit correctly. */ |
| 432 | |
| 433 | /* Struct used to convey info from pthread_create to |
| 434 | thread_wrapper. */ |
| 435 | typedef |
| 436 | struct { |
| 437 | pthread_attr_t* attr; |
| 438 | void* (*root_fn) ( void* ); |
| 439 | void* arg; |
| 440 | } |
| 441 | NewThreadInfo; |
| 442 | |
| 443 | |
| 444 | /* This is passed to the VG_USERREQ__APPLY_IN_NEW_THREAD and so must |
| 445 | not return. Note that this runs in the new thread, not the |
| 446 | parent. */ |
| 447 | static |
| 448 | __attribute__((noreturn)) |
| 449 | void thread_wrapper ( NewThreadInfo* info ) |
| 450 | { |
| 451 | int res; |
| 452 | pthread_attr_t* attr; |
| 453 | void* (*root_fn) ( void* ); |
| 454 | void* arg; |
| 455 | void* ret_val; |
| 456 | |
| 457 | attr = info->attr; |
| 458 | root_fn = info->root_fn; |
| 459 | arg = info->arg; |
| 460 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 461 | /* Free up the arg block that pthread_create malloced. */ |
| 462 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 463 | VG_USERREQ__FREE, info, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 464 | my_assert(res == 0); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 465 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 466 | /* Minimally observe the attributes supplied. */ |
| 467 | if (attr) { |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 468 | my_assert(attr->__detachstate == PTHREAD_CREATE_DETACHED |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 469 | || attr->__detachstate == PTHREAD_CREATE_JOINABLE); |
| 470 | if (attr->__detachstate == PTHREAD_CREATE_DETACHED) |
| 471 | pthread_detach(pthread_self()); |
| 472 | } |
| 473 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 474 | /* The root function might not return. But if it does we simply |
| 475 | move along to thread_exit_wrapper. All other ways out for the |
| 476 | thread (cancellation, or calling pthread_exit) lead there |
| 477 | too. */ |
| 478 | ret_val = root_fn(arg); |
| 479 | thread_exit_wrapper(ret_val); |
| 480 | /* NOTREACHED */ |
| 481 | } |
| 482 | |
| 483 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 484 | /* --------------------------------------------------- |
| 485 | THREADs |
| 486 | ------------------------------------------------ */ |
| 487 | |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 488 | __attribute__((weak)) |
| 489 | int pthread_yield ( void ) |
| 490 | { |
| 491 | int res; |
| 492 | ensure_valgrind("pthread_yield"); |
| 493 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 494 | VG_USERREQ__PTHREAD_YIELD, 0, 0, 0, 0); |
| 495 | return 0; |
| 496 | } |
| 497 | |
| 498 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 499 | int pthread_equal(pthread_t thread1, pthread_t thread2) |
| 500 | { |
| 501 | return thread1 == thread2 ? 1 : 0; |
| 502 | } |
| 503 | |
| 504 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 505 | /* Bundle up the args into a malloc'd block and create a new thread |
| 506 | consisting of thread_wrapper() applied to said malloc'd block. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 507 | int |
| 508 | pthread_create (pthread_t *__restrict __thread, |
| 509 | __const pthread_attr_t *__restrict __attr, |
| 510 | void *(*__start_routine) (void *), |
| 511 | void *__restrict __arg) |
| 512 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 513 | int tid_child; |
| 514 | NewThreadInfo* info; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 515 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 516 | ensure_valgrind("pthread_create"); |
| 517 | |
| 518 | /* Allocate space for the arg block. thread_wrapper will free |
| 519 | it. */ |
| 520 | VALGRIND_MAGIC_SEQUENCE(info, NULL /* default */, |
| 521 | VG_USERREQ__MALLOC, |
| 522 | sizeof(NewThreadInfo), 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 523 | my_assert(info != NULL); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 524 | |
| 525 | info->attr = (pthread_attr_t*)__attr; |
| 526 | info->root_fn = __start_routine; |
| 527 | info->arg = __arg; |
| 528 | VALGRIND_MAGIC_SEQUENCE(tid_child, VG_INVALID_THREADID /* default */, |
| 529 | VG_USERREQ__APPLY_IN_NEW_THREAD, |
| 530 | &thread_wrapper, info, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 531 | my_assert(tid_child != VG_INVALID_THREADID); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 532 | |
| 533 | if (__thread) |
| 534 | *__thread = tid_child; |
| 535 | return 0; /* success */ |
| 536 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 537 | |
| 538 | |
| 539 | int |
| 540 | pthread_join (pthread_t __th, void **__thread_return) |
| 541 | { |
| 542 | int res; |
| 543 | ensure_valgrind("pthread_join"); |
| 544 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 545 | VG_USERREQ__PTHREAD_JOIN, |
| 546 | __th, __thread_return, 0, 0); |
| 547 | return res; |
| 548 | } |
| 549 | |
| 550 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 551 | void pthread_exit(void *retval) |
| 552 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 553 | ensure_valgrind("pthread_exit"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 554 | /* Simple! */ |
| 555 | thread_exit_wrapper(retval); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 556 | } |
| 557 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 558 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 559 | pthread_t pthread_self(void) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 560 | { |
| 561 | int tid; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 562 | ensure_valgrind("pthread_self"); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 563 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 564 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 565 | 0, 0, 0, 0); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 566 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 567 | barf("pthread_self: invalid ThreadId"); |
| 568 | return tid; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 572 | int pthread_detach(pthread_t th) |
| 573 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 574 | int res; |
| 575 | ensure_valgrind("pthread_detach"); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 576 | /* First we enquire as to the current detach state. */ |
| 577 | VALGRIND_MAGIC_SEQUENCE(res, (-2) /* default */, |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 578 | VG_USERREQ__SET_OR_GET_DETACH, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 579 | 2 /* get */, th, 0, 0); |
| 580 | if (res == -1) /* not found */ |
| 581 | return ESRCH; |
| 582 | if (res == 1) /* already detached */ |
| 583 | return EINVAL; |
| 584 | if (res == 0) { |
| 585 | VALGRIND_MAGIC_SEQUENCE(res, (-2) /* default */, |
| 586 | VG_USERREQ__SET_OR_GET_DETACH, |
| 587 | 1 /* set */, th, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 588 | my_assert(res == 0); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 589 | return 0; |
| 590 | } |
| 591 | barf("pthread_detach"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 592 | } |
| 593 | |
| 594 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 595 | /* --------------------------------------------------- |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 596 | CLEANUP STACKS |
| 597 | ------------------------------------------------ */ |
| 598 | |
| 599 | void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, |
| 600 | void (*__routine) (void *), |
| 601 | void *__arg) |
| 602 | { |
| 603 | int res; |
| 604 | CleanupEntry cu; |
| 605 | ensure_valgrind("_pthread_cleanup_push"); |
| 606 | cu.fn = __routine; |
| 607 | cu.arg = __arg; |
| 608 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 609 | VG_USERREQ__CLEANUP_PUSH, |
| 610 | &cu, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 611 | my_assert(res == 0); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *__buffer, |
| 616 | void (*__routine) (void *), |
| 617 | void *__arg) |
| 618 | { |
| 619 | /* As _pthread_cleanup_push, but first save the thread's original |
| 620 | cancellation type in __buffer and set it to Deferred. */ |
| 621 | int orig_ctype; |
| 622 | ensure_valgrind("_pthread_cleanup_push_defer"); |
| 623 | /* Set to Deferred, and put the old cancellation type in res. */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 624 | my_assert(-1 != PTHREAD_CANCEL_DEFERRED); |
| 625 | my_assert(-1 != PTHREAD_CANCEL_ASYNCHRONOUS); |
| 626 | my_assert(sizeof(struct _pthread_cleanup_buffer) >= sizeof(int)); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 627 | VALGRIND_MAGIC_SEQUENCE(orig_ctype, (-1) /* default */, |
| 628 | VG_USERREQ__SET_CANCELTYPE, |
| 629 | PTHREAD_CANCEL_DEFERRED, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 630 | my_assert(orig_ctype != -1); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 631 | *((int*)(__buffer)) = orig_ctype; |
| 632 | /* Now push the cleanup. */ |
| 633 | _pthread_cleanup_push(NULL, __routine, __arg); |
| 634 | } |
| 635 | |
| 636 | |
| 637 | void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, |
| 638 | int __execute) |
| 639 | { |
| 640 | int res; |
| 641 | CleanupEntry cu; |
| 642 | ensure_valgrind("_pthread_cleanup_push"); |
| 643 | cu.fn = cu.arg = NULL; /* paranoia */ |
| 644 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 645 | VG_USERREQ__CLEANUP_POP, |
| 646 | &cu, 0, 0, 0); |
| 647 | if (res == 0) { |
| 648 | /* pop succeeded */ |
| 649 | if (__execute) { |
| 650 | cu.fn ( cu.arg ); |
| 651 | } |
| 652 | return; |
| 653 | } |
| 654 | if (res == -1) { |
| 655 | /* stack underflow */ |
| 656 | return; |
| 657 | } |
| 658 | barf("_pthread_cleanup_pop"); |
| 659 | } |
| 660 | |
| 661 | |
| 662 | void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *__buffer, |
| 663 | int __execute) |
| 664 | { |
| 665 | int orig_ctype, fake_ctype; |
| 666 | /* As _pthread_cleanup_pop, but after popping/running the handler, |
| 667 | restore the thread's original cancellation type from the first |
| 668 | word of __buffer. */ |
| 669 | _pthread_cleanup_pop(NULL, __execute); |
| 670 | orig_ctype = *((int*)(__buffer)); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 671 | my_assert(orig_ctype == PTHREAD_CANCEL_DEFERRED |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 672 | || orig_ctype == PTHREAD_CANCEL_ASYNCHRONOUS); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 673 | my_assert(-1 != PTHREAD_CANCEL_DEFERRED); |
| 674 | my_assert(-1 != PTHREAD_CANCEL_ASYNCHRONOUS); |
| 675 | my_assert(sizeof(struct _pthread_cleanup_buffer) >= sizeof(int)); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 676 | VALGRIND_MAGIC_SEQUENCE(fake_ctype, (-1) /* default */, |
| 677 | VG_USERREQ__SET_CANCELTYPE, |
| 678 | orig_ctype, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 679 | my_assert(fake_ctype == PTHREAD_CANCEL_DEFERRED); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | |
| 683 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 684 | MUTEX ATTRIBUTES |
| 685 | ------------------------------------------------ */ |
| 686 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 687 | int __pthread_mutexattr_init(pthread_mutexattr_t *attr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 688 | { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 689 | attr->__mutexkind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 690 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 691 | } |
| 692 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 693 | int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 694 | { |
| 695 | switch (type) { |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 696 | # ifndef GLIBC_2_1 |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 697 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 698 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 699 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 700 | # ifdef GLIBC_2_1 |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 701 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 702 | # endif |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 703 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 704 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 705 | attr->__mutexkind = type; |
| 706 | return 0; |
| 707 | default: |
| 708 | return EINVAL; |
| 709 | } |
| 710 | } |
| 711 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 712 | int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 713 | { |
| 714 | return 0; |
| 715 | } |
| 716 | |
| 717 | |
| 718 | /* --------------------------------------------------- |
| 719 | MUTEXes |
| 720 | ------------------------------------------------ */ |
| 721 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 722 | int __pthread_mutex_init(pthread_mutex_t *mutex, |
| 723 | const pthread_mutexattr_t *mutexattr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 724 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 725 | mutex->__m_count = 0; |
| 726 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 727 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
| 728 | if (mutexattr) |
| 729 | mutex->__m_kind = mutexattr->__mutexkind; |
| 730 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 731 | } |
| 732 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 733 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 734 | int __pthread_mutex_lock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 735 | { |
| 736 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 737 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 738 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 739 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 740 | VG_USERREQ__PTHREAD_MUTEX_LOCK, |
| 741 | mutex, 0, 0, 0); |
| 742 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 743 | } else { |
| 744 | if (moans-- > 0) |
| 745 | not_inside("pthread_mutex_lock"); |
| 746 | return 0; /* success */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 750 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 751 | int __pthread_mutex_trylock(pthread_mutex_t *mutex) |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 752 | { |
| 753 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 754 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 755 | if (RUNNING_ON_VALGRIND) { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 756 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 757 | VG_USERREQ__PTHREAD_MUTEX_TRYLOCK, |
| 758 | mutex, 0, 0, 0); |
| 759 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 760 | } else { |
| 761 | if (moans-- > 0) |
| 762 | not_inside("pthread_mutex_trylock"); |
| 763 | return 0; |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 764 | } |
| 765 | } |
| 766 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 767 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 768 | int __pthread_mutex_unlock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 769 | { |
| 770 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 771 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 772 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 773 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 774 | VG_USERREQ__PTHREAD_MUTEX_UNLOCK, |
| 775 | mutex, 0, 0, 0); |
| 776 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 777 | } else { |
| 778 | if (moans-- > 0) |
| 779 | not_inside("pthread_mutex_unlock"); |
| 780 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 781 | } |
| 782 | } |
| 783 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 784 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 785 | int __pthread_mutex_destroy(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 786 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 787 | /* Valgrind doesn't hold any resources on behalf of the mutex, so no |
| 788 | need to involve it. */ |
| 789 | if (mutex->__m_count > 0) |
| 790 | return EBUSY; |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 791 | mutex->__m_count = 0; |
| 792 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 793 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 794 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 798 | /* --------------------------------------------------- |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 799 | CONDITION VARIABLES |
| 800 | ------------------------------------------------ */ |
| 801 | |
| 802 | /* LinuxThreads supports no attributes for conditions. Hence ... */ |
| 803 | |
| 804 | int pthread_condattr_init(pthread_condattr_t *attr) |
| 805 | { |
| 806 | return 0; |
| 807 | } |
| 808 | |
sewardj | 0738a59 | 2002-04-20 13:59:33 +0000 | [diff] [blame] | 809 | int pthread_condattr_destroy(pthread_condattr_t *attr) |
| 810 | { |
| 811 | return 0; |
| 812 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 813 | |
| 814 | int pthread_cond_init( pthread_cond_t *cond, |
| 815 | const pthread_condattr_t *cond_attr) |
| 816 | { |
| 817 | cond->__c_waiting = (_pthread_descr)VG_INVALID_THREADID; |
| 818 | return 0; |
| 819 | } |
| 820 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 821 | int pthread_cond_destroy(pthread_cond_t *cond) |
| 822 | { |
| 823 | /* should check that no threads are waiting on this CV */ |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 824 | static int moans = N_MOANS; |
| 825 | if (moans-- > 0) |
| 826 | kludged("pthread_cond_destroy"); |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 827 | return 0; |
| 828 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 829 | |
| 830 | /* --------------------------------------------------- |
| 831 | SCHEDULING |
| 832 | ------------------------------------------------ */ |
| 833 | |
| 834 | /* This is completely bogus. */ |
| 835 | int pthread_getschedparam(pthread_t target_thread, |
| 836 | int *policy, |
| 837 | struct sched_param *param) |
| 838 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 839 | static int moans = N_MOANS; |
| 840 | if (moans-- > 0) |
| 841 | kludged("pthread_getschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 842 | if (policy) *policy = SCHED_OTHER; |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 843 | # ifdef GLIBC_2_1 |
| 844 | if (param) param->sched_priority = 0; /* who knows */ |
| 845 | # else |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 846 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 847 | # endif |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | int pthread_setschedparam(pthread_t target_thread, |
| 852 | int policy, |
| 853 | const struct sched_param *param) |
| 854 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 855 | static int moans = N_MOANS; |
| 856 | if (moans-- > 0) |
| 857 | ignored("pthread_setschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 858 | return 0; |
| 859 | } |
| 860 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 861 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 862 | { |
| 863 | int res; |
| 864 | ensure_valgrind("pthread_cond_wait"); |
| 865 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 866 | VG_USERREQ__PTHREAD_COND_WAIT, |
| 867 | cond, mutex, 0, 0); |
| 868 | return res; |
| 869 | } |
| 870 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 871 | int pthread_cond_timedwait ( pthread_cond_t *cond, |
| 872 | pthread_mutex_t *mutex, |
| 873 | const struct timespec *abstime ) |
| 874 | { |
| 875 | int res; |
| 876 | unsigned int ms_now, ms_end; |
| 877 | struct timeval timeval_now; |
| 878 | unsigned long long int ull_ms_now_after_1970; |
| 879 | unsigned long long int ull_ms_end_after_1970; |
| 880 | |
| 881 | ensure_valgrind("pthread_cond_timedwait"); |
| 882 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 883 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 884 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 885 | my_assert(ms_now != 0xFFFFFFFF); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 886 | res = gettimeofday(&timeval_now, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 887 | my_assert(res == 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 888 | |
| 889 | ull_ms_now_after_1970 |
| 890 | = 1000ULL * ((unsigned long long int)(timeval_now.tv_sec)) |
| 891 | + ((unsigned long long int)(timeval_now.tv_usec / 1000000)); |
| 892 | ull_ms_end_after_1970 |
| 893 | = 1000ULL * ((unsigned long long int)(abstime->tv_sec)) |
| 894 | + ((unsigned long long int)(abstime->tv_nsec / 1000000)); |
sewardj | d8e919e | 2002-05-29 20:13:53 +0000 | [diff] [blame] | 895 | if (ull_ms_end_after_1970 < ull_ms_now_after_1970) |
| 896 | ull_ms_end_after_1970 = ull_ms_now_after_1970; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 897 | ms_end |
| 898 | = ms_now + (unsigned int)(ull_ms_end_after_1970 - ull_ms_now_after_1970); |
| 899 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 900 | VG_USERREQ__PTHREAD_COND_TIMEDWAIT, |
| 901 | cond, mutex, ms_end, 0); |
| 902 | return res; |
| 903 | } |
| 904 | |
| 905 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 906 | int pthread_cond_signal(pthread_cond_t *cond) |
| 907 | { |
| 908 | int res; |
| 909 | ensure_valgrind("pthread_cond_signal"); |
| 910 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 911 | VG_USERREQ__PTHREAD_COND_SIGNAL, |
| 912 | cond, 0, 0, 0); |
| 913 | return res; |
| 914 | } |
| 915 | |
| 916 | int pthread_cond_broadcast(pthread_cond_t *cond) |
| 917 | { |
| 918 | int res; |
| 919 | ensure_valgrind("pthread_cond_broadcast"); |
| 920 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 921 | VG_USERREQ__PTHREAD_COND_BROADCAST, |
| 922 | cond, 0, 0, 0); |
| 923 | return res; |
| 924 | } |
| 925 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 926 | |
| 927 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 928 | CANCELLATION |
| 929 | ------------------------------------------------ */ |
| 930 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 931 | int pthread_setcancelstate(int state, int *oldstate) |
| 932 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 933 | int res; |
| 934 | ensure_valgrind("pthread_setcancelstate"); |
| 935 | if (state != PTHREAD_CANCEL_ENABLE |
| 936 | && state != PTHREAD_CANCEL_DISABLE) |
| 937 | return EINVAL; |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 938 | my_assert(-1 != PTHREAD_CANCEL_ENABLE); |
| 939 | my_assert(-1 != PTHREAD_CANCEL_DISABLE); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 940 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 941 | VG_USERREQ__SET_CANCELSTATE, |
| 942 | state, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 943 | my_assert(res != -1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 944 | if (oldstate) |
| 945 | *oldstate = res; |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 946 | return 0; |
| 947 | } |
| 948 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 949 | int pthread_setcanceltype(int type, int *oldtype) |
| 950 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 951 | int res; |
| 952 | ensure_valgrind("pthread_setcanceltype"); |
| 953 | if (type != PTHREAD_CANCEL_DEFERRED |
| 954 | && type != PTHREAD_CANCEL_ASYNCHRONOUS) |
| 955 | return EINVAL; |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 956 | my_assert(-1 != PTHREAD_CANCEL_DEFERRED); |
| 957 | my_assert(-1 != PTHREAD_CANCEL_ASYNCHRONOUS); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 958 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 959 | VG_USERREQ__SET_CANCELTYPE, |
| 960 | type, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 961 | my_assert(res != -1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 962 | if (oldtype) |
| 963 | *oldtype = res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 964 | return 0; |
| 965 | } |
| 966 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 967 | int pthread_cancel(pthread_t thread) |
| 968 | { |
| 969 | int res; |
| 970 | ensure_valgrind("pthread_cancel"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 971 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 972 | VG_USERREQ__SET_CANCELPEND, |
| 973 | thread, &thread_exit_wrapper, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 974 | my_assert(res != -1); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 975 | return res; |
| 976 | } |
| 977 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 978 | static __inline__ |
| 979 | void __my_pthread_testcancel(void) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 980 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 981 | int res; |
| 982 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 983 | VG_USERREQ__TESTCANCEL, |
| 984 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 985 | my_assert(res == 0); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 986 | } |
| 987 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 988 | void pthread_testcancel ( void ) |
| 989 | { |
| 990 | __my_pthread_testcancel(); |
| 991 | } |
| 992 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 993 | |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 994 | /* Not really sure what this is for. I suspect for doing the POSIX |
| 995 | requirements for fork() and exec(). We do this internally anyway |
| 996 | whenever those syscalls are observed, so this could be superfluous, |
| 997 | but hey ... |
| 998 | */ |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 999 | void __pthread_kill_other_threads_np ( void ) |
| 1000 | { |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 1001 | int res; |
| 1002 | ensure_valgrind("__pthread_kill_other_threads_np"); |
| 1003 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 1004 | VG_USERREQ__NUKE_OTHER_THREADS, |
| 1005 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1006 | my_assert(res == 0); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1009 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1010 | /* --------------------------------------------------- |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1011 | SIGNALS |
| 1012 | ------------------------------------------------ */ |
| 1013 | |
| 1014 | #include <signal.h> |
| 1015 | |
| 1016 | int pthread_sigmask(int how, const sigset_t *newmask, |
| 1017 | sigset_t *oldmask) |
| 1018 | { |
| 1019 | int res; |
| 1020 | |
| 1021 | /* A bit subtle, because the scheduler expects newmask and oldmask |
| 1022 | to be vki_sigset_t* rather than sigset_t*, and the two are |
| 1023 | different. Fortunately the first 64 bits of a sigset_t are |
| 1024 | exactly a vki_sigset_t, so we just pass the pointers through |
| 1025 | unmodified. Haaaack! |
| 1026 | |
| 1027 | Also mash the how value so that the SIG_ constants from glibc |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1028 | constants to VKI_ constants, so that the former do not have to |
| 1029 | be included into vg_scheduler.c. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1030 | |
| 1031 | ensure_valgrind("pthread_sigmask"); |
| 1032 | |
| 1033 | switch (how) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1034 | case SIG_SETMASK: how = VKI_SIG_SETMASK; break; |
| 1035 | case SIG_BLOCK: how = VKI_SIG_BLOCK; break; |
| 1036 | case SIG_UNBLOCK: how = VKI_SIG_UNBLOCK; break; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1037 | default: return EINVAL; |
| 1038 | } |
| 1039 | |
| 1040 | /* Crude check */ |
| 1041 | if (newmask == NULL) |
| 1042 | return EFAULT; |
| 1043 | |
| 1044 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1045 | VG_USERREQ__PTHREAD_SIGMASK, |
| 1046 | how, newmask, oldmask, 0); |
| 1047 | |
| 1048 | /* The scheduler tells us of any memory violations. */ |
| 1049 | return res == 0 ? 0 : EFAULT; |
| 1050 | } |
| 1051 | |
| 1052 | |
| 1053 | int sigwait ( const sigset_t* set, int* sig ) |
| 1054 | { |
| 1055 | int res; |
| 1056 | ensure_valgrind("sigwait"); |
| 1057 | /* As with pthread_sigmask we deliberately confuse sigset_t with |
| 1058 | vki_ksigset_t. */ |
| 1059 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1060 | VG_USERREQ__SIGWAIT, |
| 1061 | set, sig, 0, 0); |
| 1062 | return res; |
| 1063 | } |
| 1064 | |
| 1065 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1066 | int pthread_kill(pthread_t thread, int signo) |
| 1067 | { |
| 1068 | int res; |
| 1069 | ensure_valgrind("pthread_kill"); |
| 1070 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1071 | VG_USERREQ__PTHREAD_KILL, |
| 1072 | thread, signo, 0, 0); |
| 1073 | return res; |
| 1074 | } |
| 1075 | |
| 1076 | |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1077 | /* Copied verbatim from Linuxthreads */ |
| 1078 | /* Redefine raise() to send signal to calling thread only, |
| 1079 | as per POSIX 1003.1c */ |
| 1080 | int raise (int sig) |
| 1081 | { |
| 1082 | int retcode = pthread_kill(pthread_self(), sig); |
| 1083 | if (retcode == 0) |
| 1084 | return 0; |
| 1085 | else { |
| 1086 | errno = retcode; |
| 1087 | return -1; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1092 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1093 | THREAD-SPECIFICs |
| 1094 | ------------------------------------------------ */ |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1095 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1096 | int __pthread_key_create(pthread_key_t *key, |
| 1097 | void (*destr_function) (void *)) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1098 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1099 | int res; |
| 1100 | ensure_valgrind("pthread_key_create"); |
| 1101 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1102 | VG_USERREQ__PTHREAD_KEY_CREATE, |
| 1103 | key, destr_function, 0, 0); |
| 1104 | return res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | int pthread_key_delete(pthread_key_t key) |
| 1108 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1109 | static int moans = N_MOANS; |
| 1110 | if (moans-- > 0) |
| 1111 | ignored("pthread_key_delete"); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1112 | return 0; |
| 1113 | } |
| 1114 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1115 | int __pthread_setspecific(pthread_key_t key, const void *pointer) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1116 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1117 | int res; |
| 1118 | ensure_valgrind("pthread_setspecific"); |
| 1119 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1120 | VG_USERREQ__PTHREAD_SETSPECIFIC, |
| 1121 | key, pointer, 0, 0); |
| 1122 | return res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1125 | void * __pthread_getspecific(pthread_key_t key) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1126 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1127 | int res; |
| 1128 | ensure_valgrind("pthread_getspecific"); |
| 1129 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1130 | VG_USERREQ__PTHREAD_GETSPECIFIC, |
| 1131 | key, 0 , 0, 0); |
| 1132 | return (void*)res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1135 | |
| 1136 | /* --------------------------------------------------- |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1137 | ONCEry |
| 1138 | ------------------------------------------------ */ |
| 1139 | |
| 1140 | static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER; |
| 1141 | |
| 1142 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1143 | int __pthread_once ( pthread_once_t *once_control, |
| 1144 | void (*init_routine) (void) ) |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1145 | { |
| 1146 | int res; |
| 1147 | ensure_valgrind("pthread_once"); |
| 1148 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1149 | res = __pthread_mutex_lock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1150 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1151 | if (res != 0) { |
| 1152 | printf("res = %d\n",res); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1153 | barf("pthread_once: Looks like your program's " |
| 1154 | "init routine calls back to pthread_once() ?!"); |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1155 | } |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1156 | |
| 1157 | if (*once_control == 0) { |
| 1158 | *once_control = 1; |
| 1159 | init_routine(); |
| 1160 | } |
| 1161 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1162 | __pthread_mutex_unlock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | |
| 1168 | /* --------------------------------------------------- |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1169 | MISC |
| 1170 | ------------------------------------------------ */ |
| 1171 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1172 | int __pthread_atfork ( void (*prepare)(void), |
| 1173 | void (*parent)(void), |
| 1174 | void (*child)(void) ) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1175 | { |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 1176 | /* We have to do this properly or not at all; faking it isn't an |
| 1177 | option. */ |
| 1178 | vgPlain_unimp("__pthread_atfork"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1179 | } |
| 1180 | |
| 1181 | |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 1182 | __attribute__((weak)) |
| 1183 | void __pthread_initialize ( void ) |
| 1184 | { |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 1185 | ensure_valgrind("__pthread_initialize"); |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1189 | /* --------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1190 | LIBRARY-PRIVATE THREAD SPECIFIC STATE |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1191 | ------------------------------------------------ */ |
| 1192 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1193 | #include <resolv.h> |
| 1194 | static int thread_specific_errno[VG_N_THREADS]; |
| 1195 | static int thread_specific_h_errno[VG_N_THREADS]; |
| 1196 | static struct __res_state |
| 1197 | thread_specific_res_state[VG_N_THREADS]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1198 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1199 | int* __errno_location ( void ) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1200 | { |
| 1201 | int tid; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1202 | /* ensure_valgrind("__errno_location"); */ |
| 1203 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1204 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 1205 | 0, 0, 0, 0); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1206 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1207 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1208 | barf("__errno_location: invalid ThreadId"); |
| 1209 | return & thread_specific_errno[tid]; |
| 1210 | } |
| 1211 | |
| 1212 | int* __h_errno_location ( void ) |
| 1213 | { |
| 1214 | int tid; |
| 1215 | /* ensure_valgrind("__h_errno_location"); */ |
| 1216 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 1217 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 1218 | 0, 0, 0, 0); |
| 1219 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1220 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1221 | barf("__h_errno_location: invalid ThreadId"); |
| 1222 | return & thread_specific_h_errno[tid]; |
| 1223 | } |
| 1224 | |
| 1225 | struct __res_state* __res_state ( void ) |
| 1226 | { |
| 1227 | int tid; |
| 1228 | /* ensure_valgrind("__res_state"); */ |
| 1229 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 1230 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 1231 | 0, 0, 0, 0); |
| 1232 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1233 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1234 | barf("__res_state: invalid ThreadId"); |
| 1235 | return & thread_specific_res_state[tid]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
| 1238 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1239 | /* --------------------------------------------------- |
| 1240 | LIBC-PRIVATE SPECIFIC DATA |
| 1241 | ------------------------------------------------ */ |
| 1242 | |
| 1243 | /* Relies on assumption that initial private data is NULL. This |
| 1244 | should be fixed somehow. */ |
| 1245 | |
| 1246 | /* The allowable keys (indices) (all 2 of them). |
| 1247 | From sysdeps/pthread/bits/libc-tsd.h |
| 1248 | */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1249 | #define N_LIBC_TSD_EXTRA_KEYS 1 |
| 1250 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1251 | enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0, |
| 1252 | _LIBC_TSD_KEY_DL_ERROR, |
| 1253 | _LIBC_TSD_KEY_N }; |
| 1254 | |
| 1255 | /* Auto-initialising subsystem. libc_specifics_inited is set |
| 1256 | after initialisation. libc_specifics_inited_mx guards it. */ |
| 1257 | static int libc_specifics_inited = 0; |
| 1258 | static pthread_mutex_t libc_specifics_inited_mx = PTHREAD_MUTEX_INITIALIZER; |
| 1259 | |
| 1260 | /* These are the keys we must initialise the first time. */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1261 | static pthread_key_t libc_specifics_keys[_LIBC_TSD_KEY_N |
| 1262 | + N_LIBC_TSD_EXTRA_KEYS]; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1263 | |
| 1264 | /* Initialise the keys, if they are not already initialise. */ |
| 1265 | static |
| 1266 | void init_libc_tsd_keys ( void ) |
| 1267 | { |
| 1268 | int res, i; |
| 1269 | pthread_key_t k; |
| 1270 | |
| 1271 | res = pthread_mutex_lock(&libc_specifics_inited_mx); |
| 1272 | if (res != 0) barf("init_libc_tsd_keys: lock"); |
| 1273 | |
| 1274 | if (libc_specifics_inited == 0) { |
| 1275 | /* printf("INIT libc specifics\n"); */ |
| 1276 | libc_specifics_inited = 1; |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1277 | 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] | 1278 | res = pthread_key_create(&k, NULL); |
| 1279 | if (res != 0) barf("init_libc_tsd_keys: create"); |
| 1280 | libc_specifics_keys[i] = k; |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | res = pthread_mutex_unlock(&libc_specifics_inited_mx); |
| 1285 | if (res != 0) barf("init_libc_tsd_keys: unlock"); |
| 1286 | } |
| 1287 | |
| 1288 | |
| 1289 | static int |
| 1290 | libc_internal_tsd_set ( enum __libc_tsd_key_t key, |
| 1291 | const void * pointer ) |
| 1292 | { |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1293 | int res; |
| 1294 | static int moans = N_MOANS; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1295 | /* printf("SET SET SET key %d ptr %p\n", key, pointer); */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1296 | if (key < _LIBC_TSD_KEY_MALLOC |
| 1297 | || key >= _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1298 | barf("libc_internal_tsd_set: invalid key"); |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1299 | if (key >= _LIBC_TSD_KEY_N && moans-- > 0) |
| 1300 | fprintf(stderr, |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1301 | "valgrind's libpthread.so: libc_internal_tsd_set: " |
| 1302 | "dubious key %d\n", key); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1303 | init_libc_tsd_keys(); |
| 1304 | res = pthread_setspecific(libc_specifics_keys[key], pointer); |
| 1305 | if (res != 0) barf("libc_internal_tsd_set: setspecific failed"); |
| 1306 | return 0; |
| 1307 | } |
| 1308 | |
| 1309 | static void * |
| 1310 | libc_internal_tsd_get ( enum __libc_tsd_key_t key ) |
| 1311 | { |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1312 | void* v; |
| 1313 | static int moans = N_MOANS; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1314 | /* printf("GET GET GET key %d\n", key); */ |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1315 | if (key < _LIBC_TSD_KEY_MALLOC |
| 1316 | || key >= _LIBC_TSD_KEY_N + N_LIBC_TSD_EXTRA_KEYS) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1317 | barf("libc_internal_tsd_get: invalid key"); |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1318 | if (key >= _LIBC_TSD_KEY_N && moans-- > 0) |
| 1319 | fprintf(stderr, |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1320 | "valgrind's libpthread.so: libc_internal_tsd_get: " |
| 1321 | "dubious key %d\n", key); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1322 | init_libc_tsd_keys(); |
| 1323 | v = pthread_getspecific(libc_specifics_keys[key]); |
| 1324 | /* if (v == NULL) barf("libc_internal_tsd_set: getspecific failed"); */ |
| 1325 | return v; |
| 1326 | } |
| 1327 | |
| 1328 | |
| 1329 | |
| 1330 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1331 | int (*__libc_internal_tsd_set) |
| 1332 | (enum __libc_tsd_key_t key, const void * pointer) |
| 1333 | = libc_internal_tsd_set; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1334 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1335 | void* (*__libc_internal_tsd_get) |
| 1336 | (enum __libc_tsd_key_t key) |
| 1337 | = libc_internal_tsd_get; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1338 | |
| 1339 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1340 | /* --------------------------------------------------------------------- |
| 1341 | These are here (I think) because they are deemed cancellation |
| 1342 | points by POSIX. For the moment we'll simply pass the call along |
| 1343 | to the corresponding thread-unaware (?) libc routine. |
| 1344 | ------------------------------------------------------------------ */ |
| 1345 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1346 | #include <stdlib.h> |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1347 | #include <sys/types.h> |
| 1348 | #include <sys/socket.h> |
| 1349 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1350 | #ifdef GLIBC_2_1 |
| 1351 | extern |
| 1352 | int __sigaction |
| 1353 | (int signum, |
| 1354 | const struct sigaction *act, |
| 1355 | struct sigaction *oldact); |
| 1356 | #else |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1357 | extern |
| 1358 | int __libc_sigaction |
| 1359 | (int signum, |
| 1360 | const struct sigaction *act, |
| 1361 | struct sigaction *oldact); |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1362 | #endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1363 | int sigaction(int signum, |
| 1364 | const struct sigaction *act, |
| 1365 | struct sigaction *oldact) |
| 1366 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1367 | __my_pthread_testcancel(); |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 1368 | # ifdef GLIBC_2_1 |
| 1369 | return __sigaction(signum, act, oldact); |
| 1370 | # else |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1371 | return __libc_sigaction(signum, act, oldact); |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 1372 | # endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1373 | } |
| 1374 | |
| 1375 | |
| 1376 | extern |
| 1377 | int __libc_connect(int sockfd, |
| 1378 | const struct sockaddr *serv_addr, |
| 1379 | socklen_t addrlen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1380 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1381 | int connect(int sockfd, |
| 1382 | const struct sockaddr *serv_addr, |
| 1383 | socklen_t addrlen) |
| 1384 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1385 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1386 | return __libc_connect(sockfd, serv_addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1387 | } |
| 1388 | |
| 1389 | |
| 1390 | extern |
| 1391 | int __libc_fcntl(int fd, int cmd, long arg); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1392 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1393 | int fcntl(int fd, int cmd, long arg) |
| 1394 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1395 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1396 | return __libc_fcntl(fd, cmd, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | |
| 1400 | extern |
| 1401 | ssize_t __libc_write(int fd, const void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1402 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1403 | ssize_t write(int fd, const void *buf, size_t count) |
| 1404 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1405 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1406 | return __libc_write(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
| 1409 | |
| 1410 | extern |
| 1411 | ssize_t __libc_read(int fd, void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1412 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1413 | ssize_t read(int fd, void *buf, size_t count) |
| 1414 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1415 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1416 | return __libc_read(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1417 | } |
| 1418 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1419 | |
| 1420 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1421 | int __libc_open64(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1422 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1423 | int open64(const char *pathname, int flags, mode_t mode) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1424 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1425 | __my_pthread_testcancel(); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1426 | return __libc_open64(pathname, flags, mode); |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1427 | } |
| 1428 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1429 | |
| 1430 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1431 | int __libc_open(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1432 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1433 | int open(const char *pathname, int flags, mode_t mode) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1434 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1435 | __my_pthread_testcancel(); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1436 | return __libc_open(pathname, flags, mode); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | |
| 1440 | extern |
| 1441 | int __libc_close(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1442 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1443 | int close(int fd) |
| 1444 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1445 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1446 | return __libc_close(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | |
| 1450 | extern |
| 1451 | int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1452 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1453 | int accept(int s, struct sockaddr *addr, socklen_t *addrlen) |
| 1454 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1455 | __my_pthread_testcancel(); |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1456 | wait_for_fd_to_be_readable_or_erring(s); |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1457 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1458 | return __libc_accept(s, addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
| 1461 | |
| 1462 | extern |
| 1463 | pid_t __libc_fork(void); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1464 | pid_t __fork(void) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1465 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1466 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1467 | return __libc_fork(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | |
| 1471 | extern |
| 1472 | pid_t __libc_waitpid(pid_t pid, int *status, int options); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1473 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1474 | pid_t waitpid(pid_t pid, int *status, int options) |
| 1475 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1476 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1477 | return __libc_waitpid(pid, status, options); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1478 | } |
| 1479 | |
| 1480 | |
| 1481 | extern |
| 1482 | int __libc_nanosleep(const struct timespec *req, struct timespec *rem); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1483 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1484 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 1485 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1486 | __my_pthread_testcancel(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1487 | return __libc_nanosleep(req, rem); |
| 1488 | } |
| 1489 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1490 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1491 | extern |
| 1492 | int __libc_fsync(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1493 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1494 | int fsync(int fd) |
| 1495 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1496 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1497 | return __libc_fsync(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1500 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1501 | extern |
| 1502 | off_t __libc_lseek(int fildes, off_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1503 | __attribute__((weak)) |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1504 | off_t lseek(int fildes, off_t offset, int whence) |
| 1505 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1506 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1507 | return __libc_lseek(fildes, offset, whence); |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1510 | |
| 1511 | extern |
| 1512 | __off64_t __libc_lseek64(int fildes, __off64_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1513 | __attribute__((weak)) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1514 | __off64_t lseek64(int fildes, __off64_t offset, int whence) |
| 1515 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1516 | __my_pthread_testcancel(); |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1517 | return __libc_lseek64(fildes, offset, whence); |
| 1518 | } |
| 1519 | |
| 1520 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1521 | extern |
| 1522 | ssize_t __libc_pread64 (int __fd, void *__buf, size_t __nbytes, |
| 1523 | __off64_t __offset); |
| 1524 | ssize_t __pread64 (int __fd, void *__buf, size_t __nbytes, |
| 1525 | __off64_t __offset) |
| 1526 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1527 | __my_pthread_testcancel(); |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1528 | return __libc_pread64(__fd, __buf, __nbytes, __offset); |
| 1529 | } |
| 1530 | |
| 1531 | |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 1532 | extern |
| 1533 | ssize_t __libc_pwrite64 (int __fd, const void *__buf, size_t __nbytes, |
| 1534 | __off64_t __offset); |
| 1535 | ssize_t __pwrite64 (int __fd, const void *__buf, size_t __nbytes, |
| 1536 | __off64_t __offset) |
| 1537 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1538 | __my_pthread_testcancel(); |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 1539 | return __libc_pwrite64(__fd, __buf, __nbytes, __offset); |
| 1540 | } |
| 1541 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1542 | |
sewardj | 39b93b1 | 2002-05-18 10:56:27 +0000 | [diff] [blame] | 1543 | extern |
| 1544 | ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset); |
| 1545 | __attribute__((weak)) |
| 1546 | ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) |
| 1547 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1548 | __my_pthread_testcancel(); |
sewardj | 39b93b1 | 2002-05-18 10:56:27 +0000 | [diff] [blame] | 1549 | return __libc_pwrite(fd, buf, count, offset); |
| 1550 | } |
| 1551 | |
| 1552 | |
| 1553 | extern |
| 1554 | ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset); |
| 1555 | __attribute__((weak)) |
| 1556 | ssize_t pread(int fd, void *buf, size_t count, off_t offset) |
| 1557 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1558 | __my_pthread_testcancel(); |
sewardj | 39b93b1 | 2002-05-18 10:56:27 +0000 | [diff] [blame] | 1559 | return __libc_pread(fd, buf, count, offset); |
| 1560 | } |
| 1561 | |
| 1562 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1563 | extern |
| 1564 | void __libc_longjmp(jmp_buf env, int val) __attribute((noreturn)); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1565 | /* not weak: __attribute__((weak)) */ |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1566 | void longjmp(jmp_buf env, int val) |
| 1567 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1568 | __my_pthread_testcancel(); |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1569 | __libc_longjmp(env, val); |
| 1570 | } |
| 1571 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1572 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1573 | extern |
| 1574 | int __libc_send(int s, const void *msg, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1575 | __attribute__((weak)) |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1576 | int send(int s, const void *msg, size_t len, int flags) |
| 1577 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1578 | __my_pthread_testcancel(); |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1579 | return __libc_send(s, msg, len, flags); |
| 1580 | } |
| 1581 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1582 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1583 | extern |
| 1584 | int __libc_recv(int s, void *buf, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1585 | __attribute__((weak)) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1586 | int recv(int s, void *buf, size_t len, int flags) |
| 1587 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1588 | __my_pthread_testcancel(); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1589 | return __libc_recv(s, buf, len, flags); |
| 1590 | } |
| 1591 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1592 | |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1593 | extern |
| 1594 | int __libc_sendmsg(int s, const struct msghdr *msg, int flags); |
| 1595 | __attribute__((weak)) |
| 1596 | int sendmsg(int s, const struct msghdr *msg, int flags) |
| 1597 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1598 | __my_pthread_testcancel(); |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1599 | return __libc_sendmsg(s, msg, flags); |
| 1600 | } |
| 1601 | |
| 1602 | |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1603 | extern |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1604 | int __libc_recvfrom(int s, void *buf, size_t len, int flags, |
| 1605 | struct sockaddr *from, socklen_t *fromlen); |
| 1606 | __attribute__((weak)) |
| 1607 | int recvfrom(int s, void *buf, size_t len, int flags, |
| 1608 | struct sockaddr *from, socklen_t *fromlen) |
| 1609 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1610 | __my_pthread_testcancel(); |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1611 | return __libc_recvfrom(s, buf, len, flags, from, fromlen); |
| 1612 | } |
| 1613 | |
| 1614 | |
| 1615 | extern |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1616 | int __libc_sendto(int s, const void *msg, size_t len, int flags, |
| 1617 | const struct sockaddr *to, socklen_t tolen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1618 | __attribute__((weak)) |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1619 | int sendto(int s, const void *msg, size_t len, int flags, |
| 1620 | const struct sockaddr *to, socklen_t tolen) |
| 1621 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1622 | __my_pthread_testcancel(); |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1623 | return __libc_sendto(s, msg, len, flags, to, tolen); |
| 1624 | } |
| 1625 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1626 | |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 1627 | extern |
| 1628 | int __libc_system(const char* str); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1629 | __attribute__((weak)) |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 1630 | int system(const char* str) |
| 1631 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1632 | __my_pthread_testcancel(); |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 1633 | return __libc_system(str); |
| 1634 | } |
| 1635 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1636 | |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 1637 | extern |
| 1638 | pid_t __libc_wait(int *status); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1639 | __attribute__((weak)) |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 1640 | pid_t wait(int *status) |
| 1641 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1642 | __my_pthread_testcancel(); |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 1643 | return __libc_wait(status); |
| 1644 | } |
| 1645 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1646 | |
sewardj | 67f1d58 | 2002-05-24 02:11:32 +0000 | [diff] [blame] | 1647 | extern |
| 1648 | int __libc_msync(const void *start, size_t length, int flags); |
| 1649 | __attribute__((weak)) |
| 1650 | int msync(const void *start, size_t length, int flags) |
| 1651 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1652 | __my_pthread_testcancel(); |
sewardj | 67f1d58 | 2002-05-24 02:11:32 +0000 | [diff] [blame] | 1653 | return __libc_msync(start, length, flags); |
| 1654 | } |
| 1655 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1656 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1657 | /* --------------------------------------------------------------------- |
| 1658 | Nonblocking implementations of select() and poll(). This stuff will |
| 1659 | surely rot your mind. |
| 1660 | ------------------------------------------------------------------ */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1661 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1662 | /*--------------------------------------------------*/ |
| 1663 | |
| 1664 | #include "vg_kerneliface.h" |
| 1665 | |
| 1666 | static |
| 1667 | __inline__ |
| 1668 | int is_kerror ( int res ) |
| 1669 | { |
| 1670 | if (res >= -4095 && res <= -1) |
| 1671 | return 1; |
| 1672 | else |
| 1673 | return 0; |
| 1674 | } |
| 1675 | |
| 1676 | |
| 1677 | static |
| 1678 | int my_do_syscall1 ( int syscallno, int arg1 ) |
| 1679 | { |
| 1680 | int __res; |
| 1681 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1682 | : "=a" (__res) |
| 1683 | : "0" (syscallno), |
| 1684 | "d" (arg1) ); |
| 1685 | return __res; |
| 1686 | } |
| 1687 | |
| 1688 | static |
| 1689 | int my_do_syscall2 ( int syscallno, |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1690 | int arg1, int arg2 ) |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1691 | { |
| 1692 | int __res; |
| 1693 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 1694 | : "=a" (__res) |
| 1695 | : "0" (syscallno), |
| 1696 | "d" (arg1), |
| 1697 | "c" (arg2) ); |
| 1698 | return __res; |
| 1699 | } |
| 1700 | |
| 1701 | static |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1702 | int my_do_syscall3 ( int syscallno, |
| 1703 | int arg1, int arg2, int arg3 ) |
| 1704 | { |
| 1705 | int __res; |
| 1706 | __asm__ volatile ("pushl %%ebx; movl %%esi,%%ebx ; int $0x80 ; popl %%ebx" |
| 1707 | : "=a" (__res) |
| 1708 | : "0" (syscallno), |
| 1709 | "S" (arg1), |
| 1710 | "c" (arg2), |
| 1711 | "d" (arg3) ); |
| 1712 | return __res; |
| 1713 | } |
| 1714 | |
| 1715 | static |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1716 | int do_syscall_select( int n, |
| 1717 | vki_fd_set* readfds, |
| 1718 | vki_fd_set* writefds, |
| 1719 | vki_fd_set* exceptfds, |
| 1720 | struct vki_timeval * timeout ) |
| 1721 | { |
| 1722 | int res; |
| 1723 | int args[5]; |
| 1724 | args[0] = n; |
| 1725 | args[1] = (int)readfds; |
| 1726 | args[2] = (int)writefds; |
| 1727 | args[3] = (int)exceptfds; |
| 1728 | args[4] = (int)timeout; |
| 1729 | res = my_do_syscall1(__NR_select, (int)(&(args[0])) ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1730 | return res; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | |
| 1734 | /* This is a wrapper round select(), which makes it thread-safe, |
| 1735 | meaning that only this thread will block, rather than the entire |
| 1736 | process. This wrapper in turn depends on nanosleep() not to block |
| 1737 | the entire process, but I think (hope? suspect?) that POSIX |
| 1738 | pthreads guarantees that to be the case. |
| 1739 | |
| 1740 | Basic idea is: modify the timeout parameter to select so that it |
| 1741 | returns immediately. Poll like this until select returns non-zero, |
| 1742 | indicating something interesting happened, or until our time is up. |
| 1743 | Space out the polls with nanosleeps of say 20 milliseconds, which |
| 1744 | is required to be nonblocking; this allows other threads to run. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1745 | |
| 1746 | Assumes: |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1747 | * (checked via my_assert) types fd_set and vki_fd_set are identical. |
| 1748 | * (checked via my_assert) types timeval and vki_timeval are identical. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1749 | * (unchecked) libc error numbers (EINTR etc) are the negation of the |
| 1750 | kernel's error numbers (VKI_EINTR etc). |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1751 | */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1752 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1753 | /* __attribute__((weak)) */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1754 | int select ( int n, |
| 1755 | fd_set *rfds, |
| 1756 | fd_set *wfds, |
| 1757 | fd_set *xfds, |
| 1758 | struct timeval *timeout ) |
| 1759 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1760 | unsigned int ms_now, ms_end; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1761 | int res; |
| 1762 | fd_set rfds_copy; |
| 1763 | fd_set wfds_copy; |
| 1764 | fd_set xfds_copy; |
| 1765 | struct vki_timeval t_now; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1766 | struct vki_timeval zero_timeout; |
| 1767 | struct vki_timespec nanosleep_interval; |
| 1768 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1769 | __my_pthread_testcancel(); |
| 1770 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1771 | /* gcc's complains about ms_end being used uninitialised -- classic |
| 1772 | case it can't understand, where ms_end is both defined and used |
| 1773 | only if timeout != NULL. Hence ... */ |
| 1774 | ms_end = 0; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1775 | |
| 1776 | /* We assume that the kernel and libc data layouts are identical |
| 1777 | for the following types. These asserts provide a crude |
| 1778 | check. */ |
| 1779 | if (sizeof(fd_set) != sizeof(vki_fd_set) |
| 1780 | || sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1781 | barf("valgrind's hacky non-blocking select(): data sizes error"); |
| 1782 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1783 | /* Detect the current time and simultaneously find out if we are |
| 1784 | running on Valgrind. */ |
| 1785 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1786 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1787 | 0, 0, 0, 0); |
| 1788 | |
| 1789 | /* If a zero timeout specified, this call is harmless. Also go |
| 1790 | this route if we're not running on Valgrind, for whatever |
| 1791 | reason. */ |
| 1792 | if ( (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) |
| 1793 | || (ms_now == 0xFFFFFFFF) ) { |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1794 | res = do_syscall_select( n, (vki_fd_set*)rfds, |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1795 | (vki_fd_set*)wfds, |
| 1796 | (vki_fd_set*)xfds, |
| 1797 | (struct vki_timeval*)timeout); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1798 | if (is_kerror(res)) { |
| 1799 | * (__errno_location()) = -res; |
| 1800 | return -1; |
| 1801 | } else { |
| 1802 | return res; |
| 1803 | } |
| 1804 | } |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1805 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1806 | /* If a timeout was specified, set ms_end to be the end millisecond |
| 1807 | counter [wallclock] time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1808 | if (timeout) { |
| 1809 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1810 | my_assert(res == 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1811 | ms_end = ms_now; |
| 1812 | ms_end += (timeout->tv_usec / 1000); |
| 1813 | ms_end += (timeout->tv_sec * 1000); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1814 | /* Stay sane ... */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1815 | my_assert (ms_end >= ms_now); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | /* fprintf(stderr, "MY_SELECT: before loop\n"); */ |
| 1819 | |
| 1820 | /* Either timeout == NULL, meaning wait indefinitely, or timeout != |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1821 | NULL, in which case ms_end holds the end time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1822 | while (1) { |
| 1823 | if (timeout) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1824 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1825 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1826 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1827 | my_assert(ms_now != 0xFFFFFFFF); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1828 | if (ms_now >= ms_end) { |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1829 | /* timeout; nothing interesting happened. */ |
| 1830 | if (rfds) FD_ZERO(rfds); |
| 1831 | if (wfds) FD_ZERO(wfds); |
| 1832 | if (xfds) FD_ZERO(xfds); |
| 1833 | return 0; |
| 1834 | } |
| 1835 | } |
| 1836 | |
| 1837 | /* These could be trashed each time round the loop, so restore |
| 1838 | them each time. */ |
| 1839 | if (rfds) rfds_copy = *rfds; |
| 1840 | if (wfds) wfds_copy = *wfds; |
| 1841 | if (xfds) xfds_copy = *xfds; |
| 1842 | |
| 1843 | zero_timeout.tv_sec = zero_timeout.tv_usec = 0; |
| 1844 | |
| 1845 | res = do_syscall_select( n, |
| 1846 | rfds ? (vki_fd_set*)(&rfds_copy) : NULL, |
| 1847 | wfds ? (vki_fd_set*)(&wfds_copy) : NULL, |
| 1848 | xfds ? (vki_fd_set*)(&xfds_copy) : NULL, |
| 1849 | & zero_timeout ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1850 | if (is_kerror(res)) { |
| 1851 | /* Some kind of error (including EINTR). Set errno and |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1852 | return. The sets are unspecified in this case. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1853 | * (__errno_location()) = -res; |
| 1854 | return -1; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1855 | } |
| 1856 | if (res > 0) { |
| 1857 | /* one or more fds is ready. Copy out resulting sets and |
| 1858 | return. */ |
| 1859 | if (rfds) *rfds = rfds_copy; |
| 1860 | if (wfds) *wfds = wfds_copy; |
| 1861 | if (xfds) *xfds = xfds_copy; |
| 1862 | return res; |
| 1863 | } |
| 1864 | /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ |
| 1865 | /* nanosleep and go round again */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 1866 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1867 | nanosleep_interval.tv_nsec = 50 * 1000 * 1000; /* 50 milliseconds */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1868 | /* It's critical here that valgrind's nanosleep implementation |
| 1869 | is nonblocking. */ |
| 1870 | (void)my_do_syscall2(__NR_nanosleep, |
| 1871 | (int)(&nanosleep_interval), (int)NULL); |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | |
| 1876 | |
| 1877 | |
| 1878 | #include <sys/poll.h> |
| 1879 | |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 1880 | #ifdef GLIBC_2_1 |
| 1881 | typedef unsigned long int nfds_t; |
| 1882 | #endif |
| 1883 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1884 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1885 | /* __attribute__((weak)) */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1886 | int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) |
| 1887 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1888 | unsigned int ms_now, ms_end; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1889 | int res, i; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1890 | struct vki_timespec nanosleep_interval; |
| 1891 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1892 | __my_pthread_testcancel(); |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1893 | ensure_valgrind("poll"); |
| 1894 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1895 | /* Detect the current time and simultaneously find out if we are |
| 1896 | running on Valgrind. */ |
| 1897 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1898 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1899 | 0, 0, 0, 0); |
| 1900 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1901 | if (/* CHECK SIZES FOR struct pollfd */ |
| 1902 | sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 1903 | barf("valgrind's hacky non-blocking poll(): data sizes error"); |
| 1904 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1905 | /* dummy initialisation to keep gcc -Wall happy */ |
| 1906 | ms_end = 0; |
| 1907 | |
| 1908 | /* If a zero timeout specified, this call is harmless. Also do |
| 1909 | this if not running on Valgrind. */ |
| 1910 | if (__timeout == 0 || ms_now == 0xFFFFFFFF) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1911 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, __timeout); |
| 1912 | if (is_kerror(res)) { |
| 1913 | * (__errno_location()) = -res; |
| 1914 | return -1; |
| 1915 | } else { |
| 1916 | return res; |
| 1917 | } |
| 1918 | } |
| 1919 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1920 | /* If a timeout was specified, set ms_end to be the end wallclock |
| 1921 | time. Easy considering that __timeout is in milliseconds. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1922 | if (__timeout > 0) { |
sewardj | 650ac00 | 2002-04-29 12:20:34 +0000 | [diff] [blame] | 1923 | ms_end = ms_now + (unsigned int)__timeout; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1924 | } |
| 1925 | |
| 1926 | /* fprintf(stderr, "MY_POLL: before loop\n"); */ |
| 1927 | |
| 1928 | /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, |
| 1929 | in which case t_end holds the end time. */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1930 | my_assert(__timeout != 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1931 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1932 | while (1) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1933 | if (__timeout > 0) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1934 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1935 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1936 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1937 | my_assert(ms_now != 0xFFFFFFFF); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1938 | if (ms_now >= ms_end) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1939 | /* timeout; nothing interesting happened. */ |
| 1940 | for (i = 0; i < __nfds; i++) |
| 1941 | __fds[i].revents = 0; |
| 1942 | return 0; |
| 1943 | } |
| 1944 | } |
| 1945 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1946 | /* Do a return-immediately poll. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 1947 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); |
| 1948 | if (is_kerror(res)) { |
| 1949 | /* Some kind of error. Set errno and return. */ |
| 1950 | * (__errno_location()) = -res; |
| 1951 | return -1; |
| 1952 | } |
| 1953 | if (res > 0) { |
| 1954 | /* One or more fds is ready. Return now. */ |
| 1955 | return res; |
| 1956 | } |
| 1957 | /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ |
| 1958 | /* nanosleep and go round again */ |
| 1959 | nanosleep_interval.tv_sec = 0; |
sewardj | 956cc1b | 2002-04-25 01:33:50 +0000 | [diff] [blame] | 1960 | nanosleep_interval.tv_nsec = 51 * 1000 * 1000; /* 51 milliseconds */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 1961 | /* It's critical here that valgrind's nanosleep implementation |
| 1962 | is nonblocking. */ |
| 1963 | (void)my_do_syscall2(__NR_nanosleep, |
| 1964 | (int)(&nanosleep_interval), (int)NULL); |
| 1965 | } |
| 1966 | } |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1967 | |
| 1968 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1969 | /* Helper function used to make accept() non-blocking. Idea is to use |
| 1970 | the above nonblocking poll() to make this thread ONLY wait for the |
| 1971 | specified fd to become ready, and then return. */ |
| 1972 | static void wait_for_fd_to_be_readable_or_erring ( int fd ) |
| 1973 | { |
| 1974 | struct pollfd pfd; |
sewardj | 6e6cbaa | 2002-05-24 02:12:52 +0000 | [diff] [blame] | 1975 | /* fprintf(stderr, "wait_for_fd_to_be_readable_or_erring %d\n", fd); */ |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1976 | pfd.fd = fd; |
| 1977 | pfd.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL; |
| 1978 | /* ... but not POLLOUT, you may notice. */ |
| 1979 | pfd.revents = 0; |
| 1980 | (void)poll(&pfd, 1, -1 /* forever */); |
| 1981 | } |
| 1982 | |
| 1983 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1984 | /* --------------------------------------------------------------------- |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 1985 | Hacky implementation of semaphores. |
| 1986 | ------------------------------------------------------------------ */ |
| 1987 | |
| 1988 | #include <semaphore.h> |
| 1989 | |
| 1990 | /* This is a terrible way to do the remapping. Plan is to import an |
| 1991 | AVL tree at some point. */ |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 1992 | |
| 1993 | typedef |
| 1994 | struct { |
| 1995 | pthread_mutex_t se_mx; |
| 1996 | pthread_cond_t se_cv; |
| 1997 | int count; |
| 1998 | } |
| 1999 | vg_sem_t; |
| 2000 | |
| 2001 | static pthread_mutex_t se_remap_mx = PTHREAD_MUTEX_INITIALIZER; |
| 2002 | |
| 2003 | static int se_remap_used = 0; |
| 2004 | static sem_t* se_remap_orig[VG_N_SEMAPHORES]; |
| 2005 | static vg_sem_t se_remap_new[VG_N_SEMAPHORES]; |
| 2006 | |
| 2007 | static vg_sem_t* se_remap ( sem_t* orig ) |
| 2008 | { |
| 2009 | int res, i; |
| 2010 | res = __pthread_mutex_lock(&se_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2011 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2012 | |
| 2013 | for (i = 0; i < se_remap_used; i++) { |
| 2014 | if (se_remap_orig[i] == orig) |
| 2015 | break; |
| 2016 | } |
| 2017 | if (i == se_remap_used) { |
| 2018 | if (se_remap_used == VG_N_SEMAPHORES) { |
| 2019 | res = pthread_mutex_unlock(&se_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2020 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2021 | barf("VG_N_SEMAPHORES is too low. Increase and recompile."); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2022 | } |
| 2023 | se_remap_used++; |
| 2024 | se_remap_orig[i] = orig; |
| 2025 | /* printf("allocated semaphore %d\n", i); */ |
| 2026 | } |
| 2027 | res = __pthread_mutex_unlock(&se_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2028 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2029 | return &se_remap_new[i]; |
| 2030 | } |
| 2031 | |
| 2032 | |
| 2033 | int sem_init(sem_t *sem, int pshared, unsigned int value) |
| 2034 | { |
| 2035 | int res; |
| 2036 | vg_sem_t* vg_sem; |
| 2037 | ensure_valgrind("sem_init"); |
| 2038 | if (pshared != 0) { |
| 2039 | errno = ENOSYS; |
| 2040 | return -1; |
| 2041 | } |
| 2042 | vg_sem = se_remap(sem); |
| 2043 | res = pthread_mutex_init(&vg_sem->se_mx, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2044 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2045 | res = pthread_cond_init(&vg_sem->se_cv, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2046 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2047 | vg_sem->count = value; |
| 2048 | return 0; |
| 2049 | } |
| 2050 | |
| 2051 | |
| 2052 | int sem_wait ( sem_t* sem ) |
| 2053 | { |
| 2054 | int res; |
| 2055 | vg_sem_t* vg_sem; |
| 2056 | ensure_valgrind("sem_wait"); |
| 2057 | vg_sem = se_remap(sem); |
| 2058 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2059 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2060 | while (vg_sem->count == 0) { |
| 2061 | res = pthread_cond_wait(&vg_sem->se_cv, &vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2062 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2063 | } |
| 2064 | vg_sem->count--; |
| 2065 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2066 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2067 | return 0; |
| 2068 | } |
| 2069 | |
| 2070 | int sem_post ( sem_t* sem ) |
| 2071 | { |
| 2072 | int res; |
| 2073 | vg_sem_t* vg_sem; |
| 2074 | ensure_valgrind("sem_post"); |
| 2075 | vg_sem = se_remap(sem); |
| 2076 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2077 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2078 | if (vg_sem->count == 0) { |
| 2079 | vg_sem->count++; |
| 2080 | res = pthread_cond_broadcast(&vg_sem->se_cv); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2081 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2082 | } else { |
| 2083 | vg_sem->count++; |
| 2084 | } |
| 2085 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2086 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2087 | return 0; |
| 2088 | } |
| 2089 | |
| 2090 | |
| 2091 | int sem_trywait ( sem_t* sem ) |
| 2092 | { |
| 2093 | int ret, res; |
| 2094 | vg_sem_t* vg_sem; |
| 2095 | ensure_valgrind("sem_trywait"); |
| 2096 | vg_sem = se_remap(sem); |
| 2097 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2098 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2099 | if (vg_sem->count > 0) { |
| 2100 | vg_sem->count--; |
| 2101 | ret = 0; |
| 2102 | } else { |
| 2103 | ret = -1; |
| 2104 | errno = EAGAIN; |
| 2105 | } |
| 2106 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2107 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2108 | return ret; |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | int sem_getvalue(sem_t* sem, int * sval) |
| 2113 | { |
| 2114 | vg_sem_t* vg_sem; |
| 2115 | ensure_valgrind("sem_trywait"); |
| 2116 | vg_sem = se_remap(sem); |
| 2117 | *sval = vg_sem->count; |
| 2118 | return 0; |
| 2119 | } |
| 2120 | |
| 2121 | |
| 2122 | int sem_destroy(sem_t * sem) |
| 2123 | { |
| 2124 | kludged("sem_destroy"); |
| 2125 | /* if someone waiting on this semaphore, errno = EBUSY, return -1 */ |
| 2126 | return 0; |
| 2127 | } |
| 2128 | |
| 2129 | |
| 2130 | /* --------------------------------------------------------------------- |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2131 | Reader-writer locks. |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2132 | ------------------------------------------------------------------ */ |
| 2133 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2134 | typedef |
| 2135 | struct { |
| 2136 | int initted; /* != 0 --> in use; sanity check only */ |
| 2137 | int prefer_w; /* != 0 --> prefer writer */ |
| 2138 | int nwait_r; /* # of waiting readers */ |
| 2139 | int nwait_w; /* # of waiting writers */ |
| 2140 | pthread_cond_t cv_r; /* for signalling readers */ |
| 2141 | pthread_cond_t cv_w; /* for signalling writers */ |
| 2142 | pthread_mutex_t mx; |
| 2143 | int status; |
| 2144 | /* allowed range for status: >= -1. -1 means 1 writer currently |
| 2145 | active, >= 0 means N readers currently active. */ |
| 2146 | } |
| 2147 | vg_rwlock_t; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2148 | |
| 2149 | |
| 2150 | static pthread_mutex_t rw_remap_mx = PTHREAD_MUTEX_INITIALIZER; |
| 2151 | |
| 2152 | static int rw_remap_used = 0; |
| 2153 | static pthread_rwlock_t* rw_remap_orig[VG_N_RWLOCKS]; |
| 2154 | static vg_rwlock_t rw_remap_new[VG_N_RWLOCKS]; |
| 2155 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2156 | |
| 2157 | static |
| 2158 | void init_vg_rwlock ( vg_rwlock_t* vg_rwl ) |
| 2159 | { |
| 2160 | int res = 0; |
| 2161 | vg_rwl->initted = 1; |
| 2162 | vg_rwl->prefer_w = 1; |
| 2163 | vg_rwl->nwait_r = 0; |
| 2164 | vg_rwl->nwait_w = 0; |
| 2165 | vg_rwl->status = 0; |
| 2166 | res = pthread_mutex_init(&vg_rwl->mx, NULL); |
| 2167 | res |= pthread_cond_init(&vg_rwl->cv_r, NULL); |
| 2168 | res |= pthread_cond_init(&vg_rwl->cv_w, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2169 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2170 | } |
| 2171 | |
| 2172 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2173 | /* Take the address of a LinuxThreads rwlock_t and return the shadow |
| 2174 | address of our version. Further, if the LinuxThreads version |
| 2175 | appears to have been statically initialised, do the same to the one |
| 2176 | we allocate here. The pthread_rwlock_t.__rw_readers field is set |
| 2177 | to zero by PTHREAD_RWLOCK_INITIALIZER, so we take zero as meaning |
| 2178 | uninitialised and non-zero meaning initialised. |
| 2179 | */ |
| 2180 | static vg_rwlock_t* rw_remap ( pthread_rwlock_t* orig ) |
| 2181 | { |
| 2182 | int res, i; |
| 2183 | vg_rwlock_t* vg_rwl; |
| 2184 | res = __pthread_mutex_lock(&rw_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2185 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2186 | |
| 2187 | for (i = 0; i < rw_remap_used; i++) { |
| 2188 | if (rw_remap_orig[i] == orig) |
| 2189 | break; |
| 2190 | } |
| 2191 | if (i == rw_remap_used) { |
| 2192 | if (rw_remap_used == VG_N_RWLOCKS) { |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2193 | res = __pthread_mutex_unlock(&rw_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2194 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2195 | barf("VG_N_RWLOCKS is too low. Increase and recompile."); |
| 2196 | } |
| 2197 | rw_remap_used++; |
| 2198 | rw_remap_orig[i] = orig; |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2199 | rw_remap_new[i].initted = 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2200 | if (0) printf("allocated rwlock %d\n", i); |
| 2201 | } |
| 2202 | res = __pthread_mutex_unlock(&rw_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2203 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2204 | vg_rwl = &rw_remap_new[i]; |
| 2205 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2206 | /* Initialise the shadow, if required. */ |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2207 | if (orig->__rw_readers == 0) { |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2208 | orig->__rw_readers = 1; |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2209 | init_vg_rwlock(vg_rwl); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2210 | if (orig->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP) |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2211 | vg_rwl->prefer_w = 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2212 | } |
| 2213 | |
| 2214 | return vg_rwl; |
| 2215 | } |
| 2216 | |
| 2217 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2218 | int pthread_rwlock_init ( pthread_rwlock_t* orig, |
| 2219 | const pthread_rwlockattr_t* attr ) |
| 2220 | { |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2221 | vg_rwlock_t* rwl; |
| 2222 | if (0) printf ("pthread_rwlock_init\n"); |
| 2223 | /* Force the remapper to initialise the shadow. */ |
| 2224 | orig->__rw_readers = 0; |
| 2225 | /* Install the lock preference; the remapper needs to know it. */ |
| 2226 | orig->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP; |
| 2227 | if (attr) |
| 2228 | orig->__rw_kind = attr->__lockkind; |
| 2229 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2230 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2231 | } |
| 2232 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2233 | |
| 2234 | static |
| 2235 | void pthread_rwlock_rdlock_CANCEL_HDLR ( void* rwl_v ) |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2236 | { |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2237 | vg_rwlock_t* rwl = (vg_rwlock_t*)rwl_v; |
| 2238 | rwl->nwait_r--; |
| 2239 | pthread_mutex_unlock (&rwl->mx); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2240 | } |
| 2241 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2242 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2243 | int pthread_rwlock_rdlock ( pthread_rwlock_t* orig ) |
| 2244 | { |
| 2245 | int res; |
| 2246 | vg_rwlock_t* rwl; |
| 2247 | if (0) printf ("pthread_rwlock_rdlock\n"); |
| 2248 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2249 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2250 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2251 | if (!rwl->initted) { |
| 2252 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2253 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2254 | return EINVAL; |
| 2255 | } |
| 2256 | if (rwl->status < 0) { |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2257 | my_assert(rwl->status == -1); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2258 | rwl->nwait_r++; |
| 2259 | pthread_cleanup_push( pthread_rwlock_rdlock_CANCEL_HDLR, rwl ); |
| 2260 | while (1) { |
| 2261 | if (rwl->status == 0) break; |
| 2262 | res = pthread_cond_wait(&rwl->cv_r, &rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2263 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2264 | } |
| 2265 | pthread_cleanup_pop(0); |
| 2266 | rwl->nwait_r--; |
| 2267 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2268 | my_assert(rwl->status >= 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2269 | rwl->status++; |
| 2270 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2271 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2272 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2273 | } |
| 2274 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2275 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2276 | int pthread_rwlock_tryrdlock ( pthread_rwlock_t* orig ) |
| 2277 | { |
| 2278 | int res; |
| 2279 | vg_rwlock_t* rwl; |
| 2280 | if (0) printf ("pthread_rwlock_tryrdlock\n"); |
| 2281 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2282 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2283 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2284 | if (!rwl->initted) { |
| 2285 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2286 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2287 | return EINVAL; |
| 2288 | } |
| 2289 | if (rwl->status == -1) { |
| 2290 | /* Writer active; we have to give up. */ |
| 2291 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2292 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2293 | return EBUSY; |
| 2294 | } |
| 2295 | /* Success */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2296 | my_assert(rwl->status >= 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2297 | rwl->status++; |
| 2298 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2299 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2300 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2303 | |
| 2304 | static |
| 2305 | void pthread_rwlock_wrlock_CANCEL_HDLR ( void* rwl_v ) |
| 2306 | { |
| 2307 | vg_rwlock_t* rwl = (vg_rwlock_t*)rwl_v; |
| 2308 | rwl->nwait_w--; |
| 2309 | pthread_mutex_unlock (&rwl->mx); |
| 2310 | } |
| 2311 | |
| 2312 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2313 | int pthread_rwlock_wrlock ( pthread_rwlock_t* orig ) |
| 2314 | { |
| 2315 | int res; |
| 2316 | vg_rwlock_t* rwl; |
| 2317 | if (0) printf ("pthread_rwlock_wrlock\n"); |
| 2318 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2319 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2320 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2321 | if (!rwl->initted) { |
| 2322 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2323 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2324 | return EINVAL; |
| 2325 | } |
| 2326 | if (rwl->status != 0) { |
| 2327 | rwl->nwait_w++; |
| 2328 | pthread_cleanup_push( pthread_rwlock_wrlock_CANCEL_HDLR, rwl ); |
| 2329 | while (1) { |
| 2330 | if (rwl->status == 0) break; |
| 2331 | res = pthread_cond_wait(&rwl->cv_w, &rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2332 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2333 | } |
| 2334 | pthread_cleanup_pop(0); |
| 2335 | rwl->nwait_w--; |
| 2336 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2337 | my_assert(rwl->status == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2338 | rwl->status = -1; |
| 2339 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2340 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2341 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2342 | } |
| 2343 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2344 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2345 | int pthread_rwlock_trywrlock ( pthread_rwlock_t* orig ) |
| 2346 | { |
| 2347 | int res; |
| 2348 | vg_rwlock_t* rwl; |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2349 | if (0) printf ("pthread_wrlock_trywrlock\n"); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2350 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2351 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2352 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2353 | if (!rwl->initted) { |
| 2354 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2355 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2356 | return EINVAL; |
| 2357 | } |
| 2358 | if (rwl->status != 0) { |
| 2359 | /* Reader(s) or a writer active; we have to give up. */ |
| 2360 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2361 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2362 | return EBUSY; |
| 2363 | } |
| 2364 | /* Success */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2365 | my_assert(rwl->status == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2366 | rwl->status = -1; |
| 2367 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2368 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2369 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2372 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2373 | int pthread_rwlock_unlock ( pthread_rwlock_t* orig ) |
| 2374 | { |
| 2375 | int res; |
| 2376 | vg_rwlock_t* rwl; |
| 2377 | if (0) printf ("pthread_rwlock_unlock\n"); |
| 2378 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2379 | rwl = rw_remap ( orig ); |
| 2380 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2381 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2382 | if (!rwl->initted) { |
| 2383 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2384 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2385 | return EINVAL; |
| 2386 | } |
| 2387 | if (rwl->status == 0) { |
| 2388 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2389 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2390 | return EPERM; |
| 2391 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2392 | my_assert(rwl->status != 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2393 | if (rwl->status == -1) { |
| 2394 | rwl->status = 0; |
| 2395 | } else { |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2396 | my_assert(rwl->status > 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2397 | rwl->status--; |
| 2398 | } |
| 2399 | |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2400 | my_assert(rwl->status >= 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2401 | |
| 2402 | if (rwl->prefer_w) { |
| 2403 | |
| 2404 | /* Favour waiting writers, if any. */ |
| 2405 | if (rwl->nwait_w > 0) { |
| 2406 | /* Writer(s) are waiting. */ |
| 2407 | if (rwl->status == 0) { |
| 2408 | /* We can let a writer in. */ |
| 2409 | res = pthread_cond_signal(&rwl->cv_w); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2410 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2411 | } else { |
| 2412 | /* There are still readers active. Do nothing; eventually |
| 2413 | they will disappear, at which point a writer will be |
| 2414 | admitted. */ |
| 2415 | } |
| 2416 | } |
| 2417 | else |
| 2418 | /* No waiting writers. */ |
| 2419 | if (rwl->nwait_r > 0) { |
| 2420 | /* Let in a waiting reader. */ |
| 2421 | res = pthread_cond_signal(&rwl->cv_r); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2422 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | } else { |
| 2426 | |
| 2427 | /* Favour waiting readers, if any. */ |
| 2428 | if (rwl->nwait_r > 0) { |
| 2429 | /* Reader(s) are waiting; let one in. */ |
| 2430 | res = pthread_cond_signal(&rwl->cv_r); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2431 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2432 | } |
| 2433 | else |
| 2434 | /* No waiting readers. */ |
| 2435 | if (rwl->nwait_w > 0 && rwl->status == 0) { |
| 2436 | /* We have waiting writers and no active readers; let a |
| 2437 | writer in. */ |
| 2438 | res = pthread_cond_signal(&rwl->cv_w); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2439 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2440 | } |
| 2441 | } |
| 2442 | |
| 2443 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2444 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2445 | return 0; |
| 2446 | } |
| 2447 | |
| 2448 | |
| 2449 | int pthread_rwlock_destroy ( pthread_rwlock_t *orig ) |
| 2450 | { |
| 2451 | int res; |
| 2452 | vg_rwlock_t* rwl; |
| 2453 | if (0) printf ("pthread_rwlock_destroy\n"); |
| 2454 | rwl = rw_remap ( orig ); |
| 2455 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2456 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2457 | if (!rwl->initted) { |
| 2458 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2459 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2460 | return EINVAL; |
| 2461 | } |
| 2462 | if (rwl->status != 0 || rwl->nwait_r > 0 || rwl->nwait_w > 0) { |
| 2463 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2464 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2465 | return EBUSY; |
| 2466 | } |
| 2467 | rwl->initted = 0; |
| 2468 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2469 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2470 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
| 2473 | |
| 2474 | /* --------------------------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2475 | B'stard. |
| 2476 | ------------------------------------------------------------------ */ |
| 2477 | |
| 2478 | # define strong_alias(name, aliasname) \ |
| 2479 | extern __typeof (name) aliasname __attribute__ ((alias (#name))); |
| 2480 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2481 | # define weak_alias(name, aliasname) \ |
| 2482 | extern __typeof (name) aliasname __attribute__ ((weak, alias (#name))); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2483 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2484 | strong_alias(__pthread_mutex_lock, pthread_mutex_lock) |
| 2485 | strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock) |
| 2486 | strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock) |
| 2487 | strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init) |
| 2488 | weak_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype) |
| 2489 | strong_alias(__pthread_mutex_init, pthread_mutex_init) |
| 2490 | strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy) |
| 2491 | strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy) |
| 2492 | strong_alias(__pthread_once, pthread_once) |
| 2493 | strong_alias(__pthread_atfork, pthread_atfork) |
| 2494 | strong_alias(__pthread_key_create, pthread_key_create) |
| 2495 | strong_alias(__pthread_getspecific, pthread_getspecific) |
| 2496 | strong_alias(__pthread_setspecific, pthread_setspecific) |
| 2497 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 2498 | #ifndef GLIBC_2_1 |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2499 | strong_alias(sigaction, __sigaction) |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 2500 | #endif |
| 2501 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2502 | strong_alias(close, __close) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2503 | strong_alias(fcntl, __fcntl) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2504 | strong_alias(lseek, __lseek) |
| 2505 | strong_alias(open, __open) |
| 2506 | strong_alias(open64, __open64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2507 | strong_alias(read, __read) |
| 2508 | strong_alias(wait, __wait) |
| 2509 | strong_alias(write, __write) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2510 | strong_alias(connect, __connect) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2511 | strong_alias(send, __send) |
| 2512 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 2513 | weak_alias (__pread64, pread64) |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 2514 | weak_alias (__pwrite64, pwrite64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2515 | weak_alias(__fork, fork) |
sewardj | 7f6456d | 2002-05-21 00:51:21 +0000 | [diff] [blame] | 2516 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2517 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2518 | |
| 2519 | /*--------------------------------------------------*/ |
| 2520 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2521 | weak_alias(pthread_rwlock_rdlock, __pthread_rwlock_rdlock) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2522 | weak_alias(pthread_rwlock_unlock, __pthread_rwlock_unlock) |
sewardj | 262b029 | 2002-05-01 00:03:16 +0000 | [diff] [blame] | 2523 | weak_alias(pthread_rwlock_wrlock, __pthread_rwlock_wrlock) |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 2524 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2525 | weak_alias(pthread_rwlock_destroy, __pthread_rwlock_destroy) |
| 2526 | weak_alias(pthread_rwlock_init, __pthread_rwlock_init) |
| 2527 | weak_alias(pthread_rwlock_tryrdlock, __pthread_rwlock_tryrdlock) |
| 2528 | weak_alias(pthread_rwlock_trywrlock, __pthread_rwlock_trywrlock) |
| 2529 | |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 2530 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2531 | /* I've no idea what these are, but they get called quite a lot. |
| 2532 | Anybody know? */ |
| 2533 | |
| 2534 | #undef _IO_flockfile |
| 2535 | void _IO_flockfile ( _IO_FILE * file ) |
| 2536 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 2537 | pthread_mutex_lock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2538 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2539 | weak_alias(_IO_flockfile, flockfile); |
| 2540 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2541 | |
| 2542 | #undef _IO_funlockfile |
| 2543 | void _IO_funlockfile ( _IO_FILE * file ) |
| 2544 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 2545 | pthread_mutex_unlock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2546 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2547 | weak_alias(_IO_funlockfile, funlockfile); |
| 2548 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2549 | |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 2550 | /* This doesn't seem to be needed to simulate libpthread.so's external |
| 2551 | interface, but many people complain about its absence. */ |
| 2552 | |
| 2553 | strong_alias(__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np) |
| 2554 | weak_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np) |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 2555 | |
| 2556 | |
| 2557 | /*--------------------------------------------------------------------*/ |
| 2558 | /*--- end vg_libpthread.c ---*/ |
| 2559 | /*--------------------------------------------------------------------*/ |