Skip to content

Commit

Permalink
Use a static Boost random UUID generator (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
svwilliams committed Apr 8, 2020
1 parent 58f5f09 commit 5f28532
Showing 1 changed file with 6 additions and 57 deletions.
63 changes: 6 additions & 57 deletions fuse_core/src/uuid.cpp
Expand Up @@ -51,69 +51,18 @@ namespace fuse_core
namespace uuid
{

// Use the stdlib random number generators. They seem to be significantly faster than the Boost equivalents.
// Implementation mimics original Boost UUID random generator implementation here:
// https://www.boost.org/doc/libs/1_65_0/boost/uuid/random_generator.hpp
//
// Boost random_generator.hpp header file ----------------------------------------------//
// Copyright 2010 Andy Tompkins.
// Distributed under 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)
//
// Boost Software License - Version 1.0 - August 17th, 2003
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
UUID generate()
{
static std::random_device rd;
static std::mt19937 generator(rd());
static std::uniform_int_distribution<uint64_t> distibution;
static std::mutex distribution_mutex;
static boost::uuids::random_generator generator;
static std::mutex generator_mutex;

uint64_t random1;
uint64_t random2;
UUID uuid;
{
std::lock_guard<std::mutex> lock(distribution_mutex);
random1 = generator();
random2 = generator();
std::lock_guard<std::mutex> lock(generator_mutex);
uuid = generator();
}

UUID u;
std::memcpy(u.data, &random1, 8);
std::memcpy(u.data + 8, &random2, 8);

// set variant
// must be 0b10xxxxxx
*(u.begin() + 8) &= 0xBF;
*(u.begin() + 8) |= 0x80;

// set version
// must be 0b0100xxxx
*(u.begin() + 6) &= 0x4F; // 0b01001111
*(u.begin() + 6) |= 0x40; // 0b01000000

return u;
return uuid;
}

UUID generate(const std::string& namespace_string, const ros::Time& stamp)
Expand Down

0 comments on commit 5f28532

Please sign in to comment.