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