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