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