summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/signals2/test/threading_models_test.cpp
blob: d9bf00c2d6311b8a186663b70bcf5ff06912baf5 (plain)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// thread_safe_signals library
// basic test for alternate threading models

// Copyright Frank Mori Hess 2008
// Use, modification and
// distribution is subject to the Boost Software License, Version
// 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)

// For more information, see http://www.boost.org

// note boost/test/minimal.hpp can cause windows.h to get included, which
// can screw up our checks of _WIN32_WINNT if it is included
// after boost/signals2/mutex.hpp.  Frank Hess 2009-03-07.
#include <boost/test/minimal.hpp>

#include <boost/signals2.hpp>
#include <boost/thread/mutex.hpp>

// combiner that returns the number of slots invoked
struct slot_counter {
  typedef unsigned result_type;
  template<typename InputIterator>
  unsigned operator()(InputIterator first, InputIterator last) const
  {
    unsigned count = 0;
    for (; first != last; ++first)
    {
      try
      {
        *first;
        ++count;
      }
      catch(const boost::bad_weak_ptr &)
      {}
    }
    return count;
  }
};

void myslot()
{
}

template<typename signal_type>
void simple_test()
{
  signal_type sig;
  sig.connect(typename signal_type::slot_type(&myslot));
  BOOST_CHECK(sig() == 1);
  sig.disconnect(&myslot);
  BOOST_CHECK(sig() == 0);
}

class recursion_checking_dummy_mutex
{
  int recursion_count;
public:
  recursion_checking_dummy_mutex(): recursion_count(0)
  {}
  void lock() 
  { 
    BOOST_REQUIRE(recursion_count == 0);
    ++recursion_count;
  }
  bool try_lock() 
  { 
    lock(); 
    return true;
  }
  void unlock() 
  { 
    --recursion_count;
    BOOST_REQUIRE(recursion_count == 0);
  }
};

int test_main(int, char*[])
{
  typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
    boost::function<void (const boost::signals2::connection &)>, recursion_checking_dummy_mutex> sig0_rc_type;
  simple_test<sig0_rc_type>();
   typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
     boost::function<void (const boost::signals2::connection &)>, boost::mutex> sig0_mt_type;
   simple_test<sig0_mt_type>();
  typedef boost::signals2::signal<void (), slot_counter, int, std::less<int>, boost::function<void ()>,
    boost::function<void (const boost::signals2::connection &)>, boost::signals2::dummy_mutex> sig0_st_type;
  simple_test<sig0_st_type>();
  return 0;
}