Skip to content

Commit eb4ecd9

Browse files
committed
C++17 filesystem and chrono alternative implementations
1 parent 791eb1e commit eb4ecd9

File tree

9 files changed

+97
-97
lines changed

9 files changed

+97
-97
lines changed

lib/common/grid_util/DebugConsoleDriver.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
//
66
#include "DebugConsoleDriver.h"
77

8+
#include <chrono>
89
#include <iostream>
9-
#include <unistd.h>
1010

1111
namespace scene_rdl2 {
1212
namespace grid_util {
@@ -89,7 +89,7 @@ DebugConsoleDriver::threadMain(DebugConsoleDriver *driver)
8989
if (recvByte == 0 || recvByte == -1) {
9090
// empty or EOF
9191
driver->mThreadState = ThreadState::IDLE;
92-
usleep(10000); // 10000us = 10ms : wake up every 10ms and check TlSvr
92+
std::this_thread::sleep_for(std::chrono::microseconds(10000)); // 10000us = 10ms : wake up every 10ms and check TlSvr
9393

9494
} else if (recvByte < -1) { // error
9595
std::cerr << "telnet server failed\n";

lib/common/grid_util/LatencyLog.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
#include <scene_rdl2/scene/rdl2/ValueContainerDeq.h>
1717
#include <scene_rdl2/scene/rdl2/ValueContainerEnq.h>
1818

19+
#include <chrono>
1920
#include <string>
2021
#include <vector>
2122

22-
#include <sys/time.h>
23-
2423
//
2524
// We should always use variable length coding.
2625
// This directive is designed for performance comparison purpose.
@@ -196,9 +195,10 @@ class LatencyItem
196195
finline uint64_t
197196
LatencyItem::getCurrentMicroSec()
198197
{
199-
struct timeval tv;
200-
gettimeofday(&tv, 0x0);
201-
uint64_t microSec = static_cast<uint64_t>(tv.tv_sec) * 1000 * 1000 + static_cast<uint64_t>(tv.tv_usec);
198+
const auto tnow = std::chrono::high_resolution_clock::now().time_since_epoch();
199+
const auto cTime = std::chrono::duration_cast<std::chrono::microseconds>(tnow);
200+
uint64_t microSec = static_cast<uint64_t>(static_cast<int64_t>(cTime.count()));
201+
202202
if (LatencyClockOffset::getInstance().isPositive()) {
203203
microSec += LatencyClockOffset::getInstance().getAbsOffsetMicroSec();
204204
} else {

lib/common/rec_time/RecTime.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
// SPDX-License-Identifier: Apache-2.0
33
#pragma once
44

5+
#include <chrono>
56
#include <sstream>
67

7-
#include <sys/time.h>
88
#include <stdint.h>
99
#include <time.h> // clock_gettime()
1010

@@ -38,9 +38,9 @@ class RecTime
3838

3939
static uint64_t getCurrentMicroSec() // MTsafe
4040
{
41-
struct timeval tv;
42-
gettimeofday(&tv, 0x0);
43-
return static_cast<uint64_t>(tv.tv_sec) * 1000000 + static_cast<uint64_t>(tv.tv_usec);
41+
const auto tnow = std::chrono::high_resolution_clock::now().time_since_epoch();
42+
const auto cTime = std::chrono::duration_cast<std::chrono::microseconds>(tnow);
43+
return cTime.count();
4444
}
4545

4646
protected:
@@ -159,7 +159,7 @@ class RecTimeRDTSC
159159
//
160160
const uint64_t c0 = __rdtsc();
161161
const uint64_t ns0 = RecTimeVDSO::getCurrentNanoSec(); // ns
162-
usleep(100000); // 100ms
162+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
163163
const uint64_t c1 = __rdtsc();
164164
const uint64_t ns1 = RecTimeVDSO::getCurrentNanoSec(); // ns
165165
const double deltaNs = static_cast<double>(ns1 - ns0);

lib/render/util/Files.cc

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
#endif
3535
#include <unistd.h>
3636

37+
#include <filesystem>
38+
3739
namespace scene_rdl2 {
3840
namespace util {
3941

@@ -49,12 +51,9 @@ struct FreeDeleter
4951
std::pair<std::string, std::string>
5052
splitPath(const std::string& filePath)
5153
{
52-
const char* path = filePath.c_str();
53-
std::unique_ptr<char, FreeDeleter> dirStr(strdup(path));
54-
std::unique_ptr<char, FreeDeleter> baseStr(strdup(path));
55-
56-
std::string directory(dirname(dirStr.get()));
57-
std::string filename(basename(baseStr.get()));
54+
std::filesystem::path p(filePath);
55+
std::string directory(p.parent_path().string());
56+
std::string filename(p.filename().string());
5857

5958
return std::make_pair(std::move(directory), std::move(filename));
6059
}

lib/render/util/ThreadPoolExecutor.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <pthread.h> // pthread_setaffinity_np
1010
#include <sstream>
1111
#include <thread>
12-
#include <unistd.h> // usleep
12+
#include <chrono>
1313

1414
//#define DEBUG_MSG_THREAD
1515
//#define DEBUG_MSG_THREAD_CPUAFFINITY
@@ -325,10 +325,7 @@ ThreadPoolExecutor::testBootShutdown()
325325
// This simulates MoonRay's MCRT thread boot logic
326326
++bootedThreadTotal;
327327
while (bootedThreadTotal < threadTotal) {
328-
struct timespec tm;
329-
tm.tv_sec = 0;
330-
tm.tv_nsec = 1000; // 0.001ms
331-
nanosleep(&tm, NULL); // yield CPU resources
328+
std::this_thread::sleep_for(std::chrono::nanoseconds(1000)); // yield CPU resources
332329
}
333330

334331
sum += static_cast<int>(threadId);

lib/scene/rdl2/Dso.cc

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <dlfcn.h>
2424
#include <libgen.h>
2525
#include <unistd.h>
26+
#include <filesystem>
2627

2728
namespace scene_rdl2 {
2829
namespace rdl2 {
@@ -56,12 +57,9 @@ classNameFromFileName(const std::string& baseName,
5657
std::string
5758
Dso::classNameFromFileName(const std::string& filePath)
5859
{
59-
char* dirStr = strdup(filePath.c_str());
60-
std::string directory(dirname(dirStr));
61-
free(dirStr);
62-
char* baseStr = strdup(filePath.c_str());
63-
std::string baseName(basename(baseStr));
64-
free(baseStr);
60+
std::filesystem::path p(filePath);
61+
std::string directory(p.parent_path().string());
62+
std::string baseName(p.stem().string());
6563

6664
// Bail early if we can't determine the class name.
6765

@@ -228,15 +226,10 @@ Dso::getDestroy()
228226
bool
229227
Dso::isValidDso(const std::string& filePath, bool proxyModeEnabled)
230228
{
231-
// Break the path into directory and basename components. Painfully, both
232-
// dirname() and basename() may do just about anything with your pointers,
233-
// so its safest to make a copy first.
234-
char* dirStr = strdup(filePath.c_str());
235-
std::string directory(dirname(dirStr));
236-
free(dirStr);
237-
char* baseStr = strdup(filePath.c_str());
238-
std::string baseName(basename(baseStr));
239-
free(baseStr);
229+
// Break the path into directory and basename components.
230+
std::filesystem::path p = filePath;
231+
std::string directory(p.parent_path().string());
232+
std::string baseName(p.filename().string());
240233

241234
// Bail early if we can't determine the class name.
242235
const char* extension = (proxyModeEnabled) ? ".so.proxy" : ".so";

lib/scene/rdl2/DsoFinder.cc

Lines changed: 60 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,89 @@
99

1010
#include <cstdlib>
1111
#include <vector>
12-
#include <dirent.h>
13-
#include <libgen.h>
12+
13+
#include <filesystem>
1414

1515
namespace scene_rdl2 {
1616
namespace rdl2 {
1717

18-
using util::Args;
18+
#ifdef _WIN32
19+
static const std::string sRaasRender("moonray.exe");
20+
static const std::string sOsPathSep(";");
21+
#else
22+
static const std::string sRaasRender("moonray");
23+
static const std::string sOsPathSep(":");
24+
#endif
1925

20-
int isMatching(const dirent* entry) {
21-
std::string name = "raas_render";
22-
if (name == std::string(entry->d_name)) {
23-
return 1;
24-
}
25-
26-
return 0;
27-
}
26+
using util::Args;
2827

2928
std::string
3029
DsoFinder::guessDsoPath()
3130
{
3231
std::string dsoPath = "";
33-
dirent** nameList;
34-
32+
3533
// First, search PATH for raas_render executable
3634
const std::string pathEnv = util::getenv<std::string>("PATH");
3735
if (pathEnv.empty()) {
3836
return "";
3937
}
40-
41-
size_t found = pathEnv.find(':');
42-
int numFound;
43-
std::string path;
38+
bool pathFound = false;
39+
size_t found = pathEnv.find(sOsPathSep);
40+
std::filesystem::path path;
4441
if (found == std::string::npos) { // single path
45-
path = pathEnv;
46-
numFound = scandir(path.c_str(), &nameList, isMatching, alphasort);
42+
path = std::filesystem::path(pathEnv).make_preferred();
43+
44+
if (std::filesystem::exists(path)) {
45+
for (auto const& dirEntry : std::filesystem::directory_iterator(path)) {
46+
if (dirEntry.path().filename().string() == sRaasRender) {
47+
pathFound = true;
48+
break;
49+
}
50+
}
51+
}
4752
} else {
4853
int counter = 0;
4954
while (found != std::string::npos) {
50-
path = pathEnv.substr(counter, found - counter);
51-
numFound = scandir(path.c_str(), &nameList, isMatching, alphasort);
52-
if (numFound > 0) {
55+
path = std::filesystem::path(pathEnv.substr(counter, found - counter)).make_preferred();
56+
if (std::filesystem::exists(path)) {
57+
for ( const auto & dirEntry : std::filesystem::directory_iterator(path)) {
58+
std::string file = dirEntry.path().stem().string();
59+
if (file == sRaasRender) {
60+
pathFound = true;
61+
break;
62+
}
63+
}
64+
}
65+
if (pathFound) {
5366
break;
5467
}
5568
counter = found + 1;
56-
found = pathEnv.find(':', found + 1);
69+
found = pathEnv.find(sOsPathSep, found + 1);
5770
}
58-
59-
if (numFound <= 0) { // Haven't found raas_render yet
60-
// Process last path
61-
path = pathEnv.substr(counter);
62-
numFound = scandir(path.c_str(), &nameList, isMatching, alphasort);
71+
}
72+
73+
if (pathFound) {
74+
std::filesystem::path raasRdl2dso = std::filesystem::path(std::filesystem::absolute(path.parent_path()) / "rdl2dso").make_preferred();
75+
if (std::filesystem::exists(raasRdl2dso)) {
76+
dsoPath = raasRdl2dso.string();
6377
}
6478
}
65-
66-
if (numFound > 0) {
67-
// We found raas_render, now construct path to rdl2dso
68-
// This assumes that the immediate parent directory is /bin
69-
char* buf = realpath(path.c_str(), NULL); // Resolve any relative links
70-
dsoPath = std::string(dirname(buf)) + "/" + "rdl2dso";
71-
free(buf);
79+
#ifndef __APPLE__
80+
else {
81+
#ifdef _WIN32
82+
char exePathCStr[MAX_PATH];
83+
::GetModuleFileNameA(nullptr, exePathCStr, MAX_PATH);
84+
std::filesystem::path exePath(exePathCStr);
85+
#else
86+
std::filesystem::path exePath("/proc/self/exe");
87+
exePath = std::filesystem::canonical(exePath);
88+
#endif
89+
std::filesystem::path raasRdl2dso = std::filesystem::path(std::filesystem::absolute(exePath.parent_path().parent_path()) / "rdl2dso").make_preferred();
90+
if (std::filesystem::exists(raasRdl2dso)) {
91+
dsoPath = raasRdl2dso.string();
92+
}
7293
}
73-
74-
// clean up
75-
/*while (numFound--) {
76-
free(nameList[numFound]);
77-
}*/
78-
free(nameList);
79-
94+
#endif
8095
return dsoPath;
8196
}
8297

@@ -85,14 +100,14 @@ std::string DsoFinder::find() {
85100
std::string dsoPathString = "."; // Search '.' path first
86101
if (const char* const dsoPathEnvVar = util::getenv<const char*>("RDL2_DSO_PATH")) {
87102
// append dso path as sourced from RDL2_DSO_PATH
88-
dsoPathString += ":" + std::string(dsoPathEnvVar);
103+
dsoPathString += sOsPathSep + std::string(dsoPathEnvVar);
89104
}
90105

91106
// finally, guess dso path based on location of raas_render
92107
std::string guessedDsoPath = guessDsoPath();
93108
if (!guessedDsoPath.empty()) {
94109
// append dso path as sourced from location of raas_render executable
95-
dsoPathString += ":" + guessedDsoPath;
110+
dsoPathString += sOsPathSep + guessedDsoPath;
96111
}
97112

98113
return dsoPathString;
@@ -125,7 +140,7 @@ std::string DsoFinder::parseDsoPath(int argc, char* argv[]) {
125140

126141
if (!dsoPath.empty()) {
127142
// prepend dso path as sourced from command line
128-
return dsoPath + ":" + findPath;
143+
return dsoPath + sOsPathSep + findPath;
129144
}
130145

131146
return findPath;

lib/scene/rdl2/SceneContext.cc

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#include <sys/types.h>
5050
#include <unistd.h>
5151

52+
#include <filesystem>
53+
5254
namespace scene_rdl2 {
5355

5456
using logging::Logger;
@@ -725,23 +727,18 @@ SceneContext::loadAllSceneClasses()
725727
// Grab the next path entry.
726728
std::size_t colonPos = remaining.find_first_of(':');
727729
std::string directory = remaining.substr(0, colonPos);
728-
729-
// Grab the contents of each directory.
730-
DIR* directoryPtr = opendir(directory.c_str());
731-
if (directoryPtr != nullptr) {
732-
struct dirent* entryPtr;
733-
while ((entryPtr = readdir(directoryPtr))) {
734-
std::string fileName(entryPtr->d_name);
735-
730+
std::filesystem::path p(directory);
731+
if (std::filesystem::exists(p)) {
732+
for (auto const& dirEntry : std::filesystem::directory_iterator(p)) {
736733
// If the file is a valid DSO, then create a SceneClass from it.
737-
if (Dso::isValidDso(directory + '/' + fileName, mProxyModeEnabled)) {
734+
if (Dso::isValidDso(dirEntry.path().string(), mProxyModeEnabled)) {
738735
// Class name is the file name without ".so" (or
739736
// ".so.proxy" in proxy mode).
740737
std::string className;
741738
if (mProxyModeEnabled) {
742-
className = fileName.substr(0, fileName.size() - 9);
739+
className = dirEntry.path().stem().stem().string();
743740
} else {
744-
className = fileName.substr(0, fileName.size() - 3);
741+
className = dirEntry.path().stem().string();
745742
}
746743

747744
try {
@@ -754,7 +751,6 @@ SceneContext::loadAllSceneClasses()
754751
}
755752
}
756753
}
757-
closedir(directoryPtr);
758754

759755
// Move to the next path entry.
760756
if (colonPos != std::string::npos) {

tests/lib/render/util/TestThreadPoolExecutor.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <scene_rdl2/common/rec_time/RecTime.h>
77

88
#include <thread>
9-
#include <unistd.h>
9+
#include <chrono>
1010

1111
// This directive should not commented out for the release version.
1212
// This is only used for local debugging purposes.
@@ -101,7 +101,7 @@ TestThreadPoolExecutor::watcherThreadMain(const float maxTestDurationSec)
101101
<< " duration:" << maxTestDurationSec << " sec\n";
102102
exit(1);
103103
}
104-
usleep(10000);
104+
std::this_thread::sleep_for(std::chrono::microseconds(10000));
105105
}
106106

107107
std::cerr << ">> Watcher thread shutdown <<\n";

0 commit comments

Comments
 (0)