named_semaphore_test.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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/named_semaphore.hpp>
  12. #include <boost/interprocess/detail/interprocess_tester.hpp>
  13. #include <boost/interprocess/exceptions.hpp>
  14. #include <boost/date_time/posix_time/posix_time_types.hpp>
  15. #include "named_creation_template.hpp"
  16. #include "mutex_test_template.hpp"
  17. #include <string>
  18. #include "get_process_id_name.hpp"
  19. using namespace boost::interprocess;
  20. static const std::size_t RecSemCount = 100;
  21. static const char * SemName = test::get_process_id_name();
  22. //This wrapper is necessary to plug this class
  23. //in lock tests
  24. class lock_test_wrapper
  25. : public named_semaphore
  26. {
  27. public:
  28. lock_test_wrapper(create_only_t, const char *name, unsigned int count = 1)
  29. : named_semaphore(create_only, name, count)
  30. {}
  31. lock_test_wrapper(open_only_t, const char *name)
  32. : named_semaphore(open_only, name)
  33. {}
  34. lock_test_wrapper(open_or_create_t, const char *name, unsigned int count = 1)
  35. : named_semaphore(open_or_create, name, count)
  36. {}
  37. ~lock_test_wrapper()
  38. {}
  39. void lock()
  40. { this->wait(); }
  41. bool try_lock()
  42. { return this->try_wait(); }
  43. bool timed_lock(const boost::posix_time::ptime &pt)
  44. { return this->timed_wait(pt); }
  45. void unlock()
  46. { this->post(); }
  47. };
  48. //This wrapper is necessary to plug this class
  49. //in recursive tests
  50. class recursive_test_wrapper
  51. : public lock_test_wrapper
  52. {
  53. public:
  54. recursive_test_wrapper(create_only_t, const char *name)
  55. : lock_test_wrapper(create_only, name, RecSemCount)
  56. {}
  57. recursive_test_wrapper(open_only_t, const char *name)
  58. : lock_test_wrapper(open_only, name)
  59. {}
  60. recursive_test_wrapper(open_or_create_t, const char *name)
  61. : lock_test_wrapper(open_or_create, name, RecSemCount)
  62. {}
  63. };
  64. bool test_named_semaphore_specific()
  65. {
  66. //Test persistance
  67. {
  68. named_semaphore sem(create_only, SemName, 3);
  69. }
  70. {
  71. named_semaphore sem(open_only, SemName);
  72. BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
  73. BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
  74. BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
  75. BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
  76. sem.post();
  77. }
  78. {
  79. named_semaphore sem(open_only, SemName);
  80. BOOST_INTERPROCESS_CHECK(sem.try_wait() == true);
  81. BOOST_INTERPROCESS_CHECK(sem.try_wait() == false);
  82. }
  83. named_semaphore::remove(SemName);
  84. return true;
  85. }
  86. int main ()
  87. {
  88. try{
  89. named_semaphore::remove(SemName);
  90. test::test_named_creation< test::named_sync_creation_test_wrapper<lock_test_wrapper> >();
  91. test::test_all_lock< test::named_sync_wrapper<lock_test_wrapper> >();
  92. test::test_all_mutex<test::named_sync_wrapper<lock_test_wrapper> >();
  93. test::test_all_recursive_lock<test::named_sync_wrapper<recursive_test_wrapper> >();
  94. test_named_semaphore_specific();
  95. }
  96. catch(std::exception &ex){
  97. named_semaphore::remove(SemName);
  98. std::cout << ex.what() << std::endl;
  99. return 1;
  100. }
  101. named_semaphore::remove(SemName);
  102. return 0;
  103. }
  104. #include <boost/interprocess/detail/config_end.hpp>