named_sharable_mutex_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 "mutex_test_template.hpp"
  12. #include "sharable_mutex_test_template.hpp"
  13. #include "named_creation_template.hpp"
  14. #include <boost/interprocess/sync/named_sharable_mutex.hpp>
  15. #include <string>
  16. #include "get_process_id_name.hpp"
  17. using namespace boost::interprocess;
  18. struct mutex_deleter
  19. {
  20. ~mutex_deleter()
  21. { named_sharable_mutex::remove(test::get_process_id_name()); }
  22. };
  23. //This wrapper is necessary to have a default constructor
  24. //in generic mutex_test_template functions
  25. class named_sharable_mutex_lock_test_wrapper
  26. : public named_sharable_mutex
  27. {
  28. public:
  29. named_sharable_mutex_lock_test_wrapper()
  30. : named_sharable_mutex(open_or_create, test::get_process_id_name())
  31. { ++count_; }
  32. ~named_sharable_mutex_lock_test_wrapper()
  33. {
  34. if(--count_){
  35. ipcdetail::interprocess_tester::
  36. dont_close_on_destruction(static_cast<named_sharable_mutex&>(*this));
  37. }
  38. }
  39. static int count_;
  40. };
  41. int named_sharable_mutex_lock_test_wrapper::count_ = 0;
  42. //This wrapper is necessary to have a common constructor
  43. //in generic named_creation_template functions
  44. class named_sharable_mutex_creation_test_wrapper
  45. : public mutex_deleter, public named_sharable_mutex
  46. {
  47. public:
  48. named_sharable_mutex_creation_test_wrapper
  49. (create_only_t)
  50. : named_sharable_mutex(create_only, test::get_process_id_name())
  51. { ++count_; }
  52. named_sharable_mutex_creation_test_wrapper
  53. (open_only_t)
  54. : named_sharable_mutex(open_only, test::get_process_id_name())
  55. { ++count_; }
  56. named_sharable_mutex_creation_test_wrapper
  57. (open_or_create_t)
  58. : named_sharable_mutex(open_or_create, test::get_process_id_name())
  59. { ++count_; }
  60. ~named_sharable_mutex_creation_test_wrapper()
  61. {
  62. if(--count_){
  63. ipcdetail::interprocess_tester::
  64. dont_close_on_destruction(static_cast<named_sharable_mutex&>(*this));
  65. }
  66. }
  67. static int count_;
  68. };
  69. int named_sharable_mutex_creation_test_wrapper::count_ = 0;
  70. int main ()
  71. {
  72. try{
  73. named_sharable_mutex::remove(test::get_process_id_name());
  74. test::test_named_creation< test::named_sync_creation_test_wrapper<named_sharable_mutex> >();
  75. test::test_all_lock< test::named_sync_wrapper<named_sharable_mutex> >();
  76. test::test_all_mutex<test::named_sync_wrapper<named_sharable_mutex> >();
  77. test::test_all_sharable_mutex<test::named_sync_wrapper<named_sharable_mutex> >();
  78. }
  79. catch(std::exception &ex){
  80. named_sharable_mutex::remove(test::get_process_id_name());
  81. std::cout << ex.what() << std::endl;
  82. return 1;
  83. }
  84. named_sharable_mutex::remove(test::get_process_id_name());
  85. return 0;
  86. }
  87. #include <boost/interprocess/detail/config_end.hpp>