Skip to content

Commit

Permalink
Add random timestamp-based id for xsi_key
Browse files Browse the repository at this point in the history
  • Loading branch information
igaztanaga committed Jan 12, 2024
1 parent 4465a67 commit 0614661
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
16 changes: 16 additions & 0 deletions include/boost/interprocess/detail/os_thread_functions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ inline OS_highres_count_t get_current_system_highres_count()
return (OS_highres_count_t)count;
}

inline unsigned get_current_system_highres_rand()
{
unsigned __int64 count = (unsigned __int64) get_current_system_highres_count();
return static_cast<unsigned>(count + (count >> 32u));
}

inline void zero_highres_count(OS_highres_count_t &count)
{ count = 0; }

Expand Down Expand Up @@ -354,6 +360,16 @@ inline OS_highres_count_t get_current_system_highres_count()
#endif
}

inline unsigned get_current_system_highres_rand()
{
OS_highres_count_t count = get_current_system_highres_count();
#if defined(BOOST_INTERPROCESS_CLOCK_MONOTONIC)
return static_cast<unsigned>(count.tv_sec + count.tv_nsec);
#elif defined(BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME)
return static_cast<unsigned>(count);
#endif
}

#ifndef BOOST_INTERPROCESS_MATCH_ABSOLUTE_TIME

inline void zero_highres_count(OS_highres_count_t &count)
Expand Down
3 changes: 2 additions & 1 deletion test/managed_xsi_shared_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ int main ()

file_mapping::remove(ShmemName);
{ ipcdetail::file_wrapper(create_only, ShmemName, read_write); }
xsi_key key(ShmemName, 1);

xsi_key key(ShmemName, static_cast<int>(boost::interprocess::ipcdetail::get_current_system_highres_rand()));
file_mapping::remove(ShmemName);
int shmid;

Expand Down
3 changes: 2 additions & 1 deletion test/xsi_shared_memory_mapping_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/file_mapping.hpp>
#include <boost/interprocess/detail/file_wrapper.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <string>
#include <iostream>
#include "get_process_id_name.hpp"
Expand Down Expand Up @@ -57,7 +58,7 @@ int main ()
file_mapping::remove(names[0]);
{ ipcdetail::file_wrapper(create_only, names[0], read_write); }

xsi_key key(names[0], 1);
xsi_key key(names[0], static_cast<int>(boost::interprocess::ipcdetail::get_current_system_highres_rand()));
file_mapping::remove(names[0]);
remove_shared_memory(key);

Expand Down

0 comments on commit 0614661

Please sign in to comment.