file_lock_test.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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/detail/workaround.hpp>
  12. #include <boost/interprocess/sync/file_lock.hpp>
  13. #include <boost/interprocess/sync/scoped_lock.hpp>
  14. #include <boost/interprocess/file_mapping.hpp>
  15. #include <boost/date_time/posix_time/posix_time_types.hpp>
  16. #include "mutex_test_template.hpp"
  17. #include "sharable_mutex_test_template.hpp"
  18. #include "get_process_id_name.hpp"
  19. #include <fstream>
  20. #include <cstdio> //std::remove
  21. using namespace boost::interprocess;
  22. inline std::string get_filename()
  23. {
  24. std::string ret (ipcdetail::get_temporary_path());
  25. ret += "/";
  26. ret += test::get_process_id_name();
  27. return ret;
  28. }
  29. //This wrapper is necessary to have a default constructor
  30. //in generic mutex_test_template functions
  31. class file_lock_lock_test_wrapper
  32. : public boost::interprocess::file_lock
  33. {
  34. public:
  35. file_lock_lock_test_wrapper()
  36. : boost::interprocess::file_lock(get_filename().c_str())
  37. {}
  38. };
  39. int main ()
  40. {
  41. //Destroy and create file
  42. {
  43. std::remove(get_filename().c_str());
  44. std::ofstream file(get_filename().c_str());
  45. if(!file){
  46. return 1;
  47. }
  48. file_lock flock(get_filename().c_str());
  49. {
  50. scoped_lock<file_lock> sl(flock);
  51. }
  52. {
  53. scoped_lock<file_lock> sl(flock, try_to_lock);
  54. }
  55. {
  56. scoped_lock<file_lock> sl(flock, test::delay(1));
  57. }
  58. }
  59. {
  60. //Now test move semantics
  61. file_lock mapping(get_filename().c_str());
  62. file_lock move_ctor(boost::move(mapping));
  63. file_lock move_assign;
  64. move_assign = boost::move(move_ctor);
  65. mapping.swap(move_assign);
  66. }
  67. //test::test_all_lock<file_lock_lock_test_wrapper>();
  68. //test::test_all_mutex<file_lock_lock_test_wrapper>();
  69. //test::test_all_sharable_mutex<file_lock_lock_test_wrapper>();
  70. std::remove(get_filename().c_str());
  71. return 0;
  72. }
  73. #include <boost/interprocess/detail/config_end.hpp>