Просмотр исходного кода

Refine library init/exit functions

bel 8 лет назад
Родитель
Сommit
3162552d17
2 измененных файлов с 31 добавлено и 8 удалено
  1. 15 4
      include/civetweb.h
  2. 16 4
      src/civetweb.c

+ 15 - 4
include/civetweb.h

@@ -49,12 +49,23 @@ extern "C" {
 #endif /* __cplusplus */
 #endif /* __cplusplus */
 
 
 
 
-/* Initialize this library. This should be called before any other function. */
-int mg_init_library(unsigned features);
+/* Initialize this library. This should be called once before any other
+ * function from this library. This function is not guaranteed to be
+ * thread safe.
+ * Parameters:
+ *   features: bit mask for features to be initialized.
+ * Return value:
+ *   initialized features
+ *   0: error
+ */
+unsigned mg_init_library(unsigned features);
 
 
 
 
-/* Un-initialize this library. */
-int mg_exit_library(void);
+/* Un-initialize this library.
+ * Return value:
+ *   0: error
+ */
+unsigned mg_exit_library(void);
 
 
 
 
 struct mg_context;    /* Handle for the HTTP service itself */
 struct mg_context;    /* Handle for the HTTP service itself */

+ 16 - 4
src/civetweb.c

@@ -14721,20 +14721,32 @@ mg_print_system_info(int prm1, char *prm2)
 }
 }
 
 
 
 
-/* Initialize this library. */
-int
+/* mg_init_library counter */
+static int mg_init_library_called = 0;
+
+
+/* Initialize this library. This function does not need to be thread safe. */
+unsigned
 mg_init_library(unsigned features)
 mg_init_library(unsigned features)
 {
 {
 	/* Currently we do nothing here. This is planned for Version 1.10.
 	/* Currently we do nothing here. This is planned for Version 1.10.
 	 * For now, we just add this function, so clients can be changed early. */
 	 * For now, we just add this function, so clients can be changed early. */
-	return 1;
+	if (mg_init_library_called <= 0) {
+		mg_init_library_called = 1;
+		return mg_check_feature(features & 0xFFu);
+	}
+	return 0;
 }
 }
 
 
 
 
 /* Un-initialize this library. */
 /* Un-initialize this library. */
-int
+unsigned
 mg_exit_library(void)
 mg_exit_library(void)
 {
 {
+	if (mg_init_library_called <= 0) {
+		return 0;
+	}
+	mg_init_library_called--;
 	return 1;
 	return 1;
 }
 }