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