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