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