Sfoglia il codice sorgente

feat: 新增exit模块

SongZihuan 3 anni fa
parent
commit
9bfce03496
6 ha cambiato i file con 121 aggiunte e 10 eliminazioni
  1. 16 0
      include/tool/exit_.h
  2. 1 0
      include/tool/tool.h
  3. 2 2
      src/core/core_init.c
  4. 6 6
      src/main.c
  5. 84 0
      src/tool/exit_.c
  6. 12 2
      src/tool/log.c

+ 16 - 0
include/tool/exit_.h

@@ -0,0 +1,16 @@
+#ifndef AFUN_EXIT_H
+#define AFUN_EXIT_H
+#include <stdlib.h>
+
+typedef void aFunExitFunc(void *);
+
+#define aFunExitSuccess EXIT_SUCCESS
+#define aFunExitFail EXIT_FAILURE
+
+AFUN_TOOL_EXPORT _Noreturn void aFunExit(int exit_code);
+AFUN_TOOL_EXPORT int aFunTryExitPseudo(void);
+AFUN_TOOL_EXPORT int aFunExitPseudo(void);
+AFUN_TOOL_EXPORT int aFunAtExitTry(aFunExitFunc *func, void *data);
+AFUN_TOOL_EXPORT int aFunAtExit(aFunExitFunc *func, void *data);
+
+#endif //AFUN_EXIT_H

+ 1 - 0
include/tool/tool.h

@@ -12,6 +12,7 @@
 #include "aFunToolExport.h"
 #include "aFunToolExport.h"
 
 
 #include "stdio_.h"
 #include "stdio_.h"
+#include "exit_.h"
 #include "btye.h"
 #include "btye.h"
 #include "dlc.h"
 #include "dlc.h"
 #include "file.h"
 #include "file.h"

+ 2 - 2
src/core/core_init.c

@@ -15,7 +15,7 @@ char *log_path = NULL;
 char *lang_path = NULL;
 char *lang_path = NULL;
 char *varlib_path = NULL;
 char *varlib_path = NULL;
 
 
-static void destructCoreExit(void) {
+static void destructCoreExit(void *data) {
     free(log_path);
     free(log_path);
     free(lang_path);
     free(lang_path);
     free(varlib_path);
     free(varlib_path);
@@ -39,7 +39,7 @@ bool aFunCoreInit(aFunCoreInitInfo *info) {
     log_path = strJoin(info->base_dir, SEP aFunLogDir SEP, false, false);
     log_path = strJoin(info->base_dir, SEP aFunLogDir SEP, false, false);
     lang_path = strJoin(info->base_dir, SEP aFunLangDir SEP, false, false);
     lang_path = strJoin(info->base_dir, SEP aFunLangDir SEP, false, false);
     varlib_path = strJoin(info->base_dir, SEP aFunVarLibDir SEP, false, false);
     varlib_path = strJoin(info->base_dir, SEP aFunVarLibDir SEP, false, false);
-    atexit(destructCoreExit);
+    aFunAtExit(destructCoreExit, NULL);
 
 
     char *log = strJoin(log_path, "aFunlang", false, false);
     char *log = strJoin(log_path, "aFunlang", false, false);
     bool re = initLogSystem(log, info->log_asyn);
     bool re = initLogSystem(log, info->log_asyn);

+ 6 - 6
src/main.c

@@ -47,7 +47,7 @@ static Logger aFunlangLogger_;
 Logger *aFunlangLogger = &aFunlangLogger_;
 Logger *aFunlangLogger = &aFunlangLogger_;
 static bool tty_stdin = false;
 static bool tty_stdin = false;
 
 
-void freeBaseName(void) {
+void freeBaseName(void *_) {
     free(base_path);
     free(base_path);
 }
 }
 
 
@@ -57,10 +57,10 @@ int main(int argc, char **argv) {
     base_path = getExedir(1);
     base_path = getExedir(1);
     if (base_path == NULL)
     if (base_path == NULL)
         goto INIT_ERROR;
         goto INIT_ERROR;
-    atexit(freeBaseName);
+    aFunAtExit(freeBaseName, NULL);
 
 
     if (setjmp(main_buf) == 1)
     if (setjmp(main_buf) == 1)
-        return EXIT_FAILURE;
+        aFunExit(aFunExitFail);
 
 
     aFunInitInfo info = {.base_dir=base_path,
     aFunInitInfo info = {.base_dir=base_path,
 #ifdef aFunDEBUG
 #ifdef aFunDEBUG
@@ -74,12 +74,12 @@ int main(int argc, char **argv) {
 
 
     ff_FFlags *ff = ff_initFFlags(argc, argv, true, false, stderr, aFunlang_exe);
     ff_FFlags *ff = ff_initFFlags(argc, argv, true, false, stderr, aFunlang_exe);
     if (ff == NULL)
     if (ff == NULL)
-        return EXIT_FAILURE;
+        aFunExit(aFunExitFail);
 
 
     if (!aFunInit(&info)) {
     if (!aFunInit(&info)) {
 INIT_ERROR:
 INIT_ERROR:
         printf_stderr(0, "aFunlang init error.");
         printf_stderr(0, "aFunlang init error.");
-        return EXIT_FAILURE;
+        aFunExit(aFunExitFail);
     }
     }
 
 
     initLogger(aFunlangLogger, "aFunlang-exe", info.level);
     initLogger(aFunlangLogger, "aFunlang-exe", info.level);
@@ -101,7 +101,7 @@ INIT_ERROR:
 
 
     ff_freeFFlags(ff);
     ff_freeFFlags(ff);
     aFunDestruct();
     aFunDestruct();
-    return exit_code;
+    aFunExit(exit_code);
 }
 }
 
 
 static void printVersion(void) {
 static void printVersion(void) {

+ 84 - 0
src/tool/exit_.c

@@ -0,0 +1,84 @@
+#include "tool.h"
+#include "pthread.h"
+
+#define EXIT_FUNC_SIZE (1024)
+
+static pthread_mutex_t exit_mutex = PTHREAD_MUTEX_INITIALIZER;
+struct ExitFuncData {
+    aFunExitFunc *func;
+    void *data;
+} exit_func[EXIT_FUNC_SIZE];
+
+_Noreturn void aFunExit(int exit_code) {
+    if (pthread_mutex_trylock(&exit_mutex) == 0) {
+        int count = 0;
+        for (struct ExitFuncData *tmp = exit_func; tmp->func != NULL && count < EXIT_FUNC_SIZE; tmp++, count++)
+            tmp->func(tmp->data);
+        pthread_mutex_unlock(&exit_mutex);
+    }
+    exit(exit_code);
+}
+
+int aFunTryExitPseudo(void) {
+    if (pthread_mutex_trylock(&exit_mutex) == 0) {
+        int count = 0;
+        for (struct ExitFuncData *tmp = exit_func; tmp->func != NULL && count < EXIT_FUNC_SIZE; tmp++, count++) {
+            tmp->func(tmp->data);
+            tmp->data = NULL;
+            tmp->func = NULL;
+        }
+        pthread_mutex_unlock(&exit_mutex);
+        return 1;
+    }
+    return 0;
+}
+
+int aFunExitPseudo(void) {
+    if (pthread_mutex_lock(&exit_mutex) == 0) {
+        int count = 0;
+        for (struct ExitFuncData *tmp = exit_func; tmp->func != NULL && count < EXIT_FUNC_SIZE; tmp++, count++) {
+            tmp->func(tmp->data);
+            tmp->data = NULL;
+            tmp->func = NULL;
+        }
+        pthread_mutex_unlock(&exit_mutex);
+        return 1;
+    }
+    return 0;
+}
+
+int aFunAtExitTry(aFunExitFunc *func, void *data) {
+    if (pthread_mutex_trylock(&exit_mutex) == 0) {
+        struct ExitFuncData *tmp = exit_func;
+        int count = 0;
+        for (NULL; tmp->func != NULL; tmp++, count++) {
+            if (count >= EXIT_FUNC_SIZE) {
+                pthread_mutex_unlock(&exit_mutex);
+                return -1;
+            }
+        }
+        tmp->func = func;
+        tmp->data = data;
+        pthread_mutex_unlock(&exit_mutex);
+        return count;
+    }
+    return -1;
+}
+
+int aFunAtExit(aFunExitFunc *func, void *data) {
+    if (pthread_mutex_lock(&exit_mutex) == 0) {
+        struct ExitFuncData *tmp = exit_func;
+        int count = 0;
+        for (NULL; tmp->func != NULL; tmp++, count++) {
+            if (count >= EXIT_FUNC_SIZE) {
+                pthread_mutex_unlock(&exit_mutex);
+                return -1;
+            }
+        }
+        tmp->func = func;
+        tmp->data = data;
+        pthread_mutex_unlock(&exit_mutex);
+        return count;
+    }
+    return -1;
+}

+ 12 - 2
src/tool/log.c

@@ -8,6 +8,7 @@
  * file.h 中 getFileSize
  * file.h 中 getFileSize
  * stdio_.h
  * stdio_.h
  * str.h
  * str.h
+ * exit_.h
  * pthread.h
  * pthread.h
  */
  */
 
 
@@ -22,6 +23,7 @@
 #include "file.h"
 #include "file.h"
 #include "stdio_.h"
 #include "stdio_.h"
 #include "str.h"
 #include "str.h"
+#include "exit_.h"
 #include "pthread.h"
 #include "pthread.h"
 
 
 #ifdef aFunWIN32
 #ifdef aFunWIN32
@@ -71,6 +73,7 @@ static struct LogFactory {
 static pthread_mutex_t log_factory_mutex = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t log_factory_mutex = PTHREAD_MUTEX_INITIALIZER;
 #define MUTEX (&log_factory_mutex)
 #define MUTEX (&log_factory_mutex)
 
 
+static void destructLogSystemAtExit(void *data);
 static void *ansyWritrLog_(void *_);
 static void *ansyWritrLog_(void *_);
 
 
 /*
 /*
@@ -140,9 +143,14 @@ int initLogSystem(FilePath path, bool asyn){
     writeInfoLog(NULL, "Log system init success");
     writeInfoLog(NULL, "Log system init success");
     writeInfoLog(NULL, "Log .log size %lld", log_size);
     writeInfoLog(NULL, "Log .log size %lld", log_size);
     writeInfoLog(NULL, "Log .csv size %lld", csv_size);
     writeInfoLog(NULL, "Log .csv size %lld", csv_size);
+    aFunAtExit(destructLogSystemAtExit, NULL);
     return re;
     return re;
 }
 }
 
 
+static void destructLogSystemAtExit(void *data) {
+    destructLogSystem();
+}
+
 int destructLogSystem(void) {
 int destructLogSystem(void) {
     int re = 1;
     int re = 1;
     pthread_mutex_lock(MUTEX);
     pthread_mutex_lock(MUTEX);
@@ -371,11 +379,12 @@ int writeSendErrorLog_(Logger *logger, char *file, int line, char *func, char *f
     writeLog_(logger, aFunConsoleSendError, log_send_error, file, line, func, format, ap);
     writeLog_(logger, aFunConsoleSendError, log_send_error, file, line, func, format, ap);
 #endif
 #endif
 
 
+    destructLogSystem();
     if (buf != NULL) {
     if (buf != NULL) {
         initLogger(logger, NULL, 0);  // 清零
         initLogger(logger, NULL, 0);  // 清零
         longjmp(*buf, 1);
         longjmp(*buf, 1);
     } else
     } else
-        exit(EXIT_FAILURE);
+        aFunExit(aFunExitFail);
 #endif
 #endif
 }
 }
 
 
@@ -389,9 +398,10 @@ int writeFatalErrorLog_(Logger *logger, char *file, int line, char *func, int ex
     writeLog_(logger, aFunConsoleFatalError, log_fatal_error, file, line, func, format, ap);
     writeLog_(logger, aFunConsoleFatalError, log_fatal_error, file, line, func, format, ap);
 #endif
 #endif
 
 
+    destructLogSystem();
     if (exit_code == EXIT_SUCCESS)
     if (exit_code == EXIT_SUCCESS)
         abort();
         abort();
     else
     else
-        exit(exit_code);
+        aFunExit(exit_code);
 #endif
 #endif
 }
 }