Skip to content

Commit 32de640

Browse files
committed
C++17 filesystem and chrono alternative implementations
1 parent cdb43c1 commit 32de640

File tree

9 files changed

+76
-98
lines changed

9 files changed

+76
-98
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 = (uint64_t)(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: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33

44
#pragma once
55

6+
#include <chrono>
67
#include <sstream>
78

8-
#include <sys/time.h>
99
#include <stdint.h>
1010

1111
namespace scene_rdl2 {
@@ -26,10 +26,9 @@ class RecTime
2626
inline float end() const { return (float)(getCurrentMicroSec() - mStartTime) * 0.000001f; } // return sec
2727

2828
static long long getCurrentMicroSec() {
29-
struct timeval tv;
30-
gettimeofday(&tv, 0x0);
31-
long long cTime = (long long)tv.tv_sec * 1000 * 1000 + (long long)tv.tv_usec;
32-
return cTime;
29+
const auto tnow = std::chrono::high_resolution_clock::now().time_since_epoch();
30+
const auto cTime = std::chrono::duration_cast<std::chrono::microseconds>(tnow);
31+
return cTime.count();
3332
}
3433

3534
protected:

lib/render/util/Files.cc

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

37+
#include <filesystem>
38+
namespace fs = std::filesystem;
39+
3740
namespace scene_rdl2 {
3841
namespace util {
3942

@@ -49,12 +52,9 @@ struct FreeDeleter
4952
std::pair<std::string, std::string>
5053
splitPath(const std::string& filePath)
5154
{
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()));
55+
fs::path p(filePath);
56+
std::string directory(p.parent_path().string());
57+
std::string filename(p.filename().string());
5858

5959
return std::make_pair(std::move(directory), std::move(filename));
6060
}

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 <iostream>
1010
#include <pthread.h> // pthread_setaffinity_np
1111
#include <sstream>
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 += threadId;

lib/scene/rdl2/Dso.cc

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
#include <dlfcn.h>
2424
#include <libgen.h>
2525
#include <unistd.h>
26+
#include <filesystem>
27+
namespace fs = std::filesystem;
2628

2729
namespace scene_rdl2 {
2830
namespace rdl2 {
@@ -56,12 +58,9 @@ classNameFromFileName(const std::string& baseName,
5658
std::string
5759
Dso::classNameFromFileName(const std::string& filePath)
5860
{
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);
61+
fs::path p(filePath);
62+
std::string directory(p.parent_path().string());
63+
std::string baseName(p.stem().string());
6564

6665
// Bail early if we can't determine the class name.
6766

@@ -228,15 +227,10 @@ Dso::getDestroy()
228227
bool
229228
Dso::isValidDso(const std::string& filePath, bool proxyModeEnabled)
230229
{
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);
230+
// Break the path into directory and basename components.
231+
fs::path p = filePath;
232+
std::string directory(p.parent_path().string());
233+
std::string baseName(p.stem().string());
240234

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

lib/scene/rdl2/DsoFinder.cc

Lines changed: 37 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,65 @@
99

1010
#include <cstdlib>
1111
#include <vector>
12-
#include <dirent.h>
13-
#include <libgen.h>
12+
13+
#include <filesystem>
14+
namespace fs = std::filesystem;
1415

1516
namespace scene_rdl2 {
1617
namespace rdl2 {
1718

18-
using util::Args;
19+
const std::string g_raasRender("raas_render");
20+
const std::string g_osPathSep(":");
1921

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-
}
22+
using util::Args;
2823

2924
std::string
3025
DsoFinder::guessDsoPath()
3126
{
3227
std::string dsoPath = "";
33-
dirent** nameList;
34-
28+
3529
// First, search PATH for raas_render executable
3630
const std::string pathEnv = util::getenv<std::string>("PATH");
3731
if (pathEnv.empty()) {
3832
return "";
3933
}
40-
41-
size_t found = pathEnv.find(':');
42-
int numFound;
43-
std::string path;
34+
size_t found = pathEnv.find(g_osPathSep);
35+
fs::path path;
4436
if (found == std::string::npos) { // single path
45-
path = pathEnv;
46-
numFound = scandir(path.c_str(), &nameList, isMatching, alphasort);
37+
path = fs::path(pathEnv).make_preferred();
38+
39+
if (fs::exists(path)) {
40+
for (auto const& dirEntry : std::filesystem::directory_iterator(path)) {
41+
if (dirEntry.path().filename().string() == g_raasRender) {
42+
break;
43+
}
44+
}
45+
}
4746
} else {
4847
int counter = 0;
48+
bool pathFound = false;
4949
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) {
50+
path = fs::path(pathEnv.substr(counter, found - counter)).make_preferred();
51+
if (fs::exists(path)) {
52+
for ( const auto & dirEntry : std::filesystem::directory_iterator(path)) {
53+
std::string file = dirEntry.path().stem().string();
54+
if (file == g_raasRender) {
55+
pathFound = true;
56+
break;
57+
}
58+
}
59+
}
60+
if (pathFound) {
5361
break;
5462
}
5563
counter = found + 1;
56-
found = pathEnv.find(':', found + 1);
57-
}
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);
64+
found = pathEnv.find(g_osPathSep, found + 1);
6365
}
6466
}
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);
72-
}
73-
74-
// clean up
75-
/*while (numFound--) {
76-
free(nameList[numFound]);
77-
}*/
78-
free(nameList);
7967

68+
if (!path.empty()) {
69+
dsoPath = fs::path(fs::absolute(path.parent_path()) / "rdl2dso").make_preferred().string();
70+
}
8071
return dsoPath;
8172
}
8273

@@ -85,14 +76,14 @@ std::string DsoFinder::find() {
8576
std::string dsoPathString = "."; // Search '.' path first
8677
if (const char* const dsoPathEnvVar = util::getenv<const char*>("RDL2_DSO_PATH")) {
8778
// append dso path as sourced from RDL2_DSO_PATH
88-
dsoPathString += ":" + std::string(dsoPathEnvVar);
79+
dsoPathString += g_osPathSep + std::string(dsoPathEnvVar);
8980
}
9081

9182
// finally, guess dso path based on location of raas_render
9283
std::string guessedDsoPath = guessDsoPath();
9384
if (!guessedDsoPath.empty()) {
9485
// append dso path as sourced from location of raas_render executable
95-
dsoPathString += ":" + guessedDsoPath;
86+
dsoPathString += g_osPathSep + guessedDsoPath;
9687
}
9788

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

126117
if (!dsoPath.empty()) {
127118
// prepend dso path as sourced from command line
128-
return dsoPath + ":" + findPath;
119+
return dsoPath + g_osPathSep + findPath;
129120
}
130121

131122
return findPath;

lib/scene/rdl2/SceneContext.cc

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

52+
#include <filesystem>
53+
namespace fs = std::filesystem;
54+
5255
namespace scene_rdl2 {
5356

5457
using logging::Logger;
@@ -725,23 +728,18 @@ SceneContext::loadAllSceneClasses()
725728
// Grab the next path entry.
726729
std::size_t colonPos = remaining.find_first_of(':');
727730
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-
731+
fs::path p(directory);
732+
if (fs::exists(p)) {
733+
for (auto const& dirEntry : std::filesystem::directory_iterator(p)) {
736734
// If the file is a valid DSO, then create a SceneClass from it.
737-
if (Dso::isValidDso(directory + '/' + fileName, mProxyModeEnabled)) {
735+
if (Dso::isValidDso(dirEntry.path().string(), mProxyModeEnabled)) {
738736
// Class name is the file name without ".so" (or
739737
// ".so.proxy" in proxy mode).
740738
std::string className;
741739
if (mProxyModeEnabled) {
742-
className = fileName.substr(0, fileName.size() - 9);
740+
className = dirEntry.path().stem().stem().string();
743741
} else {
744-
className = fileName.substr(0, fileName.size() - 3);
742+
className = dirEntry.path().stem().string();
745743
}
746744

747745
try {
@@ -754,7 +752,6 @@ SceneContext::loadAllSceneClasses()
754752
}
755753
}
756754
}
757-
closedir(directoryPtr);
758755

759756
// Move to the next path entry.
760757
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
@@ -4,7 +4,7 @@
44

55
#include <scene_rdl2/common/rec_time/RecTime.h>
66

7-
#include <unistd.h>
7+
#include <chrono>
88

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

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

0 commit comments

Comments
 (0)