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 | /* |
njn | c953984 | 2002-10-02 13:26:35 +0000 | [diff] [blame] | 8 | This file is part of Valgrind, an extensible x86 protected-mode |
| 9 | emulator for monitoring program execution on x86-Unixes. |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 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 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 29 | The GNU General Public License is contained in the file COPYING. |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 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 | |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 78 | static |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 79 | int my_do_syscall1 ( int syscallno, int arg1 ); |
| 80 | |
| 81 | static |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 82 | int my_do_syscall2 ( int syscallno, |
| 83 | int arg1, int arg2 ); |
| 84 | |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 85 | static |
| 86 | int my_do_syscall3 ( int syscallno, |
| 87 | int arg1, int arg2, int arg3 ); |
| 88 | |
| 89 | |
| 90 | #ifdef GLIBC_2_3 |
| 91 | /* kludge by JRS (not from glibc) ... */ |
| 92 | typedef void* __locale_t; |
| 93 | |
| 94 | /* Copied from locale/locale.h in glibc-2.2.93 sources */ |
| 95 | /* This value can be passed to `uselocale' and may be returned by |
| 96 | it. Passing this value to any other function has undefined |
| 97 | behavior. */ |
| 98 | # define LC_GLOBAL_LOCALE ((__locale_t) -1L) |
| 99 | extern __locale_t __uselocale ( __locale_t ); |
| 100 | #endif |
| 101 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 102 | static |
| 103 | void init_libc_tsd_keys ( void ); |
| 104 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 105 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 106 | /* --------------------------------------------------------------------- |
| 107 | Helpers. We have to be pretty self-sufficient. |
| 108 | ------------------------------------------------------------------ */ |
| 109 | |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 110 | /* Number of times any given error message is printed. */ |
| 111 | #define N_MOANS 3 |
| 112 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 113 | /* Extract from Valgrind the value of VG_(clo_trace_pthread_level). |
| 114 | Returns 0 (none) if not running on Valgrind. */ |
| 115 | static |
| 116 | int get_pt_trace_level ( void ) |
| 117 | { |
| 118 | int res; |
| 119 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 120 | VG_USERREQ__GET_PTHREAD_TRACE_LEVEL, |
| 121 | 0, 0, 0, 0); |
| 122 | return res; |
| 123 | } |
| 124 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 125 | static |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 126 | void my_exit ( int arg ) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 127 | { |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 128 | my_do_syscall1(__NR_exit, arg); |
| 129 | /*NOTREACHED*/ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 130 | } |
| 131 | |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 132 | static |
| 133 | void my_write ( int fd, const void *buf, int count ) |
| 134 | { |
| 135 | my_do_syscall3(__NR_write, fd, (int)buf, count ); |
| 136 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 137 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 138 | /* We need this guy -- it's in valgrind.so. */ |
| 139 | extern void VG_(startup) ( void ); |
| 140 | |
| 141 | |
| 142 | /* Just start up Valgrind if it's not already going. VG_(startup)() |
| 143 | detects and ignores second and subsequent calls. */ |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 144 | static __inline__ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 145 | void ensure_valgrind ( char* caller ) |
| 146 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 147 | VG_(startup)(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 148 | } |
| 149 | |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 150 | /* While we're at it ... hook our own startup function into this |
| 151 | game. */ |
| 152 | __asm__ ( |
| 153 | ".section .init\n" |
| 154 | "\tcall vgPlain_startup" |
| 155 | ); |
| 156 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 157 | |
| 158 | static |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 159 | __attribute__((noreturn)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 160 | void barf ( char* str ) |
| 161 | { |
| 162 | char buf[100]; |
| 163 | buf[0] = 0; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 164 | strcat(buf, "\nvalgrind's libpthread.so: "); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 165 | strcat(buf, str); |
| 166 | strcat(buf, "\n\n"); |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 167 | my_write(2, buf, strlen(buf)); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 168 | my_exit(1); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 169 | /* We have to persuade gcc into believing this doesn't return. */ |
| 170 | while (1) { }; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 174 | static void ignored ( char* msg ) |
| 175 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 176 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 177 | char* ig = "valgrind's libpthread.so: IGNORED call to: "; |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 178 | my_write(2, ig, strlen(ig)); |
| 179 | my_write(2, msg, strlen(msg)); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 180 | ig = "\n"; |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 181 | my_write(2, ig, strlen(ig)); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 182 | } |
sewardj | 2a3d28c | 2002-04-14 13:27:00 +0000 | [diff] [blame] | 183 | } |
| 184 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 185 | static void kludged ( char* msg ) |
| 186 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 187 | if (get_pt_trace_level() >= 0) { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 188 | char* ig = "valgrind's libpthread.so: KLUDGED call to: "; |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 189 | my_write(2, ig, strlen(ig)); |
| 190 | my_write(2, msg, strlen(msg)); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 191 | ig = "\n"; |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 192 | my_write(2, ig, strlen(ig)); |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 193 | } |
| 194 | } |
| 195 | |
| 196 | static void not_inside ( char* msg ) |
| 197 | { |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 198 | VG_(startup)(); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 199 | } |
| 200 | |
sewardj | ccef2e6 | 2002-05-29 19:26:32 +0000 | [diff] [blame] | 201 | __attribute__((noreturn)) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 202 | void vgPlain_unimp ( char* what ) |
| 203 | { |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 204 | char* ig = "valgrind's libpthread.so: UNIMPLEMENTED FUNCTION: "; |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 205 | my_write(2, ig, strlen(ig)); |
| 206 | my_write(2, what, strlen(what)); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 207 | ig = "\n"; |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 208 | my_write(2, ig, strlen(ig)); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 209 | barf("Please report this bug to me at: jseward@acm.org"); |
| 210 | } |
| 211 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 212 | |
sewardj | 457cc47 | 2002-06-03 23:13:47 +0000 | [diff] [blame] | 213 | static |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 214 | void my_assert_fail ( Char* expr, Char* file, Int line, Char* fn ) |
| 215 | { |
| 216 | static Bool entered = False; |
| 217 | if (entered) |
| 218 | my_exit(2); |
| 219 | entered = True; |
| 220 | fprintf(stderr, "\n%s: %s:%d (%s): Assertion `%s' failed.\n", |
| 221 | "valgrind", file, line, fn, expr ); |
| 222 | fprintf(stderr, "Please report this bug to me at: %s\n\n", |
| 223 | VG_EMAIL_ADDR); |
| 224 | my_exit(1); |
| 225 | } |
| 226 | |
| 227 | #define MY__STRING(__str) #__str |
| 228 | |
| 229 | #define my_assert(expr) \ |
| 230 | ((void) ((expr) ? 0 : \ |
| 231 | (my_assert_fail (MY__STRING(expr), \ |
| 232 | __FILE__, __LINE__, \ |
| 233 | __PRETTY_FUNCTION__), 0))) |
| 234 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 235 | static |
| 236 | void my_free ( void* ptr ) |
| 237 | { |
| 238 | int res; |
| 239 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 240 | VG_USERREQ__FREE, ptr, 0, 0, 0); |
| 241 | my_assert(res == 0); |
| 242 | } |
| 243 | |
| 244 | |
| 245 | static |
| 246 | void* my_malloc ( int nbytes ) |
| 247 | { |
| 248 | void* res; |
| 249 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 250 | VG_USERREQ__MALLOC, nbytes, 0, 0, 0); |
| 251 | my_assert(res != (void*)0); |
| 252 | return res; |
| 253 | } |
| 254 | |
| 255 | |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 256 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 257 | /* --------------------------------------------------------------------- |
| 258 | Pass pthread_ calls to Valgrind's request mechanism. |
| 259 | ------------------------------------------------------------------ */ |
| 260 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 261 | #include <errno.h> |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 262 | #include <sys/time.h> /* gettimeofday */ |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 263 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 264 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 265 | /* --------------------------------------------------- |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 266 | Ummm .. |
| 267 | ------------------------------------------------ */ |
| 268 | |
| 269 | static |
| 270 | void pthread_error ( const char* msg ) |
| 271 | { |
| 272 | int res; |
| 273 | VALGRIND_MAGIC_SEQUENCE(res, 0, |
| 274 | VG_USERREQ__PTHREAD_ERROR, |
| 275 | msg, 0, 0, 0); |
| 276 | } |
| 277 | |
| 278 | |
| 279 | /* --------------------------------------------------- |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 280 | Here so it can be inlined without complaint. |
| 281 | ------------------------------------------------ */ |
| 282 | |
| 283 | __inline__ |
| 284 | pthread_t pthread_self(void) |
| 285 | { |
| 286 | int tid; |
| 287 | ensure_valgrind("pthread_self"); |
| 288 | VALGRIND_MAGIC_SEQUENCE(tid, 0 /* default */, |
| 289 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 290 | 0, 0, 0, 0); |
| 291 | if (tid < 1 || tid >= VG_N_THREADS) |
| 292 | barf("pthread_self: invalid ThreadId"); |
| 293 | return tid; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 298 | THREAD ATTRIBUTES |
| 299 | ------------------------------------------------ */ |
| 300 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 301 | int pthread_attr_init(pthread_attr_t *attr) |
| 302 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 303 | /* Just initialise the fields which we might look at. */ |
| 304 | attr->__detachstate = PTHREAD_CREATE_JOINABLE; |
sewardj | 111b14c | 2002-10-20 16:22:57 +0000 | [diff] [blame] | 305 | /* Linuxthreads sets this field to the value __getpagesize(), so I |
| 306 | guess the following is OK. */ |
| 307 | attr->__guardsize = VKI_BYTES_PER_PAGE; return 0; |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | int pthread_attr_setdetachstate(pthread_attr_t *attr, int detachstate) |
| 311 | { |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 312 | if (detachstate != PTHREAD_CREATE_JOINABLE |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 313 | && detachstate != PTHREAD_CREATE_DETACHED) { |
| 314 | pthread_error("pthread_attr_setdetachstate: " |
| 315 | "detachstate is invalid"); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 316 | return EINVAL; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 317 | } |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 318 | attr->__detachstate = detachstate; |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 319 | return 0; |
| 320 | } |
| 321 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 322 | int pthread_attr_getdetachstate(const pthread_attr_t *attr, int *detachstate) |
| 323 | { |
| 324 | *detachstate = attr->__detachstate; |
| 325 | return 0; |
| 326 | } |
| 327 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 328 | int pthread_attr_setinheritsched(pthread_attr_t *attr, int inherit) |
| 329 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 330 | static int moans = N_MOANS; |
| 331 | if (moans-- > 0) |
| 332 | ignored("pthread_attr_setinheritsched"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 333 | return 0; |
| 334 | } |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 335 | |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 336 | __attribute__((weak)) |
| 337 | int pthread_attr_setstacksize (pthread_attr_t *__attr, |
| 338 | size_t __stacksize) |
| 339 | { |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 340 | size_t limit; |
sewardj | 3d7c8d6 | 2002-06-04 20:59:16 +0000 | [diff] [blame] | 341 | char buf[1024]; |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 342 | ensure_valgrind("pthread_attr_setstacksize"); |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 343 | limit = VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 344 | - 1000; /* paranoia */ |
| 345 | if (__stacksize < limit) |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 346 | return 0; |
sewardj | 3d7c8d6 | 2002-06-04 20:59:16 +0000 | [diff] [blame] | 347 | snprintf(buf, sizeof(buf), "pthread_attr_setstacksize: " |
| 348 | "requested size %d >= VG_PTHREAD_STACK_SIZE\n " |
| 349 | "edit vg_include.h and rebuild.", __stacksize); |
| 350 | buf[sizeof(buf)-1] = '\0'; /* Make sure it is zero terminated */ |
| 351 | barf(buf); |
sewardj | 0286dd5 | 2002-05-16 20:51:15 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 355 | /* This is completely bogus. */ |
| 356 | int pthread_attr_getschedparam(const pthread_attr_t *attr, |
| 357 | struct sched_param *param) |
| 358 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 359 | static int moans = N_MOANS; |
| 360 | if (moans-- > 0) |
| 361 | kludged("pthread_attr_getschedparam"); |
sewardj | 3e909ce | 2002-06-03 13:27:15 +0000 | [diff] [blame] | 362 | # ifdef HAVE_SCHED_PRIORITY |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 363 | if (param) param->sched_priority = 0; /* who knows */ |
| 364 | # else |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 365 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 366 | # endif |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | int pthread_attr_setschedparam(pthread_attr_t *attr, |
| 371 | const struct sched_param *param) |
| 372 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 373 | static int moans = N_MOANS; |
| 374 | if (moans-- > 0) |
| 375 | ignored("pthread_attr_setschedparam"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | int pthread_attr_destroy(pthread_attr_t *attr) |
| 380 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 381 | static int moans = N_MOANS; |
| 382 | if (moans-- > 0) |
| 383 | ignored("pthread_attr_destroy"); |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 384 | return 0; |
| 385 | } |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 386 | |
sewardj | 0d84423 | 2002-06-02 09:29:31 +0000 | [diff] [blame] | 387 | /* These are no-ops, as with LinuxThreads. */ |
| 388 | int pthread_attr_setscope ( pthread_attr_t *attr, int scope ) |
| 389 | { |
| 390 | ensure_valgrind("pthread_attr_setscope"); |
| 391 | if (scope == PTHREAD_SCOPE_SYSTEM) |
| 392 | return 0; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 393 | pthread_error("pthread_attr_setscope: " |
| 394 | "invalid or unsupported scope"); |
sewardj | 0d84423 | 2002-06-02 09:29:31 +0000 | [diff] [blame] | 395 | if (scope == PTHREAD_SCOPE_PROCESS) |
| 396 | return ENOTSUP; |
| 397 | return EINVAL; |
| 398 | } |
| 399 | |
| 400 | int pthread_attr_getscope ( const pthread_attr_t *attr, int *scope ) |
| 401 | { |
| 402 | ensure_valgrind("pthread_attr_setscope"); |
| 403 | if (scope) |
| 404 | *scope = PTHREAD_SCOPE_SYSTEM; |
| 405 | return 0; |
| 406 | } |
| 407 | |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 408 | |
| 409 | /* Pretty bogus. Avoid if possible. */ |
| 410 | int pthread_getattr_np (pthread_t thread, pthread_attr_t *attr) |
| 411 | { |
| 412 | int detached; |
| 413 | size_t limit; |
| 414 | ensure_valgrind("pthread_getattr_np"); |
| 415 | kludged("pthread_getattr_np"); |
| 416 | limit = VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 417 | - 1000; /* paranoia */ |
| 418 | attr->__detachstate = PTHREAD_CREATE_JOINABLE; |
| 419 | attr->__schedpolicy = SCHED_OTHER; |
| 420 | attr->__schedparam.sched_priority = 0; |
| 421 | attr->__inheritsched = PTHREAD_EXPLICIT_SCHED; |
| 422 | attr->__scope = PTHREAD_SCOPE_SYSTEM; |
| 423 | attr->__guardsize = VKI_BYTES_PER_PAGE; |
| 424 | attr->__stackaddr = NULL; |
| 425 | attr->__stackaddr_set = 0; |
| 426 | attr->__stacksize = limit; |
| 427 | VALGRIND_MAGIC_SEQUENCE(detached, (-1) /* default */, |
| 428 | VG_USERREQ__SET_OR_GET_DETACH, |
| 429 | 2 /* get */, thread, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 430 | my_assert(detached == 0 || detached == 1); |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 431 | if (detached) |
| 432 | attr->__detachstate = PTHREAD_CREATE_DETACHED; |
| 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | /* Bogus ... */ |
sewardj | 111b14c | 2002-10-20 16:22:57 +0000 | [diff] [blame] | 438 | __attribute__((weak)) |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 439 | int pthread_attr_getstackaddr ( const pthread_attr_t * attr, |
| 440 | void ** stackaddr ) |
| 441 | { |
| 442 | ensure_valgrind("pthread_attr_getstackaddr"); |
| 443 | kludged("pthread_attr_getstackaddr"); |
| 444 | if (stackaddr) |
| 445 | *stackaddr = NULL; |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | /* Not bogus (!) */ |
sewardj | 111b14c | 2002-10-20 16:22:57 +0000 | [diff] [blame] | 450 | __attribute__((weak)) |
sewardj | 64039bb | 2002-06-03 00:58:18 +0000 | [diff] [blame] | 451 | int pthread_attr_getstacksize ( const pthread_attr_t * _attr, |
| 452 | size_t * __stacksize ) |
| 453 | { |
| 454 | size_t limit; |
| 455 | ensure_valgrind("pthread_attr_getstacksize"); |
| 456 | limit = VG_PTHREAD_STACK_SIZE - VG_AR_CLIENT_STACKBASE_REDZONE_SZB |
| 457 | - 1000; /* paranoia */ |
| 458 | if (__stacksize) |
| 459 | *__stacksize = limit; |
| 460 | return 0; |
| 461 | } |
| 462 | |
sewardj | a3be12f | 2002-06-17 12:19:44 +0000 | [diff] [blame] | 463 | int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy) |
| 464 | { |
| 465 | if (policy != SCHED_OTHER && policy != SCHED_FIFO && policy != SCHED_RR) |
| 466 | return EINVAL; |
| 467 | attr->__schedpolicy = policy; |
| 468 | return 0; |
| 469 | } |
| 470 | |
| 471 | int pthread_attr_getschedpolicy(const pthread_attr_t *attr, int *policy) |
| 472 | { |
| 473 | *policy = attr->__schedpolicy; |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | |
sewardj | 111b14c | 2002-10-20 16:22:57 +0000 | [diff] [blame] | 478 | /* This is completely bogus. We reject all attempts to change it from |
| 479 | VKI_BYTES_PER_PAGE. I don't have a clue what it's for so it seems |
| 480 | safest to be paranoid. */ |
| 481 | __attribute__((weak)) |
| 482 | int pthread_attr_setguardsize(pthread_attr_t *attr, size_t guardsize) |
| 483 | { |
| 484 | static int moans = N_MOANS; |
| 485 | |
| 486 | if (guardsize == VKI_BYTES_PER_PAGE) |
| 487 | return 0; |
| 488 | |
| 489 | if (moans-- > 0) |
| 490 | ignored("pthread_attr_setguardsize: ignoring guardsize != 4096"); |
| 491 | |
| 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /* A straight copy of the LinuxThreads code. */ |
| 496 | __attribute__((weak)) |
| 497 | int pthread_attr_getguardsize(const pthread_attr_t *attr, size_t *guardsize) |
| 498 | { |
| 499 | *guardsize = attr->__guardsize; |
| 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 504 | /* --------------------------------------------------- |
| 505 | Helper functions for running a thread |
| 506 | and for clearing up afterwards. |
| 507 | ------------------------------------------------ */ |
| 508 | |
| 509 | /* All exiting threads eventually pass through here, bearing the |
| 510 | return value, or PTHREAD_CANCELED, in ret_val. */ |
| 511 | static |
| 512 | __attribute__((noreturn)) |
| 513 | void thread_exit_wrapper ( void* ret_val ) |
| 514 | { |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 515 | int detached, res; |
| 516 | CleanupEntry cu; |
| 517 | pthread_key_t key; |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 518 | void** specifics_ptr; |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 519 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 520 | /* Run this thread's cleanup handlers. */ |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 521 | while (1) { |
| 522 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 523 | VG_USERREQ__CLEANUP_POP, |
| 524 | &cu, 0, 0, 0); |
| 525 | if (res == -1) break; /* stack empty */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 526 | my_assert(res == 0); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 527 | if (0) printf("running exit cleanup handler"); |
| 528 | cu.fn ( cu.arg ); |
| 529 | } |
| 530 | |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 531 | /* Run this thread's key finalizers. Really this should be run |
| 532 | PTHREAD_DESTRUCTOR_ITERATIONS times. */ |
| 533 | for (key = 0; key < VG_N_THREAD_KEYS; key++) { |
| 534 | VALGRIND_MAGIC_SEQUENCE(res, (-2) /* default */, |
| 535 | VG_USERREQ__GET_KEY_D_AND_S, |
| 536 | key, &cu, 0, 0 ); |
| 537 | if (res == 0) { |
| 538 | /* valid key */ |
| 539 | if (cu.fn && cu.arg) |
| 540 | cu.fn /* destructor for key */ |
| 541 | ( cu.arg /* specific for key for this thread */ ); |
| 542 | continue; |
| 543 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 544 | my_assert(res == -1); |
sewardj | 870497a | 2002-05-29 01:06:47 +0000 | [diff] [blame] | 545 | } |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 546 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 547 | /* Free up my specifics space, if any. */ |
| 548 | VALGRIND_MAGIC_SEQUENCE(specifics_ptr, 3 /* default */, |
| 549 | VG_USERREQ__PTHREAD_GETSPECIFIC_PTR, |
| 550 | pthread_self(), 0, 0, 0); |
| 551 | my_assert(specifics_ptr != (void**)3); |
| 552 | my_assert(specifics_ptr != (void**)1); /* 1 means invalid thread */ |
| 553 | if (specifics_ptr != NULL) |
| 554 | my_free(specifics_ptr); |
| 555 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 556 | /* Decide on my final disposition. */ |
| 557 | VALGRIND_MAGIC_SEQUENCE(detached, (-1) /* default */, |
| 558 | VG_USERREQ__SET_OR_GET_DETACH, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 559 | 2 /* get */, pthread_self(), 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 560 | my_assert(detached == 0 || detached == 1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 561 | |
| 562 | if (detached) { |
| 563 | /* Detached; I just quit right now. */ |
| 564 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 565 | VG_USERREQ__QUIT, 0, 0, 0, 0); |
| 566 | } else { |
| 567 | /* Not detached; so I wait for a joiner. */ |
| 568 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 569 | VG_USERREQ__WAIT_JOINER, ret_val, 0, 0, 0); |
| 570 | } |
| 571 | /* NOTREACHED */ |
| 572 | barf("thread_exit_wrapper: still alive?!"); |
| 573 | } |
| 574 | |
| 575 | |
| 576 | /* This function is a wrapper function for running a thread. It runs |
| 577 | the root function specified in pthread_create, and then, should the |
| 578 | root function return a value, it arranges to run the thread's |
| 579 | cleanup handlers and exit correctly. */ |
| 580 | |
sewardj | 728a527 | 2002-06-20 10:25:37 +0000 | [diff] [blame] | 581 | /* Struct used to convey info from pthread_create to thread_wrapper. |
| 582 | Must be careful not to pass to the child thread any pointers to |
| 583 | objects which might be on the parent's stack. */ |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 584 | typedef |
| 585 | struct { |
sewardj | 728a527 | 2002-06-20 10:25:37 +0000 | [diff] [blame] | 586 | int attr__detachstate; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 587 | void* (*root_fn) ( void* ); |
| 588 | void* arg; |
| 589 | } |
| 590 | NewThreadInfo; |
| 591 | |
| 592 | |
| 593 | /* This is passed to the VG_USERREQ__APPLY_IN_NEW_THREAD and so must |
| 594 | not return. Note that this runs in the new thread, not the |
| 595 | parent. */ |
| 596 | static |
| 597 | __attribute__((noreturn)) |
| 598 | void thread_wrapper ( NewThreadInfo* info ) |
| 599 | { |
sewardj | 728a527 | 2002-06-20 10:25:37 +0000 | [diff] [blame] | 600 | int attr__detachstate; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 601 | void* (*root_fn) ( void* ); |
| 602 | void* arg; |
| 603 | void* ret_val; |
| 604 | |
sewardj | 728a527 | 2002-06-20 10:25:37 +0000 | [diff] [blame] | 605 | attr__detachstate = info->attr__detachstate; |
| 606 | root_fn = info->root_fn; |
| 607 | arg = info->arg; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 608 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 609 | /* Free up the arg block that pthread_create malloced. */ |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 610 | my_free(info); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 611 | |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 612 | /* Minimally observe the attributes supplied. */ |
sewardj | 728a527 | 2002-06-20 10:25:37 +0000 | [diff] [blame] | 613 | if (attr__detachstate != PTHREAD_CREATE_DETACHED |
| 614 | && attr__detachstate != PTHREAD_CREATE_JOINABLE) |
| 615 | pthread_error("thread_wrapper: invalid attr->__detachstate"); |
| 616 | if (attr__detachstate == PTHREAD_CREATE_DETACHED) |
| 617 | pthread_detach(pthread_self()); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 618 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 619 | # ifdef GLIBC_2_3 |
| 620 | /* Set this thread's locale to the global (default) locale. A hack |
| 621 | in support of glibc-2.3. This does the biz for the all new |
| 622 | threads; the root thread is done with a horrible hack in |
| 623 | init_libc_tsd_keys() below. |
| 624 | */ |
| 625 | __uselocale(LC_GLOBAL_LOCALE); |
| 626 | # endif |
| 627 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 628 | /* The root function might not return. But if it does we simply |
| 629 | move along to thread_exit_wrapper. All other ways out for the |
| 630 | thread (cancellation, or calling pthread_exit) lead there |
| 631 | too. */ |
| 632 | ret_val = root_fn(arg); |
| 633 | thread_exit_wrapper(ret_val); |
| 634 | /* NOTREACHED */ |
| 635 | } |
| 636 | |
| 637 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 638 | /* --------------------------------------------------- |
| 639 | THREADs |
| 640 | ------------------------------------------------ */ |
| 641 | |
sewardj | ff42d1d | 2002-05-22 13:17:31 +0000 | [diff] [blame] | 642 | __attribute__((weak)) |
| 643 | int pthread_yield ( void ) |
| 644 | { |
| 645 | int res; |
| 646 | ensure_valgrind("pthread_yield"); |
| 647 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 648 | VG_USERREQ__PTHREAD_YIELD, 0, 0, 0, 0); |
| 649 | return 0; |
| 650 | } |
| 651 | |
| 652 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 653 | int pthread_equal(pthread_t thread1, pthread_t thread2) |
| 654 | { |
| 655 | return thread1 == thread2 ? 1 : 0; |
| 656 | } |
| 657 | |
| 658 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 659 | /* Bundle up the args into a malloc'd block and create a new thread |
| 660 | consisting of thread_wrapper() applied to said malloc'd block. */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 661 | int |
sewardj | 1462c8b | 2002-07-24 09:41:52 +0000 | [diff] [blame] | 662 | pthread_create (pthread_t *__restrict __thredd, |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 663 | __const pthread_attr_t *__restrict __attr, |
| 664 | void *(*__start_routine) (void *), |
| 665 | void *__restrict __arg) |
| 666 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 667 | int tid_child; |
| 668 | NewThreadInfo* info; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 669 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 670 | ensure_valgrind("pthread_create"); |
| 671 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 672 | /* make sure the tsd keys, and hence locale info, are initialised |
| 673 | before we get into complications making new threads. */ |
| 674 | init_libc_tsd_keys(); |
| 675 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 676 | /* Allocate space for the arg block. thread_wrapper will free |
| 677 | it. */ |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 678 | info = my_malloc(sizeof(NewThreadInfo)); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 679 | my_assert(info != NULL); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 680 | |
sewardj | 728a527 | 2002-06-20 10:25:37 +0000 | [diff] [blame] | 681 | if (__attr) |
| 682 | info->attr__detachstate = __attr->__detachstate; |
| 683 | else |
| 684 | info->attr__detachstate = PTHREAD_CREATE_JOINABLE; |
| 685 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 686 | info->root_fn = __start_routine; |
| 687 | info->arg = __arg; |
| 688 | VALGRIND_MAGIC_SEQUENCE(tid_child, VG_INVALID_THREADID /* default */, |
| 689 | VG_USERREQ__APPLY_IN_NEW_THREAD, |
| 690 | &thread_wrapper, info, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 691 | my_assert(tid_child != VG_INVALID_THREADID); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 692 | |
sewardj | 1462c8b | 2002-07-24 09:41:52 +0000 | [diff] [blame] | 693 | if (__thredd) |
| 694 | *__thredd = tid_child; |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 695 | return 0; /* success */ |
| 696 | } |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 697 | |
| 698 | |
| 699 | int |
| 700 | pthread_join (pthread_t __th, void **__thread_return) |
| 701 | { |
| 702 | int res; |
| 703 | ensure_valgrind("pthread_join"); |
| 704 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 705 | VG_USERREQ__PTHREAD_JOIN, |
| 706 | __th, __thread_return, 0, 0); |
| 707 | return res; |
| 708 | } |
| 709 | |
| 710 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 711 | void pthread_exit(void *retval) |
| 712 | { |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 713 | ensure_valgrind("pthread_exit"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 714 | /* Simple! */ |
| 715 | thread_exit_wrapper(retval); |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 716 | } |
| 717 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 718 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 719 | int pthread_detach(pthread_t th) |
| 720 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 721 | int res; |
| 722 | ensure_valgrind("pthread_detach"); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 723 | /* First we enquire as to the current detach state. */ |
| 724 | VALGRIND_MAGIC_SEQUENCE(res, (-2) /* default */, |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 725 | VG_USERREQ__SET_OR_GET_DETACH, |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 726 | 2 /* get */, th, 0, 0); |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 727 | if (res == -1) { |
| 728 | /* not found */ |
| 729 | pthread_error("pthread_detach: " |
| 730 | "invalid target thread"); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 731 | return ESRCH; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 732 | } |
| 733 | if (res == 1) { |
| 734 | /* already detached */ |
| 735 | pthread_error("pthread_detach: " |
| 736 | "target thread is already detached"); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 737 | return EINVAL; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 738 | } |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 739 | if (res == 0) { |
| 740 | VALGRIND_MAGIC_SEQUENCE(res, (-2) /* default */, |
| 741 | VG_USERREQ__SET_OR_GET_DETACH, |
| 742 | 1 /* set */, th, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 743 | my_assert(res == 0); |
sewardj | 7989d0c | 2002-05-28 11:00:01 +0000 | [diff] [blame] | 744 | return 0; |
| 745 | } |
| 746 | barf("pthread_detach"); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 747 | } |
| 748 | |
| 749 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 750 | /* --------------------------------------------------- |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 751 | CLEANUP STACKS |
| 752 | ------------------------------------------------ */ |
| 753 | |
| 754 | void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, |
| 755 | void (*__routine) (void *), |
| 756 | void *__arg) |
| 757 | { |
| 758 | int res; |
| 759 | CleanupEntry cu; |
| 760 | ensure_valgrind("_pthread_cleanup_push"); |
| 761 | cu.fn = __routine; |
| 762 | cu.arg = __arg; |
| 763 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 764 | VG_USERREQ__CLEANUP_PUSH, |
| 765 | &cu, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 766 | my_assert(res == 0); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 767 | } |
| 768 | |
| 769 | |
| 770 | void _pthread_cleanup_push_defer (struct _pthread_cleanup_buffer *__buffer, |
| 771 | void (*__routine) (void *), |
| 772 | void *__arg) |
| 773 | { |
| 774 | /* As _pthread_cleanup_push, but first save the thread's original |
| 775 | cancellation type in __buffer and set it to Deferred. */ |
| 776 | int orig_ctype; |
| 777 | ensure_valgrind("_pthread_cleanup_push_defer"); |
| 778 | /* Set to Deferred, and put the old cancellation type in res. */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 779 | my_assert(-1 != PTHREAD_CANCEL_DEFERRED); |
| 780 | my_assert(-1 != PTHREAD_CANCEL_ASYNCHRONOUS); |
| 781 | my_assert(sizeof(struct _pthread_cleanup_buffer) >= sizeof(int)); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 782 | VALGRIND_MAGIC_SEQUENCE(orig_ctype, (-1) /* default */, |
| 783 | VG_USERREQ__SET_CANCELTYPE, |
| 784 | PTHREAD_CANCEL_DEFERRED, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 785 | my_assert(orig_ctype != -1); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 786 | *((int*)(__buffer)) = orig_ctype; |
| 787 | /* Now push the cleanup. */ |
| 788 | _pthread_cleanup_push(NULL, __routine, __arg); |
| 789 | } |
| 790 | |
| 791 | |
| 792 | void _pthread_cleanup_pop (struct _pthread_cleanup_buffer *__buffer, |
| 793 | int __execute) |
| 794 | { |
| 795 | int res; |
| 796 | CleanupEntry cu; |
| 797 | ensure_valgrind("_pthread_cleanup_push"); |
| 798 | cu.fn = cu.arg = NULL; /* paranoia */ |
| 799 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 800 | VG_USERREQ__CLEANUP_POP, |
| 801 | &cu, 0, 0, 0); |
| 802 | if (res == 0) { |
| 803 | /* pop succeeded */ |
| 804 | if (__execute) { |
| 805 | cu.fn ( cu.arg ); |
| 806 | } |
| 807 | return; |
| 808 | } |
| 809 | if (res == -1) { |
| 810 | /* stack underflow */ |
| 811 | return; |
| 812 | } |
| 813 | barf("_pthread_cleanup_pop"); |
| 814 | } |
| 815 | |
| 816 | |
| 817 | void _pthread_cleanup_pop_restore (struct _pthread_cleanup_buffer *__buffer, |
| 818 | int __execute) |
| 819 | { |
| 820 | int orig_ctype, fake_ctype; |
| 821 | /* As _pthread_cleanup_pop, but after popping/running the handler, |
| 822 | restore the thread's original cancellation type from the first |
| 823 | word of __buffer. */ |
| 824 | _pthread_cleanup_pop(NULL, __execute); |
| 825 | orig_ctype = *((int*)(__buffer)); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 826 | my_assert(orig_ctype == PTHREAD_CANCEL_DEFERRED |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 827 | || orig_ctype == PTHREAD_CANCEL_ASYNCHRONOUS); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 828 | my_assert(-1 != PTHREAD_CANCEL_DEFERRED); |
| 829 | my_assert(-1 != PTHREAD_CANCEL_ASYNCHRONOUS); |
| 830 | my_assert(sizeof(struct _pthread_cleanup_buffer) >= sizeof(int)); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 831 | VALGRIND_MAGIC_SEQUENCE(fake_ctype, (-1) /* default */, |
| 832 | VG_USERREQ__SET_CANCELTYPE, |
| 833 | orig_ctype, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 834 | my_assert(fake_ctype == PTHREAD_CANCEL_DEFERRED); |
sewardj | 8ad94e1 | 2002-05-29 00:10:20 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| 837 | |
| 838 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 839 | MUTEX ATTRIBUTES |
| 840 | ------------------------------------------------ */ |
| 841 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 842 | int __pthread_mutexattr_init(pthread_mutexattr_t *attr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 843 | { |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 844 | attr->__mutexkind = PTHREAD_MUTEX_ERRORCHECK_NP; |
sewardj | 8937c81 | 2002-04-12 20:12:20 +0000 | [diff] [blame] | 845 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 846 | } |
| 847 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 848 | int __pthread_mutexattr_settype(pthread_mutexattr_t *attr, int type) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 849 | { |
| 850 | switch (type) { |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 851 | # ifndef GLIBC_2_1 |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 852 | case PTHREAD_MUTEX_TIMED_NP: |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 853 | case PTHREAD_MUTEX_ADAPTIVE_NP: |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 854 | # endif |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 855 | # ifdef GLIBC_2_1 |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 856 | case PTHREAD_MUTEX_FAST_NP: |
sewardj | a1679dd | 2002-05-10 22:31:40 +0000 | [diff] [blame] | 857 | # endif |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 858 | case PTHREAD_MUTEX_RECURSIVE_NP: |
| 859 | case PTHREAD_MUTEX_ERRORCHECK_NP: |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 860 | attr->__mutexkind = type; |
| 861 | return 0; |
| 862 | default: |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 863 | pthread_error("pthread_mutexattr_settype: " |
| 864 | "invalid type"); |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 865 | return EINVAL; |
| 866 | } |
| 867 | } |
| 868 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 869 | int __pthread_mutexattr_destroy(pthread_mutexattr_t *attr) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 870 | { |
| 871 | return 0; |
| 872 | } |
| 873 | |
| 874 | |
| 875 | /* --------------------------------------------------- |
| 876 | MUTEXes |
| 877 | ------------------------------------------------ */ |
| 878 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 879 | int __pthread_mutex_init(pthread_mutex_t *mutex, |
| 880 | const pthread_mutexattr_t *mutexattr) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 881 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 882 | mutex->__m_count = 0; |
| 883 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 884 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
| 885 | if (mutexattr) |
| 886 | mutex->__m_kind = mutexattr->__mutexkind; |
| 887 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 888 | } |
| 889 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 890 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 891 | int __pthread_mutex_lock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 892 | { |
| 893 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 894 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 895 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 896 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 897 | VG_USERREQ__PTHREAD_MUTEX_LOCK, |
| 898 | mutex, 0, 0, 0); |
| 899 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 900 | } else { |
| 901 | if (moans-- > 0) |
| 902 | not_inside("pthread_mutex_lock"); |
| 903 | return 0; /* success */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 904 | } |
| 905 | } |
| 906 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 907 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 908 | int __pthread_mutex_trylock(pthread_mutex_t *mutex) |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 909 | { |
| 910 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 911 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 912 | if (RUNNING_ON_VALGRIND) { |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 913 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 914 | VG_USERREQ__PTHREAD_MUTEX_TRYLOCK, |
| 915 | mutex, 0, 0, 0); |
| 916 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 917 | } else { |
| 918 | if (moans-- > 0) |
| 919 | not_inside("pthread_mutex_trylock"); |
| 920 | return 0; |
sewardj | 30671ff | 2002-04-21 00:13:57 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
| 923 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 924 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 925 | int __pthread_mutex_unlock(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 926 | { |
| 927 | int res; |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 928 | static int moans = N_MOANS; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 929 | if (RUNNING_ON_VALGRIND) { |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 930 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 931 | VG_USERREQ__PTHREAD_MUTEX_UNLOCK, |
| 932 | mutex, 0, 0, 0); |
| 933 | return res; |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 934 | } else { |
| 935 | if (moans-- > 0) |
| 936 | not_inside("pthread_mutex_unlock"); |
| 937 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 938 | } |
| 939 | } |
| 940 | |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 941 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 942 | int __pthread_mutex_destroy(pthread_mutex_t *mutex) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 943 | { |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 944 | /* Valgrind doesn't hold any resources on behalf of the mutex, so no |
| 945 | need to involve it. */ |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 946 | if (mutex->__m_count > 0) { |
| 947 | pthread_error("pthread_mutex_destroy: " |
| 948 | "mutex is still in use"); |
sewardj | 604ec3c | 2002-04-18 22:38:41 +0000 | [diff] [blame] | 949 | return EBUSY; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 950 | } |
| 951 | mutex->__m_count = 0; |
| 952 | mutex->__m_owner = (_pthread_descr)VG_INVALID_THREADID; |
| 953 | mutex->__m_kind = PTHREAD_MUTEX_ERRORCHECK_NP; |
| 954 | return 0; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 958 | /* --------------------------------------------------- |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 959 | CONDITION VARIABLES |
| 960 | ------------------------------------------------ */ |
| 961 | |
| 962 | /* LinuxThreads supports no attributes for conditions. Hence ... */ |
| 963 | |
| 964 | int pthread_condattr_init(pthread_condattr_t *attr) |
| 965 | { |
| 966 | return 0; |
| 967 | } |
| 968 | |
sewardj | 0738a59 | 2002-04-20 13:59:33 +0000 | [diff] [blame] | 969 | int pthread_condattr_destroy(pthread_condattr_t *attr) |
| 970 | { |
| 971 | return 0; |
| 972 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 973 | |
| 974 | int pthread_cond_init( pthread_cond_t *cond, |
| 975 | const pthread_condattr_t *cond_attr) |
| 976 | { |
| 977 | cond->__c_waiting = (_pthread_descr)VG_INVALID_THREADID; |
| 978 | return 0; |
| 979 | } |
| 980 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 981 | int pthread_cond_destroy(pthread_cond_t *cond) |
| 982 | { |
| 983 | /* should check that no threads are waiting on this CV */ |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 984 | static int moans = N_MOANS; |
| 985 | if (moans-- > 0) |
| 986 | kludged("pthread_cond_destroy"); |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 987 | return 0; |
| 988 | } |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 989 | |
| 990 | /* --------------------------------------------------- |
| 991 | SCHEDULING |
| 992 | ------------------------------------------------ */ |
| 993 | |
| 994 | /* This is completely bogus. */ |
| 995 | int pthread_getschedparam(pthread_t target_thread, |
| 996 | int *policy, |
| 997 | struct sched_param *param) |
| 998 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 999 | static int moans = N_MOANS; |
| 1000 | if (moans-- > 0) |
| 1001 | kludged("pthread_getschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1002 | if (policy) *policy = SCHED_OTHER; |
sewardj | 3e909ce | 2002-06-03 13:27:15 +0000 | [diff] [blame] | 1003 | # ifdef HAVE_SCHED_PRIORITY |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 1004 | if (param) param->sched_priority = 0; /* who knows */ |
| 1005 | # else |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1006 | if (param) param->__sched_priority = 0; /* who knows */ |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 1007 | # endif |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1008 | return 0; |
| 1009 | } |
| 1010 | |
| 1011 | int pthread_setschedparam(pthread_t target_thread, |
| 1012 | int policy, |
| 1013 | const struct sched_param *param) |
| 1014 | { |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1015 | static int moans = N_MOANS; |
| 1016 | if (moans-- > 0) |
| 1017 | ignored("pthread_setschedparam"); |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1018 | return 0; |
| 1019 | } |
| 1020 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1021 | int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex) |
| 1022 | { |
| 1023 | int res; |
| 1024 | ensure_valgrind("pthread_cond_wait"); |
| 1025 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1026 | VG_USERREQ__PTHREAD_COND_WAIT, |
| 1027 | cond, mutex, 0, 0); |
| 1028 | return res; |
| 1029 | } |
| 1030 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1031 | int pthread_cond_timedwait ( pthread_cond_t *cond, |
| 1032 | pthread_mutex_t *mutex, |
| 1033 | const struct timespec *abstime ) |
| 1034 | { |
| 1035 | int res; |
| 1036 | unsigned int ms_now, ms_end; |
| 1037 | struct timeval timeval_now; |
| 1038 | unsigned long long int ull_ms_now_after_1970; |
| 1039 | unsigned long long int ull_ms_end_after_1970; |
| 1040 | |
| 1041 | ensure_valgrind("pthread_cond_timedwait"); |
| 1042 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 1043 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 1044 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1045 | my_assert(ms_now != 0xFFFFFFFF); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1046 | res = gettimeofday(&timeval_now, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1047 | my_assert(res == 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1048 | |
| 1049 | ull_ms_now_after_1970 |
| 1050 | = 1000ULL * ((unsigned long long int)(timeval_now.tv_sec)) |
| 1051 | + ((unsigned long long int)(timeval_now.tv_usec / 1000000)); |
| 1052 | ull_ms_end_after_1970 |
| 1053 | = 1000ULL * ((unsigned long long int)(abstime->tv_sec)) |
| 1054 | + ((unsigned long long int)(abstime->tv_nsec / 1000000)); |
sewardj | d8e919e | 2002-05-29 20:13:53 +0000 | [diff] [blame] | 1055 | if (ull_ms_end_after_1970 < ull_ms_now_after_1970) |
| 1056 | ull_ms_end_after_1970 = ull_ms_now_after_1970; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1057 | ms_end |
| 1058 | = ms_now + (unsigned int)(ull_ms_end_after_1970 - ull_ms_now_after_1970); |
| 1059 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1060 | VG_USERREQ__PTHREAD_COND_TIMEDWAIT, |
| 1061 | cond, mutex, ms_end, 0); |
| 1062 | return res; |
| 1063 | } |
| 1064 | |
| 1065 | |
sewardj | 3b5d886 | 2002-04-20 13:53:23 +0000 | [diff] [blame] | 1066 | int pthread_cond_signal(pthread_cond_t *cond) |
| 1067 | { |
| 1068 | int res; |
| 1069 | ensure_valgrind("pthread_cond_signal"); |
| 1070 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1071 | VG_USERREQ__PTHREAD_COND_SIGNAL, |
| 1072 | cond, 0, 0, 0); |
| 1073 | return res; |
| 1074 | } |
| 1075 | |
| 1076 | int pthread_cond_broadcast(pthread_cond_t *cond) |
| 1077 | { |
| 1078 | int res; |
| 1079 | ensure_valgrind("pthread_cond_broadcast"); |
| 1080 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1081 | VG_USERREQ__PTHREAD_COND_BROADCAST, |
| 1082 | cond, 0, 0, 0); |
| 1083 | return res; |
| 1084 | } |
| 1085 | |
sewardj | 6072c36 | 2002-04-19 14:40:57 +0000 | [diff] [blame] | 1086 | |
| 1087 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1088 | CANCELLATION |
| 1089 | ------------------------------------------------ */ |
| 1090 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1091 | int pthread_setcancelstate(int state, int *oldstate) |
| 1092 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1093 | int res; |
| 1094 | ensure_valgrind("pthread_setcancelstate"); |
| 1095 | if (state != PTHREAD_CANCEL_ENABLE |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1096 | && state != PTHREAD_CANCEL_DISABLE) { |
| 1097 | pthread_error("pthread_setcancelstate: " |
| 1098 | "invalid state"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1099 | return EINVAL; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1100 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1101 | my_assert(-1 != PTHREAD_CANCEL_ENABLE); |
| 1102 | my_assert(-1 != PTHREAD_CANCEL_DISABLE); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1103 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 1104 | VG_USERREQ__SET_CANCELSTATE, |
| 1105 | state, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1106 | my_assert(res != -1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1107 | if (oldstate) |
| 1108 | *oldstate = res; |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1109 | return 0; |
| 1110 | } |
| 1111 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1112 | int pthread_setcanceltype(int type, int *oldtype) |
| 1113 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1114 | int res; |
| 1115 | ensure_valgrind("pthread_setcanceltype"); |
| 1116 | if (type != PTHREAD_CANCEL_DEFERRED |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1117 | && type != PTHREAD_CANCEL_ASYNCHRONOUS) { |
| 1118 | pthread_error("pthread_setcanceltype: " |
| 1119 | "invalid type"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1120 | return EINVAL; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1121 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1122 | my_assert(-1 != PTHREAD_CANCEL_DEFERRED); |
| 1123 | my_assert(-1 != PTHREAD_CANCEL_ASYNCHRONOUS); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1124 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 1125 | VG_USERREQ__SET_CANCELTYPE, |
| 1126 | type, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1127 | my_assert(res != -1); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1128 | if (oldtype) |
| 1129 | *oldtype = res; |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1130 | return 0; |
| 1131 | } |
| 1132 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1133 | int pthread_cancel(pthread_t thread) |
| 1134 | { |
| 1135 | int res; |
| 1136 | ensure_valgrind("pthread_cancel"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1137 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 1138 | VG_USERREQ__SET_CANCELPEND, |
| 1139 | thread, &thread_exit_wrapper, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1140 | my_assert(res != -1); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1141 | return res; |
| 1142 | } |
| 1143 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1144 | static __inline__ |
| 1145 | void __my_pthread_testcancel(void) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1146 | { |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1147 | int res; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1148 | ensure_valgrind("__my_pthread_testcancel"); |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1149 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 1150 | VG_USERREQ__TESTCANCEL, |
| 1151 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1152 | my_assert(res == 0); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1155 | void pthread_testcancel ( void ) |
| 1156 | { |
| 1157 | __my_pthread_testcancel(); |
| 1158 | } |
| 1159 | |
sewardj | 20917d8 | 2002-05-28 01:36:45 +0000 | [diff] [blame] | 1160 | |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 1161 | /* Not really sure what this is for. I suspect for doing the POSIX |
| 1162 | requirements for fork() and exec(). We do this internally anyway |
| 1163 | whenever those syscalls are observed, so this could be superfluous, |
| 1164 | but hey ... |
| 1165 | */ |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1166 | void __pthread_kill_other_threads_np ( void ) |
| 1167 | { |
sewardj | ef037c7 | 2002-05-30 00:40:03 +0000 | [diff] [blame] | 1168 | int res; |
| 1169 | ensure_valgrind("__pthread_kill_other_threads_np"); |
| 1170 | VALGRIND_MAGIC_SEQUENCE(res, (-1) /* default */, |
| 1171 | VG_USERREQ__NUKE_OTHER_THREADS, |
| 1172 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 1173 | my_assert(res == 0); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1176 | |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1177 | /* --------------------------------------------------- |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1178 | SIGNALS |
| 1179 | ------------------------------------------------ */ |
| 1180 | |
| 1181 | #include <signal.h> |
| 1182 | |
| 1183 | int pthread_sigmask(int how, const sigset_t *newmask, |
| 1184 | sigset_t *oldmask) |
| 1185 | { |
| 1186 | int res; |
| 1187 | |
| 1188 | /* A bit subtle, because the scheduler expects newmask and oldmask |
| 1189 | to be vki_sigset_t* rather than sigset_t*, and the two are |
| 1190 | different. Fortunately the first 64 bits of a sigset_t are |
| 1191 | exactly a vki_sigset_t, so we just pass the pointers through |
| 1192 | unmodified. Haaaack! |
| 1193 | |
| 1194 | Also mash the how value so that the SIG_ constants from glibc |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1195 | constants to VKI_ constants, so that the former do not have to |
| 1196 | be included into vg_scheduler.c. */ |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1197 | |
| 1198 | ensure_valgrind("pthread_sigmask"); |
| 1199 | |
| 1200 | switch (how) { |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1201 | case SIG_SETMASK: how = VKI_SIG_SETMASK; break; |
| 1202 | case SIG_BLOCK: how = VKI_SIG_BLOCK; break; |
| 1203 | case SIG_UNBLOCK: how = VKI_SIG_UNBLOCK; break; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1204 | default: pthread_error("pthread_sigmask: invalid how"); |
| 1205 | return EINVAL; |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /* Crude check */ |
| 1209 | if (newmask == NULL) |
| 1210 | return EFAULT; |
| 1211 | |
| 1212 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1213 | VG_USERREQ__PTHREAD_SIGMASK, |
| 1214 | how, newmask, oldmask, 0); |
| 1215 | |
| 1216 | /* The scheduler tells us of any memory violations. */ |
| 1217 | return res == 0 ? 0 : EFAULT; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | int sigwait ( const sigset_t* set, int* sig ) |
| 1222 | { |
| 1223 | int res; |
| 1224 | ensure_valgrind("sigwait"); |
| 1225 | /* As with pthread_sigmask we deliberately confuse sigset_t with |
| 1226 | vki_ksigset_t. */ |
| 1227 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1228 | VG_USERREQ__SIGWAIT, |
| 1229 | set, sig, 0, 0); |
| 1230 | return res; |
| 1231 | } |
| 1232 | |
| 1233 | |
sewardj | 018f762 | 2002-05-15 21:13:39 +0000 | [diff] [blame] | 1234 | int pthread_kill(pthread_t thread, int signo) |
| 1235 | { |
| 1236 | int res; |
| 1237 | ensure_valgrind("pthread_kill"); |
| 1238 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1239 | VG_USERREQ__PTHREAD_KILL, |
| 1240 | thread, signo, 0, 0); |
| 1241 | return res; |
| 1242 | } |
| 1243 | |
| 1244 | |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1245 | /* Copied verbatim from Linuxthreads */ |
| 1246 | /* Redefine raise() to send signal to calling thread only, |
| 1247 | as per POSIX 1003.1c */ |
| 1248 | int raise (int sig) |
| 1249 | { |
| 1250 | int retcode = pthread_kill(pthread_self(), sig); |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1251 | if (retcode == 0) { |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1252 | return 0; |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 1253 | } else { |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1254 | errno = retcode; |
| 1255 | return -1; |
| 1256 | } |
| 1257 | } |
| 1258 | |
| 1259 | |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 1260 | int pause ( void ) |
| 1261 | { |
| 1262 | unsigned int n_orig, n_now; |
| 1263 | struct vki_timespec nanosleep_interval; |
| 1264 | ensure_valgrind("pause"); |
| 1265 | |
| 1266 | /* This is surely a cancellation point. */ |
| 1267 | __my_pthread_testcancel(); |
| 1268 | |
| 1269 | VALGRIND_MAGIC_SEQUENCE(n_orig, 0xFFFFFFFF /* default */, |
| 1270 | VG_USERREQ__GET_N_SIGS_RETURNED, |
| 1271 | 0, 0, 0, 0); |
| 1272 | my_assert(n_orig != 0xFFFFFFFF); |
| 1273 | |
| 1274 | while (1) { |
| 1275 | VALGRIND_MAGIC_SEQUENCE(n_now, 0xFFFFFFFF /* default */, |
| 1276 | VG_USERREQ__GET_N_SIGS_RETURNED, |
| 1277 | 0, 0, 0, 0); |
| 1278 | my_assert(n_now != 0xFFFFFFFF); |
| 1279 | my_assert(n_now >= n_orig); |
| 1280 | if (n_now != n_orig) break; |
| 1281 | |
| 1282 | nanosleep_interval.tv_sec = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1283 | nanosleep_interval.tv_nsec = 12 * 1000 * 1000; /* 12 milliseconds */ |
sewardj | 9a2224b | 2002-06-19 10:17:40 +0000 | [diff] [blame] | 1284 | /* It's critical here that valgrind's nanosleep implementation |
| 1285 | is nonblocking. */ |
| 1286 | (void)my_do_syscall2(__NR_nanosleep, |
| 1287 | (int)(&nanosleep_interval), (int)NULL); |
| 1288 | } |
| 1289 | |
| 1290 | * (__errno_location()) = EINTR; |
| 1291 | return -1; |
| 1292 | } |
| 1293 | |
| 1294 | |
sewardj | b48e500 | 2002-05-13 00:16:03 +0000 | [diff] [blame] | 1295 | /* --------------------------------------------------- |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1296 | THREAD-SPECIFICs |
| 1297 | ------------------------------------------------ */ |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1298 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1299 | static |
| 1300 | int key_is_valid (pthread_key_t key) |
| 1301 | { |
| 1302 | int res; |
| 1303 | VALGRIND_MAGIC_SEQUENCE(res, 2 /* default */, |
| 1304 | VG_USERREQ__PTHREAD_KEY_VALIDATE, |
| 1305 | key, 0, 0, 0); |
| 1306 | my_assert(res != 2); |
| 1307 | return res; |
| 1308 | } |
| 1309 | |
| 1310 | |
| 1311 | /* Returns NULL if thread is invalid. Otherwise, if the thread |
| 1312 | already has a specifics area, return that. Otherwise allocate it |
| 1313 | one. */ |
| 1314 | static |
| 1315 | void** get_or_allocate_specifics_ptr ( pthread_t thread ) |
| 1316 | { |
| 1317 | int res, i; |
| 1318 | void** specifics_ptr; |
| 1319 | ensure_valgrind("get_or_allocate_specifics_ptr"); |
| 1320 | |
| 1321 | /* Returns zero if the thread has no specific_ptr. One if thread |
| 1322 | is invalid. Otherwise, the specific_ptr value. This is |
| 1323 | allocated with my_malloc and so is aligned and cannot be |
| 1324 | confused with 1 or 3. */ |
| 1325 | VALGRIND_MAGIC_SEQUENCE(specifics_ptr, 3 /* default */, |
| 1326 | VG_USERREQ__PTHREAD_GETSPECIFIC_PTR, |
| 1327 | thread, 0, 0, 0); |
| 1328 | my_assert(specifics_ptr != (void**)3); |
| 1329 | |
| 1330 | if (specifics_ptr == (void**)1) |
| 1331 | return NULL; /* invalid thread */ |
| 1332 | |
| 1333 | if (specifics_ptr != NULL) |
| 1334 | return specifics_ptr; /* already has a specifics ptr. */ |
| 1335 | |
| 1336 | /* None yet ... allocate a new one. Should never fail. */ |
| 1337 | specifics_ptr = my_malloc( VG_N_THREAD_KEYS * sizeof(void*) ); |
| 1338 | my_assert(specifics_ptr != NULL); |
| 1339 | |
| 1340 | VALGRIND_MAGIC_SEQUENCE(res, -1 /* default */, |
| 1341 | VG_USERREQ__PTHREAD_SETSPECIFIC_PTR, |
| 1342 | specifics_ptr, 0, 0, 0); |
| 1343 | my_assert(res == 0); |
| 1344 | |
| 1345 | /* POSIX sez: "Upon thread creation, the value NULL shall be |
| 1346 | associated with all defined keys in the new thread." This |
| 1347 | allocation is in effect a delayed allocation of the specific |
| 1348 | data for a thread, at its first-use. Hence we initialise it |
| 1349 | here. */ |
| 1350 | for (i = 0; i < VG_N_THREAD_KEYS; i++) { |
| 1351 | specifics_ptr[i] = NULL; |
| 1352 | } |
| 1353 | |
| 1354 | return specifics_ptr; |
| 1355 | } |
| 1356 | |
| 1357 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1358 | int __pthread_key_create(pthread_key_t *key, |
| 1359 | void (*destr_function) (void *)) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1360 | { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1361 | void** specifics_ptr; |
| 1362 | int res, i; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1363 | ensure_valgrind("pthread_key_create"); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* This writes *key if successful. It should never fail. */ |
| 1366 | VALGRIND_MAGIC_SEQUENCE(res, 1 /* default */, |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1367 | VG_USERREQ__PTHREAD_KEY_CREATE, |
| 1368 | key, destr_function, 0, 0); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1369 | my_assert(res == 0); |
| 1370 | |
| 1371 | /* POSIX sez: "Upon key creation, the value NULL shall be |
| 1372 | associated with the new key in all active threads." */ |
| 1373 | for (i = 0; i < VG_N_THREADS; i++) { |
| 1374 | specifics_ptr = get_or_allocate_specifics_ptr(i); |
| 1375 | /* we get NULL if i is an invalid thread. */ |
| 1376 | if (specifics_ptr != NULL) |
| 1377 | specifics_ptr[*key] = NULL; |
| 1378 | } |
| 1379 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1380 | return res; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | int pthread_key_delete(pthread_key_t key) |
| 1384 | { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1385 | int res; |
| 1386 | ensure_valgrind("pthread_key_create"); |
| 1387 | if (!key_is_valid(key)) |
| 1388 | return EINVAL; |
| 1389 | VALGRIND_MAGIC_SEQUENCE(res, 0 /* default */, |
| 1390 | VG_USERREQ__PTHREAD_KEY_DELETE, |
| 1391 | key, 0, 0, 0); |
| 1392 | my_assert(res == 0); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1393 | return 0; |
| 1394 | } |
| 1395 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1396 | int __pthread_setspecific(pthread_key_t key, const void *pointer) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1397 | { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1398 | void** specifics_ptr; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1399 | ensure_valgrind("pthread_setspecific"); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1400 | |
| 1401 | if (!key_is_valid(key)) |
| 1402 | return EINVAL; |
| 1403 | |
| 1404 | specifics_ptr = get_or_allocate_specifics_ptr(pthread_self()); |
| 1405 | specifics_ptr[key] = (void*)pointer; |
| 1406 | return 0; |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1409 | void * __pthread_getspecific(pthread_key_t key) |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1410 | { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1411 | void** specifics_ptr; |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 1412 | ensure_valgrind("pthread_getspecific"); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1413 | |
| 1414 | if (!key_is_valid(key)) |
| 1415 | return NULL; |
| 1416 | |
| 1417 | specifics_ptr = get_or_allocate_specifics_ptr(pthread_self()); |
| 1418 | return specifics_ptr[key]; |
| 1419 | } |
| 1420 | |
| 1421 | |
sewardj | 9aa918d | 2002-10-20 16:25:55 +0000 | [diff] [blame] | 1422 | #ifdef GLIBC_2_3 |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1423 | static |
| 1424 | void ** __pthread_getspecific_addr(pthread_key_t key) |
| 1425 | { |
| 1426 | void** specifics_ptr; |
| 1427 | ensure_valgrind("pthread_getspecific_addr"); |
| 1428 | |
| 1429 | if (!key_is_valid(key)) |
| 1430 | return NULL; |
| 1431 | |
| 1432 | specifics_ptr = get_or_allocate_specifics_ptr(pthread_self()); |
| 1433 | return &(specifics_ptr[key]); |
sewardj | 5e5fa51 | 2002-04-14 13:13:05 +0000 | [diff] [blame] | 1434 | } |
sewardj | 9aa918d | 2002-10-20 16:25:55 +0000 | [diff] [blame] | 1435 | #endif |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1436 | |
| 1437 | /* --------------------------------------------------- |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1438 | ONCEry |
| 1439 | ------------------------------------------------ */ |
| 1440 | |
| 1441 | static pthread_mutex_t once_masterlock = PTHREAD_MUTEX_INITIALIZER; |
| 1442 | |
| 1443 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1444 | int __pthread_once ( pthread_once_t *once_control, |
| 1445 | void (*init_routine) (void) ) |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1446 | { |
| 1447 | int res; |
| 1448 | ensure_valgrind("pthread_once"); |
| 1449 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1450 | res = __pthread_mutex_lock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1451 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1452 | if (res != 0) { |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1453 | barf("pthread_once: Looks like your program's " |
| 1454 | "init routine calls back to pthread_once() ?!"); |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1455 | } |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1456 | |
| 1457 | if (*once_control == 0) { |
| 1458 | *once_control = 1; |
| 1459 | init_routine(); |
| 1460 | } |
| 1461 | |
sewardj | 68b2dd9 | 2002-05-10 21:03:56 +0000 | [diff] [blame] | 1462 | __pthread_mutex_unlock(&once_masterlock); |
sewardj | 89d3d85 | 2002-04-24 19:21:39 +0000 | [diff] [blame] | 1463 | |
| 1464 | return 0; |
| 1465 | } |
| 1466 | |
| 1467 | |
| 1468 | /* --------------------------------------------------- |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1469 | MISC |
| 1470 | ------------------------------------------------ */ |
| 1471 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 1472 | static pthread_mutex_t pthread_atfork_lock |
| 1473 | = PTHREAD_MUTEX_INITIALIZER; |
| 1474 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1475 | int __pthread_atfork ( void (*prepare)(void), |
| 1476 | void (*parent)(void), |
| 1477 | void (*child)(void) ) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1478 | { |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 1479 | int n, res; |
| 1480 | ForkHandlerEntry entry; |
| 1481 | |
| 1482 | ensure_valgrind("pthread_atfork"); |
| 1483 | __pthread_mutex_lock(&pthread_atfork_lock); |
| 1484 | |
| 1485 | /* Fetch old counter */ |
| 1486 | VALGRIND_MAGIC_SEQUENCE(n, -2 /* default */, |
| 1487 | VG_USERREQ__GET_FHSTACK_USED, |
| 1488 | 0, 0, 0, 0); |
| 1489 | my_assert(n >= 0 && n < VG_N_FORKHANDLERSTACK); |
| 1490 | if (n == VG_N_FORKHANDLERSTACK-1) |
| 1491 | barf("pthread_atfork: VG_N_FORKHANDLERSTACK is too low; " |
| 1492 | "increase and recompile"); |
| 1493 | |
| 1494 | /* Add entry */ |
| 1495 | entry.prepare = *prepare; |
| 1496 | entry.parent = *parent; |
| 1497 | entry.child = *child; |
| 1498 | VALGRIND_MAGIC_SEQUENCE(res, -2 /* default */, |
| 1499 | VG_USERREQ__SET_FHSTACK_ENTRY, |
| 1500 | n, &entry, 0, 0); |
| 1501 | my_assert(res == 0); |
| 1502 | |
| 1503 | /* Bump counter */ |
| 1504 | VALGRIND_MAGIC_SEQUENCE(res, -2 /* default */, |
| 1505 | VG_USERREQ__SET_FHSTACK_USED, |
| 1506 | n+1, 0, 0, 0); |
| 1507 | my_assert(res == 0); |
| 1508 | |
| 1509 | __pthread_mutex_unlock(&pthread_atfork_lock); |
| 1510 | return 0; |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 1514 | __attribute__((weak)) |
| 1515 | void __pthread_initialize ( void ) |
| 1516 | { |
sewardj | bea1caa | 2002-05-10 23:20:58 +0000 | [diff] [blame] | 1517 | ensure_valgrind("__pthread_initialize"); |
sewardj | bb99078 | 2002-05-08 02:01:14 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1521 | /* --------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1522 | LIBRARY-PRIVATE THREAD SPECIFIC STATE |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1523 | ------------------------------------------------ */ |
| 1524 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1525 | #include <resolv.h> |
| 1526 | static int thread_specific_errno[VG_N_THREADS]; |
| 1527 | static int thread_specific_h_errno[VG_N_THREADS]; |
| 1528 | static struct __res_state |
| 1529 | thread_specific_res_state[VG_N_THREADS]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1530 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1531 | int* __errno_location ( void ) |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1532 | { |
| 1533 | int tid; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1534 | /* ensure_valgrind("__errno_location"); */ |
| 1535 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1536 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 1537 | 0, 0, 0, 0); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1538 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1539 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1540 | barf("__errno_location: invalid ThreadId"); |
| 1541 | return & thread_specific_errno[tid]; |
| 1542 | } |
| 1543 | |
| 1544 | int* __h_errno_location ( void ) |
| 1545 | { |
| 1546 | int tid; |
| 1547 | /* ensure_valgrind("__h_errno_location"); */ |
| 1548 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 1549 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 1550 | 0, 0, 0, 0); |
| 1551 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1552 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1553 | barf("__h_errno_location: invalid ThreadId"); |
| 1554 | return & thread_specific_h_errno[tid]; |
| 1555 | } |
| 1556 | |
sewardj | b0ff103 | 2002-08-06 09:02:53 +0000 | [diff] [blame] | 1557 | |
| 1558 | #undef _res |
| 1559 | extern struct __res_state _res; |
| 1560 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1561 | struct __res_state* __res_state ( void ) |
| 1562 | { |
| 1563 | int tid; |
| 1564 | /* ensure_valgrind("__res_state"); */ |
| 1565 | VALGRIND_MAGIC_SEQUENCE(tid, 1 /* default */, |
| 1566 | VG_USERREQ__PTHREAD_GET_THREADID, |
| 1567 | 0, 0, 0, 0); |
| 1568 | /* 'cos I'm paranoid ... */ |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 1569 | if (tid < 1 || tid >= VG_N_THREADS) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1570 | barf("__res_state: invalid ThreadId"); |
sewardj | b0ff103 | 2002-08-06 09:02:53 +0000 | [diff] [blame] | 1571 | if (tid == 1) |
| 1572 | return & _res; |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 1573 | return & thread_specific_res_state[tid]; |
sewardj | f8f819e | 2002-04-17 23:21:37 +0000 | [diff] [blame] | 1574 | } |
| 1575 | |
| 1576 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1577 | /* --------------------------------------------------- |
| 1578 | LIBC-PRIVATE SPECIFIC DATA |
| 1579 | ------------------------------------------------ */ |
| 1580 | |
| 1581 | /* Relies on assumption that initial private data is NULL. This |
| 1582 | should be fixed somehow. */ |
| 1583 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1584 | /* The allowable keys (indices) (all 3 of them). |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1585 | From sysdeps/pthread/bits/libc-tsd.h |
| 1586 | */ |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1587 | /* as per glibc anoncvs HEAD of 20021001. */ |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1588 | enum __libc_tsd_key_t { _LIBC_TSD_KEY_MALLOC = 0, |
| 1589 | _LIBC_TSD_KEY_DL_ERROR, |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 1590 | _LIBC_TSD_KEY_RPC_VARS, |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1591 | _LIBC_TSD_KEY_LOCALE, |
| 1592 | _LIBC_TSD_KEY_CTYPE_B, |
| 1593 | _LIBC_TSD_KEY_CTYPE_TOLOWER, |
| 1594 | _LIBC_TSD_KEY_CTYPE_TOUPPER, |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1595 | _LIBC_TSD_KEY_N }; |
| 1596 | |
| 1597 | /* Auto-initialising subsystem. libc_specifics_inited is set |
| 1598 | after initialisation. libc_specifics_inited_mx guards it. */ |
| 1599 | static int libc_specifics_inited = 0; |
| 1600 | static pthread_mutex_t libc_specifics_inited_mx = PTHREAD_MUTEX_INITIALIZER; |
| 1601 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1602 | |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1603 | /* These are the keys we must initialise the first time. */ |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1604 | static pthread_key_t libc_specifics_keys[_LIBC_TSD_KEY_N]; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1605 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1606 | |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1607 | /* Initialise the keys, if they are not already initialised. */ |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1608 | static |
| 1609 | void init_libc_tsd_keys ( void ) |
| 1610 | { |
| 1611 | int res, i; |
| 1612 | pthread_key_t k; |
| 1613 | |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 1614 | /* Don't fall into deadlock if we get called again whilst we still |
| 1615 | hold the lock, via the __uselocale() call herein. */ |
| 1616 | if (libc_specifics_inited != 0) |
| 1617 | return; |
| 1618 | |
| 1619 | /* Take the lock. */ |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1620 | res = __pthread_mutex_lock(&libc_specifics_inited_mx); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1621 | if (res != 0) barf("init_libc_tsd_keys: lock"); |
| 1622 | |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 1623 | /* Now test again, to be sure there is no mistake. */ |
| 1624 | if (libc_specifics_inited != 0) { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1625 | res = __pthread_mutex_unlock(&libc_specifics_inited_mx); |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 1626 | if (res != 0) barf("init_libc_tsd_keys: unlock(1)"); |
| 1627 | return; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1628 | } |
| 1629 | |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 1630 | /* Actually do the initialisation. */ |
| 1631 | /* printf("INIT libc specifics\n"); */ |
| 1632 | for (i = 0; i < _LIBC_TSD_KEY_N; i++) { |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1633 | res = __pthread_key_create(&k, NULL); |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 1634 | if (res != 0) barf("init_libc_tsd_keys: create"); |
| 1635 | libc_specifics_keys[i] = k; |
| 1636 | } |
| 1637 | |
| 1638 | /* Signify init done. */ |
| 1639 | libc_specifics_inited = 1; |
| 1640 | |
| 1641 | # ifdef GLIBC_2_3 |
| 1642 | /* Set the initialising thread's locale to the global (default) |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1643 | locale. A hack in support of glibc-2.3. This does the biz for |
| 1644 | the root thread. For all other threads we run this in |
| 1645 | thread_wrapper(), which does the real work of |
| 1646 | pthread_create(). */ |
| 1647 | /* assert that we are the root thread. I don't know if this is |
| 1648 | really a valid assertion to make; if it breaks I'll reconsider |
| 1649 | it. */ |
| 1650 | my_assert(pthread_self() == 1); |
sewardj | 08c7f01 | 2002-10-07 23:56:55 +0000 | [diff] [blame] | 1651 | __uselocale(LC_GLOBAL_LOCALE); |
| 1652 | # endif |
| 1653 | |
| 1654 | /* Unlock and return. */ |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1655 | res = __pthread_mutex_unlock(&libc_specifics_inited_mx); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1656 | if (res != 0) barf("init_libc_tsd_keys: unlock"); |
| 1657 | } |
| 1658 | |
| 1659 | |
| 1660 | static int |
| 1661 | libc_internal_tsd_set ( enum __libc_tsd_key_t key, |
| 1662 | const void * pointer ) |
| 1663 | { |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1664 | int res; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1665 | /* printf("SET SET SET key %d ptr %p\n", key, pointer); */ |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1666 | if (key < _LIBC_TSD_KEY_MALLOC || key >= _LIBC_TSD_KEY_N) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1667 | barf("libc_internal_tsd_set: invalid key"); |
| 1668 | init_libc_tsd_keys(); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1669 | res = __pthread_setspecific(libc_specifics_keys[key], pointer); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1670 | if (res != 0) barf("libc_internal_tsd_set: setspecific failed"); |
| 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | static void * |
| 1675 | libc_internal_tsd_get ( enum __libc_tsd_key_t key ) |
| 1676 | { |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1677 | void* v; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1678 | /* printf("GET GET GET key %d\n", key); */ |
sewardj | cb7f08a | 2002-10-02 09:41:49 +0000 | [diff] [blame] | 1679 | if (key < _LIBC_TSD_KEY_MALLOC || key >= _LIBC_TSD_KEY_N) |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1680 | barf("libc_internal_tsd_get: invalid key"); |
| 1681 | init_libc_tsd_keys(); |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1682 | v = __pthread_getspecific(libc_specifics_keys[key]); |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1683 | /* if (v == NULL) barf("libc_internal_tsd_set: getspecific failed"); */ |
| 1684 | return v; |
| 1685 | } |
| 1686 | |
| 1687 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1688 | int (*__libc_internal_tsd_set) |
| 1689 | (enum __libc_tsd_key_t key, const void * pointer) |
| 1690 | = libc_internal_tsd_set; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1691 | |
sewardj | 70adeb2 | 2002-04-27 01:35:38 +0000 | [diff] [blame] | 1692 | void* (*__libc_internal_tsd_get) |
| 1693 | (enum __libc_tsd_key_t key) |
| 1694 | = libc_internal_tsd_get; |
sewardj | 5716dbb | 2002-04-26 03:28:18 +0000 | [diff] [blame] | 1695 | |
| 1696 | |
sewardj | 00a66b1 | 2002-10-12 16:42:35 +0000 | [diff] [blame] | 1697 | #ifdef GLIBC_2_3 |
| 1698 | /* This one was first spotted be me in the glibc-2.2.93 sources. */ |
| 1699 | static void** |
| 1700 | libc_internal_tsd_address ( enum __libc_tsd_key_t key ) |
| 1701 | { |
| 1702 | void** v; |
| 1703 | /* printf("ADDR ADDR ADDR key %d\n", key); */ |
| 1704 | if (key < _LIBC_TSD_KEY_MALLOC || key >= _LIBC_TSD_KEY_N) |
| 1705 | barf("libc_internal_tsd_address: invalid key"); |
| 1706 | init_libc_tsd_keys(); |
| 1707 | v = __pthread_getspecific_addr(libc_specifics_keys[key]); |
| 1708 | return v; |
| 1709 | } |
| 1710 | |
| 1711 | void ** (*__libc_internal_tsd_address) |
| 1712 | (enum __libc_tsd_key_t key) |
| 1713 | = libc_internal_tsd_address; |
| 1714 | #endif |
| 1715 | |
| 1716 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1717 | /* --------------------------------------------------------------------- |
| 1718 | These are here (I think) because they are deemed cancellation |
| 1719 | points by POSIX. For the moment we'll simply pass the call along |
| 1720 | to the corresponding thread-unaware (?) libc routine. |
| 1721 | ------------------------------------------------------------------ */ |
| 1722 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1723 | #include <stdlib.h> |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1724 | #include <sys/types.h> |
| 1725 | #include <sys/socket.h> |
| 1726 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1727 | #ifdef GLIBC_2_1 |
| 1728 | extern |
| 1729 | int __sigaction |
| 1730 | (int signum, |
| 1731 | const struct sigaction *act, |
| 1732 | struct sigaction *oldact); |
| 1733 | #else |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1734 | extern |
| 1735 | int __libc_sigaction |
| 1736 | (int signum, |
| 1737 | const struct sigaction *act, |
| 1738 | struct sigaction *oldact); |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 1739 | #endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1740 | int sigaction(int signum, |
| 1741 | const struct sigaction *act, |
| 1742 | struct sigaction *oldact) |
| 1743 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1744 | __my_pthread_testcancel(); |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 1745 | # ifdef GLIBC_2_1 |
| 1746 | return __sigaction(signum, act, oldact); |
| 1747 | # else |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1748 | return __libc_sigaction(signum, act, oldact); |
sewardj | 2a1dcce | 2002-04-22 12:45:25 +0000 | [diff] [blame] | 1749 | # endif |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1750 | } |
| 1751 | |
| 1752 | |
| 1753 | extern |
| 1754 | int __libc_connect(int sockfd, |
| 1755 | const struct sockaddr *serv_addr, |
| 1756 | socklen_t addrlen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1757 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1758 | int connect(int sockfd, |
| 1759 | const struct sockaddr *serv_addr, |
| 1760 | socklen_t addrlen) |
| 1761 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1762 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1763 | return __libc_connect(sockfd, serv_addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1764 | } |
| 1765 | |
| 1766 | |
| 1767 | extern |
| 1768 | int __libc_fcntl(int fd, int cmd, long arg); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1769 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1770 | int fcntl(int fd, int cmd, long arg) |
| 1771 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1772 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1773 | return __libc_fcntl(fd, cmd, arg); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1774 | } |
| 1775 | |
| 1776 | |
| 1777 | extern |
| 1778 | ssize_t __libc_write(int fd, const void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1779 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1780 | ssize_t write(int fd, const void *buf, size_t count) |
| 1781 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1782 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1783 | return __libc_write(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
| 1786 | |
| 1787 | extern |
| 1788 | ssize_t __libc_read(int fd, void *buf, size_t count); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1789 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1790 | ssize_t read(int fd, void *buf, size_t count) |
| 1791 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1792 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1793 | return __libc_read(fd, buf, count); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1794 | } |
| 1795 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1796 | |
| 1797 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1798 | int __libc_open64(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1799 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1800 | int open64(const char *pathname, int flags, mode_t mode) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1801 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1802 | __my_pthread_testcancel(); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1803 | return __libc_open64(pathname, flags, mode); |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1804 | } |
| 1805 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1806 | |
| 1807 | extern |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1808 | int __libc_open(const char *pathname, int flags, mode_t mode); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1809 | __attribute__((weak)) |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1810 | int open(const char *pathname, int flags, mode_t mode) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1811 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1812 | __my_pthread_testcancel(); |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 1813 | return __libc_open(pathname, flags, mode); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | |
| 1817 | extern |
| 1818 | int __libc_close(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1819 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1820 | int close(int fd) |
| 1821 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1822 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1823 | return __libc_close(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1824 | } |
| 1825 | |
| 1826 | |
| 1827 | extern |
| 1828 | int __libc_accept(int s, struct sockaddr *addr, socklen_t *addrlen); |
sewardj | f220ccc | 2002-10-23 21:53:49 +0000 | [diff] [blame^] | 1829 | |
| 1830 | int VGL_(accept)(int s, struct sockaddr *addr, socklen_t *addrlen) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1831 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1832 | __my_pthread_testcancel(); |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 1833 | wait_for_fd_to_be_readable_or_erring(s); |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1834 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1835 | return __libc_accept(s, addr, addrlen); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | |
| 1839 | extern |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1840 | pid_t __libc_waitpid(pid_t pid, int *status, int options); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1841 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1842 | pid_t waitpid(pid_t pid, int *status, int options) |
| 1843 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1844 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1845 | return __libc_waitpid(pid, status, options); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
| 1848 | |
| 1849 | extern |
| 1850 | int __libc_nanosleep(const struct timespec *req, struct timespec *rem); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1851 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1852 | int nanosleep(const struct timespec *req, struct timespec *rem) |
| 1853 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1854 | __my_pthread_testcancel(); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1855 | return __libc_nanosleep(req, rem); |
| 1856 | } |
| 1857 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1858 | |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1859 | extern |
| 1860 | int __libc_fsync(int fd); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1861 | __attribute__((weak)) |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1862 | int fsync(int fd) |
| 1863 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1864 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1865 | return __libc_fsync(fd); |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 1866 | } |
| 1867 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1868 | |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1869 | extern |
| 1870 | off_t __libc_lseek(int fildes, off_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1871 | __attribute__((weak)) |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1872 | off_t lseek(int fildes, off_t offset, int whence) |
| 1873 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1874 | __my_pthread_testcancel(); |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 1875 | return __libc_lseek(fildes, offset, whence); |
sewardj | 70c7536 | 2002-04-13 04:18:32 +0000 | [diff] [blame] | 1876 | } |
| 1877 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1878 | |
| 1879 | extern |
| 1880 | __off64_t __libc_lseek64(int fildes, __off64_t offset, int whence); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1881 | __attribute__((weak)) |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1882 | __off64_t lseek64(int fildes, __off64_t offset, int whence) |
| 1883 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1884 | __my_pthread_testcancel(); |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1885 | return __libc_lseek64(fildes, offset, whence); |
| 1886 | } |
| 1887 | |
| 1888 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1889 | extern |
| 1890 | ssize_t __libc_pread64 (int __fd, void *__buf, size_t __nbytes, |
| 1891 | __off64_t __offset); |
| 1892 | ssize_t __pread64 (int __fd, void *__buf, size_t __nbytes, |
| 1893 | __off64_t __offset) |
| 1894 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1895 | __my_pthread_testcancel(); |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1896 | return __libc_pread64(__fd, __buf, __nbytes, __offset); |
| 1897 | } |
| 1898 | |
| 1899 | |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 1900 | extern |
| 1901 | ssize_t __libc_pwrite64 (int __fd, const void *__buf, size_t __nbytes, |
| 1902 | __off64_t __offset); |
| 1903 | ssize_t __pwrite64 (int __fd, const void *__buf, size_t __nbytes, |
| 1904 | __off64_t __offset) |
| 1905 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1906 | __my_pthread_testcancel(); |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 1907 | return __libc_pwrite64(__fd, __buf, __nbytes, __offset); |
| 1908 | } |
| 1909 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 1910 | |
sewardj | 39b93b1 | 2002-05-18 10:56:27 +0000 | [diff] [blame] | 1911 | extern |
| 1912 | ssize_t __libc_pwrite(int fd, const void *buf, size_t count, off_t offset); |
| 1913 | __attribute__((weak)) |
| 1914 | ssize_t pwrite(int fd, const void *buf, size_t count, off_t offset) |
| 1915 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1916 | __my_pthread_testcancel(); |
sewardj | 39b93b1 | 2002-05-18 10:56:27 +0000 | [diff] [blame] | 1917 | return __libc_pwrite(fd, buf, count, offset); |
| 1918 | } |
| 1919 | |
| 1920 | |
| 1921 | extern |
| 1922 | ssize_t __libc_pread(int fd, void *buf, size_t count, off_t offset); |
| 1923 | __attribute__((weak)) |
| 1924 | ssize_t pread(int fd, void *buf, size_t count, off_t offset) |
| 1925 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1926 | __my_pthread_testcancel(); |
sewardj | 39b93b1 | 2002-05-18 10:56:27 +0000 | [diff] [blame] | 1927 | return __libc_pread(fd, buf, count, offset); |
| 1928 | } |
| 1929 | |
| 1930 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1931 | extern |
| 1932 | void __libc_longjmp(jmp_buf env, int val) __attribute((noreturn)); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1933 | /* not weak: __attribute__((weak)) */ |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1934 | void longjmp(jmp_buf env, int val) |
| 1935 | { |
| 1936 | __libc_longjmp(env, val); |
| 1937 | } |
| 1938 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1939 | |
sewardj | 436c2db | 2002-06-18 09:07:54 +0000 | [diff] [blame] | 1940 | extern void __libc_siglongjmp (sigjmp_buf env, int val) |
| 1941 | __attribute__ ((noreturn)); |
| 1942 | void siglongjmp(sigjmp_buf env, int val) |
| 1943 | { |
| 1944 | kludged("siglongjmp (cleanup handlers are ignored)"); |
| 1945 | __libc_siglongjmp(env, val); |
| 1946 | } |
| 1947 | |
| 1948 | |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1949 | extern |
| 1950 | int __libc_send(int s, const void *msg, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1951 | __attribute__((weak)) |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1952 | int send(int s, const void *msg, size_t len, int flags) |
| 1953 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1954 | __my_pthread_testcancel(); |
sewardj | 6af4b5d | 2002-04-16 04:40:49 +0000 | [diff] [blame] | 1955 | return __libc_send(s, msg, len, flags); |
| 1956 | } |
| 1957 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1958 | |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1959 | extern |
| 1960 | int __libc_recv(int s, void *buf, size_t len, int flags); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 1961 | __attribute__((weak)) |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1962 | int recv(int s, void *buf, size_t len, int flags) |
| 1963 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1964 | __my_pthread_testcancel(); |
sewardj | 1d6b3a2 | 2002-06-20 07:58:33 +0000 | [diff] [blame] | 1965 | wait_for_fd_to_be_readable_or_erring(s); |
| 1966 | __my_pthread_testcancel(); |
sewardj | 1e8cdc9 | 2002-04-18 11:37:52 +0000 | [diff] [blame] | 1967 | return __libc_recv(s, buf, len, flags); |
| 1968 | } |
| 1969 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 1970 | |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1971 | extern |
| 1972 | int __libc_sendmsg(int s, const struct msghdr *msg, int flags); |
| 1973 | __attribute__((weak)) |
| 1974 | int sendmsg(int s, const struct msghdr *msg, int flags) |
| 1975 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1976 | __my_pthread_testcancel(); |
sewardj | 3665ded | 2002-05-16 16:57:25 +0000 | [diff] [blame] | 1977 | return __libc_sendmsg(s, msg, flags); |
| 1978 | } |
| 1979 | |
| 1980 | |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 1981 | extern |
sewardj | 59da27a | 2002-06-06 08:33:54 +0000 | [diff] [blame] | 1982 | int __libc_recvmsg(int s, struct msghdr *msg, int flags); |
| 1983 | __attribute__((weak)) |
| 1984 | int recvmsg(int s, struct msghdr *msg, int flags) |
| 1985 | { |
| 1986 | __my_pthread_testcancel(); |
| 1987 | return __libc_recvmsg(s, msg, flags); |
| 1988 | } |
| 1989 | |
| 1990 | |
| 1991 | extern |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 1992 | int __libc_recvfrom(int s, void *buf, size_t len, int flags, |
| 1993 | struct sockaddr *from, socklen_t *fromlen); |
| 1994 | __attribute__((weak)) |
| 1995 | int recvfrom(int s, void *buf, size_t len, int flags, |
| 1996 | struct sockaddr *from, socklen_t *fromlen) |
| 1997 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 1998 | __my_pthread_testcancel(); |
sewardj | 2e20763 | 2002-06-13 17:29:53 +0000 | [diff] [blame] | 1999 | wait_for_fd_to_be_readable_or_erring(s); |
| 2000 | __my_pthread_testcancel(); |
sewardj | 436e058 | 2002-04-26 14:31:40 +0000 | [diff] [blame] | 2001 | return __libc_recvfrom(s, buf, len, flags, from, fromlen); |
| 2002 | } |
| 2003 | |
| 2004 | |
| 2005 | extern |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 2006 | int __libc_sendto(int s, const void *msg, size_t len, int flags, |
| 2007 | const struct sockaddr *to, socklen_t tolen); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2008 | __attribute__((weak)) |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 2009 | int sendto(int s, const void *msg, size_t len, int flags, |
| 2010 | const struct sockaddr *to, socklen_t tolen) |
| 2011 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 2012 | __my_pthread_testcancel(); |
sewardj | 796d6a2 | 2002-04-24 02:28:34 +0000 | [diff] [blame] | 2013 | return __libc_sendto(s, msg, len, flags, to, tolen); |
| 2014 | } |
| 2015 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 2016 | |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 2017 | extern |
| 2018 | int __libc_system(const char* str); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2019 | __attribute__((weak)) |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 2020 | int system(const char* str) |
| 2021 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 2022 | __my_pthread_testcancel(); |
sewardj | 369b170 | 2002-04-24 13:28:15 +0000 | [diff] [blame] | 2023 | return __libc_system(str); |
| 2024 | } |
| 2025 | |
sewardj | be32e45 | 2002-04-24 20:29:58 +0000 | [diff] [blame] | 2026 | |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 2027 | extern |
| 2028 | pid_t __libc_wait(int *status); |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2029 | __attribute__((weak)) |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 2030 | pid_t wait(int *status) |
| 2031 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 2032 | __my_pthread_testcancel(); |
sewardj | ab0b1c3 | 2002-04-24 19:26:47 +0000 | [diff] [blame] | 2033 | return __libc_wait(status); |
| 2034 | } |
| 2035 | |
sewardj | 45b4b37 | 2002-04-16 22:50:32 +0000 | [diff] [blame] | 2036 | |
sewardj | 67f1d58 | 2002-05-24 02:11:32 +0000 | [diff] [blame] | 2037 | extern |
| 2038 | int __libc_msync(const void *start, size_t length, int flags); |
| 2039 | __attribute__((weak)) |
| 2040 | int msync(const void *start, size_t length, int flags) |
| 2041 | { |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 2042 | __my_pthread_testcancel(); |
sewardj | 67f1d58 | 2002-05-24 02:11:32 +0000 | [diff] [blame] | 2043 | return __libc_msync(start, length, flags); |
| 2044 | } |
| 2045 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2046 | |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2047 | /*--- fork and its helper ---*/ |
| 2048 | |
| 2049 | static |
| 2050 | void run_fork_handlers ( int what ) |
| 2051 | { |
| 2052 | ForkHandlerEntry entry; |
| 2053 | int n_h, n_handlers, i, res; |
| 2054 | |
| 2055 | my_assert(what == 0 || what == 1 || what == 2); |
| 2056 | |
| 2057 | /* Fetch old counter */ |
| 2058 | VALGRIND_MAGIC_SEQUENCE(n_handlers, -2 /* default */, |
| 2059 | VG_USERREQ__GET_FHSTACK_USED, |
| 2060 | 0, 0, 0, 0); |
| 2061 | my_assert(n_handlers >= 0 && n_handlers < VG_N_FORKHANDLERSTACK); |
| 2062 | |
| 2063 | /* Prepare handlers (what == 0) are called in opposite order of |
| 2064 | calls to pthread_atfork. Parent and child handlers are called |
| 2065 | in the same order as calls to pthread_atfork. */ |
| 2066 | if (what == 0) |
| 2067 | n_h = n_handlers - 1; |
| 2068 | else |
| 2069 | n_h = 0; |
| 2070 | |
| 2071 | for (i = 0; i < n_handlers; i++) { |
| 2072 | VALGRIND_MAGIC_SEQUENCE(res, -2 /* default */, |
| 2073 | VG_USERREQ__GET_FHSTACK_ENTRY, |
| 2074 | n_h, &entry, 0, 0); |
| 2075 | my_assert(res == 0); |
| 2076 | switch (what) { |
| 2077 | case 0: if (entry.prepare) entry.prepare(); |
| 2078 | n_h--; break; |
| 2079 | case 1: if (entry.parent) entry.parent(); |
| 2080 | n_h++; break; |
| 2081 | case 2: if (entry.child) entry.child(); |
| 2082 | n_h++; break; |
| 2083 | default: barf("run_fork_handlers: invalid what"); |
| 2084 | } |
| 2085 | } |
| 2086 | |
| 2087 | if (what != 0 /* prepare */) { |
| 2088 | /* Empty out the stack. */ |
| 2089 | VALGRIND_MAGIC_SEQUENCE(res, -2 /* default */, |
| 2090 | VG_USERREQ__SET_FHSTACK_USED, |
| 2091 | 0, 0, 0, 0); |
| 2092 | my_assert(res == 0); |
| 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | extern |
| 2097 | pid_t __libc_fork(void); |
| 2098 | pid_t __fork(void) |
| 2099 | { |
| 2100 | pid_t pid; |
| 2101 | __my_pthread_testcancel(); |
| 2102 | __pthread_mutex_lock(&pthread_atfork_lock); |
| 2103 | |
| 2104 | run_fork_handlers(0 /* prepare */); |
| 2105 | pid = __libc_fork(); |
| 2106 | if (pid == 0) { |
| 2107 | /* I am the child */ |
| 2108 | run_fork_handlers(2 /* child */); |
| 2109 | __pthread_mutex_init(&pthread_atfork_lock, NULL); |
| 2110 | } else { |
| 2111 | /* I am the parent */ |
| 2112 | run_fork_handlers(1 /* parent */); |
| 2113 | __pthread_mutex_unlock(&pthread_atfork_lock); |
| 2114 | } |
| 2115 | return pid; |
| 2116 | } |
| 2117 | |
| 2118 | |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2119 | pid_t __vfork(void) |
| 2120 | { |
| 2121 | return __fork(); |
| 2122 | } |
sewardj | 2cb0034 | 2002-06-28 01:46:26 +0000 | [diff] [blame] | 2123 | |
| 2124 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2125 | /* --------------------------------------------------------------------- |
| 2126 | Nonblocking implementations of select() and poll(). This stuff will |
| 2127 | surely rot your mind. |
| 2128 | ------------------------------------------------------------------ */ |
sewardj | e663cb9 | 2002-04-12 10:26:32 +0000 | [diff] [blame] | 2129 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2130 | /*--------------------------------------------------*/ |
| 2131 | |
| 2132 | #include "vg_kerneliface.h" |
| 2133 | |
| 2134 | static |
| 2135 | __inline__ |
| 2136 | int is_kerror ( int res ) |
| 2137 | { |
| 2138 | if (res >= -4095 && res <= -1) |
| 2139 | return 1; |
| 2140 | else |
| 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | |
| 2145 | static |
| 2146 | int my_do_syscall1 ( int syscallno, int arg1 ) |
| 2147 | { |
| 2148 | int __res; |
| 2149 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 2150 | : "=a" (__res) |
| 2151 | : "0" (syscallno), |
| 2152 | "d" (arg1) ); |
| 2153 | return __res; |
| 2154 | } |
| 2155 | |
| 2156 | static |
| 2157 | int my_do_syscall2 ( int syscallno, |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2158 | int arg1, int arg2 ) |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2159 | { |
| 2160 | int __res; |
| 2161 | __asm__ volatile ("pushl %%ebx; movl %%edx,%%ebx ; int $0x80 ; popl %%ebx" |
| 2162 | : "=a" (__res) |
| 2163 | : "0" (syscallno), |
| 2164 | "d" (arg1), |
| 2165 | "c" (arg2) ); |
| 2166 | return __res; |
| 2167 | } |
| 2168 | |
| 2169 | static |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2170 | int my_do_syscall3 ( int syscallno, |
| 2171 | int arg1, int arg2, int arg3 ) |
| 2172 | { |
| 2173 | int __res; |
| 2174 | __asm__ volatile ("pushl %%ebx; movl %%esi,%%ebx ; int $0x80 ; popl %%ebx" |
| 2175 | : "=a" (__res) |
| 2176 | : "0" (syscallno), |
| 2177 | "S" (arg1), |
| 2178 | "c" (arg2), |
| 2179 | "d" (arg3) ); |
| 2180 | return __res; |
| 2181 | } |
| 2182 | |
sewardj | d5bef57 | 2002-10-23 21:49:33 +0000 | [diff] [blame] | 2183 | static inline |
| 2184 | int my_do_syscall5 ( int syscallno, |
| 2185 | int arg1, int arg2, int arg3, int arg4, int arg5 ) |
| 2186 | { |
| 2187 | int __res; |
| 2188 | __asm__ volatile ("int $0x80" |
| 2189 | : "=a" (__res) |
| 2190 | : "0" (syscallno), |
| 2191 | "b" (arg1), |
| 2192 | "c" (arg2), |
| 2193 | "d" (arg3), |
| 2194 | "S" (arg4), |
| 2195 | "D" (arg5)); |
| 2196 | return __res; |
| 2197 | } |
| 2198 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2199 | static |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2200 | int do_syscall_select( int n, |
| 2201 | vki_fd_set* readfds, |
| 2202 | vki_fd_set* writefds, |
| 2203 | vki_fd_set* exceptfds, |
| 2204 | struct vki_timeval * timeout ) |
| 2205 | { |
| 2206 | int res; |
| 2207 | int args[5]; |
| 2208 | args[0] = n; |
| 2209 | args[1] = (int)readfds; |
| 2210 | args[2] = (int)writefds; |
| 2211 | args[3] = (int)exceptfds; |
| 2212 | args[4] = (int)timeout; |
| 2213 | res = my_do_syscall1(__NR_select, (int)(&(args[0])) ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2214 | return res; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | |
| 2218 | /* This is a wrapper round select(), which makes it thread-safe, |
| 2219 | meaning that only this thread will block, rather than the entire |
| 2220 | process. This wrapper in turn depends on nanosleep() not to block |
| 2221 | the entire process, but I think (hope? suspect?) that POSIX |
| 2222 | pthreads guarantees that to be the case. |
| 2223 | |
| 2224 | Basic idea is: modify the timeout parameter to select so that it |
| 2225 | returns immediately. Poll like this until select returns non-zero, |
| 2226 | indicating something interesting happened, or until our time is up. |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2227 | Space out the polls with nanosleeps of say 11 milliseconds, which |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2228 | is required to be nonblocking; this allows other threads to run. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2229 | |
| 2230 | Assumes: |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2231 | * (checked via my_assert) types fd_set and vki_fd_set are identical. |
| 2232 | * (checked via my_assert) types timeval and vki_timeval are identical. |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2233 | * (unchecked) libc error numbers (EINTR etc) are the negation of the |
| 2234 | kernel's error numbers (VKI_EINTR etc). |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2235 | */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2236 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2237 | /* __attribute__((weak)) */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2238 | int select ( int n, |
| 2239 | fd_set *rfds, |
| 2240 | fd_set *wfds, |
| 2241 | fd_set *xfds, |
| 2242 | struct timeval *timeout ) |
| 2243 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2244 | unsigned int ms_now, ms_end; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2245 | int res; |
| 2246 | fd_set rfds_copy; |
| 2247 | fd_set wfds_copy; |
| 2248 | fd_set xfds_copy; |
| 2249 | struct vki_timeval t_now; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2250 | struct vki_timeval zero_timeout; |
| 2251 | struct vki_timespec nanosleep_interval; |
| 2252 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 2253 | __my_pthread_testcancel(); |
| 2254 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2255 | /* gcc's complains about ms_end being used uninitialised -- classic |
| 2256 | case it can't understand, where ms_end is both defined and used |
| 2257 | only if timeout != NULL. Hence ... */ |
| 2258 | ms_end = 0; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2259 | |
| 2260 | /* We assume that the kernel and libc data layouts are identical |
| 2261 | for the following types. These asserts provide a crude |
| 2262 | check. */ |
| 2263 | if (sizeof(fd_set) != sizeof(vki_fd_set) |
| 2264 | || sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 2265 | barf("valgrind's hacky non-blocking select(): data sizes error"); |
| 2266 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2267 | /* Detect the current time and simultaneously find out if we are |
| 2268 | running on Valgrind. */ |
| 2269 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 2270 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 2271 | 0, 0, 0, 0); |
| 2272 | |
| 2273 | /* If a zero timeout specified, this call is harmless. Also go |
| 2274 | this route if we're not running on Valgrind, for whatever |
| 2275 | reason. */ |
| 2276 | if ( (timeout && timeout->tv_sec == 0 && timeout->tv_usec == 0) |
| 2277 | || (ms_now == 0xFFFFFFFF) ) { |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2278 | res = do_syscall_select( n, (vki_fd_set*)rfds, |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2279 | (vki_fd_set*)wfds, |
| 2280 | (vki_fd_set*)xfds, |
| 2281 | (struct vki_timeval*)timeout); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2282 | if (is_kerror(res)) { |
| 2283 | * (__errno_location()) = -res; |
| 2284 | return -1; |
| 2285 | } else { |
| 2286 | return res; |
| 2287 | } |
| 2288 | } |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2289 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2290 | /* If a timeout was specified, set ms_end to be the end millisecond |
| 2291 | counter [wallclock] time. */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2292 | if (timeout) { |
| 2293 | res = my_do_syscall2(__NR_gettimeofday, (int)&t_now, (int)NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2294 | my_assert(res == 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2295 | ms_end = ms_now; |
| 2296 | ms_end += (timeout->tv_usec / 1000); |
| 2297 | ms_end += (timeout->tv_sec * 1000); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2298 | /* Stay sane ... */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2299 | my_assert (ms_end >= ms_now); |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | /* fprintf(stderr, "MY_SELECT: before loop\n"); */ |
| 2303 | |
| 2304 | /* Either timeout == NULL, meaning wait indefinitely, or timeout != |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2305 | NULL, in which case ms_end holds the end time. */ |
sewardj | 05bb2c9 | 2002-06-26 00:47:17 +0000 | [diff] [blame] | 2306 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2307 | while (1) { |
sewardj | 05bb2c9 | 2002-06-26 00:47:17 +0000 | [diff] [blame] | 2308 | |
| 2309 | /* First, do a return-immediately select(). */ |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2310 | |
| 2311 | /* These could be trashed each time round the loop, so restore |
| 2312 | them each time. */ |
| 2313 | if (rfds) rfds_copy = *rfds; |
| 2314 | if (wfds) wfds_copy = *wfds; |
| 2315 | if (xfds) xfds_copy = *xfds; |
| 2316 | |
| 2317 | zero_timeout.tv_sec = zero_timeout.tv_usec = 0; |
| 2318 | |
| 2319 | res = do_syscall_select( n, |
| 2320 | rfds ? (vki_fd_set*)(&rfds_copy) : NULL, |
| 2321 | wfds ? (vki_fd_set*)(&wfds_copy) : NULL, |
| 2322 | xfds ? (vki_fd_set*)(&xfds_copy) : NULL, |
| 2323 | & zero_timeout ); |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2324 | if (is_kerror(res)) { |
| 2325 | /* Some kind of error (including EINTR). Set errno and |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2326 | return. The sets are unspecified in this case. */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2327 | * (__errno_location()) = -res; |
| 2328 | return -1; |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2329 | } |
| 2330 | if (res > 0) { |
| 2331 | /* one or more fds is ready. Copy out resulting sets and |
| 2332 | return. */ |
| 2333 | if (rfds) *rfds = rfds_copy; |
| 2334 | if (wfds) *wfds = wfds_copy; |
| 2335 | if (xfds) *xfds = xfds_copy; |
| 2336 | return res; |
| 2337 | } |
sewardj | 05bb2c9 | 2002-06-26 00:47:17 +0000 | [diff] [blame] | 2338 | |
| 2339 | /* Nothing interesting happened, so we go to sleep for a |
| 2340 | while. */ |
| 2341 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2342 | /* fprintf(stderr, "MY_SELECT: nanosleep\n"); */ |
| 2343 | /* nanosleep and go round again */ |
sewardj | 02535bc | 2002-04-21 01:08:26 +0000 | [diff] [blame] | 2344 | nanosleep_interval.tv_sec = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2345 | nanosleep_interval.tv_nsec = 11 * 1000 * 1000; /* 11 milliseconds */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2346 | /* It's critical here that valgrind's nanosleep implementation |
| 2347 | is nonblocking. */ |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 2348 | res = my_do_syscall2(__NR_nanosleep, |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2349 | (int)(&nanosleep_interval), (int)NULL); |
sewardj | 645030e | 2002-06-06 01:27:39 +0000 | [diff] [blame] | 2350 | if (res == -VKI_EINTR) { |
| 2351 | /* The nanosleep was interrupted by a signal. So we do the |
| 2352 | same. */ |
| 2353 | * (__errno_location()) = EINTR; |
| 2354 | return -1; |
| 2355 | } |
sewardj | 05bb2c9 | 2002-06-26 00:47:17 +0000 | [diff] [blame] | 2356 | |
| 2357 | /* Sleeping finished. If a finite timeout, check to see if it |
| 2358 | has expired yet. */ |
| 2359 | if (timeout) { |
| 2360 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 2361 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 2362 | 0, 0, 0, 0); |
| 2363 | my_assert(ms_now != 0xFFFFFFFF); |
| 2364 | if (ms_now >= ms_end) { |
| 2365 | /* timeout; nothing interesting happened. */ |
| 2366 | if (rfds) FD_ZERO(rfds); |
| 2367 | if (wfds) FD_ZERO(wfds); |
| 2368 | if (xfds) FD_ZERO(xfds); |
| 2369 | return 0; |
| 2370 | } |
| 2371 | } |
| 2372 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2373 | } |
| 2374 | } |
| 2375 | |
| 2376 | |
| 2377 | |
| 2378 | |
| 2379 | #include <sys/poll.h> |
| 2380 | |
sewardj | 3e909ce | 2002-06-03 13:27:15 +0000 | [diff] [blame] | 2381 | #ifndef HAVE_NFDS_T |
sewardj | 72d5848 | 2002-04-24 02:20:20 +0000 | [diff] [blame] | 2382 | typedef unsigned long int nfds_t; |
| 2383 | #endif |
| 2384 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 2385 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 2386 | /* __attribute__((weak)) */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2387 | int poll (struct pollfd *__fds, nfds_t __nfds, int __timeout) |
| 2388 | { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2389 | unsigned int ms_now, ms_end; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2390 | int res, i; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2391 | struct vki_timespec nanosleep_interval; |
| 2392 | |
sewardj | d140e44 | 2002-05-29 01:21:19 +0000 | [diff] [blame] | 2393 | __my_pthread_testcancel(); |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2394 | ensure_valgrind("poll"); |
| 2395 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2396 | /* Detect the current time and simultaneously find out if we are |
| 2397 | running on Valgrind. */ |
| 2398 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 2399 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 2400 | 0, 0, 0, 0); |
| 2401 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2402 | if (/* CHECK SIZES FOR struct pollfd */ |
| 2403 | sizeof(struct timeval) != sizeof(struct vki_timeval)) |
| 2404 | barf("valgrind's hacky non-blocking poll(): data sizes error"); |
| 2405 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2406 | /* dummy initialisation to keep gcc -Wall happy */ |
| 2407 | ms_end = 0; |
| 2408 | |
| 2409 | /* If a zero timeout specified, this call is harmless. Also do |
| 2410 | this if not running on Valgrind. */ |
| 2411 | if (__timeout == 0 || ms_now == 0xFFFFFFFF) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2412 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, __timeout); |
| 2413 | if (is_kerror(res)) { |
| 2414 | * (__errno_location()) = -res; |
| 2415 | return -1; |
| 2416 | } else { |
| 2417 | return res; |
| 2418 | } |
| 2419 | } |
| 2420 | |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2421 | /* If a timeout was specified, set ms_end to be the end wallclock |
| 2422 | time. Easy considering that __timeout is in milliseconds. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2423 | if (__timeout > 0) { |
sewardj | 650ac00 | 2002-04-29 12:20:34 +0000 | [diff] [blame] | 2424 | ms_end = ms_now + (unsigned int)__timeout; |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
| 2427 | /* fprintf(stderr, "MY_POLL: before loop\n"); */ |
| 2428 | |
| 2429 | /* Either timeout < 0, meaning wait indefinitely, or timeout > 0, |
| 2430 | in which case t_end holds the end time. */ |
sewardj | 05bb2c9 | 2002-06-26 00:47:17 +0000 | [diff] [blame] | 2431 | |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2432 | my_assert(__timeout != 0); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2433 | |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2434 | while (1) { |
sewardj | 05bb2c9 | 2002-06-26 00:47:17 +0000 | [diff] [blame] | 2435 | |
| 2436 | /* Do a return-immediately poll. */ |
| 2437 | |
| 2438 | res = my_do_syscall3(__NR_poll, (int)__fds, __nfds, 0 ); |
| 2439 | if (is_kerror(res)) { |
| 2440 | /* Some kind of error. Set errno and return. */ |
| 2441 | * (__errno_location()) = -res; |
| 2442 | return -1; |
| 2443 | } |
| 2444 | if (res > 0) { |
| 2445 | /* One or more fds is ready. Return now. */ |
| 2446 | return res; |
| 2447 | } |
| 2448 | |
| 2449 | /* Nothing interesting happened, so we go to sleep for a |
| 2450 | while. */ |
| 2451 | |
| 2452 | /* fprintf(stderr, "MY_POLL: nanosleep\n"); */ |
| 2453 | /* nanosleep and go round again */ |
| 2454 | nanosleep_interval.tv_sec = 0; |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 2455 | nanosleep_interval.tv_nsec = 13 * 1000 * 1000; /* 13 milliseconds */ |
sewardj | 05bb2c9 | 2002-06-26 00:47:17 +0000 | [diff] [blame] | 2456 | /* It's critical here that valgrind's nanosleep implementation |
| 2457 | is nonblocking. */ |
| 2458 | (void)my_do_syscall2(__NR_nanosleep, |
| 2459 | (int)(&nanosleep_interval), (int)NULL); |
| 2460 | |
| 2461 | /* Sleeping finished. If a finite timeout, check to see if it |
| 2462 | has expired yet. */ |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2463 | if (__timeout > 0) { |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2464 | VALGRIND_MAGIC_SEQUENCE(ms_now, 0xFFFFFFFF /* default */, |
| 2465 | VG_USERREQ__READ_MILLISECOND_TIMER, |
| 2466 | 0, 0, 0, 0); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2467 | my_assert(ms_now != 0xFFFFFFFF); |
sewardj | 5f07b66 | 2002-04-23 16:52:51 +0000 | [diff] [blame] | 2468 | if (ms_now >= ms_end) { |
sewardj | f854f47 | 2002-04-21 12:19:41 +0000 | [diff] [blame] | 2469 | /* timeout; nothing interesting happened. */ |
| 2470 | for (i = 0; i < __nfds; i++) |
| 2471 | __fds[i].revents = 0; |
| 2472 | return 0; |
| 2473 | } |
| 2474 | } |
| 2475 | |
sewardj | 08a4c3f | 2002-04-13 03:45:44 +0000 | [diff] [blame] | 2476 | } |
| 2477 | } |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2478 | |
| 2479 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 2480 | /* Helper function used to make accept() non-blocking. Idea is to use |
| 2481 | the above nonblocking poll() to make this thread ONLY wait for the |
| 2482 | specified fd to become ready, and then return. */ |
sewardj | d1c1cf2 | 2002-06-26 00:13:36 +0000 | [diff] [blame] | 2483 | |
| 2484 | /* Sigh -- a hack. We're not supposed to include this file directly; |
| 2485 | should do it via /usr/include/fcntl.h, but that introduces a |
| 2486 | varargs prototype for fcntl itself, which we can't mimic. */ |
| 2487 | #define _FCNTL_H |
| 2488 | #include <bits/fcntl.h> |
| 2489 | |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 2490 | static void wait_for_fd_to_be_readable_or_erring ( int fd ) |
| 2491 | { |
| 2492 | struct pollfd pfd; |
sewardj | d1c1cf2 | 2002-06-26 00:13:36 +0000 | [diff] [blame] | 2493 | int res; |
| 2494 | |
sewardj | 6e6cbaa | 2002-05-24 02:12:52 +0000 | [diff] [blame] | 2495 | /* fprintf(stderr, "wait_for_fd_to_be_readable_or_erring %d\n", fd); */ |
sewardj | d1c1cf2 | 2002-06-26 00:13:36 +0000 | [diff] [blame] | 2496 | |
| 2497 | /* First check to see if the fd is nonblocking, and/or invalid. In |
| 2498 | either case return immediately. */ |
| 2499 | res = __libc_fcntl(fd, F_GETFL, 0); |
| 2500 | if (res == -1) return; /* fd is invalid somehow */ |
| 2501 | if (res & O_NONBLOCK) return; /* fd is nonblocking */ |
| 2502 | |
| 2503 | /* Ok, we'd better wait with poll. */ |
sewardj | 705d3cb | 2002-05-23 13:13:12 +0000 | [diff] [blame] | 2504 | pfd.fd = fd; |
| 2505 | pfd.events = POLLIN | POLLPRI | POLLERR | POLLHUP | POLLNVAL; |
| 2506 | /* ... but not POLLOUT, you may notice. */ |
| 2507 | pfd.revents = 0; |
| 2508 | (void)poll(&pfd, 1, -1 /* forever */); |
| 2509 | } |
| 2510 | |
| 2511 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 2512 | /* --------------------------------------------------------------------- |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2513 | Hacky implementation of semaphores. |
| 2514 | ------------------------------------------------------------------ */ |
| 2515 | |
| 2516 | #include <semaphore.h> |
| 2517 | |
| 2518 | /* This is a terrible way to do the remapping. Plan is to import an |
| 2519 | AVL tree at some point. */ |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2520 | |
| 2521 | typedef |
| 2522 | struct { |
| 2523 | pthread_mutex_t se_mx; |
| 2524 | pthread_cond_t se_cv; |
| 2525 | int count; |
| 2526 | } |
| 2527 | vg_sem_t; |
| 2528 | |
| 2529 | static pthread_mutex_t se_remap_mx = PTHREAD_MUTEX_INITIALIZER; |
| 2530 | |
| 2531 | static int se_remap_used = 0; |
| 2532 | static sem_t* se_remap_orig[VG_N_SEMAPHORES]; |
| 2533 | static vg_sem_t se_remap_new[VG_N_SEMAPHORES]; |
| 2534 | |
| 2535 | static vg_sem_t* se_remap ( sem_t* orig ) |
| 2536 | { |
| 2537 | int res, i; |
| 2538 | res = __pthread_mutex_lock(&se_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2539 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2540 | |
| 2541 | for (i = 0; i < se_remap_used; i++) { |
| 2542 | if (se_remap_orig[i] == orig) |
| 2543 | break; |
| 2544 | } |
| 2545 | if (i == se_remap_used) { |
| 2546 | if (se_remap_used == VG_N_SEMAPHORES) { |
| 2547 | res = pthread_mutex_unlock(&se_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2548 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2549 | barf("VG_N_SEMAPHORES is too low. Increase and recompile."); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2550 | } |
| 2551 | se_remap_used++; |
| 2552 | se_remap_orig[i] = orig; |
| 2553 | /* printf("allocated semaphore %d\n", i); */ |
| 2554 | } |
| 2555 | res = __pthread_mutex_unlock(&se_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2556 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2557 | return &se_remap_new[i]; |
| 2558 | } |
| 2559 | |
| 2560 | |
| 2561 | int sem_init(sem_t *sem, int pshared, unsigned int value) |
| 2562 | { |
| 2563 | int res; |
| 2564 | vg_sem_t* vg_sem; |
| 2565 | ensure_valgrind("sem_init"); |
| 2566 | if (pshared != 0) { |
sewardj | 4dced35 | 2002-06-04 22:54:20 +0000 | [diff] [blame] | 2567 | pthread_error("sem_init: unsupported pshared value"); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2568 | errno = ENOSYS; |
| 2569 | return -1; |
| 2570 | } |
| 2571 | vg_sem = se_remap(sem); |
| 2572 | res = pthread_mutex_init(&vg_sem->se_mx, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2573 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2574 | res = pthread_cond_init(&vg_sem->se_cv, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2575 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2576 | vg_sem->count = value; |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
| 2580 | |
| 2581 | int sem_wait ( sem_t* sem ) |
| 2582 | { |
| 2583 | int res; |
| 2584 | vg_sem_t* vg_sem; |
| 2585 | ensure_valgrind("sem_wait"); |
| 2586 | vg_sem = se_remap(sem); |
| 2587 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2588 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2589 | while (vg_sem->count == 0) { |
| 2590 | res = pthread_cond_wait(&vg_sem->se_cv, &vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2591 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2592 | } |
| 2593 | vg_sem->count--; |
| 2594 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2595 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2596 | return 0; |
| 2597 | } |
| 2598 | |
| 2599 | int sem_post ( sem_t* sem ) |
| 2600 | { |
| 2601 | int res; |
| 2602 | vg_sem_t* vg_sem; |
| 2603 | ensure_valgrind("sem_post"); |
| 2604 | vg_sem = se_remap(sem); |
| 2605 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2606 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2607 | if (vg_sem->count == 0) { |
| 2608 | vg_sem->count++; |
| 2609 | res = pthread_cond_broadcast(&vg_sem->se_cv); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2610 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2611 | } else { |
| 2612 | vg_sem->count++; |
| 2613 | } |
| 2614 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2615 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2616 | return 0; |
| 2617 | } |
| 2618 | |
| 2619 | |
| 2620 | int sem_trywait ( sem_t* sem ) |
| 2621 | { |
| 2622 | int ret, res; |
| 2623 | vg_sem_t* vg_sem; |
| 2624 | ensure_valgrind("sem_trywait"); |
| 2625 | vg_sem = se_remap(sem); |
| 2626 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2627 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2628 | if (vg_sem->count > 0) { |
| 2629 | vg_sem->count--; |
| 2630 | ret = 0; |
| 2631 | } else { |
| 2632 | ret = -1; |
| 2633 | errno = EAGAIN; |
| 2634 | } |
| 2635 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2636 | my_assert(res == 0); |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2637 | return ret; |
| 2638 | } |
| 2639 | |
| 2640 | |
| 2641 | int sem_getvalue(sem_t* sem, int * sval) |
| 2642 | { |
| 2643 | vg_sem_t* vg_sem; |
| 2644 | ensure_valgrind("sem_trywait"); |
| 2645 | vg_sem = se_remap(sem); |
| 2646 | *sval = vg_sem->count; |
| 2647 | return 0; |
| 2648 | } |
| 2649 | |
| 2650 | |
| 2651 | int sem_destroy(sem_t * sem) |
| 2652 | { |
| 2653 | kludged("sem_destroy"); |
| 2654 | /* if someone waiting on this semaphore, errno = EBUSY, return -1 */ |
| 2655 | return 0; |
| 2656 | } |
| 2657 | |
sewardj | 9ad92d9 | 2002-10-16 19:45:06 +0000 | [diff] [blame] | 2658 | |
| 2659 | int sem_timedwait(sem_t* sem, const struct timespec *abstime) |
| 2660 | { |
| 2661 | int res; |
| 2662 | vg_sem_t* vg_sem; |
| 2663 | ensure_valgrind("sem_timedwait"); |
| 2664 | vg_sem = se_remap(sem); |
| 2665 | res = __pthread_mutex_lock(&vg_sem->se_mx); |
| 2666 | my_assert(res == 0); |
| 2667 | while ( vg_sem->count == 0 && res != ETIMEDOUT ) { |
| 2668 | res = pthread_cond_timedwait(&vg_sem->se_cv, &vg_sem->se_mx, abstime); |
| 2669 | } |
| 2670 | if ( vg_sem->count > 0 ) { |
| 2671 | vg_sem->count--; |
| 2672 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 2673 | my_assert(res == 0 ); |
| 2674 | return 0; |
| 2675 | } else { |
| 2676 | res = __pthread_mutex_unlock(&vg_sem->se_mx); |
| 2677 | my_assert(res == 0 ); |
| 2678 | *(__errno_location()) = ETIMEDOUT; |
| 2679 | return -1; |
| 2680 | } |
| 2681 | } |
| 2682 | |
sewardj | 8f253ff | 2002-05-19 00:13:34 +0000 | [diff] [blame] | 2683 | |
| 2684 | /* --------------------------------------------------------------------- |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2685 | Reader-writer locks. |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2686 | ------------------------------------------------------------------ */ |
| 2687 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2688 | typedef |
| 2689 | struct { |
| 2690 | int initted; /* != 0 --> in use; sanity check only */ |
| 2691 | int prefer_w; /* != 0 --> prefer writer */ |
| 2692 | int nwait_r; /* # of waiting readers */ |
| 2693 | int nwait_w; /* # of waiting writers */ |
| 2694 | pthread_cond_t cv_r; /* for signalling readers */ |
| 2695 | pthread_cond_t cv_w; /* for signalling writers */ |
| 2696 | pthread_mutex_t mx; |
| 2697 | int status; |
| 2698 | /* allowed range for status: >= -1. -1 means 1 writer currently |
| 2699 | active, >= 0 means N readers currently active. */ |
| 2700 | } |
| 2701 | vg_rwlock_t; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2702 | |
| 2703 | |
| 2704 | static pthread_mutex_t rw_remap_mx = PTHREAD_MUTEX_INITIALIZER; |
| 2705 | |
| 2706 | static int rw_remap_used = 0; |
| 2707 | static pthread_rwlock_t* rw_remap_orig[VG_N_RWLOCKS]; |
| 2708 | static vg_rwlock_t rw_remap_new[VG_N_RWLOCKS]; |
| 2709 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2710 | |
| 2711 | static |
| 2712 | void init_vg_rwlock ( vg_rwlock_t* vg_rwl ) |
| 2713 | { |
| 2714 | int res = 0; |
| 2715 | vg_rwl->initted = 1; |
| 2716 | vg_rwl->prefer_w = 1; |
| 2717 | vg_rwl->nwait_r = 0; |
| 2718 | vg_rwl->nwait_w = 0; |
| 2719 | vg_rwl->status = 0; |
| 2720 | res = pthread_mutex_init(&vg_rwl->mx, NULL); |
| 2721 | res |= pthread_cond_init(&vg_rwl->cv_r, NULL); |
| 2722 | res |= pthread_cond_init(&vg_rwl->cv_w, NULL); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2723 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2727 | /* Take the address of a LinuxThreads rwlock_t and return the shadow |
| 2728 | address of our version. Further, if the LinuxThreads version |
| 2729 | appears to have been statically initialised, do the same to the one |
| 2730 | we allocate here. The pthread_rwlock_t.__rw_readers field is set |
| 2731 | to zero by PTHREAD_RWLOCK_INITIALIZER, so we take zero as meaning |
| 2732 | uninitialised and non-zero meaning initialised. |
| 2733 | */ |
| 2734 | static vg_rwlock_t* rw_remap ( pthread_rwlock_t* orig ) |
| 2735 | { |
| 2736 | int res, i; |
| 2737 | vg_rwlock_t* vg_rwl; |
| 2738 | res = __pthread_mutex_lock(&rw_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2739 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2740 | |
| 2741 | for (i = 0; i < rw_remap_used; i++) { |
| 2742 | if (rw_remap_orig[i] == orig) |
| 2743 | break; |
| 2744 | } |
| 2745 | if (i == rw_remap_used) { |
| 2746 | if (rw_remap_used == VG_N_RWLOCKS) { |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2747 | res = __pthread_mutex_unlock(&rw_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2748 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2749 | barf("VG_N_RWLOCKS is too low. Increase and recompile."); |
| 2750 | } |
| 2751 | rw_remap_used++; |
| 2752 | rw_remap_orig[i] = orig; |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2753 | rw_remap_new[i].initted = 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2754 | if (0) printf("allocated rwlock %d\n", i); |
| 2755 | } |
| 2756 | res = __pthread_mutex_unlock(&rw_remap_mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2757 | my_assert(res == 0); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2758 | vg_rwl = &rw_remap_new[i]; |
| 2759 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2760 | /* Initialise the shadow, if required. */ |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2761 | if (orig->__rw_readers == 0) { |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2762 | orig->__rw_readers = 1; |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2763 | init_vg_rwlock(vg_rwl); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2764 | if (orig->__rw_kind == PTHREAD_RWLOCK_PREFER_READER_NP) |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2765 | vg_rwl->prefer_w = 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | return vg_rwl; |
| 2769 | } |
| 2770 | |
| 2771 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2772 | int pthread_rwlock_init ( pthread_rwlock_t* orig, |
| 2773 | const pthread_rwlockattr_t* attr ) |
| 2774 | { |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2775 | vg_rwlock_t* rwl; |
| 2776 | if (0) printf ("pthread_rwlock_init\n"); |
| 2777 | /* Force the remapper to initialise the shadow. */ |
| 2778 | orig->__rw_readers = 0; |
| 2779 | /* Install the lock preference; the remapper needs to know it. */ |
| 2780 | orig->__rw_kind = PTHREAD_RWLOCK_DEFAULT_NP; |
| 2781 | if (attr) |
| 2782 | orig->__rw_kind = attr->__lockkind; |
| 2783 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2784 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2787 | |
| 2788 | static |
| 2789 | void pthread_rwlock_rdlock_CANCEL_HDLR ( void* rwl_v ) |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2790 | { |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2791 | vg_rwlock_t* rwl = (vg_rwlock_t*)rwl_v; |
| 2792 | rwl->nwait_r--; |
| 2793 | pthread_mutex_unlock (&rwl->mx); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2796 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2797 | int pthread_rwlock_rdlock ( pthread_rwlock_t* orig ) |
| 2798 | { |
| 2799 | int res; |
| 2800 | vg_rwlock_t* rwl; |
| 2801 | if (0) printf ("pthread_rwlock_rdlock\n"); |
| 2802 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2803 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2804 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2805 | if (!rwl->initted) { |
| 2806 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2807 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2808 | return EINVAL; |
| 2809 | } |
| 2810 | if (rwl->status < 0) { |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2811 | my_assert(rwl->status == -1); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2812 | rwl->nwait_r++; |
| 2813 | pthread_cleanup_push( pthread_rwlock_rdlock_CANCEL_HDLR, rwl ); |
| 2814 | while (1) { |
| 2815 | if (rwl->status == 0) break; |
| 2816 | res = pthread_cond_wait(&rwl->cv_r, &rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2817 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2818 | } |
| 2819 | pthread_cleanup_pop(0); |
| 2820 | rwl->nwait_r--; |
| 2821 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2822 | my_assert(rwl->status >= 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2823 | rwl->status++; |
| 2824 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2825 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2826 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2829 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2830 | int pthread_rwlock_tryrdlock ( pthread_rwlock_t* orig ) |
| 2831 | { |
| 2832 | int res; |
| 2833 | vg_rwlock_t* rwl; |
| 2834 | if (0) printf ("pthread_rwlock_tryrdlock\n"); |
| 2835 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2836 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2837 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2838 | if (!rwl->initted) { |
| 2839 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2840 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2841 | return EINVAL; |
| 2842 | } |
| 2843 | if (rwl->status == -1) { |
| 2844 | /* Writer active; we have to give up. */ |
| 2845 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2846 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2847 | return EBUSY; |
| 2848 | } |
| 2849 | /* Success */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2850 | my_assert(rwl->status >= 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2851 | rwl->status++; |
| 2852 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2853 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2854 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2855 | } |
| 2856 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2857 | |
| 2858 | static |
| 2859 | void pthread_rwlock_wrlock_CANCEL_HDLR ( void* rwl_v ) |
| 2860 | { |
| 2861 | vg_rwlock_t* rwl = (vg_rwlock_t*)rwl_v; |
| 2862 | rwl->nwait_w--; |
| 2863 | pthread_mutex_unlock (&rwl->mx); |
| 2864 | } |
| 2865 | |
| 2866 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2867 | int pthread_rwlock_wrlock ( pthread_rwlock_t* orig ) |
| 2868 | { |
| 2869 | int res; |
| 2870 | vg_rwlock_t* rwl; |
| 2871 | if (0) printf ("pthread_rwlock_wrlock\n"); |
| 2872 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2873 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2874 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2875 | if (!rwl->initted) { |
| 2876 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2877 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2878 | return EINVAL; |
| 2879 | } |
| 2880 | if (rwl->status != 0) { |
| 2881 | rwl->nwait_w++; |
| 2882 | pthread_cleanup_push( pthread_rwlock_wrlock_CANCEL_HDLR, rwl ); |
| 2883 | while (1) { |
| 2884 | if (rwl->status == 0) break; |
| 2885 | res = pthread_cond_wait(&rwl->cv_w, &rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2886 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2887 | } |
| 2888 | pthread_cleanup_pop(0); |
| 2889 | rwl->nwait_w--; |
| 2890 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2891 | my_assert(rwl->status == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2892 | rwl->status = -1; |
| 2893 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2894 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2895 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2896 | } |
| 2897 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2898 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2899 | int pthread_rwlock_trywrlock ( pthread_rwlock_t* orig ) |
| 2900 | { |
| 2901 | int res; |
| 2902 | vg_rwlock_t* rwl; |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2903 | if (0) printf ("pthread_wrlock_trywrlock\n"); |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2904 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2905 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2906 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2907 | if (!rwl->initted) { |
| 2908 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2909 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2910 | return EINVAL; |
| 2911 | } |
| 2912 | if (rwl->status != 0) { |
| 2913 | /* Reader(s) or a writer active; we have to give up. */ |
| 2914 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2915 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2916 | return EBUSY; |
| 2917 | } |
| 2918 | /* Success */ |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2919 | my_assert(rwl->status == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2920 | rwl->status = -1; |
| 2921 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2922 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2923 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2924 | } |
| 2925 | |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2926 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 2927 | int pthread_rwlock_unlock ( pthread_rwlock_t* orig ) |
| 2928 | { |
| 2929 | int res; |
| 2930 | vg_rwlock_t* rwl; |
| 2931 | if (0) printf ("pthread_rwlock_unlock\n"); |
| 2932 | rwl = rw_remap ( orig ); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2933 | rwl = rw_remap ( orig ); |
| 2934 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2935 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2936 | if (!rwl->initted) { |
| 2937 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2938 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2939 | return EINVAL; |
| 2940 | } |
| 2941 | if (rwl->status == 0) { |
| 2942 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2943 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2944 | return EPERM; |
| 2945 | } |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2946 | my_assert(rwl->status != 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2947 | if (rwl->status == -1) { |
| 2948 | rwl->status = 0; |
| 2949 | } else { |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2950 | my_assert(rwl->status > 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2951 | rwl->status--; |
| 2952 | } |
| 2953 | |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2954 | my_assert(rwl->status >= 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2955 | |
| 2956 | if (rwl->prefer_w) { |
| 2957 | |
| 2958 | /* Favour waiting writers, if any. */ |
| 2959 | if (rwl->nwait_w > 0) { |
| 2960 | /* Writer(s) are waiting. */ |
| 2961 | if (rwl->status == 0) { |
| 2962 | /* We can let a writer in. */ |
| 2963 | res = pthread_cond_signal(&rwl->cv_w); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2964 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2965 | } else { |
| 2966 | /* There are still readers active. Do nothing; eventually |
| 2967 | they will disappear, at which point a writer will be |
| 2968 | admitted. */ |
| 2969 | } |
| 2970 | } |
| 2971 | else |
| 2972 | /* No waiting writers. */ |
| 2973 | if (rwl->nwait_r > 0) { |
| 2974 | /* Let in a waiting reader. */ |
| 2975 | res = pthread_cond_signal(&rwl->cv_r); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2976 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | } else { |
| 2980 | |
| 2981 | /* Favour waiting readers, if any. */ |
| 2982 | if (rwl->nwait_r > 0) { |
| 2983 | /* Reader(s) are waiting; let one in. */ |
| 2984 | res = pthread_cond_signal(&rwl->cv_r); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2985 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2986 | } |
| 2987 | else |
| 2988 | /* No waiting readers. */ |
| 2989 | if (rwl->nwait_w > 0 && rwl->status == 0) { |
| 2990 | /* We have waiting writers and no active readers; let a |
| 2991 | writer in. */ |
| 2992 | res = pthread_cond_signal(&rwl->cv_w); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2993 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2994 | } |
| 2995 | } |
| 2996 | |
| 2997 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 2998 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 2999 | return 0; |
| 3000 | } |
| 3001 | |
| 3002 | |
| 3003 | int pthread_rwlock_destroy ( pthread_rwlock_t *orig ) |
| 3004 | { |
| 3005 | int res; |
| 3006 | vg_rwlock_t* rwl; |
| 3007 | if (0) printf ("pthread_rwlock_destroy\n"); |
| 3008 | rwl = rw_remap ( orig ); |
| 3009 | res = __pthread_mutex_lock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 3010 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 3011 | if (!rwl->initted) { |
| 3012 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 3013 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 3014 | return EINVAL; |
| 3015 | } |
| 3016 | if (rwl->status != 0 || rwl->nwait_r > 0 || rwl->nwait_w > 0) { |
| 3017 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 3018 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 3019 | return EBUSY; |
| 3020 | } |
| 3021 | rwl->initted = 0; |
| 3022 | res = __pthread_mutex_unlock(&rwl->mx); |
sewardj | 2d94c11 | 2002-06-03 01:25:54 +0000 | [diff] [blame] | 3023 | my_assert(res == 0); |
sewardj | 2d8b3f0 | 2002-06-01 14:14:19 +0000 | [diff] [blame] | 3024 | return 0; |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | |
sewardj | 47e4e31 | 2002-06-18 09:24:34 +0000 | [diff] [blame] | 3028 | /* Copied directly from LinuxThreads. */ |
| 3029 | int |
| 3030 | pthread_rwlockattr_init (pthread_rwlockattr_t *attr) |
| 3031 | { |
| 3032 | attr->__lockkind = 0; |
| 3033 | attr->__pshared = PTHREAD_PROCESS_PRIVATE; |
| 3034 | |
| 3035 | return 0; |
| 3036 | } |
| 3037 | |
sewardj | fe18eb8 | 2002-07-13 12:58:44 +0000 | [diff] [blame] | 3038 | /* Copied directly from LinuxThreads. */ |
| 3039 | int |
| 3040 | pthread_rwlockattr_setpshared (pthread_rwlockattr_t *attr, int pshared) |
| 3041 | { |
| 3042 | if (pshared != PTHREAD_PROCESS_PRIVATE && pshared != PTHREAD_PROCESS_SHARED) |
| 3043 | return EINVAL; |
| 3044 | |
| 3045 | /* For now it is not possible to shared a conditional variable. */ |
| 3046 | if (pshared != PTHREAD_PROCESS_PRIVATE) |
| 3047 | return ENOSYS; |
| 3048 | |
| 3049 | attr->__pshared = pshared; |
| 3050 | |
| 3051 | return 0; |
| 3052 | } |
| 3053 | |
sewardj | 47e4e31 | 2002-06-18 09:24:34 +0000 | [diff] [blame] | 3054 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 3055 | /* --------------------------------------------------------------------- |
sewardj | d5bef57 | 2002-10-23 21:49:33 +0000 | [diff] [blame] | 3056 | Make SYSV IPC not block everything |
| 3057 | ------------------------------------------------------------------ */ |
| 3058 | |
| 3059 | #include <sys/ipc.h> |
| 3060 | #include <sys/msg.h> |
| 3061 | #include <asm/ipc.h> /* for ipc_kludge */ |
| 3062 | |
| 3063 | static inline int sys_ipc(unsigned call, int first, int second, int third, void *ptr) |
| 3064 | { |
| 3065 | return my_do_syscall5(__NR_ipc, call, first, second, third, (int)ptr); |
| 3066 | } |
| 3067 | |
| 3068 | /* Turn a blocking msgsnd() into a polling non-blocking one, so that |
| 3069 | other threads make progress */ |
sewardj | f220ccc | 2002-10-23 21:53:49 +0000 | [diff] [blame^] | 3070 | int VGL_(msgsnd)(int msgid, const void *msgp, size_t msgsz, int msgflg) |
sewardj | d5bef57 | 2002-10-23 21:49:33 +0000 | [diff] [blame] | 3071 | { |
| 3072 | struct vki_timespec nanosleep_interval; |
| 3073 | int err; |
| 3074 | |
| 3075 | ensure_valgrind("msgsnd"); |
| 3076 | |
| 3077 | nanosleep_interval.tv_sec = 0; |
| 3078 | nanosleep_interval.tv_nsec = 13 * 1000 * 1000; /* 13 milliseconds */ |
| 3079 | |
| 3080 | if (msgflg & IPC_NOWAIT) { |
| 3081 | /* If we aren't blocking anyway, just do it */ |
| 3082 | err = sys_ipc(11, msgid, msgsz, msgflg, (void *)msgp); |
| 3083 | } else { |
| 3084 | /* Otherwise poll on the queue to let other things run */ |
| 3085 | for(;;) { |
| 3086 | err = sys_ipc(11, msgid, msgsz, msgflg | IPC_NOWAIT, (void *)msgp); |
| 3087 | |
| 3088 | if (err != -EAGAIN) |
| 3089 | break; |
| 3090 | |
| 3091 | (void)my_do_syscall2(__NR_nanosleep, |
| 3092 | (int)(&nanosleep_interval), (int)NULL); |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | if (is_kerror(err)) { |
| 3097 | *(__errno_location()) = -err; |
| 3098 | return -1; |
| 3099 | } |
| 3100 | return 0; |
| 3101 | } |
| 3102 | |
| 3103 | /* Turn a blocking msgrcv() into a polling non-blocking one, so that |
| 3104 | other threads make progress */ |
sewardj | f220ccc | 2002-10-23 21:53:49 +0000 | [diff] [blame^] | 3105 | int VGL_(msgrcv)( int msqid, void *msgp, size_t msgsz, long msgtyp, int msgflg ) |
sewardj | d5bef57 | 2002-10-23 21:49:33 +0000 | [diff] [blame] | 3106 | { |
| 3107 | struct vki_timespec nanosleep_interval; |
| 3108 | int err; |
| 3109 | struct ipc_kludge tmp; |
| 3110 | |
| 3111 | ensure_valgrind("msgrcv"); |
| 3112 | |
| 3113 | nanosleep_interval.tv_sec = 0; |
| 3114 | nanosleep_interval.tv_nsec = 13 * 1000 * 1000; /* 13 milliseconds */ |
| 3115 | |
| 3116 | tmp.msgp = msgp; |
| 3117 | tmp.msgtyp = msgtyp; |
| 3118 | |
| 3119 | if (msgflg & IPC_NOWAIT) { |
| 3120 | /* If we aren't blocking anyway, just do it */ |
| 3121 | err = sys_ipc(12, msqid, msgsz, msgflg, &tmp ); |
| 3122 | } else { |
| 3123 | /* Otherwise poll on the queue to let other things run */ |
| 3124 | for(;;) { |
| 3125 | err = sys_ipc(12, msqid, msgsz, msgflg | IPC_NOWAIT, &tmp ); |
| 3126 | |
| 3127 | if (err != -ENOMSG) |
| 3128 | break; |
| 3129 | |
| 3130 | (void)my_do_syscall2(__NR_nanosleep, |
| 3131 | (int)(&nanosleep_interval), (int)NULL); |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | if (is_kerror(err)) { |
| 3136 | *(__errno_location()) = -err; |
| 3137 | return -1; |
| 3138 | } |
| 3139 | |
| 3140 | return 0; |
| 3141 | } |
| 3142 | |
| 3143 | |
| 3144 | |
| 3145 | /* --------------------------------------------------------------------- |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3146 | B'stard. |
| 3147 | ------------------------------------------------------------------ */ |
| 3148 | |
| 3149 | # define strong_alias(name, aliasname) \ |
| 3150 | extern __typeof (name) aliasname __attribute__ ((alias (#name))); |
| 3151 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3152 | # define weak_alias(name, aliasname) \ |
| 3153 | extern __typeof (name) aliasname __attribute__ ((weak, alias (#name))); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3154 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3155 | strong_alias(__pthread_mutex_lock, pthread_mutex_lock) |
| 3156 | strong_alias(__pthread_mutex_trylock, pthread_mutex_trylock) |
| 3157 | strong_alias(__pthread_mutex_unlock, pthread_mutex_unlock) |
| 3158 | strong_alias(__pthread_mutexattr_init, pthread_mutexattr_init) |
| 3159 | weak_alias(__pthread_mutexattr_settype, pthread_mutexattr_settype) |
| 3160 | strong_alias(__pthread_mutex_init, pthread_mutex_init) |
| 3161 | strong_alias(__pthread_mutexattr_destroy, pthread_mutexattr_destroy) |
| 3162 | strong_alias(__pthread_mutex_destroy, pthread_mutex_destroy) |
| 3163 | strong_alias(__pthread_once, pthread_once) |
| 3164 | strong_alias(__pthread_atfork, pthread_atfork) |
| 3165 | strong_alias(__pthread_key_create, pthread_key_create) |
| 3166 | strong_alias(__pthread_getspecific, pthread_getspecific) |
| 3167 | strong_alias(__pthread_setspecific, pthread_setspecific) |
| 3168 | |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 3169 | #ifndef GLIBC_2_1 |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3170 | strong_alias(sigaction, __sigaction) |
sewardj | d529a44 | 2002-05-04 19:49:21 +0000 | [diff] [blame] | 3171 | #endif |
| 3172 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3173 | strong_alias(close, __close) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3174 | strong_alias(fcntl, __fcntl) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3175 | strong_alias(lseek, __lseek) |
| 3176 | strong_alias(open, __open) |
| 3177 | strong_alias(open64, __open64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3178 | strong_alias(read, __read) |
| 3179 | strong_alias(wait, __wait) |
| 3180 | strong_alias(write, __write) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3181 | strong_alias(connect, __connect) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3182 | strong_alias(send, __send) |
sewardj | 3fd559e | 2002-10-20 16:24:04 +0000 | [diff] [blame] | 3183 | strong_alias(poll, __poll) |
| 3184 | strong_alias(select, __select) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3185 | |
sewardj | 726c412 | 2002-05-16 23:39:10 +0000 | [diff] [blame] | 3186 | weak_alias (__pread64, pread64) |
sewardj | a18e210 | 2002-05-18 10:43:22 +0000 | [diff] [blame] | 3187 | weak_alias (__pwrite64, pwrite64) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3188 | weak_alias(__fork, fork) |
njn25 | e49d8e7 | 2002-09-23 09:36:25 +0000 | [diff] [blame] | 3189 | weak_alias(__vfork, vfork) |
sewardj | 7f6456d | 2002-05-21 00:51:21 +0000 | [diff] [blame] | 3190 | |
sewardj | f0b0645 | 2002-06-04 08:38:04 +0000 | [diff] [blame] | 3191 | weak_alias (__pthread_kill_other_threads_np, pthread_kill_other_threads_np) |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3192 | |
| 3193 | /*--------------------------------------------------*/ |
| 3194 | |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3195 | weak_alias(pthread_rwlock_rdlock, __pthread_rwlock_rdlock) |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3196 | weak_alias(pthread_rwlock_unlock, __pthread_rwlock_unlock) |
sewardj | 262b029 | 2002-05-01 00:03:16 +0000 | [diff] [blame] | 3197 | weak_alias(pthread_rwlock_wrlock, __pthread_rwlock_wrlock) |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 3198 | |
sewardj | a1ac5cb | 2002-05-27 13:00:05 +0000 | [diff] [blame] | 3199 | weak_alias(pthread_rwlock_destroy, __pthread_rwlock_destroy) |
| 3200 | weak_alias(pthread_rwlock_init, __pthread_rwlock_init) |
| 3201 | weak_alias(pthread_rwlock_tryrdlock, __pthread_rwlock_tryrdlock) |
| 3202 | weak_alias(pthread_rwlock_trywrlock, __pthread_rwlock_trywrlock) |
| 3203 | |
sewardj | 060b04f | 2002-04-26 21:01:13 +0000 | [diff] [blame] | 3204 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3205 | /* I've no idea what these are, but they get called quite a lot. |
| 3206 | Anybody know? */ |
| 3207 | |
| 3208 | #undef _IO_flockfile |
| 3209 | void _IO_flockfile ( _IO_FILE * file ) |
| 3210 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 3211 | pthread_mutex_lock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3212 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3213 | weak_alias(_IO_flockfile, flockfile); |
| 3214 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3215 | |
| 3216 | #undef _IO_funlockfile |
| 3217 | void _IO_funlockfile ( _IO_FILE * file ) |
| 3218 | { |
sewardj | 853f55d | 2002-04-26 00:27:53 +0000 | [diff] [blame] | 3219 | pthread_mutex_unlock(file->_lock); |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3220 | } |
sewardj | 5905fae | 2002-04-26 13:25:00 +0000 | [diff] [blame] | 3221 | weak_alias(_IO_funlockfile, funlockfile); |
| 3222 | |
sewardj | 3b13f0e | 2002-04-25 20:17:29 +0000 | [diff] [blame] | 3223 | |
sewardj | d4f2c71 | 2002-04-30 10:20:10 +0000 | [diff] [blame] | 3224 | /* This doesn't seem to be needed to simulate libpthread.so's external |
| 3225 | interface, but many people complain about its absence. */ |
| 3226 | |
| 3227 | strong_alias(__pthread_mutexattr_settype, __pthread_mutexattr_setkind_np) |
| 3228 | weak_alias(__pthread_mutexattr_setkind_np, pthread_mutexattr_setkind_np) |
sewardj | 439d45e | 2002-05-03 20:43:10 +0000 | [diff] [blame] | 3229 | |
| 3230 | |
| 3231 | /*--------------------------------------------------------------------*/ |
| 3232 | /*--- end vg_libpthread.c ---*/ |
| 3233 | /*--------------------------------------------------------------------*/ |