boost/libs/interprocess/test/named_semaphore_test.cpp

148 lines
4.4 KiB
C++
Raw Permalink Normal View History

2018-01-12 21:47:58 +01:00
//////////////////////////////////////////////////////////////////////////////
//
// (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.
//
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/sync/named_semaphore.hpp>
#include <boost/interprocess/detail/interprocess_tester.hpp>
#include <boost/interprocess/exceptions.hpp>
#include <boost/date_time/posix_time/posix_time_types.hpp>
#include "named_creation_template.hpp"
#include "mutex_test_template.hpp"
#include "get_process_id_name.hpp"
2021-10-05 21:37:46 +02:00
#include <exception>
#if defined(BOOST_INTERPROCESS_WINDOWS)
#include <boost/interprocess/sync/windows/named_semaphore.hpp>
#endif
2018-01-12 21:47:58 +01:00
using namespace boost::interprocess;
static const std::size_t RecSemCount = 100;
//This wrapper is necessary to plug this class
//in lock tests
2021-10-05 21:37:46 +02:00
template<class NamedSemaphore>
2018-01-12 21:47:58 +01:00
class lock_test_wrapper
2021-10-05 21:37:46 +02:00
: public NamedSemaphore
2018-01-12 21:47:58 +01:00
{
public:
2021-10-05 21:37:46 +02:00
template <class CharT>
lock_test_wrapper(create_only_t, const CharT *name, unsigned int count = 1)
: NamedSemaphore(create_only, name, count)
2018-01-12 21:47:58 +01:00
{}
2021-10-05 21:37:46 +02:00
template <class CharT>
lock_test_wrapper(open_only_t, const CharT *name)
: NamedSemaphore(open_only, name)
2018-01-12 21:47:58 +01:00
{}
2021-10-05 21:37:46 +02:00
template <class CharT>
lock_test_wrapper(open_or_create_t, const CharT *name, unsigned int count = 1)
: NamedSemaphore(open_or_create, name, count)
2018-01-12 21:47:58 +01:00
{}
~lock_test_wrapper()
{}
void lock()
{ this->wait(); }
bool try_lock()
{ return this->try_wait(); }
bool timed_lock(const boost::posix_time::ptime &pt)
{ return this->timed_wait(pt); }
void unlock()
{ this->post(); }
};
//This wrapper is necessary to plug this class
//in recursive tests
2021-10-05 21:37:46 +02:00
template<class NamedSemaphore>
2018-01-12 21:47:58 +01:00
class recursive_test_wrapper
2021-10-05 21:37:46 +02:00
: public lock_test_wrapper<NamedSemaphore>
2018-01-12 21:47:58 +01:00
{
public:
recursive_test_wrapper(create_only_t, const char *name)
2021-10-05 21:37:46 +02:00
: lock_test_wrapper<NamedSemaphore>(create_only, name, RecSemCount)
2018-01-12 21:47:58 +01:00
{}
recursive_test_wrapper(open_only_t, const char *name)
2021-10-05 21:37:46 +02:00
: lock_test_wrapper<NamedSemaphore>(open_only, name)
2018-01-12 21:47:58 +01:00
{}
recursive_test_wrapper(open_or_create_t, const char *name)
2021-10-05 21:37:46 +02:00
: lock_test_wrapper<NamedSemaphore>(open_or_create, name, RecSemCount)
2018-01-12 21:47:58 +01:00
{}
};
2021-10-05 21:37:46 +02:00
template<class NamedSemaphore>
2018-01-12 21:47:58 +01:00
bool test_named_semaphore_specific()
{
2021-10-05 21:37:46 +02:00
NamedSemaphore::remove(test::get_process_id_name());
2018-01-12 21:47:58 +01:00
//Test persistance
{
2021-10-05 21:37:46 +02:00
NamedSemaphore sem(create_only, test::get_process_id_name(), 3);
2018-01-12 21:47:58 +01:00
}
{
2021-10-05 21:37:46 +02:00
NamedSemaphore sem(open_only, test::get_process_id_name());
2018-01-12 21:47:58 +01:00
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
sem.post();
}
{
2021-10-05 21:37:46 +02:00
NamedSemaphore sem(open_only, test::get_process_id_name());
2018-01-12 21:47:58 +01:00
BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
}
2021-10-05 21:37:46 +02:00
NamedSemaphore::remove(test::get_process_id_name());
2018-01-12 21:47:58 +01:00
return true;
}
2021-10-05 21:37:46 +02:00
template<class NamedSemaphore>
int test_named_semaphore()
2018-01-12 21:47:58 +01:00
{
2021-10-05 21:37:46 +02:00
int ret = 0;
2018-01-12 21:47:58 +01:00
try{
2021-10-05 21:37:46 +02:00
test::test_named_creation< test::named_sync_creation_test_wrapper<lock_test_wrapper<NamedSemaphore> > >();
#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
test::test_named_creation< test::named_sync_creation_test_wrapper_w<lock_test_wrapper<NamedSemaphore> > >();
#endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES)
test::test_all_lock< test::named_sync_wrapper<lock_test_wrapper<NamedSemaphore> > >();
test::test_all_mutex<test::named_sync_wrapper<lock_test_wrapper<NamedSemaphore> > >();
test::test_all_recursive_lock<test::named_sync_wrapper<recursive_test_wrapper<NamedSemaphore> > >();
test_named_semaphore_specific<NamedSemaphore>();
2018-01-12 21:47:58 +01:00
}
catch(std::exception &ex){
std::cout << ex.what() << std::endl;
2021-10-05 21:37:46 +02:00
ret = 1;
2018-01-12 21:47:58 +01:00
}
2021-10-05 21:37:46 +02:00
NamedSemaphore::remove(test::get_process_id_name());
return ret;
2018-01-12 21:47:58 +01:00
}
2021-10-05 21:37:46 +02:00
int main()
{
int ret;
#if defined(BOOST_INTERPROCESS_WINDOWS)
ret = test_named_semaphore<ipcdetail::winapi_named_semaphore>();
if (ret)
return ret;
#endif
ret = test_named_semaphore<named_semaphore>();
return ret;
}