Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialize random udp package id using nanosecond timestamp #1270

Merged
merged 2 commits into from
Nov 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion ecal/core/src/io/udp/snd_raw_buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
* @brief raw message buffer handling
**/

#include <chrono>
#include <mutex>
#include <thread>

#include "ecal_process.h"
Expand All @@ -31,8 +33,11 @@
namespace
{
// random number generator
std::mutex xorshf96_mtx;
unsigned long xorshf96(unsigned long& x, unsigned long& y, unsigned long& z) // period 2^96-1
{
const std::lock_guard<std::mutex> lock(xorshf96_mtx);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you have a mutex here? what do you want to protect? This function has no global variables which it might access, hence it is reentrant.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is right :)

unsigned long t;
x ^= x << 16;
x ^= x >> 5;
Expand Down Expand Up @@ -129,7 +134,11 @@ namespace eCAL
msg_header.type = msg_type_header;
{
// create random number for message id
static unsigned long x = 123456789, y = 362436069, z = 521288629;
static unsigned long x = std::chrono::duration_cast<std::chrono::nanoseconds>(
std::chrono::high_resolution_clock::now().time_since_epoch()
).count();
static unsigned long y = 362436069;
static unsigned long z = 521288629;
msg_header.id = xorshf96(x, y, z);
}
msg_header.num = total_packet_num;
Expand Down
Loading