Skip to content

Commit faed3cf

Browse files
Merge pull request #1486 from paullouisageneau/thread-pool-size-api
Add SetThreadPoolSize to configure the global thread count
2 parents 2e4d6d8 + 7463e23 commit faed3cf

File tree

8 files changed

+61
-35
lines changed

8 files changed

+61
-35
lines changed

include/rtc/global.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ typedef std::function<void(LogLevel level, string message)> LogCallback;
3131

3232
RTC_CPP_EXPORT void InitLogger(LogLevel level, LogCallback callback = nullptr);
3333

34-
RTC_CPP_EXPORT void Preload();
35-
RTC_CPP_EXPORT std::shared_future<void> Cleanup();
34+
RTC_CPP_EXPORT void SetThreadPoolSize(unsigned int count); // 0: hardware concurrency
3635

3736
struct SctpSettings {
3837
// For the following settings, not set means optimized default
@@ -52,6 +51,10 @@ struct SctpSettings {
5251

5352
RTC_CPP_EXPORT void SetSctpSettings(SctpSettings s);
5453

54+
// Optional global preload and cleanup
55+
RTC_CPP_EXPORT void Preload();
56+
RTC_CPP_EXPORT std::shared_future<void> Cleanup();
57+
5558
RTC_CPP_EXPORT std::ostream &operator<<(std::ostream &out, LogLevel level);
5659

5760
} // namespace rtc

include/rtc/rtc.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -513,12 +513,10 @@ RTC_C_EXPORT int rtcGetWebSocketServerPort(int wsserver);
513513

514514
#endif
515515

516-
// Optional global preload and cleanup
517-
518-
RTC_C_EXPORT void rtcPreload(void);
519-
RTC_C_EXPORT void rtcCleanup(void);
516+
// Global settings
520517

521-
// SCTP global settings
518+
// Note: Applied when threads are spawned
519+
RTC_C_EXPORT int rtcSetThreadPoolSize(unsigned int count);
522520

523521
typedef struct {
524522
int recvBufferSize; // in bytes, <= 0 means optimized default
@@ -538,6 +536,10 @@ typedef struct {
538536
// Note: SCTP settings apply to newly-created PeerConnections only
539537
RTC_C_EXPORT int rtcSetSctpSettings(const rtcSctpSettings *settings);
540538

539+
// Optional global preload and cleanup
540+
RTC_C_EXPORT void rtcPreload(void);
541+
RTC_C_EXPORT void rtcCleanup(void);
542+
541543
#ifdef __cplusplus
542544
} // extern "C"
543545
#endif

src/capi.cpp

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ int rtcAddTrackEx(int pc, const rtcTrackInit *init) {
10961096
case RTC_CODEC_OPUS:
10971097
case RTC_CODEC_PCMU:
10981098
case RTC_CODEC_PCMA:
1099-
case RTC_CODEC_AAC:
1099+
case RTC_CODEC_AAC:
11001100
case RTC_CODEC_G722: {
11011101
auto audio = std::make_unique<Description::Audio>(mid, direction);
11021102
switch (init->codec) {
@@ -1687,28 +1687,11 @@ int rtcGetWebSocketServerPort(int wsserver) {
16871687

16881688
#endif
16891689

1690-
void rtcPreload() {
1691-
try {
1692-
rtc::Preload();
1693-
} catch (const std::exception &e) {
1694-
PLOG_ERROR << e.what();
1695-
}
1696-
}
1697-
1698-
void rtcCleanup() {
1699-
try {
1700-
size_t count = eraseAll();
1701-
if (count != 0) {
1702-
PLOG_INFO << count << " objects were not properly destroyed before cleanup";
1703-
}
1704-
1705-
if (rtc::Cleanup().wait_for(10s) == std::future_status::timeout)
1706-
throw std::runtime_error(
1707-
"Cleanup timeout (possible deadlock or undestructible object)");
1708-
1709-
} catch (const std::exception &e) {
1710-
PLOG_ERROR << e.what();
1711-
}
1690+
int rtcSetThreadPoolSize(unsigned int count) {
1691+
return wrap([&] {
1692+
SetThreadPoolSize(count);
1693+
return RTC_ERR_SUCCESS;
1694+
});
17121695
}
17131696

17141697
int rtcSetSctpSettings(const rtcSctpSettings *settings) {
@@ -1759,3 +1742,28 @@ int rtcSetSctpSettings(const rtcSctpSettings *settings) {
17591742
return RTC_ERR_SUCCESS;
17601743
});
17611744
}
1745+
1746+
void rtcPreload() {
1747+
try {
1748+
rtc::Preload();
1749+
} catch (const std::exception &e) {
1750+
PLOG_ERROR << e.what();
1751+
}
1752+
}
1753+
1754+
void rtcCleanup() {
1755+
try {
1756+
size_t count = eraseAll();
1757+
if (count != 0) {
1758+
PLOG_INFO << count << " objects were not properly destroyed before cleanup";
1759+
}
1760+
1761+
if (rtc::Cleanup().wait_for(10s) == std::future_status::timeout)
1762+
throw std::runtime_error(
1763+
"Cleanup timeout (possible deadlock or undestructible object)");
1764+
1765+
} catch (const std::exception &e) {
1766+
PLOG_ERROR << e.what();
1767+
}
1768+
}
1769+

src/global.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,12 @@ void InitLogger(plog::Severity severity, plog::IAppender *appender) {
8383
plogInit(severity, appender);
8484
}
8585

86+
void SetThreadPoolSize(unsigned int count) { impl::Init::Instance().setThreadPoolSize(count); }
87+
void SetSctpSettings(SctpSettings s) { impl::Init::Instance().setSctpSettings(std::move(s)); }
88+
8689
void Preload() { impl::Init::Instance().preload(); }
8790
std::shared_future<void> Cleanup() { return impl::Init::Instance().cleanup(); }
8891

89-
void SetSctpSettings(SctpSettings s) { impl::Init::Instance().setSctpSettings(std::move(s)); }
90-
9192
std::ostream &operator<<(std::ostream &out, LogLevel level) {
9293
switch (level) {
9394
case LogLevel::Fatal:

src/impl/init.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,12 @@ std::shared_future<void> Init::cleanup() {
9696
return mCleanupFuture;
9797
}
9898

99+
void Init::setThreadPoolSize(unsigned int count) {
100+
std::lock_guard lock(mMutex);
101+
mThreadPoolSize = count;
102+
103+
}
104+
99105
void Init::setSctpSettings(SctpSettings s) {
100106
std::lock_guard lock(mMutex);
101107
if (mGlobal)
@@ -118,8 +124,8 @@ void Init::doInit() {
118124
throw std::runtime_error("WSAStartup failed, error=" + std::to_string(WSAGetLastError()));
119125
#endif
120126

121-
int concurrency = std::thread::hardware_concurrency();
122-
int count = std::max(concurrency, MIN_THREADPOOL_SIZE);
127+
unsigned int count = mThreadPoolSize > 0 ? mThreadPoolSize : std::thread::hardware_concurrency();
128+
count = std::max(count, MIN_THREADPOOL_SIZE);
123129
PLOG_DEBUG << "Spawning " << count << " threads";
124130
ThreadPool::Instance().spawn(count);
125131

src/impl/init.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ class Init {
3232
init_token token();
3333
void preload();
3434
std::shared_future<void> cleanup();
35+
36+
void setThreadPoolSize(unsigned int count);
3537
void setSctpSettings(SctpSettings s);
3638

3739
private:
@@ -45,6 +47,7 @@ class Init {
4547
weak_ptr<void> mWeak;
4648
bool mInitialized = false;
4749
SctpSettings mCurrentSctpSettings = {};
50+
unsigned int mThreadPoolSize = 0;
4851
std::mutex mMutex;
4952
std::shared_future<void> mCleanupFuture;
5053

src/impl/internals.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const size_t DEFAULT_WS_MAX_MESSAGE_SIZE = 256 * 1024; // Default max message
4545

4646
const size_t RECV_QUEUE_LIMIT = 1024; // Max per-channel queue size (messages)
4747

48-
const int MIN_THREADPOOL_SIZE = 4; // Minimum number of threads in the global thread pool (>= 2)
48+
const unsigned int MIN_THREADPOOL_SIZE = 2; // Minimum number of threads in the global thread pool (>= 2)
4949

5050
const size_t DEFAULT_MTU = RTC_DEFAULT_MTU; // defined in rtc.h
5151

test/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ static const vector<Test> tests = {
9696
};
9797

9898
int main(int argc, char **argv) {
99+
rtc::SetThreadPoolSize(4);
100+
99101
int success_tests = 0;
100102
int failed_tests = 0;
101103
steady_clock::time_point startTime, endTime;
@@ -118,6 +120,7 @@ int main(int argc, char **argv) {
118120
cout << "Finished " << success_tests + failed_tests << " tests in " << durationS.count()
119121
<< "s (" << durationMs.count() << " ms). Succeeded: " << success_tests
120122
<< ". Failed: " << failed_tests << "." << endl;
123+
121124
/*
122125
// Benchmark
123126
try {

0 commit comments

Comments
 (0)