Go to the documentation of this file.00001 #ifndef SHARED_MEMORY_BLOCK_DESCR_H
00002 #define SHARED_MEMORY_BLOCK_DESCR_H
00003
00004 #include <cassert>
00005 #include <ros/ros.h>
00006
00007 #include <boost/interprocess/managed_shared_memory.hpp>
00008 #include <boost/interprocess/sync/interprocess_mutex.hpp>
00009 #include <boost/interprocess/sync/interprocess_condition.hpp>
00010 #include <boost/interprocess/sync/scoped_lock.hpp>
00011 #include <boost/date_time/posix_time/posix_time.hpp>
00012
00013 namespace sharedmem_transport {
00014
00015
00016 struct SharedMemoryBlockDescriptor
00017 {
00018 SharedMemoryBlockDescriptor();
00019 uint32_t getSize() const ;
00020
00021 void recordSize(uint32_t newsize, uint32_t alloc=0) ;
00022
00023 void allocate(const char name[256]) ;
00024
00025 void reset() ;
00026
00027 bool matchName(const char * name) const;
00028
00029 void check_clients(boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> & lock) {
00030 if (num_clients) {
00031 ROS_DEBUG("Waiting lock (%d clients)",num_clients);
00032 lockcond.wait(lock);
00033 }
00034 }
00035
00036 void register_client() {
00037 ROS_DEBUG("register_client:: Locking");
00038 boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> lock(mutex);
00039 num_clients ++;
00040 ROS_DEBUG("Registered client %d",num_clients);
00041 }
00042
00043 void unregister_client() {
00044 ROS_DEBUG("unregister_client:: Locking");
00045 boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> lock(mutex);
00046 num_clients --;
00047 ROS_DEBUG("Unregistered client, %d remaining",num_clients);
00048 assert(num_clients >= 0);
00049 if (num_clients == 0) {
00050 ROS_DEBUG("Lock is free");
00051 lockcond.notify_all();
00052 }
00053 ROS_DEBUG("unregister_client:: Unlocking");
00054 }
00055
00056 bool wait_data_and_register_client(boost::interprocess::scoped_lock<boost::interprocess::interprocess_mutex> & lock) {
00057 boost::posix_time::ptime max_wait = boost::posix_time::microsec_clock::universal_time()
00058 + boost::posix_time::seconds(1);
00059 ROS_DEBUG("%d clients before wait",num_clients);
00060 if (!datacond.timed_wait(lock,max_wait)) {
00061 ROS_DEBUG("Wait timed-out");
00062 return false;
00063 }
00064 ROS_DEBUG("Wait returned %d",num_clients);
00065 num_clients++;
00066 ROS_DEBUG("Registered client %d",num_clients);
00067 return true;
00068 }
00069
00070 void signal_data() {
00071 datacond.notify_all();
00072 }
00073
00074
00075 boost::interprocess::interprocess_mutex mutex;
00076 boost::interprocess::interprocess_condition lockcond;
00077 boost::interprocess::interprocess_condition datacond;
00078 signed int num_clients;
00079
00080 uint32_t size_, allocated_;
00081
00082 uint32_t resize_count_;
00083
00084 bool active_;
00085 char name_[256];
00086 };
00087
00088
00089 }
00090 #endif // SHARED_MEMORY_BLOCK_DESCR_H