Skip to content

Commit 228d255

Browse files
committed
C++17 filesystem and chrono alternative implementations
1 parent 3b09e1e commit 228d255

File tree

9 files changed

+72
-98
lines changed

9 files changed

+72
-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 = 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: 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: 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 <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: 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.stem().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: 36 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -9,74 +9,64 @@
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+
static const std::string sRaasRender("raas_render");
19+
static const std::string sOsPathSep(":");
1920

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-
}
21+
using util::Args;
2822

2923
std::string
3024
DsoFinder::guessDsoPath()
3125
{
3226
std::string dsoPath = "";
33-
dirent** nameList;
34-
27+
3528
// First, search PATH for raas_render executable
3629
const std::string pathEnv = util::getenv<std::string>("PATH");
3730
if (pathEnv.empty()) {
3831
return "";
3932
}
40-
41-
size_t found = pathEnv.find(':');
42-
int numFound;
43-
std::string path;
33+
size_t found = pathEnv.find(sOsPathSep);
34+
std::filesystem::path path;
4435
if (found == std::string::npos) { // single path
45-
path = pathEnv;
46-
numFound = scandir(path.c_str(), &nameList, isMatching, alphasort);
36+
path = std::filesystem::path(pathEnv).make_preferred();
37+
38+
if (std::filesystem::exists(path)) {
39+
for (auto const& dirEntry : std::filesystem::directory_iterator(path)) {
40+
if (dirEntry.path().filename().string() == sRaasRender) {
41+
break;
42+
}
43+
}
44+
}
4745
} else {
4846
int counter = 0;
47+
bool pathFound = false;
4948
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) {
49+
path = std::filesystem::path(pathEnv.substr(counter, found - counter)).make_preferred();
50+
if (std::filesystem::exists(path)) {
51+
for ( const auto & dirEntry : std::filesystem::directory_iterator(path)) {
52+
std::string file = dirEntry.path().stem().string();
53+
if (file == sRaasRender) {
54+
pathFound = true;
55+
break;
56+
}
57+
}
58+
}
59+
if (pathFound) {
5360
break;
5461
}
5562
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);
63+
found = pathEnv.find(sOsPathSep, found + 1);
6364
}
6465
}
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);
7966

67+
if (!path.empty()) {
68+
dsoPath = std::filesystem::path(std::filesystem::absolute(path.parent_path()) / "rdl2dso").make_preferred().string();
69+
}
8070
return dsoPath;
8171
}
8272

@@ -85,14 +75,14 @@ std::string DsoFinder::find() {
8575
std::string dsoPathString = "."; // Search '.' path first
8676
if (const char* const dsoPathEnvVar = util::getenv<const char*>("RDL2_DSO_PATH")) {
8777
// append dso path as sourced from RDL2_DSO_PATH
88-
dsoPathString += ":" + std::string(dsoPathEnvVar);
78+
dsoPathString += sOsPathSep + std::string(dsoPathEnvVar);
8979
}
9080

9181
// finally, guess dso path based on location of raas_render
9282
std::string guessedDsoPath = guessDsoPath();
9383
if (!guessedDsoPath.empty()) {
9484
// append dso path as sourced from location of raas_render executable
95-
dsoPathString += ":" + guessedDsoPath;
85+
dsoPathString += sOsPathSep + guessedDsoPath;
9686
}
9787

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

126116
if (!dsoPath.empty()) {
127117
// prepend dso path as sourced from command line
128-
return dsoPath + ":" + findPath;
118+
return dsoPath + sOsPathSep + findPath;
129119
}
130120

131121
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
@@ -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)