LMMP
4.3.0
Lamina High-Precision Arithmetic Library
Toggle main menu visibility
载入中...
搜索中...
未找到
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
30
static
CRITICAL_SECTION
abort_func_cs
;
31
static
INIT_ONCE
abort_cs_once
= INIT_ONCE_STATIC_INIT;
32
33
static
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
41
static
void
abort_func_lock
(
void
) {
42
InitOnceExecuteOnce(&
abort_cs_once
,
init_abort_cs
, NULL, NULL);
43
EnterCriticalSection(&
abort_func_cs
);
44
}
45
46
static
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
53
static
pthread_mutex_t abort_func_mtx = PTHREAD_MUTEX_INITIALIZER;
54
55
static
void
abort_func_lock
(
void
) {
56
pthread_mutex_lock(&abort_func_mtx);
57
}
58
59
static
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
70
static
mtx_t abort_func_mtx;
71
static
once_flag abort_func_init_flag = ONCE_FLAG_INIT;
72
73
static
void
init_abort_lock(
void
) {
74
mtx_init(&abort_func_mtx, mtx_plain);
75
}
76
77
static
void
abort_func_lock
(
void
) {
78
call_once(&abort_func_init_flag, init_abort_lock);
79
mtx_lock(&abort_func_mtx);
80
}
81
82
static
void
abort_func_unlock
(
void
) {
83
mtx_unlock(&abort_func_mtx);
84
}
85
86
#endif
87
88
static
lmmp_abort_fn
lmmp_abort_func
= NULL;
89
90
lmmp_abort_fn
lmmp_set_abort_fn
(
lmmp_abort_fn
func) {
91
abort_func_lock
();
92
lmmp_abort_fn
old =
lmmp_abort_func
;
93
lmmp_abort_func
= func;
94
abort_func_unlock
();
95
96
return
old;
97
}
98
99
static
const
char
*
type_to_str
(
lmmp_error_t
type) {
100
switch
(type) {
101
case
LMMP_ERROR_ASSERT_FAILURE
:
102
return
"ASSERT_FAILURE"
;
103
case
LMMP_ERROR_DEBUG_ASSERT_FAILURE
:
104
return
"DEBUG_ASSERT_FAILURE"
;
105
case
LMMP_ERROR_PARAM_ASSERT_FAILURE
:
106
return
"PARAM_ASSERT_FAILURE"
;
107
case
LMMP_ERROR_MEMORY_ALLOC_FAILURE
:
108
return
"MEMORY_ALLOC_FAILURE"
;
109
case
LMMP_ERROR_MEMORY_FREE_FAILURE
:
110
return
"MEMORY_FREE_FAILURE"
;
111
case
LMMP_ERROR_OUT_OF_BOUNDS
:
112
return
"OUT_OF_BOUNDS"
;
113
case
LMMP_ERROR_MEMORY_LEAK
:
114
return
"MEMORY_LEAK"
;
115
case
LMMP_ERROR_UNEXPECTED_ERROR
:
116
return
"UNEXPECTED_ERROR"
;
117
default
:
118
return
"UNKNOWN_TYPE"
;
119
}
120
}
121
122
void
lmmp_abort
(
lmmp_error_t
type,
const
char
* msg,
const
char
* func,
int
line) {
123
abort_func_lock
();
124
lmmp_abort_fn
fn =
lmmp_abort_func
;
125
abort_func_unlock
();
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
}
type_to_str
static const char * type_to_str(lmmp_error_t type)
定义
abort.c:99
init_abort_cs
static BOOL CALLBACK init_abort_cs(PINIT_ONCE once, PVOID param, PVOID *ctx)
定义
abort.c:33
abort_func_unlock
static void abort_func_unlock(void)
定义
abort.c:46
abort_func_cs
static CRITICAL_SECTION abort_func_cs
Copyright (C) 2026 HJimmyK (Jericho Knox)
定义
abort.c:30
abort_func_lock
static void abort_func_lock(void)
定义
abort.c:41
abort_cs_once
static INIT_ONCE abort_cs_once
定义
abort.c:31
lmmp_abort_func
static lmmp_abort_fn lmmp_abort_func
定义
abort.c:88
lmmp.h
lmmp_set_abort_fn
lmmp_abort_fn lmmp_set_abort_fn(lmmp_abort_fn func)
设置 LMMP 全局退出函数(所有线程均生效)
定义
abort.c:90
lmmp_abort
void lmmp_abort(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数,内部错误或断言失败时调用,若设置了全局退出函数,则会调用该函数,否则会调用默认的退出函数。
定义
abort.c:122
lmmp_abort_fn
void(*) lmmp_abort_fn(lmmp_error_t type, const char *msg, const char *func, int line)
LMMP 全局退出函数指针类型
定义
lmmp.h:299
lmmp_error_t
lmmp_error_t
定义
lmmp.h:281
LMMP_ERROR_MEMORY_ALLOC_FAILURE
@ LMMP_ERROR_MEMORY_ALLOC_FAILURE
定义
lmmp.h:285
LMMP_ERROR_ASSERT_FAILURE
@ LMMP_ERROR_ASSERT_FAILURE
定义
lmmp.h:282
LMMP_ERROR_PARAM_ASSERT_FAILURE
@ LMMP_ERROR_PARAM_ASSERT_FAILURE
定义
lmmp.h:284
LMMP_ERROR_MEMORY_FREE_FAILURE
@ LMMP_ERROR_MEMORY_FREE_FAILURE
定义
lmmp.h:286
LMMP_ERROR_UNEXPECTED_ERROR
@ LMMP_ERROR_UNEXPECTED_ERROR
定义
lmmp.h:289
LMMP_ERROR_MEMORY_LEAK
@ LMMP_ERROR_MEMORY_LEAK
定义
lmmp.h:288
LMMP_ERROR_DEBUG_ASSERT_FAILURE
@ LMMP_ERROR_DEBUG_ASSERT_FAILURE
定义
lmmp.h:283
LMMP_ERROR_OUT_OF_BOUNDS
@ LMMP_ERROR_OUT_OF_BOUNDS
定义
lmmp.h:287
src
lmmp
lmmp
abort.c
生成于
, 为 LMMP使用
1.18.0