From 4ed08b24a64dd548ec5eeb002ade5a98703d4796 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ion=20Gazta=C3=B1aga?= Date: Fri, 4 Aug 2006 19:14:25 +0000 Subject: [PATCH] First complete documentation version [SVN r34817] --- doc/code/Attic/Jamfile.v2 | 34 ++++++++++ doc/code/Attic/doc_anonymous_conditionA.cpp | 65 +++++++++++++++++++ doc/code/Attic/doc_anonymous_conditionB.cpp | 63 ++++++++++++++++++ .../doc_anonymous_condition_shared_data.hpp | 26 ++++++++ doc/code/Attic/doc_anonymous_mutexA.cpp | 64 ++++++++++++++++++ doc/code/Attic/doc_anonymous_mutexB.cpp | 55 ++++++++++++++++ .../Attic/doc_anonymous_mutex_shared_data.hpp | 22 +++++++ doc/code/Attic/doc_anonymous_semaphoreA.cpp | 55 ++++++++++++++++ doc/code/Attic/doc_anonymous_semaphoreB.cpp | 51 +++++++++++++++ .../doc_anonymous_semaphore_shared_data.hpp | 17 +++++ .../Attic/doc_anonymous_upgradable_mutexA.cpp | 64 ++++++++++++++++++ .../Attic/doc_anonymous_upgradable_mutexB.cpp | 55 ++++++++++++++++ doc/code/Attic/doc_bufferstream.cpp | 2 +- doc/code/Attic/doc_cont.cpp | 4 +- doc/code/Attic/doc_contA.cpp | 45 +++++++++++++ doc/code/Attic/doc_contB.cpp | 42 ++++++++++++ doc/code/Attic/doc_intrusive.cpp | 2 +- doc/code/Attic/doc_ipc_messageA.cpp | 10 +-- doc/code/Attic/doc_ipc_messageB.cpp | 8 +-- doc/code/Attic/doc_message_queueA.cpp | 32 +++++++++ doc/code/Attic/doc_message_queueB.cpp | 33 ++++++++++ doc/code/Attic/doc_named_allocA.cpp | 10 +-- doc/code/Attic/doc_named_allocB.cpp | 7 +- doc/code/Attic/doc_named_conditionA.cpp | 65 +++++++++++++++++++ doc/code/Attic/doc_named_conditionB.cpp | 63 ++++++++++++++++++ .../Attic/doc_named_condition_shared_data.hpp | 26 ++++++++ doc/code/Attic/doc_named_mutex.cpp | 32 +++++++++ doc/code/Attic/doc_offset_ptr.cpp | 6 +- doc/code/Attic/doc_scoped_ptr.cpp | 2 +- doc/code/Attic/doc_shared_memory.cpp | 42 ++++++++++++ doc/code/Attic/doc_shared_memory2.cpp | 43 ++++++++++++ .../doc_upgradable_mutex_shared_data.hpp | 22 +++++++ doc/code/Attic/doc_vectorstream.cpp | 2 +- doc/code/Attic/doc_where_allocate.cpp | 2 +- doc/code/Jamfile | 43 ------------ doc/code/Jamfile.v2 | 34 ++++++++++ doc/code/doc_anonymous_conditionA.cpp | 65 +++++++++++++++++++ doc/code/doc_anonymous_conditionB.cpp | 63 ++++++++++++++++++ .../doc_anonymous_condition_shared_data.hpp | 26 ++++++++ doc/code/doc_anonymous_mutexA.cpp | 64 ++++++++++++++++++ doc/code/doc_anonymous_mutexB.cpp | 55 ++++++++++++++++ doc/code/doc_anonymous_mutex_shared_data.hpp | 22 +++++++ doc/code/doc_anonymous_semaphoreA.cpp | 55 ++++++++++++++++ doc/code/doc_anonymous_semaphoreB.cpp | 51 +++++++++++++++ .../doc_anonymous_semaphore_shared_data.hpp | 17 +++++ doc/code/doc_anonymous_upgradable_mutexA.cpp | 64 ++++++++++++++++++ doc/code/doc_anonymous_upgradable_mutexB.cpp | 55 ++++++++++++++++ doc/code/doc_bufferstream.cpp | 2 +- doc/code/doc_cont.cpp | 4 +- doc/code/doc_contA.cpp | 45 +++++++++++++ doc/code/doc_contB.cpp | 42 ++++++++++++ doc/code/doc_cont_mapA.cpp | 40 ------------ doc/code/doc_cont_mapB.cpp | 39 ----------- doc/code/doc_intrusive.cpp | 2 +- doc/code/doc_ipc_messageA.cpp | 10 +-- doc/code/doc_ipc_messageB.cpp | 8 +-- doc/code/doc_message_queueA.cpp | 32 +++++++++ doc/code/doc_message_queueB.cpp | 33 ++++++++++ doc/code/doc_named_allocA.cpp | 10 +-- doc/code/doc_named_allocB.cpp | 7 +- doc/code/doc_named_conditionA.cpp | 65 +++++++++++++++++++ doc/code/doc_named_conditionB.cpp | 63 ++++++++++++++++++ doc/code/doc_named_condition_shared_data.hpp | 26 ++++++++ doc/code/doc_named_mutex.cpp | 32 +++++++++ doc/code/doc_offset_ptr.cpp | 6 +- doc/code/doc_scoped_ptr.cpp | 2 +- doc/code/doc_shared_memory.cpp | 42 ++++++++++++ doc/code/doc_shared_memory2.cpp | 43 ++++++++++++ doc/code/doc_upgradable_mutex_shared_data.hpp | 22 +++++++ doc/code/doc_vectorstream.cpp | 2 +- doc/code/doc_where_allocate.cpp | 2 +- 71 files changed, 2094 insertions(+), 170 deletions(-) create mode 100644 doc/code/Attic/Jamfile.v2 create mode 100644 doc/code/Attic/doc_anonymous_conditionA.cpp create mode 100644 doc/code/Attic/doc_anonymous_conditionB.cpp create mode 100644 doc/code/Attic/doc_anonymous_condition_shared_data.hpp create mode 100644 doc/code/Attic/doc_anonymous_mutexA.cpp create mode 100644 doc/code/Attic/doc_anonymous_mutexB.cpp create mode 100644 doc/code/Attic/doc_anonymous_mutex_shared_data.hpp create mode 100644 doc/code/Attic/doc_anonymous_semaphoreA.cpp create mode 100644 doc/code/Attic/doc_anonymous_semaphoreB.cpp create mode 100644 doc/code/Attic/doc_anonymous_semaphore_shared_data.hpp create mode 100644 doc/code/Attic/doc_anonymous_upgradable_mutexA.cpp create mode 100644 doc/code/Attic/doc_anonymous_upgradable_mutexB.cpp create mode 100644 doc/code/Attic/doc_contA.cpp create mode 100644 doc/code/Attic/doc_contB.cpp create mode 100644 doc/code/Attic/doc_message_queueA.cpp create mode 100644 doc/code/Attic/doc_message_queueB.cpp create mode 100644 doc/code/Attic/doc_named_conditionA.cpp create mode 100644 doc/code/Attic/doc_named_conditionB.cpp create mode 100644 doc/code/Attic/doc_named_condition_shared_data.hpp create mode 100644 doc/code/Attic/doc_named_mutex.cpp create mode 100644 doc/code/Attic/doc_shared_memory.cpp create mode 100644 doc/code/Attic/doc_shared_memory2.cpp create mode 100644 doc/code/Attic/doc_upgradable_mutex_shared_data.hpp delete mode 100644 doc/code/Jamfile create mode 100644 doc/code/Jamfile.v2 create mode 100644 doc/code/doc_anonymous_conditionA.cpp create mode 100644 doc/code/doc_anonymous_conditionB.cpp create mode 100644 doc/code/doc_anonymous_condition_shared_data.hpp create mode 100644 doc/code/doc_anonymous_mutexA.cpp create mode 100644 doc/code/doc_anonymous_mutexB.cpp create mode 100644 doc/code/doc_anonymous_mutex_shared_data.hpp create mode 100644 doc/code/doc_anonymous_semaphoreA.cpp create mode 100644 doc/code/doc_anonymous_semaphoreB.cpp create mode 100644 doc/code/doc_anonymous_semaphore_shared_data.hpp create mode 100644 doc/code/doc_anonymous_upgradable_mutexA.cpp create mode 100644 doc/code/doc_anonymous_upgradable_mutexB.cpp create mode 100644 doc/code/doc_contA.cpp create mode 100644 doc/code/doc_contB.cpp delete mode 100644 doc/code/doc_cont_mapA.cpp delete mode 100644 doc/code/doc_cont_mapB.cpp create mode 100644 doc/code/doc_message_queueA.cpp create mode 100644 doc/code/doc_message_queueB.cpp create mode 100644 doc/code/doc_named_conditionA.cpp create mode 100644 doc/code/doc_named_conditionB.cpp create mode 100644 doc/code/doc_named_condition_shared_data.hpp create mode 100644 doc/code/doc_named_mutex.cpp create mode 100644 doc/code/doc_shared_memory.cpp create mode 100644 doc/code/doc_shared_memory2.cpp create mode 100644 doc/code/doc_upgradable_mutex_shared_data.hpp diff --git a/doc/code/Attic/Jamfile.v2 b/doc/code/Attic/Jamfile.v2 new file mode 100644 index 0000000..7341f24 --- /dev/null +++ b/doc/code/Attic/Jamfile.v2 @@ -0,0 +1,34 @@ +# Boost Interprocess Library Documentation test Jamfile + +# (C) Copyright Ion Gaztaņaga 2006. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# Adapted from John Maddock's TR1 Jamfile.v2 +# Copyright John Maddock 2005. +# Use, modification and distribution are subject to the +# Boost Software License, Version 1.0. (See accompanying file +# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +# this rule enumerates through all the sources and invokes +# the run rule for each source, the result is a list of all +# the run rules, which we can pass on to the test_suite rule: + +rule test_all +{ + local all_rules = ; + + for local fileb in [ glob *.cpp ] + { + all_rules += [ compile $(fileb) + : # additional args + : # test-files + : # requirements + ] ; + } + + return $(all_rules) ; +} + +test-suite interprocess_doc : [ test_all r ] ; \ No newline at end of file diff --git a/doc/code/Attic/doc_anonymous_conditionA.cpp b/doc/code/Attic/doc_anonymous_conditionA.cpp new file mode 100644 index 0000000..65bfba5 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_conditionA.cpp @@ -0,0 +1,65 @@ + #include + #include + #include + #include + #include "doc_anonymous_condition_shared_data.hpp" + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (create_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Set size + shm.truncate(sizeof(trace_queue)); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Construct the shared structure in memory + trace_queue * data = new (addr) trace_queue; + + const int NumMsg = 100; + + for(int i = 0; i < NumMsg; ++i){ + scoped_lock lock(data->mutex); + if(data->message_in){ + data->cond_full.wait(lock); + } + if(i == (NumMsg-1)) + std::sprintf(data->items, "%s", "last message"); + else + std::sprintf(data->items, "%s_%d", "my_trace", i); + + //Notify to the other process that there is a message + data->cond_empty.notify_one(); + + //Mark message buffer as full + data->message_in = true; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_anonymous_conditionB.cpp b/doc/code/Attic/doc_anonymous_conditionB.cpp new file mode 100644 index 0000000..b359c47 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_conditionB.cpp @@ -0,0 +1,63 @@ + #include + #include + #include + #include + #include "doc_anonymous_condition_shared_data.hpp" + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (open_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Obtain a pointer to the shared structure + trace_queue * data = static_cast(addr); + + //Print messages until the other process marks the end + bool end_loop = false; + do{ + scoped_lock lock(data->mutex); + if(!data->message_in){ + data->cond_empty.wait(lock); + } + if(std::strcmp(data->items, "last message") == 0){ + end_loop = true; + } + else{ + //Print the message + std::cout << data->items << std::endl; + //Notify the other process that the buffer is empty + data->message_in = false; + data->cond_full.notify_one(); + } + } + while(!end_loop); + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_anonymous_condition_shared_data.hpp b/doc/code/Attic/doc_anonymous_condition_shared_data.hpp new file mode 100644 index 0000000..d61934f --- /dev/null +++ b/doc/code/Attic/doc_anonymous_condition_shared_data.hpp @@ -0,0 +1,26 @@ + #include + #include + + struct trace_queue + { + enum { LineSize = 100 }; + + trace_queue() + : message_in(false) + {} + + //Mutex to protect access to the queue + boost::interprocess::interprocess_mutex mutex; + + //Condition to wait when the queue is empty + boost::interprocess::interprocess_condition cond_empty; + + //Condition to wait when the queue is full + boost::interprocess::interprocess_condition cond_full; + + //Items to fill + char items[LineSize]; + + //Is there any message + bool message_in; + }; diff --git a/doc/code/Attic/doc_anonymous_mutexA.cpp b/doc/code/Attic/doc_anonymous_mutexA.cpp new file mode 100644 index 0000000..7644b67 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_mutexA.cpp @@ -0,0 +1,64 @@ + #include + #include + #include "doc_anonymous_mutex_shared_data.hpp" + #include + #include + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (create_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Set size + shm.truncate(sizeof(shared_memory_log)); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Construct the shared structure in memory + shared_memory_log * data = new (addr) shared_memory_log; + + //Write some logs + for(int i = 0; i < shared_memory_log::NumItems; ++i){ + //Lock the mutex + scoped_lock lock(data->mutex); + std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems] + ,"%s_%d", "process_a", i); + if(i == (shared_memory_log::NumItems-1)) + data->end_a = true; + //Mutex is released here + } + + //Wait until the other process ends + while(1){ + scoped_lock lock(data->mutex); + if(data->end_b) + break; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_anonymous_mutexB.cpp b/doc/code/Attic/doc_anonymous_mutexB.cpp new file mode 100644 index 0000000..03bcec0 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_mutexB.cpp @@ -0,0 +1,55 @@ + #include + #include + #include "doc_anonymous_mutex_shared_data.hpp" + #include + #include + + using namespace boost::interprocess; + + int main () + { + try{ + //Open the shared memory object. + shared_memory_object shm + (open_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Construct the shared structure in memory + shared_memory_log * data = static_cast(addr); + + //Write some logs + for(int i = 0; i < 100; ++i){ + //Lock the mutex + scoped_lock lock(data->mutex); + std::sprintf(data->items[(data->current_line++) % shared_memory_log::NumItems] + ,"%s_%d", "process_a", i); + if(i == (shared_memory_log::NumItems-1)) + data->end_b = true; + //Mutex is released here + } + + //Wait until the other process ends + while(1){ + scoped_lock lock(data->mutex); + if(data->end_a) + break; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + return 0; + } diff --git a/doc/code/Attic/doc_anonymous_mutex_shared_data.hpp b/doc/code/Attic/doc_anonymous_mutex_shared_data.hpp new file mode 100644 index 0000000..3bee481 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_mutex_shared_data.hpp @@ -0,0 +1,22 @@ + #include + + struct shared_memory_log + { + enum { NumItems = 100 }; + enum { LineSize = 100 }; + + shared_memory_log() + : current_line(0) + , end_a(false) + , end_b(false) + {} + + //Mutex to protect access to the queue + boost::interprocess::interprocess_mutex mutex; + + //Items to fill + char items[NumItems][LineSize]; + int current_line; + bool end_a; + bool end_b; + }; diff --git a/doc/code/Attic/doc_anonymous_semaphoreA.cpp b/doc/code/Attic/doc_anonymous_semaphoreA.cpp new file mode 100644 index 0000000..296a596 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_semaphoreA.cpp @@ -0,0 +1,55 @@ + #include + #include + #include "doc_anonymous_semaphore_shared_data.hpp" + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (create_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Set size + shm.truncate(sizeof(shared_memory_buffer)); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Construct the shared structure in memory + shared_memory_buffer * data = new (addr) shared_memory_buffer; + + const int NumMsg = 100; + + //Insert data in the array + for(int i = 0; i < NumMsg; ++i){ + data->nempty.wait(); + data->mutex.wait(); + data->items[i % shared_memory_buffer::NumItems] = i; + data->mutex.post(); + data->nstored.post(); + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_anonymous_semaphoreB.cpp b/doc/code/Attic/doc_anonymous_semaphoreB.cpp new file mode 100644 index 0000000..2ffa6b4 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_semaphoreB.cpp @@ -0,0 +1,51 @@ + #include + #include + #include "doc_anonymous_semaphore_shared_data.hpp" + + using namespace boost::interprocess; + + int main () + { + try{ + //Create a shared memory object. + shared_memory_object shm + (open_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Obtain the shared structure + shared_memory_buffer * data = static_cast(addr); + + const int NumMsg = 100; + + int extracted_data [NumMsg]; + + //Extract the data + for(int i = 0; i < NumMsg; ++i){ + data->nstored.wait(); + data->mutex.wait(); + extracted_data[i] = data->items[i % shared_memory_buffer::NumItems]; + data->mutex.post(); + data->nempty.post(); + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_anonymous_semaphore_shared_data.hpp b/doc/code/Attic/doc_anonymous_semaphore_shared_data.hpp new file mode 100644 index 0000000..e1fcd3b --- /dev/null +++ b/doc/code/Attic/doc_anonymous_semaphore_shared_data.hpp @@ -0,0 +1,17 @@ + #include + + struct shared_memory_buffer + { + enum { NumItems = 10 }; + + shared_memory_buffer() + : mutex(1), nempty(NumItems), nstored(0) + {} + + //Semaphores to protect and synchronize access + boost::interprocess::interprocess_semaphore + mutex, nempty, nstored; + + //Items to fill + int items[NumItems]; + }; diff --git a/doc/code/Attic/doc_anonymous_upgradable_mutexA.cpp b/doc/code/Attic/doc_anonymous_upgradable_mutexA.cpp new file mode 100644 index 0000000..1d10f20 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_upgradable_mutexA.cpp @@ -0,0 +1,64 @@ + #include + #include + #include "doc_upgradable_mutex_shared_data.hpp" + #include + #include + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (create_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Set size + shm.truncate(sizeof(shared_data)); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Construct the shared structure in memory + shared_data * data = new (addr) shared_data; + + //Write some logs + for(int i = 0; i < shared_data::NumItems; ++i){ + //Lock the upgradable_mutex + scoped_lock lock(data->upgradable_mutex); + std::sprintf(data->items[(data->current_line++) % shared_data::NumItems] + ,"%s_%d", "process_a", i); + if(i == (shared_data::NumItems-1)) + data->end_a = true; + //Mutex is released here + } + + //Wait until the other process ends + while(1){ + scoped_lock lock(data->upgradable_mutex); + if(data->end_b) + break; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_anonymous_upgradable_mutexB.cpp b/doc/code/Attic/doc_anonymous_upgradable_mutexB.cpp new file mode 100644 index 0000000..72812d6 --- /dev/null +++ b/doc/code/Attic/doc_anonymous_upgradable_mutexB.cpp @@ -0,0 +1,55 @@ + #include + #include + #include "doc_upgradable_mutex_shared_data.hpp" + #include + #include + + using namespace boost::interprocess; + + int main () + { + try{ + //Open the shared memory object. + shared_memory_object shm + (open_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Construct the shared structure in memory + shared_data * data = static_cast(addr); + + //Write some logs + for(int i = 0; i < 100; ++i){ + //Lock the upgradable_mutex + scoped_lock lock(data->upgradable_mutex); + std::sprintf(data->items[(data->current_line++) % shared_data::NumItems] + ,"%s_%d", "process_a", i); + if(i == (shared_data::NumItems-1)) + data->end_b = true; + //Mutex is released here + } + + //Wait until the other process ends + while(1){ + scoped_lock lock(data->upgradable_mutex); + if(data->end_a) + break; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + return 0; + } diff --git a/doc/code/Attic/doc_bufferstream.cpp b/doc/code/Attic/doc_bufferstream.cpp index 8506890..79fb25e 100644 --- a/doc/code/Attic/doc_bufferstream.cpp +++ b/doc/code/Attic/doc_bufferstream.cpp @@ -10,7 +10,7 @@ int main () { - managed_shared_memory::remove("MySharedMemory"); + shared_memory_object::remove("MySharedMemory"); //Create shared memory managed_shared_memory segment(create_only, "MySharedMemory", //segment name diff --git a/doc/code/Attic/doc_cont.cpp b/doc/code/Attic/doc_cont.cpp index a481591..c3d7d49 100644 --- a/doc/code/Attic/doc_cont.cpp +++ b/doc/code/Attic/doc_cont.cpp @@ -8,8 +8,8 @@ { using namespace boost::interprocess; - managed_shared_memory::remove("MySharedMemory"); - //Shared memory front-end that is able to construct objects + shared_memory_object::remove("MySharedMemory"); + //A managed shared memory where we can construct objects //associated with a c-string managed_shared_memory segment(create_only, "MySharedMemory", //segment name diff --git a/doc/code/Attic/doc_contA.cpp b/doc/code/Attic/doc_contA.cpp new file mode 100644 index 0000000..f8da9eb --- /dev/null +++ b/doc/code/Attic/doc_contA.cpp @@ -0,0 +1,45 @@ + #include + #include + + #include + #include + #include + + int main () + { + using namespace boost::interprocess; + //Shared memory front-end that is able to construct objects + //associated with a c-string. Erase previous shared memory with the name + //to be used and create the memory segment at the specified address and initialize resources + shared_memory_object::remove("MySharedMemory"); + managed_shared_memory segment + (create_only + ,"MySharedMemory" //segment name + ,65536); //segment size in bytes + + //Alias an STL compatible allocator of ints that allocates ints from the managed + //shared memory segment. This allocator will allow to place containers + //in managed shared memory segments + typedef allocator + ShmemAllocator; + + //Alias a vector that uses the previous STL-like allocator + typedef vector MyVector; + + //Initialize shared memory STL-compatible allocator + const ShmemAllocator alloc_inst (segment.get_segment_manager()); + + //Construct a shared memory vector vector + MyVector *myvector = + segment.construct("MyVector") //object name + (alloc_inst);//first ctor parameter + + //Insert data in the vector + for(int i = 0; i < 100; ++i){ + myvector->push_back(i); + } + + return 0; + } + + #include diff --git a/doc/code/Attic/doc_contB.cpp b/doc/code/Attic/doc_contB.cpp new file mode 100644 index 0000000..5a422eb --- /dev/null +++ b/doc/code/Attic/doc_contB.cpp @@ -0,0 +1,42 @@ + #include + #include + + #include + #include + #include + #include + + int main () + { + using namespace boost::interprocess; + + //An special shared memory where we can + //construct objects associated with a name. + //Connect to the already created shared memory segment + //and initialize needed resources + managed_shared_memory segment + (open_only + ,"MySharedMemory"); //segment name + + //Alias an STL compatible allocator of ints that allocates ints from the managed + //shared memory segment. This allocator will allow to place containers + //in managed shared memory segments + typedef allocator + ShmemAllocator; + + //Alias a vector that uses the previous STL-like allocator + typedef vector MyVector; + + //Find the vector using the c-string name + MyVector *myvector = segment.find("MyVector").first; + + //Use vector in reverse order + std::sort(myvector->rbegin(), myvector->rend()); + // . . . + + //When done, destroy the vector from the segment + segment.destroy("MyVector"); + return 0; + } + + #include diff --git a/doc/code/Attic/doc_intrusive.cpp b/doc/code/Attic/doc_intrusive.cpp index ec1a0aa..6ee2902 100644 --- a/doc/code/Attic/doc_intrusive.cpp +++ b/doc/code/Attic/doc_intrusive.cpp @@ -59,7 +59,7 @@ int main () { - managed_shared_memory::remove("my_shmem"); + shared_memory_object::remove("my_shmem"); //Create shared memory managed_shared_memory shmem(create_only, "my_shmem", 10000); //Create the unique reference counted object in shared memory diff --git a/doc/code/Attic/doc_ipc_messageA.cpp b/doc/code/Attic/doc_ipc_messageA.cpp index 760e554..6e9dc8a 100644 --- a/doc/code/Attic/doc_ipc_messageA.cpp +++ b/doc/code/Attic/doc_ipc_messageA.cpp @@ -2,16 +2,16 @@ #include #include - #include int main () { using namespace boost::interprocess; - //A shared memory front-end that is able to - //allocate raw memory buffers from a shared memory segment - //Create the shared memory segment and initialize needed resources - managed_shared_memory::remove("MySharedMemory"); + //An special shared memory from which we are + //able to allocate raw memory buffers. + //First remove any old shared memory of the same name, create + //the shared memory segment and initialize needed resources + shared_memory_object::remove("MySharedMemory"); managed_shared_memory segment (create_only, "MySharedMemory", //segment name diff --git a/doc/code/Attic/doc_ipc_messageB.cpp b/doc/code/Attic/doc_ipc_messageB.cpp index 50aa8bf..7ab3d13 100644 --- a/doc/code/Attic/doc_ipc_messageB.cpp +++ b/doc/code/Attic/doc_ipc_messageB.cpp @@ -2,15 +2,15 @@ #include #include - #include int main () { using namespace boost::interprocess; - //A shared memory front-end that is able to - //allocate raw memory buffers from a shared memory segment - //Connect to the shared memory segment and initialize needed resources + //An special shared memory from which we are + //able to allocate raw memory buffers. + //Connect to the already created shared memory segment + //and initialize needed resources managed_shared_memory segment(open_only, "MySharedMemory"); //segment name //An handle from the base address can identify any byte of the shared diff --git a/doc/code/Attic/doc_message_queueA.cpp b/doc/code/Attic/doc_message_queueA.cpp new file mode 100644 index 0000000..1e4c30e --- /dev/null +++ b/doc/code/Attic/doc_message_queueA.cpp @@ -0,0 +1,32 @@ + #include + #include + #include + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous message queue + message_queue::remove("message_queue"); + + //Create a message_queue. + message_queue mq + (create_only //only create + ,"message_queue" //name + ,100 //max message number + ,sizeof(int) //max message size + ); + + //Send 100 numbers + for(int i = 0; i < 100; ++i){ + mq.send(&i, sizeof(i), 0); + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + return 0; + } diff --git a/doc/code/Attic/doc_message_queueB.cpp b/doc/code/Attic/doc_message_queueB.cpp new file mode 100644 index 0000000..989f341 --- /dev/null +++ b/doc/code/Attic/doc_message_queueB.cpp @@ -0,0 +1,33 @@ + #include + #include + #include + + using namespace boost::interprocess; + + int main () + { + try{ + //Open a message queue. + message_queue mq + (open_only //only create + ,"message_queue" //name + ); + + unsigned int priority; + unsigned int recvd_size; + + //Receive 100 numbers + for(int i = 0; i < 100; ++i){ + int number; + mq.receive(&number, sizeof(number), recvd_size, priority); + if(number != i || recvd_size != sizeof(number)) + return 1; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + return 0; + } diff --git a/doc/code/Attic/doc_named_allocA.cpp b/doc/code/Attic/doc_named_allocA.cpp index abd3016..309c65d 100644 --- a/doc/code/Attic/doc_named_allocA.cpp +++ b/doc/code/Attic/doc_named_allocA.cpp @@ -2,16 +2,18 @@ #include #include + #include int main () { using namespace boost::interprocess; typedef std::pair MyType; - //A shared memory front-end that is able to construct - //objects associated with a c-string - //Create the shared memory segment and initialize resources - managed_shared_memory::remove("MySharedMemory"); + //An special shared memory where we can + //construct objects associated with a name. + //First remove any old shared memory of the same name, create + //the shared memory segment and initialize needed resources + shared_memory_object::remove("MySharedMemory"); managed_shared_memory segment( create_only, "MySharedMemory", //segment name diff --git a/doc/code/Attic/doc_named_allocB.cpp b/doc/code/Attic/doc_named_allocB.cpp index d6f1d56..f063dbc 100644 --- a/doc/code/Attic/doc_named_allocB.cpp +++ b/doc/code/Attic/doc_named_allocB.cpp @@ -3,6 +3,7 @@ #include #include + #include #include int main () @@ -10,8 +11,10 @@ using namespace boost::interprocess; typedef std::pair MyType; - //A shared memory front-end that is able to construct - //objects associated with a c-string + //An special shared memory where we can + //construct objects associated with a name. + //Connect to the already created shared memory segment + //and initialize needed resources managed_shared_memory segment( open_only, "MySharedMemory"); diff --git a/doc/code/Attic/doc_named_conditionA.cpp b/doc/code/Attic/doc_named_conditionA.cpp new file mode 100644 index 0000000..65bfba5 --- /dev/null +++ b/doc/code/Attic/doc_named_conditionA.cpp @@ -0,0 +1,65 @@ + #include + #include + #include + #include + #include "doc_anonymous_condition_shared_data.hpp" + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (create_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Set size + shm.truncate(sizeof(trace_queue)); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Construct the shared structure in memory + trace_queue * data = new (addr) trace_queue; + + const int NumMsg = 100; + + for(int i = 0; i < NumMsg; ++i){ + scoped_lock lock(data->mutex); + if(data->message_in){ + data->cond_full.wait(lock); + } + if(i == (NumMsg-1)) + std::sprintf(data->items, "%s", "last message"); + else + std::sprintf(data->items, "%s_%d", "my_trace", i); + + //Notify to the other process that there is a message + data->cond_empty.notify_one(); + + //Mark message buffer as full + data->message_in = true; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_named_conditionB.cpp b/doc/code/Attic/doc_named_conditionB.cpp new file mode 100644 index 0000000..b359c47 --- /dev/null +++ b/doc/code/Attic/doc_named_conditionB.cpp @@ -0,0 +1,63 @@ + #include + #include + #include + #include + #include "doc_anonymous_condition_shared_data.hpp" + + using namespace boost::interprocess; + + int main () + { + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (open_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + + //Obtain a pointer to the shared structure + trace_queue * data = static_cast(addr); + + //Print messages until the other process marks the end + bool end_loop = false; + do{ + scoped_lock lock(data->mutex); + if(!data->message_in){ + data->cond_empty.wait(lock); + } + if(std::strcmp(data->items, "last message") == 0){ + end_loop = true; + } + else{ + //Print the message + std::cout << data->items << std::endl; + //Notify the other process that the buffer is empty + data->message_in = false; + data->cond_full.notify_one(); + } + } + while(!end_loop); + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + //Erase shared memory + shared_memory_object::remove("shared_memory"); + + return 0; + } diff --git a/doc/code/Attic/doc_named_condition_shared_data.hpp b/doc/code/Attic/doc_named_condition_shared_data.hpp new file mode 100644 index 0000000..d61934f --- /dev/null +++ b/doc/code/Attic/doc_named_condition_shared_data.hpp @@ -0,0 +1,26 @@ + #include + #include + + struct trace_queue + { + enum { LineSize = 100 }; + + trace_queue() + : message_in(false) + {} + + //Mutex to protect access to the queue + boost::interprocess::interprocess_mutex mutex; + + //Condition to wait when the queue is empty + boost::interprocess::interprocess_condition cond_empty; + + //Condition to wait when the queue is full + boost::interprocess::interprocess_condition cond_full; + + //Items to fill + char items[LineSize]; + + //Is there any message + bool message_in; + }; diff --git a/doc/code/Attic/doc_named_mutex.cpp b/doc/code/Attic/doc_named_mutex.cpp new file mode 100644 index 0000000..efca8a4 --- /dev/null +++ b/doc/code/Attic/doc_named_mutex.cpp @@ -0,0 +1,32 @@ + #include + #include + #include + + int main () + { + using namespace boost::interprocess; + try{ + + //Open or create the named mutex + named_mutex mutex(open_or_create, "cout_named_mutex"); + + std::ofstream file("file_name"); + + for(int i = 0; i < 10; ++i){ + + //Do some operations... + + //Write to console atomically + scoped_lock lock(mutex); + file << "Process name, "; + file << "This is iteration #" << i; + file << std::endl; + } + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + return 0; + } \ No newline at end of file diff --git a/doc/code/Attic/doc_offset_ptr.cpp b/doc/code/Attic/doc_offset_ptr.cpp index 19c6c9b..a548d75 100644 --- a/doc/code/Attic/doc_offset_ptr.cpp +++ b/doc/code/Attic/doc_offset_ptr.cpp @@ -15,8 +15,10 @@ int main () { - //Create shared memory - managed_shared_memory::remove("MySharedMemory"); + //Destroy any previous shared memory with the name to be used. + //Create an special shared memory from which we can + //allocate buffers of raw memory. + shared_memory_object::remove("MySharedMemory"); managed_shared_memory segment( create_only, "MySharedMemory",//segment name diff --git a/doc/code/Attic/doc_scoped_ptr.cpp b/doc/code/Attic/doc_scoped_ptr.cpp index cf17e08..625fc36 100644 --- a/doc/code/Attic/doc_scoped_ptr.cpp +++ b/doc/code/Attic/doc_scoped_ptr.cpp @@ -38,7 +38,7 @@ int main () { //Create shared memory - managed_shared_memory::remove("my_shmem"); + shared_memory_object::remove("my_shmem"); managed_shared_memory shmem(create_only, "my_shmem", 10000); //In the first try, there will be no exceptions diff --git a/doc/code/Attic/doc_shared_memory.cpp b/doc/code/Attic/doc_shared_memory.cpp new file mode 100644 index 0000000..5ee5441 --- /dev/null +++ b/doc/code/Attic/doc_shared_memory.cpp @@ -0,0 +1,42 @@ + #include + #include + #include + + int main () + { + using namespace boost::interprocess; + try{ + //Erase previous shared memory + shared_memory_object::remove("shared_memory"); + + //Create a shared memory object. + shared_memory_object shm + (create_only //only create + ,"shared_memory" //name + ,memory_mappable::read_write //read-write mode + ); + + //Set size + shm.truncate(1000); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_write //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + std::size_t size = region.get_size(); + + //Write all the memory to 1 + std::memset(addr, 1, size); + + } + catch(interprocess_exception &ex){ + std::cout << ex.what() << std::endl; + return 1; + } + + return 0; + } diff --git a/doc/code/Attic/doc_shared_memory2.cpp b/doc/code/Attic/doc_shared_memory2.cpp new file mode 100644 index 0000000..92ab95e --- /dev/null +++ b/doc/code/Attic/doc_shared_memory2.cpp @@ -0,0 +1,43 @@ + #include + #include + #include + + int main () + { + using namespace boost::interprocess; + try{ + //Open already created shared memory object. + shared_memory_object shm + (open_only //only create + ,"shared_memory" //name + ,memory_mappable::read_only //read-write mode + ); + + //Map the whole shared memory in this process + mapped_region region + (shm //What to map + ,mapped_region::read_only //Map it as read-write + ); + + //Get the address of the mapped region + void * addr = region.get_address(); + std::size_t size = region.get_size(); + + //Check that memory was initialized to 1 + const char *mem = static_cast(addr); + for(std::size_t i = 0; i < size; ++i){ + if(*mem++ != 1){ + std::cout << "Error checking memory!" << std::endl; + return 1; + } + } + + std::cout << "Test successful!" << std::endl; + } + catch(interprocess_exception &ex){ + std::cout << "Unexpected exception: " << ex.what() << std::endl; + return 1; + } + + return 0; + } diff --git a/doc/code/Attic/doc_upgradable_mutex_shared_data.hpp b/doc/code/Attic/doc_upgradable_mutex_shared_data.hpp new file mode 100644 index 0000000..1744c58 --- /dev/null +++ b/doc/code/Attic/doc_upgradable_mutex_shared_data.hpp @@ -0,0 +1,22 @@ + #include + + struct shared_data + { + enum { NumItems = 100 }; + enum { LineSize = 100 }; + + shared_data() + : current_line(0) + , end_a(false) + , end_b(false) + {} + + //Mutex to protect access to the queue + boost::interprocess::interprocess_upgradable_mutex upgradable_mutex; + + //Items to fill + char items[NumItems][LineSize]; + int current_line; + bool end_a; + bool end_b; + }; diff --git a/doc/code/Attic/doc_vectorstream.cpp b/doc/code/Attic/doc_vectorstream.cpp index 7ff921f..39ba2b9 100644 --- a/doc/code/Attic/doc_vectorstream.cpp +++ b/doc/code/Attic/doc_vectorstream.cpp @@ -21,7 +21,7 @@ int main () { //Create shared memory - managed_shared_memory::remove("MySharedMemory"); + shared_memory_object::remove("MySharedMemory"); managed_shared_memory segment( create_only, "MySharedMemory",//segment name diff --git a/doc/code/Attic/doc_where_allocate.cpp b/doc/code/Attic/doc_where_allocate.cpp index b451b97..02d570a 100644 --- a/doc/code/Attic/doc_where_allocate.cpp +++ b/doc/code/Attic/doc_where_allocate.cpp @@ -20,7 +20,7 @@ MyShmStringVector; //Open shared memory - managed_shared_memory::remove("myshm"); + shared_memory_object::remove("myshm"); managed_shared_memory shm(create_only, "myshm", 10000); //Create allocators diff --git a/doc/code/Jamfile b/doc/code/Jamfile deleted file mode 100644 index 0ba6d4d..0000000 --- a/doc/code/Jamfile +++ /dev/null @@ -1,43 +0,0 @@ -# Boost interprocess Library test Jamfile -# (C) Copyright Ion Gaztaņaga 2004-2006. -# Use, modification, and distribution are subject to the -# Boost Software License, Version 1.0. (See accompanying file -# LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) -# -# See http://www.boost.org/libs/test for the library home page. - -# Declare the location of this subproject relative to the root. -subproject libs/interprocess/doc/code ; - -# bring in rules for testing -import testing ; - -{ - template boost_interprocess_test_dll - : ## sources ## - ../../../test/build/boost_unit_test_framework - ../../../thread/build/boost_thread - : ## requirements ## - $(BOOST_ROOT) - dynamic - multi - : ## default build ## - release - ; - - test-suite "interprocess" - : - [ run doc_bufferstream.cpp