Skip to content

Commit e106057

Browse files
Fixed freeze in the polling loop (#1688)
1 parent 3e13fce commit e106057

7 files changed

Lines changed: 27 additions & 26 deletions

File tree

obs-studio-client/source/callback-manager.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
bool globalCallback::isWorkerRunning = false;
3434
bool globalCallback::worker_stop = true;
35-
uint32_t globalCallback::sleepIntervalMS = 50;
35+
std::chrono::milliseconds globalCallback::sleepInterval(50);
3636
std::thread *globalCallback::worker_thread = nullptr;
3737
Napi::ThreadSafeFunction globalCallback::js_source_callback;
3838
Napi::ThreadSafeFunction globalCallback::js_transition_callback;
@@ -244,8 +244,6 @@ void globalCallback::worker()
244244
delete data;
245245
};
246246

247-
size_t totalSleepMS = 0;
248-
249247
while (!worker_stop && !m_all_workers_stop) {
250248
auto tp_start = std::chrono::high_resolution_clock::now();
251249

@@ -382,9 +380,9 @@ void globalCallback::worker()
382380
do_sleep:
383381
auto tp_end = std::chrono::high_resolution_clock::now();
384382
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(tp_end - tp_start);
385-
totalSleepMS = std::max(0, static_cast<int>(sleepIntervalMS - dur.count()));
386-
if (totalSleepMS > 0)
387-
std::this_thread::sleep_for(std::chrono::milliseconds(totalSleepMS));
383+
const auto sleepDuration = sleepInterval - dur;
384+
if (sleepDuration > std::chrono::milliseconds(0))
385+
std::this_thread::sleep_for(sleepDuration);
388386
}
389387
return;
390388
}

obs-studio-client/source/callback-manager.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
******************************************************************************/
1818

19+
#include <chrono>
1920
#include <mutex>
2021
#include <napi.h>
2122
#include <string>
@@ -60,7 +61,7 @@ struct SourceMessageInfoData {
6061
namespace globalCallback {
6162
extern bool isWorkerRunning;
6263
extern bool worker_stop;
63-
extern uint32_t sleepIntervalMS;
64+
extern std::chrono::milliseconds sleepInterval;
6465
extern std::thread *worker_thread;
6566
extern Napi::ThreadSafeFunction js_source_callback;
6667
extern Napi::ThreadSafeFunction js_transition_callback;
@@ -91,4 +92,4 @@ Napi::Value RemoveVolmeterCallback(const Napi::CallbackInfo &info);
9192

9293
Napi::Value RegisterSourceMessageCallback(const Napi::CallbackInfo &info);
9394
Napi::Value RemoveSourceMessageCallback(const Napi::CallbackInfo &info);
94-
}
95+
}

obs-studio-client/source/nodeobs_autoconfig.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
bool autoConfig::isWorkerRunning = false;
2323
bool autoConfig::worker_stop = true;
24-
uint32_t autoConfig::sleepIntervalMS = 33;
24+
std::chrono::milliseconds autoConfig::sleepInterval(33);
2525
Napi::ThreadSafeFunction autoConfig::js_thread;
2626
std::thread *autoConfig::worker_thread = nullptr;
2727
std::vector<std::thread *> autoConfig::ac_queue_task_workers;
@@ -36,8 +36,6 @@ sem_t *ac_sem;
3636

3737
void autoConfig::worker()
3838
{
39-
size_t totalSleepMS = 0;
40-
4139
while (!worker_stop) {
4240
auto tp_start = std::chrono::high_resolution_clock::now();
4341

@@ -66,8 +64,9 @@ void autoConfig::worker()
6664
do_sleep:
6765
auto tp_end = std::chrono::high_resolution_clock::now();
6866
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(tp_end - tp_start);
69-
totalSleepMS = sleepIntervalMS - dur.count();
70-
std::this_thread::sleep_for(std::chrono::milliseconds(totalSleepMS));
67+
const auto sleepDuration = sleepInterval - dur;
68+
if (sleepDuration > std::chrono::milliseconds(0))
69+
std::this_thread::sleep_for(sleepDuration);
7170
}
7271
return;
7372
}

obs-studio-client/source/nodeobs_autoconfig.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
******************************************************************************/
1818
#pragma once
19+
#include <chrono>
1920
#include <napi.h>
2021
#include "utility-v8.hpp"
2122
#ifdef WIN32
@@ -40,7 +41,7 @@ extern sem_t *ac_sem;
4041
namespace autoConfig {
4142
extern bool isWorkerRunning;
4243
extern bool worker_stop;
43-
extern uint32_t sleepIntervalMS;
44+
extern std::chrono::milliseconds sleepInterval;
4445
extern Napi::ThreadSafeFunction js_thread;
4546
extern std::thread *worker_thread;
4647
extern std::vector<std::thread *> ac_queue_task_workers;

obs-studio-client/source/nodeobs_service.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ enum VcamInstalledStatus : uint8_t { NotInstalled = 0, LegacyInstalled = 1, Inst
4343

4444
bool service::isWorkerRunning = false;
4545
bool service::worker_stop = true;
46-
uint32_t service::sleepIntervalMS = 33;
46+
std::chrono::milliseconds service::sleepInterval(33);
4747
std::thread *service::worker_thread = nullptr;
4848
Napi::ThreadSafeFunction service::js_thread;
4949
Napi::FunctionReference service::cb;
@@ -304,7 +304,6 @@ void service::worker()
304304
}
305305
data->sent = true;
306306
};
307-
size_t totalSleepMS = 0;
308307
std::vector<ServiceSignalInfo *> signalsList;
309308
while (!worker_stop) {
310309
auto tp_start = std::chrono::high_resolution_clock::now();
@@ -348,8 +347,9 @@ void service::worker()
348347

349348
auto tp_end = std::chrono::high_resolution_clock::now();
350349
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(tp_end - tp_start);
351-
totalSleepMS = sleepIntervalMS - dur.count();
352-
std::this_thread::sleep_for(std::chrono::milliseconds(totalSleepMS));
350+
const auto sleepDuration = sleepInterval - dur;
351+
if (sleepDuration > std::chrono::milliseconds(0))
352+
std::this_thread::sleep_for(sleepDuration);
353353
}
354354

355355
for (auto &signalData : signalsList) {

obs-studio-client/source/nodeobs_service.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
1717
******************************************************************************/
1818

19+
#include <chrono>
1920
#include <mutex>
2021
#include <napi.h>
2122
#include <thread>
@@ -40,7 +41,7 @@ namespace service {
4041

4142
extern bool isWorkerRunning;
4243
extern bool worker_stop;
43-
extern uint32_t sleepIntervalMS;
44+
extern std::chrono::milliseconds sleepInterval;
4445
extern std::thread *worker_thread;
4546
extern Napi::ThreadSafeFunction js_thread;
4647
extern Napi::FunctionReference cb;
@@ -79,4 +80,4 @@ Napi::Value OBS_service_updateVirtualCam(const Napi::CallbackInfo &info);
7980
Napi::Value OBS_service_installVirtualCamPlugin(const Napi::CallbackInfo &info);
8081
Napi::Value OBS_service_uninstallVirtualCamPlugin(const Napi::CallbackInfo &info);
8182
Napi::Value OBS_service_isVirtualCamPluginInstalled(const Napi::CallbackInfo &info);
82-
}
83+
}

obs-studio-client/source/worker-signals.hpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
******************************************************************************/
1818

1919
#pragma once
20+
#include <chrono>
2021
#include <napi.h>
2122
#include "osn-error.hpp"
2223
#include "utility.hpp"
@@ -36,15 +37,15 @@ class WorkerSignals {
3637
{
3738
isWorkerRunning = false;
3839
workerStop = true;
39-
sleepIntervalMS = 33;
40+
sleepInterval = std::chrono::milliseconds(33);
4041
workerThread = nullptr;
4142
};
4243
~WorkerSignals(){};
4344

4445
protected:
4546
bool isWorkerRunning;
4647
bool workerStop;
47-
uint32_t sleepIntervalMS;
48+
std::chrono::milliseconds sleepInterval;
4849
std::thread *workerThread;
4950
Napi::ThreadSafeFunction jsThread;
5051
Napi::FunctionReference cb;
@@ -79,7 +80,6 @@ class WorkerSignals {
7980
}
8081
data->sent = true;
8182
};
82-
size_t totalSleepMS = 0;
8383
std::vector<SignalOutput *> signalsList;
8484
while (!workerStop) {
8585
auto tp_start = std::chrono::high_resolution_clock::now();
@@ -122,8 +122,9 @@ class WorkerSignals {
122122

123123
auto tp_end = std::chrono::high_resolution_clock::now();
124124
auto dur = std::chrono::duration_cast<std::chrono::milliseconds>(tp_end - tp_start);
125-
totalSleepMS = sleepIntervalMS - dur.count();
126-
std::this_thread::sleep_for(std::chrono::milliseconds(totalSleepMS));
125+
const auto sleepDuration = sleepInterval - dur;
126+
if (sleepDuration > std::chrono::milliseconds(0))
127+
std::this_thread::sleep_for(sleepDuration);
127128
}
128129

129130
return;
@@ -140,4 +141,4 @@ class WorkerSignals {
140141
workerThread->join();
141142
}
142143
}
143-
};
144+
};

0 commit comments

Comments
 (0)