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