Imported Upstream version 1.57.0
[platform/upstream/boost.git] / libs / thread / test / test_2741.cpp
1 //  Copyright (C) 2008 Vicente J. Botet Escriba
2 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
3 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4
5 #define BOOST_THREAD_VERSION 2
6
7 #include <boost/thread/detail/config.hpp>
8
9 #include <boost/thread/thread_only.hpp>
10 #include <boost/thread/xtime.hpp>
11 #include <boost/bind.hpp>
12 #include <boost/ref.hpp>
13 #include <boost/utility.hpp>
14
15 #include <iostream>
16 #include <boost/test/unit_test.hpp>
17
18 #define DEFAULT_EXECUTION_MONITOR_TYPE execution_monitor::use_sleep_only
19 #include "./util.inl"
20
21 int test_value;
22 #ifdef PTHREAD_STACK_MIN
23 #define MY_PTHREAD_STACK PTHREAD_STACK_MIN
24 #else
25 #define MY_PTHREAD_STACK 4*0x4000
26 #endif
27 void simple_thread()
28 {
29   test_value = 999;
30 }
31
32 void test_native_handle()
33 {
34
35   boost::thread_attributes attrs;
36
37   boost::thread_attributes::native_handle_type* h = attrs.native_handle();
38 #if defined(BOOST_THREAD_PLATFORM_WIN32)
39   // ... window version
40 #elif defined(BOOST_THREAD_PLATFORM_PTHREAD)
41
42   int k = pthread_attr_setstacksize(h, MY_PTHREAD_STACK);
43   std::cout << k << std::endl;
44   BOOST_CHECK(!pthread_attr_setstacksize(h, MY_PTHREAD_STACK));
45   std::size_t res;
46   BOOST_CHECK(!pthread_attr_getstacksize(h, &res));
47   BOOST_CHECK(res >= (MY_PTHREAD_STACK));
48 #else
49 #error "Boost thread unavailable on this platform"
50 #endif
51
52 }
53
54 void test_stack_size()
55 {
56   boost::thread_attributes attrs;
57
58   attrs.set_stack_size(0x4000);
59   BOOST_CHECK(attrs.get_stack_size() >= 0x4000);
60
61 }
62
63 void do_test_creation_with_attrs()
64 {
65   test_value = 0;
66   boost::thread_attributes attrs;
67   attrs.set_stack_size(0x4000);
68   boost::thread thrd(attrs, &simple_thread);
69   thrd.join();
70   BOOST_CHECK_EQUAL(test_value, 999);
71 }
72
73 void test_creation_with_attrs()
74 {
75   timed_test(&do_test_creation_with_attrs, 1);
76 }
77
78 boost::unit_test_framework::test_suite* init_unit_test_suite(int, char*[])
79 {
80   boost::unit_test_framework::test_suite* test = BOOST_TEST_SUITE("Boost.Threads: thread attributes test suite");
81
82   test->add(BOOST_TEST_CASE(test_native_handle));
83   test->add(BOOST_TEST_CASE(test_stack_size));
84   test->add(BOOST_TEST_CASE(test_creation_with_attrs));
85
86   return test;
87 }