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