LMMP 4.3.0
Lamina High-Precision Arithmetic Library
载入中...
搜索中...
未找到
abort.c
浏览该文件的文档.
1/**
2 * Copyright (C) 2026 HJimmyK (Jericho Knox)
3 *
4 * This file is part of LMMP.
5 *
6 * LMMP is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library. If not, see
18 * <https://www.gnu.org/licenses/>.
19 *
20 * SPDX-License-Identifier: LGPL-3.0-or-later
21 */
22
23#include "lmmp/lmmp.h"
24#include <stdio.h>
25#include <stdlib.h>
26
27#if defined(LMMP_WINDOWS)
28#include <windows.h>
29
30static CRITICAL_SECTION abort_func_cs;
31static INIT_ONCE abort_cs_once = INIT_ONCE_STATIC_INIT;
32
33static BOOL CALLBACK init_abort_cs(PINIT_ONCE once, PVOID param, PVOID* ctx) {
34 (void)once;
35 (void)param;
36 (void)ctx;
37 InitializeCriticalSection(&abort_func_cs);
38 return TRUE;
39}
40
41static void abort_func_lock(void) {
42 InitOnceExecuteOnce(&abort_cs_once, init_abort_cs, NULL, NULL);
43 EnterCriticalSection(&abort_func_cs);
44}
45
46static void abort_func_unlock(void) {
47 LeaveCriticalSection(&abort_func_cs);
48}
49
50#elif defined(LMMP_MACOS) || defined(LMMP_LINUX)
51#include <pthread.h>
52
53static pthread_mutex_t abort_func_mtx = PTHREAD_MUTEX_INITIALIZER;
54
55static void abort_func_lock(void) {
56 pthread_mutex_lock(&abort_func_mtx);
57}
58
59static void abort_func_unlock(void) {
60 pthread_mutex_unlock(&abort_func_mtx);
61}
62
63#else
64#ifdef __STDC_NO_THREADS__
65#error "Threads support is required for setting abort function"
66#else
67#include <threads.h>
68#endif
69
70static mtx_t abort_func_mtx;
71static once_flag abort_func_init_flag = ONCE_FLAG_INIT;
72
73static void init_abort_lock(void) {
74 mtx_init(&abort_func_mtx, mtx_plain);
75}
76
77static void abort_func_lock(void) {
78 call_once(&abort_func_init_flag, init_abort_lock);
79 mtx_lock(&abort_func_mtx);
80}
81
82static void abort_func_unlock(void) {
83 mtx_unlock(&abort_func_mtx);
84}
85
86#endif
87
89
98
99static const char* type_to_str(lmmp_error_t type) {
100 switch (type) {
102 return "ASSERT_FAILURE";
104 return "DEBUG_ASSERT_FAILURE";
106 return "PARAM_ASSERT_FAILURE";
108 return "MEMORY_ALLOC_FAILURE";
110 return "MEMORY_FREE_FAILURE";
112 return "OUT_OF_BOUNDS";
114 return "MEMORY_LEAK";
116 return "UNEXPECTED_ERROR";
117 default:
118 return "UNKNOWN_TYPE";
119 }
120}
121
122void lmmp_abort(lmmp_error_t type, const char* msg, const char* func, int line) {
126
127 if (fn != NULL) {
128 fn(type, msg, func, line);
129 } else {
130 fprintf(stderr, "LMMP abort at [%s]:%d\n", func, line);
131 fprintf(stderr, "Abort type: %s, abort msg: \n%s\n", type_to_str(type), msg);
132 fflush(stderr);
133 }
134 abort();
135}
static const char * type_to_str(lmmp_error_t type)
定义 abort.c:99
static BOOL CALLBACK init_abort_cs(PINIT_ONCE once, PVOID param, PVOID *ctx)
定义 abort.c:33
static void abort_func_unlock(void)
定义 abort.c:46
static CRITICAL_SECTION abort_func_cs
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义 abort.c:30
static void abort_func_lock(void)
定义 abort.c:41
static INIT_ONCE abort_cs_once
定义 abort.c:31
static lmmp_abort_fn lmmp_abort_func
定义 abort.c:88
lmmp_abort_fn lmmp_set_abort_fn(lmmp_abort_fn func)
设置 LMMP 全局退出函数(所有线程均生效)
定义 abort.c:90
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
void(*) lmmp_abort_fn(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数指针类型
定义 lmmp.h:299
lmmp_error_t
定义 lmmp.h:281
@ LMMP_ERROR_MEMORY_ALLOC_FAILURE
定义 lmmp.h:285
@ LMMP_ERROR_ASSERT_FAILURE
定义 lmmp.h:282
@ LMMP_ERROR_PARAM_ASSERT_FAILURE
定义 lmmp.h:284
@ LMMP_ERROR_MEMORY_FREE_FAILURE
定义 lmmp.h:286
@ LMMP_ERROR_UNEXPECTED_ERROR
定义 lmmp.h:289
@ LMMP_ERROR_MEMORY_LEAK
定义 lmmp.h:288
@ LMMP_ERROR_DEBUG_ASSERT_FAILURE
定义 lmmp.h:283
@ LMMP_ERROR_OUT_OF_BOUNDS
定义 lmmp.h:287