Skip to content

Commit

Permalink
Make it portable
Browse files Browse the repository at this point in the history
  • Loading branch information
Juan Oxoby committed Aug 18, 2020
1 parent 71677b3 commit 5bc8dde
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
4 changes: 4 additions & 0 deletions include/ignition/transport/Helpers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ namespace ignition
const std::string &_orig,
char _delim);

/// \brief Portable function to get the id of the current process.
/// \returns id of current process
unsigned int IGNITION_TRANSPORT_VISIBLE getProcessId();

// Use safer functions on Windows
#ifdef _MSC_VER
#define ign_strcat strcat_s
Expand Down
16 changes: 16 additions & 0 deletions src/Helpers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
#include <cstdlib>
#include <string>

#ifdef _WIN32
#include <processthreadsapi.h>
#else
#include <unistd.h>
#endif

#include "ignition/transport/Helpers.hh"

namespace ignition
Expand Down Expand Up @@ -59,6 +65,16 @@ namespace ignition
pieces.push_back(_orig.substr(pos1, _orig.size()-pos1));
return pieces;
}

//////////////////////////////////////////////////
unsigned int getProcessId()
{
#ifdef _WIN32
return ::GetCurrentProcessId();
#else
return ::getpid();
#endif
}
}
}
}
8 changes: 3 additions & 5 deletions src/NodeShared.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
#pragma warning(pop)
#endif

#include <unistd.h>

#include <chrono>
#include <cstring>
#include <iostream>
Expand Down Expand Up @@ -176,10 +174,10 @@ NodeShared *NodeShared::Instance()
// is not shared between different processes.

static std::shared_mutex mutex;
static std::unordered_map<pid_t, NodeShared*> nodeSharedMap;
static std::unordered_map<unsigned int, NodeShared*> nodeSharedMap;

// Get current process PID.
auto pid = ::getpid();
// Get current process ID.
auto pid = getProcessId();

// Check if there's already a NodeShared instance for this process.
// Use a shared_lock so multiple threads can read simultaneously.
Expand Down

0 comments on commit 5bc8dde

Please sign in to comment.