| 1 |
// { dg-options "-pthread" } |
| 2 |
// { dg-do run { target { *-*-linux* *-*-gnu* } } } |
| 3 |
// { dg-require-effective-target c++11 } |
| 4 |
// { dg-require-effective-target pthread } |
| 5 |
// { dg-require-gthreads "" } |
| 6 |
|
| 7 |
#include <condition_variable> |
| 8 |
#include <chrono> |
| 9 |
#include <mutex> |
| 10 |
#include <thread> |
| 11 |
|
| 12 |
// PR libstdc++/103382 |
| 13 |
|
| 14 |
template<typename F> |
| 15 |
void |
| 16 |
test_cancel(F wait) |
| 17 |
{ |
| 18 |
std::mutex m; |
| 19 |
std::condition_variable cv; |
| 20 |
bool waiting = false; |
| 21 |
|
| 22 |
std::thread t([&] { |
| 23 |
std::unique_lock<std::mutex> lock(m); |
| 24 |
waiting = true; |
| 25 |
wait(cv, lock); // __forced_unwind exception should not terminate process. |
| 26 |
}); |
| 27 |
|
| 28 |
// Ensure the condition variable is waiting before we cancel. |
| 29 |
// This shouldn't be necessary because pthread_mutex_lock is not |
| 30 |
// a cancellation point, but no harm in making sure we test what |
| 31 |
// we intend to test: that cancel during a wait doesn't abort. |
| 32 |
while (true) |
| 33 |
{ |
| 34 |
std::unique_lock<std::mutex> lock(m); |
| 35 |
if (waiting) |
| 36 |
break; |
| 37 |
} |
| 38 |
|
| 39 |
pthread_cancel(t.native_handle()); |
| 40 |
t.join(); |
| 41 |
} |
| 42 |
|
| 43 |
int main() |
| 44 |
{ |
| 45 |
test_cancel( |
| 46 |
[](std::condition_variable& cv, std::unique_lock<std::mutex>& l) { |
| 47 |
cv.wait(l); |
| 48 |
}); |
| 49 |
|
| 50 |
test_cancel( |
| 51 |
[](std::condition_variable& cv, std::unique_lock<std::mutex>& l) { |
| 52 |
cv.wait(l, []{ return false; }); |
| 53 |
}); |
| 54 |
|
| 55 |
using mins = std::chrono::minutes; |
| 56 |
|
| 57 |
test_cancel( |
| 58 |
[](std::condition_variable& cv, std::unique_lock<std::mutex>& l) { |
| 59 |
cv.wait_for(l, mins(1)); |
| 60 |
}); |
| 61 |
|
| 62 |
test_cancel( |
| 63 |
[](std::condition_variable& cv, std::unique_lock<std::mutex>& l) { |
| 64 |
cv.wait_until(l, std::chrono::system_clock::now() + mins(1)); |
| 65 |
}); |
| 66 |
} |