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