Add way to specify sleep time for threaded update

This commit is contained in:
Stephen Seo 2019-11-13 14:06:48 +09:00
parent f9e1bca4c1
commit f2b4672318
3 changed files with 32 additions and 3 deletions

View file

@ -214,6 +214,8 @@ public:
std::atomic_bool threadRunning; std::atomic_bool threadRunning;
std::mutex mutex; std::mutex mutex;
std::chrono::milliseconds threadedSleepTime;
}; // struct Context }; // struct Context
Context *verifyContext(UDPC_HContext ctx); Context *verifyContext(UDPC_HContext ctx);

View file

@ -1205,7 +1205,7 @@ void UDPC::threadedUpdate(Context *ctx) {
ctx->update_impl(); ctx->update_impl();
ctx->mutex.unlock(); ctx->mutex.unlock();
nextNow = std::chrono::steady_clock::now(); nextNow = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::milliseconds(8) - (nextNow - now)); std::this_thread::sleep_for(ctx->threadedSleepTime - (nextNow - now));
} }
} }
@ -1336,6 +1336,28 @@ UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId,
return nullptr; return nullptr;
} }
ctx->flags.set(0); ctx->flags.set(0);
ctx->threadedSleepTime = std::chrono::milliseconds(8);
ctx->thread = std::thread(UDPC::threadedUpdate, ctx);
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized threaded UDPC");
return (UDPC_HContext) ctx;
}
UDPC_HContext UDPC_init_threaded_update_ms(
UDPC_ConnectionId listenId, int isClient, int updateMS) {
UDPC::Context *ctx = (UDPC::Context *)UDPC_init(listenId, isClient);
if(!ctx) {
return nullptr;
}
ctx->flags.set(0);
if(updateMS < 4) {
ctx->threadedSleepTime = std::chrono::milliseconds(4);
} else if(updateMS > 333) {
ctx->threadedSleepTime = std::chrono::milliseconds(333);
} else {
ctx->threadedSleepTime = std::chrono::milliseconds(updateMS);
}
ctx->thread = std::thread(UDPC::threadedUpdate, ctx); ctx->thread = std::thread(UDPC::threadedUpdate, ctx);
UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized threaded UDPC"); UDPC_CHECK_LOG(ctx, UDPC_LoggingType::UDPC_INFO, "Initialized threaded UDPC");

View file

@ -114,8 +114,13 @@ UDPC_ConnectionId UDPC_create_id_anyaddr(uint16_t port);
UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port); UDPC_ConnectionId UDPC_create_id_easy(const char *addrString, uint16_t port);
UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient); UDPC_HContext UDPC_init(UDPC_ConnectionId listenId, int isClient);
UDPC_HContext UDPC_init_threaded_update(UDPC_ConnectionId listenId, UDPC_HContext UDPC_init_threaded_update(
UDPC_ConnectionId listenId,
int isClient); int isClient);
UDPC_HContext UDPC_init_threaded_update_ms(
UDPC_ConnectionId listenId,
int isClient,
int updateMS);
void UDPC_destroy(UDPC_HContext ctx); void UDPC_destroy(UDPC_HContext ctx);