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