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