1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2006-2009.
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org/libs/interprocess for documentation.
10 //////////////////////////////////////////////////////////////////////////////
11 #include <boost/interprocess/detail/config_begin.hpp>
12 #include <boost/interprocess/detail/workaround.hpp>
15 #include <boost/interprocess/managed_mapped_file.hpp>
16 #include <boost/interprocess/smart_ptr/shared_ptr.hpp>
17 #include <boost/interprocess/smart_ptr/weak_ptr.hpp>
20 #include "../test/get_process_id_name.hpp"
23 using namespace boost::interprocess;
25 //This is type of the object we want to share
29 //This is the type of a shared pointer to the previous type
30 //that will be built in the mapped file
31 typedef managed_shared_ptr<type_to_share, managed_mapped_file>::type shared_ptr_type;
32 typedef managed_weak_ptr<type_to_share, managed_mapped_file>::type weak_ptr_type;
34 //This is a type holding a shared pointer
35 struct shared_ptr_owner
37 shared_ptr_owner(const shared_ptr_type &other_shared_ptr)
38 : shared_ptr_(other_shared_ptr)
41 shared_ptr_owner(const shared_ptr_owner &other_owner)
42 : shared_ptr_(other_owner.shared_ptr_)
45 shared_ptr_type shared_ptr_;
54 std::string mapped_file(boost::interprocess::detail::get_temporary_path());
55 mapped_file += "/"; mapped_file += test::get_process_id_name();
56 const char *MappedFile = mapped_file.c_str();
59 const char *MappedFile = "MyMappedFile";
64 //Destroy any previous file with the name to be used.
67 file_remove(const char *MappedFile)
68 : MappedFile_(MappedFile) { file_mapping::remove(MappedFile_); }
69 ~file_remove(){ file_mapping::remove(MappedFile_); }
70 const char *MappedFile_;
71 } remover(MappedFile);
73 managed_mapped_file file(create_only, MappedFile, 65536);
75 //Construct the shared type in the file and
76 //pass ownership to this local shared pointer
77 shared_ptr_type local_shared_ptr = make_managed_shared_ptr
78 (file.construct<type_to_share>("object to share")(), file);
79 assert(local_shared_ptr.use_count() == 1);
81 //Share ownership of the object between local_shared_ptr and a new "owner1"
82 shared_ptr_owner *owner1 =
83 file.construct<shared_ptr_owner>("owner1")(local_shared_ptr);
84 assert(local_shared_ptr.use_count() == 2);
86 //local_shared_ptr releases object ownership
87 local_shared_ptr.reset();
88 assert(local_shared_ptr.use_count() == 0);
89 assert(owner1->shared_ptr_.use_count() == 1);
91 //Share ownership of the object between "owner1" and a new "owner2"
92 shared_ptr_owner *owner2 =
93 file.construct<shared_ptr_owner>("owner2")(*owner1);
94 assert(owner1->shared_ptr_.use_count() == 2);
95 assert(owner2->shared_ptr_.use_count() == 2);
96 assert(owner1->shared_ptr_.get() == owner2->shared_ptr_.get());
98 //The mapped file is unmapped here. Objects have been flushed to disk
101 //Reopen the mapped file and find again all owners
102 managed_mapped_file file(open_only, MappedFile);
104 shared_ptr_owner *owner1 = file.find<shared_ptr_owner>("owner1").first;
105 shared_ptr_owner *owner2 = file.find<shared_ptr_owner>("owner2").first;
106 assert(owner1 && owner2);
108 //Check everything is as expected
109 assert(file.find<type_to_share>("object to share").first != 0);
110 assert(owner1->shared_ptr_.use_count() == 2);
111 assert(owner2->shared_ptr_.use_count() == 2);
112 assert(owner1->shared_ptr_.get() == owner2->shared_ptr_.get());
114 //Now destroy one of the owners, the reference count drops.
115 file.destroy_ptr(owner1);
116 assert(owner2->shared_ptr_.use_count() == 1);
118 //Create a weak pointer
119 weak_ptr_type local_observer1(owner2->shared_ptr_);
120 assert(local_observer1.use_count() == owner2->shared_ptr_.use_count());
122 { //Create a local shared pointer from the weak pointer
123 shared_ptr_type local_shared_ptr = local_observer1.lock();
124 assert(local_observer1.use_count() == owner2->shared_ptr_.use_count());
125 assert(local_observer1.use_count() == 2);
128 //Now destroy the remaining owner. "object to share" will be destroyed
129 file.destroy_ptr(owner2);
130 assert(file.find<type_to_share>("object to share").first == 0);
133 assert(local_observer1.expired());
134 assert(local_observer1.use_count() == 0);
136 //The reference count will be deallocated when all weak pointers
137 //disappear. After that, the file is unmapped.
142 #include <boost/interprocess/detail/config_end.hpp>