123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394 |
- //////////////////////////////////////////////////////////////////////////////
- //
- // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under 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)
- //
- // See http://www.boost.org/libs/interprocess for documentation.
- //
- //////////////////////////////////////////////////////////////////////////////
- // Copyright (C) 2001-2003
- // William E. Kempf
- //
- // Permission to use, copy, modify, distribute and sell this software
- // and its documentation for any purpose is hereby granted without fee,
- // provided that the above copyright notice appear in all copies and
- // that both that copyright notice and this permission notice appear
- // in supporting documentation. William E. Kempf makes no representations
- // about the suitability of this software for any purpose.
- // It is provided "as is" without express or implied warranty.
- #ifndef BOOST_INTERPROCESS_TEST_MUTEX_TEST_TEMPLATE_HEADER
- #define BOOST_INTERPROCESS_TEST_MUTEX_TEST_TEMPLATE_HEADER
- #include <boost/interprocess/detail/config_begin.hpp>
- #include <boost/interprocess/exceptions.hpp>
- #include "boost_interprocess_check.hpp"
- #include "util.hpp"
- #include <boost/interprocess/detail/os_thread_functions.hpp>
- #include <boost/interprocess/sync/scoped_lock.hpp>
- #include <boost/date_time/posix_time/posix_time_types.hpp>
- #include <iostream>
- namespace boost { namespace interprocess { namespace test {
- template <typename M>
- struct test_lock
- {
- typedef M mutex_type;
- typedef boost::interprocess::scoped_lock<mutex_type> lock_type;
- void operator()()
- {
- mutex_type interprocess_mutex;
- // Test the lock's constructors.
- {
- lock_type lock(interprocess_mutex, boost::interprocess::defer_lock);
- BOOST_INTERPROCESS_CHECK(!lock);
- }
- lock_type lock(interprocess_mutex);
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- // Test the lock and unlock methods.
- lock.unlock();
- BOOST_INTERPROCESS_CHECK(!lock);
- lock.lock();
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- }
- };
- template <typename M>
- struct test_trylock
- {
- typedef M mutex_type;
- typedef boost::interprocess::scoped_lock<mutex_type> try_to_lock_type;
- void operator()()
- {
- mutex_type interprocess_mutex;
- // Test the lock's constructors.
- {
- try_to_lock_type lock(interprocess_mutex, boost::interprocess::try_to_lock);
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- }
- {
- try_to_lock_type lock(interprocess_mutex, boost::interprocess::defer_lock);
- BOOST_INTERPROCESS_CHECK(!lock);
- }
- try_to_lock_type lock(interprocess_mutex);
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- // Test the lock, unlock and trylock methods.
- lock.unlock();
- BOOST_INTERPROCESS_CHECK(!lock);
- lock.lock();
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- lock.unlock();
- BOOST_INTERPROCESS_CHECK(!lock);
- BOOST_INTERPROCESS_CHECK(lock.try_lock());
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- }
- };
- template <typename M>
- struct test_timedlock
- {
- typedef M mutex_type;
- typedef boost::interprocess::scoped_lock<mutex_type> timed_lock_type;
- void operator()()
- {
- mutex_type interprocess_mutex;
- // Test the lock's constructors.
- {
- // Construct and initialize an ptime for a fast time out.
- boost::posix_time::ptime pt = delay(1*BaseSeconds, 0);
- timed_lock_type lock(interprocess_mutex, pt);
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- }
- {
- timed_lock_type lock(interprocess_mutex, boost::interprocess::defer_lock);
- BOOST_INTERPROCESS_CHECK(!lock);
- }
- timed_lock_type lock(interprocess_mutex);
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- // Test the lock, unlock and timedlock methods.
- lock.unlock();
- BOOST_INTERPROCESS_CHECK(!lock);
- lock.lock();
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- lock.unlock();
- BOOST_INTERPROCESS_CHECK(!lock);
- boost::posix_time::ptime pt = delay(3*BaseSeconds, 0);
- BOOST_INTERPROCESS_CHECK(lock.timed_lock(pt));
- BOOST_INTERPROCESS_CHECK(lock ? true : false);
- }
- };
- template <typename M>
- struct test_recursive_lock
- {
- typedef M mutex_type;
- typedef boost::interprocess::scoped_lock<mutex_type> lock_type;
- void operator()()
- {
- mutex_type mx;
- {
- lock_type lock1(mx);
- lock_type lock2(mx);
- }
- {
- lock_type lock1(mx, defer_lock);
- lock_type lock2(mx, defer_lock);
- }
- {
- lock_type lock1(mx, try_to_lock);
- lock_type lock2(mx, try_to_lock);
- }
- {
- //This should always lock
- boost::posix_time::ptime pt = delay(2*BaseSeconds);
- lock_type lock1(mx, pt);
- lock_type lock2(mx, pt);
- }
- }
- };
- // plain_exclusive exercises the "infinite" lock for each
- // read_write_mutex type.
- template<typename M>
- void lock_and_sleep(void *arg, M &sm)
- {
- data<M> *pdata = static_cast<data<M>*>(arg);
- boost::interprocess::scoped_lock<M> l(sm);
- if(pdata->m_secs){
- boost::interprocess::ipcdetail::thread_sleep((1000*pdata->m_secs));
- }
- else{
- boost::interprocess::ipcdetail::thread_sleep((1000*2*BaseSeconds));
- }
- ++shared_val;
- pdata->m_value = shared_val;
- }
- template<typename M>
- void lock_and_catch_errors(void *arg, M &sm)
- {
- data<M> *pdata = static_cast<data<M>*>(arg);
- try
- {
- boost::interprocess::scoped_lock<M> l(sm);
- if(pdata->m_secs){
- boost::interprocess::ipcdetail::thread_sleep((1000*pdata->m_secs));
- }
- else{
- boost::interprocess::ipcdetail::thread_sleep((1000*2*BaseSeconds));
- }
- ++shared_val;
- pdata->m_value = shared_val;
- }
- catch(interprocess_exception const & e)
- {
- pdata->m_error = e.get_error_code();
- }
- }
- template<typename M>
- void try_lock_and_sleep(void *arg, M &sm)
- {
- data<M> *pdata = static_cast<data<M>*>(arg);
- boost::interprocess::scoped_lock<M> l(sm, boost::interprocess::defer_lock);
- if (l.try_lock()){
- boost::interprocess::ipcdetail::thread_sleep((1000*2*BaseSeconds));
- ++shared_val;
- pdata->m_value = shared_val;
- }
- }
- template<typename M>
- void timed_lock_and_sleep(void *arg, M &sm)
- {
- data<M> *pdata = static_cast<data<M>*>(arg);
- boost::posix_time::ptime pt(delay(pdata->m_secs));
- boost::interprocess::scoped_lock<M>
- l (sm, boost::interprocess::defer_lock);
- if (l.timed_lock(pt)){
- boost::interprocess::ipcdetail::thread_sleep((1000*2*BaseSeconds));
- ++shared_val;
- pdata->m_value = shared_val;
- }
- }
- template<typename M>
- void test_mutex_lock()
- {
- shared_val = 0;
- M mtx;
- data<M> d1(1);
- data<M> d2(2);
- // Locker one launches, holds the lock for 2*BaseSeconds seconds.
- boost::interprocess::ipcdetail::OS_thread_t tm1;
- boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&lock_and_sleep, &d1, mtx));
- //Wait 1*BaseSeconds
- boost::interprocess::ipcdetail::thread_sleep((1000*1*BaseSeconds));
- // Locker two launches, but it won't hold the lock for 2*BaseSeconds seconds.
- boost::interprocess::ipcdetail::OS_thread_t tm2;
- boost::interprocess::ipcdetail::thread_launch(tm2, thread_adapter<M>(&lock_and_sleep, &d2, mtx));
- //Wait completion
- boost::interprocess::ipcdetail::thread_join(tm1);
- boost::interprocess::ipcdetail::thread_sleep((1000*1*BaseSeconds));
- boost::interprocess::ipcdetail::thread_join(tm2);
- BOOST_INTERPROCESS_CHECK(d1.m_value == 1);
- BOOST_INTERPROCESS_CHECK(d2.m_value == 2);
- }
- template<typename M>
- void test_mutex_lock_timeout()
- {
- shared_val = 0;
- M mtx;
- int wait_time_s = BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS / 1000;
- if (wait_time_s == 0 )
- wait_time_s = 1;
- data<M> d1(1, wait_time_s * 3);
- data<M> d2(2, wait_time_s * 2);
- // Locker one launches, and holds the lock for wait_time_s * 2 seconds.
- boost::interprocess::ipcdetail::OS_thread_t tm1;
- boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&lock_and_sleep, &d1, mtx));
- //Wait 1*BaseSeconds
- boost::interprocess::ipcdetail::thread_sleep((1000*wait_time_s));
- // Locker two launches, and attempts to hold the lock for wait_time_s * 2 seconds.
- boost::interprocess::ipcdetail::OS_thread_t tm2;
- boost::interprocess::ipcdetail::thread_launch(tm2, thread_adapter<M>(&lock_and_catch_errors, &d2, mtx));
- //Wait completion
- boost::interprocess::ipcdetail::thread_join(tm1);
- boost::interprocess::ipcdetail::thread_sleep((1000*1*BaseSeconds));
- boost::interprocess::ipcdetail::thread_join(tm2);
- BOOST_INTERPROCESS_CHECK(d1.m_value == 1);
- BOOST_INTERPROCESS_CHECK(d2.m_value == -1);
- BOOST_INTERPROCESS_CHECK(d1.m_error == no_error);
- BOOST_INTERPROCESS_CHECK(d2.m_error == boost::interprocess::timeout_when_locking_error);
- }
- template<typename M>
- void test_mutex_try_lock()
- {
- shared_val = 0;
- M mtx;
- data<M> d1(1);
- data<M> d2(2);
- // Locker one launches, holds the lock for 2*BaseSeconds seconds.
- boost::interprocess::ipcdetail::OS_thread_t tm1;
- boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&try_lock_and_sleep, &d1, mtx));
- //Wait 1*BaseSeconds
- boost::interprocess::ipcdetail::thread_sleep((1000*1*BaseSeconds));
- // Locker two launches, but it should fail acquiring the lock
- boost::interprocess::ipcdetail::OS_thread_t tm2;
- boost::interprocess::ipcdetail::thread_launch(tm2, thread_adapter<M>(&try_lock_and_sleep, &d2, mtx));
- //Wait completion
- boost::interprocess::ipcdetail::thread_join(tm1);
- boost::interprocess::ipcdetail::thread_join(tm2);
- //Only the first should succeed locking
- BOOST_INTERPROCESS_CHECK(d1.m_value == 1);
- BOOST_INTERPROCESS_CHECK(d2.m_value == -1);
- }
- template<typename M>
- void test_mutex_timed_lock()
- {
- shared_val = 0;
- M mtx, m2;
- data<M> d1(1, 2*BaseSeconds);
- data<M> d2(2, 2*BaseSeconds);
- // Locker one launches, holds the lock for 2*BaseSeconds seconds.
- boost::interprocess::ipcdetail::OS_thread_t tm1;
- boost::interprocess::ipcdetail::thread_launch(tm1, thread_adapter<M>(&timed_lock_and_sleep, &d1, mtx));
- //Wait 1*BaseSeconds
- boost::interprocess::ipcdetail::thread_sleep((1000*1*BaseSeconds));
- // Locker two launches, holds the lock for 2*BaseSeconds seconds.
- boost::interprocess::ipcdetail::OS_thread_t tm2;
- boost::interprocess::ipcdetail::thread_launch(tm2, thread_adapter<M>(&timed_lock_and_sleep, &d2, mtx));
- //Wait completion
- boost::interprocess::ipcdetail::thread_join(tm1);
- boost::interprocess::ipcdetail::thread_join(tm2);
- //Both should succeed locking
- BOOST_INTERPROCESS_CHECK(d1.m_value == 1);
- BOOST_INTERPROCESS_CHECK(d2.m_value == 2);
- }
- template <typename M>
- inline void test_all_lock()
- {
- //Now generic interprocess_mutex tests
- std::cout << "test_lock<" << typeid(M).name() << ">" << std::endl;
- test_lock<M>()();
- std::cout << "test_trylock<" << typeid(M).name() << ">" << std::endl;
- test_trylock<M>()();
- std::cout << "test_timedlock<" << typeid(M).name() << ">" << std::endl;
- test_timedlock<M>()();
- }
- template <typename M>
- inline void test_all_recursive_lock()
- {
- //Now generic interprocess_mutex tests
- std::cout << "test_recursive_lock<" << typeid(M).name() << ">" << std::endl;
- test_recursive_lock<M>()();
- }
- template<typename M>
- void test_all_mutex()
- {
- std::cout << "test_mutex_lock<" << typeid(M).name() << ">" << std::endl;
- test_mutex_lock<M>();
- std::cout << "test_mutex_try_lock<" << typeid(M).name() << ">" << std::endl;
- test_mutex_try_lock<M>();
- std::cout << "test_mutex_timed_lock<" << typeid(M).name() << ">" << std::endl;
- test_mutex_timed_lock<M>();
- }
- }}} //namespace boost { namespace interprocess { namespace test {
- #include <boost/interprocess/detail/config_end.hpp>
- #endif //BOOST_INTERPROCESS_TEST_MUTEX_TEST_TEMPLATE_HEADER
|