Last active
August 7, 2021 22:17
-
-
Save JoniSt/bec52ad0a2f571006a047dcb98e57865 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/src/libsail/context.c b/src/libsail/context.c | |
index 77a706e9..9a8203c2 100644 | |
--- a/src/libsail/context.c | |
+++ b/src/libsail/context.c | |
@@ -40,6 +40,10 @@ sail_status_t sail_init_with_flags(int flags) { | |
return SAIL_OK; | |
} | |
+void sail_set_on_context_init(sail_status_t(* on_context_init)(int *flags)) { | |
+ g_on_sail_context_init = on_context_init; | |
+} | |
+ | |
void sail_finish(void) { | |
SAIL_LOG_INFO("Finish"); | |
diff --git a/src/libsail/context.h b/src/libsail/context.h | |
index c248d0da..ba0d8aa1 100644 | |
--- a/src/libsail/context.h | |
+++ b/src/libsail/context.h | |
@@ -97,6 +97,21 @@ enum SailInitFlags { | |
*/ | |
SAIL_EXPORT sail_status_t sail_init_with_flags(int flags); | |
+/* | |
+ * Registers a callback function that's called just before a new thread-local static context is initialized. | |
+ * | |
+ * The callback receives a pointer to the SailInitFlags used to create the new context. It may inspect | |
+ * and modify them. If the callback returns an error code other than SAIL_OK, context creation will | |
+ * fail with that error code. | |
+ * | |
+ * The callback is registered globally; there can only be one for the entire program. | |
+ * The callback may be NULL, in which case it will be unregistered. | |
+ * | |
+ * Note that this function is not thread-safe. It is an error to call this function while other threads | |
+ * may be creating new SAIL contexts. It is best to only call it once at program startup. | |
+ */ | |
+SAIL_EXPORT void sail_set_on_context_init(sail_status_t (* on_context_init)(int *flags)); | |
+ | |
/* | |
* Finalizes working with the thread-local static context that was implicitly or explicitly allocated by | |
* reading or writing functions. | |
diff --git a/src/libsail/context_private.c b/src/libsail/context_private.c | |
index 83d1941a..489d9b9d 100644 | |
--- a/src/libsail/context_private.c | |
+++ b/src/libsail/context_private.c | |
@@ -591,6 +591,8 @@ static void print_build_statistics(void) { | |
#endif | |
} | |
+SAIL_HIDDEN sail_status_t (* g_on_sail_context_init)(int *flags); | |
+ | |
/* Initializes the context and loads all the codec info files if the context is not initialized. */ | |
static sail_status_t init_context(struct sail_context *context, int flags) { | |
@@ -599,6 +601,14 @@ static sail_status_t init_context(struct sail_context *context, int flags) { | |
if (context->initialized) { | |
return SAIL_OK; | |
} | |
+ | |
+ sail_status_t callback_result = SAIL_OK; | |
+ if (g_on_sail_context_init) { | |
+ callback_result = g_on_sail_context_init(&flags); | |
+ } | |
+ if (callback_result != SAIL_OK) { | |
+ return callback_result; | |
+ } | |
context->initialized = true; | |
diff --git a/src/libsail/context_private.h b/src/libsail/context_private.h | |
index bdc4ed57..fb944d11 100644 | |
--- a/src/libsail/context_private.h | |
+++ b/src/libsail/context_private.h | |
@@ -79,4 +79,9 @@ SAIL_HIDDEN sail_status_t current_tls_context(struct sail_context **context); | |
*/ | |
SAIL_HIDDEN sail_status_t current_tls_context_with_flags(struct sail_context **context, int flags); | |
+/* | |
+ * Callback to call before context creation. | |
+ */ | |
+SAIL_HIDDEN extern sail_status_t (* g_on_sail_context_init)(int *flags); | |
+ | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment