semaphore_test.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2004-2012. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/interprocess for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #include <boost/interprocess/detail/config_begin.hpp>
  11. #include <boost/interprocess/sync/interprocess_semaphore.hpp>
  12. #include <boost/interprocess/exceptions.hpp>
  13. #include <boost/date_time/posix_time/posix_time_types.hpp>
  14. #include "named_creation_template.hpp"
  15. #include "mutex_test_template.hpp"
  16. static const std::size_t SemCount = 1;
  17. static const std::size_t RecSemCount = 100;
  18. //This wrapper is necessary to plug this class
  19. //in named creation tests and interprocess_mutex tests
  20. class semaphore_test_wrapper
  21. : public boost::interprocess::interprocess_semaphore
  22. {
  23. public:
  24. semaphore_test_wrapper()
  25. : boost::interprocess::interprocess_semaphore(SemCount)
  26. {}
  27. void lock()
  28. { this->wait(); }
  29. bool try_lock()
  30. { return this->try_wait(); }
  31. bool timed_lock(const boost::posix_time::ptime &pt)
  32. { return this->timed_wait(pt); }
  33. void unlock()
  34. { this->post(); }
  35. protected:
  36. semaphore_test_wrapper(int initial_count)
  37. : boost::interprocess::interprocess_semaphore(initial_count)
  38. {}
  39. };
  40. //This wrapper is necessary to plug this class
  41. //in recursive tests
  42. class recursive_semaphore_test_wrapper
  43. : public semaphore_test_wrapper
  44. {
  45. public:
  46. recursive_semaphore_test_wrapper()
  47. : semaphore_test_wrapper(RecSemCount)
  48. {}
  49. };
  50. int main ()
  51. {
  52. using namespace boost::interprocess;
  53. test::test_all_lock<semaphore_test_wrapper>();
  54. test::test_all_recursive_lock<recursive_semaphore_test_wrapper>();
  55. test::test_all_mutex<semaphore_test_wrapper>();
  56. return 0;
  57. }
  58. #include <boost/interprocess/detail/config_end.hpp>