1 | // Test that yield primitives aren't cancelation points |
---|---|
2 | // |
3 | // Copyright 2023 Peter Dimov |
4 | // Distributed under the Boost Software License, Version 1.0. |
5 | // https://www.boost.org/LICENSE_1_0.txt |
6 | |
7 | #include <boost/core/yield_primitives.hpp> |
8 | #include <boost/core/lightweight_test.hpp> |
9 | #include <boost/config.hpp> |
10 | #include <boost/config/pragma_message.hpp> |
11 | |
12 | #if !defined(BOOST_HAS_PTHREADS) |
13 | |
14 | BOOST_PRAGMA_MESSAGE( "Skipping test because BOOST_HAS_PTHREADS is not defined") |
15 | int main() {} |
16 | |
17 | #else |
18 | |
19 | #include <pthread.h> |
20 | |
21 | pthread_mutex_t s_mx = PTHREAD_MUTEX_INITIALIZER; |
22 | |
23 | void* threadfunc( void* ) |
24 | { |
25 | pthread_mutex_lock( mutex: &s_mx ); |
26 | pthread_mutex_unlock( mutex: &s_mx ); |
27 | |
28 | boost::core::sp_thread_pause(); |
29 | boost::core::sp_thread_yield(); |
30 | boost::core::sp_thread_sleep(); |
31 | |
32 | return 0; |
33 | } |
34 | |
35 | int main() |
36 | { |
37 | pthread_mutex_lock( mutex: &s_mx ); |
38 | |
39 | pthread_t th; |
40 | pthread_create( newthread: &th, attr: 0, start_routine: threadfunc, arg: 0 ); |
41 | |
42 | pthread_cancel( th: th ); |
43 | |
44 | pthread_mutex_unlock( mutex: &s_mx ); |
45 | |
46 | void* r; |
47 | pthread_join( th: th, thread_return: &r ); |
48 | |
49 | BOOST_TEST_EQ( r, (void*)0 ); |
50 | |
51 | return boost::report_errors(); |
52 | } |
53 | |
54 | #endif |
55 |