Skip to content

LLM inference in C/C++

C++ ggml

ggml : make ggml_time_init idempotent (#24422)

An Long1a7c25bfdbee
diff --git a/ggml/src/ggml.c b/ggml/src/ggml.c
index 0f682fd185..440095f805 100644
--- a/ggml/src/ggml.c
+++ b/ggml/src/ggml.c
@@ -525,7 +525,11 @@ const char * ggml_commit(void) {
#if defined(_MSC_VER) || defined(__MINGW32__)
static int64_t timer_freq, timer_start;
-void ggml_time_init(void) {
+static BOOL CALLBACK ggml_time_init_once(PINIT_ONCE once, PVOID param, PVOID *ctx) {
+ UNUSED(once);
+ UNUSED(param);
+ UNUSED(ctx);
+
LARGE_INTEGER t;
QueryPerformanceFrequency(&t);
timer_freq = t.QuadPart;
@@ -535,6 +539,12 @@ void ggml_time_init(void) {
// We subtract the program start time to reduce the likelihood of that happening.
QueryPerformanceCounter(&t);
timer_start = t.QuadPart;
+
+ return TRUE;
+}
+void ggml_time_init(void) {
+ static INIT_ONCE once = INIT_ONCE_STATIC_INIT;
+ InitOnceExecuteOnce(&once, ggml_time_init_once, NULL, NULL);
}
int64_t ggml_time_ms(void) {
LARGE_INTEGER t;