Commit 92e6342
libc: implement pthread_mutex_timedlock and pthread_mutex_clocklock
Both were stubs returning EINVAL, so code that uses a bounded mutex acquire
(common in databases and glibc-based libraries) could not run.
OSv's lockfree mutex has no native timed acquire, so implement them by polling
try_lock() with a short (50 us) sleep between attempts until the absolute
deadline, returning 0 on success or ETIMEDOUT when the deadline passes. The
uncontended case takes the try_lock() fast path with no sleeping.
pthread_mutex_clocklock supports CLOCK_REALTIME and CLOCK_MONOTONIC (converting
the deadline against the right clock) and returns EINVAL for any other clock; a
malformed timespec also returns EINVAL.
This is a poll-and-backoff acquire rather than a native timed wait, which is
fine for the rare contended timedlock; the lockfree mutex could grow a real
timed wait later if it ever needs to be tight.
Add tests/tst-pthread-timedlock.cc: uncontended success, contended timeout
(ETIMEDOUT from another thread while the main thread holds the lock, no
deadlock), success after unlock, CLOCK_MONOTONIC clocklock, and the EINVAL clock
path. Passes on OSv under KVM; tst-pthread continues to pass.
Fixes #834
Closes #14261 parent 3aba46c commit 92e6342
3 files changed
Lines changed: 120 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
452 | 452 | | |
453 | 453 | | |
454 | 454 | | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
| 470 | + | |
| 471 | + | |
| 472 | + | |
| 473 | + | |
| 474 | + | |
| 475 | + | |
| 476 | + | |
| 477 | + | |
| 478 | + | |
| 479 | + | |
| 480 | + | |
| 481 | + | |
| 482 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
455 | 493 | | |
456 | 494 | | |
457 | 495 | | |
458 | | - | |
459 | | - | |
| 496 | + | |
460 | 497 | | |
461 | 498 | | |
462 | 499 | | |
463 | 500 | | |
464 | 501 | | |
465 | 502 | | |
466 | | - | |
467 | | - | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
468 | 507 | | |
469 | 508 | | |
470 | 509 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
120 | 120 | | |
121 | 121 | | |
122 | 122 | | |
| 123 | + | |
123 | 124 | | |
124 | 125 | | |
125 | 126 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
0 commit comments