forked from dxFeed/dxfeed-c-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDXErrorHandling.c
243 lines (184 loc) · 6.01 KB
/
DXErrorHandling.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* The contents of this file are subject to the Mozilla Public License Version
* 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Initial Developer of the Original Code is Devexperts LLC.
* Portions created by the Initial Developer are Copyright (C) 2010
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
*/
/*
* Implementation of the error functions.
*/
#ifdef _WIN32
# pragma warning(push)
# pragma warning(disable : 5105)
# include <Windows.h>
# pragma warning(pop)
#endif /* _WIN32 */
#include "DXErrorHandling.h"
#include "DXErrorCodes.h"
#include "Logger.h"
#include "DXThreads.h"
#include "DXMemory.h"
#include "PrimitiveTypes.h"
/* -------------------------------------------------------------------------- */
/*
* Thread-specific stuff
*/
/* -------------------------------------------------------------------------- */
static int g_initialization_attempted = false;
static dx_key_t g_last_error_data_key;
static dx_error_code_t g_master_thread_last_error_code = dx_ec_success;
/* -------------------------------------------------------------------------- */
static void dx_key_data_destructor (void* data) {
if (data != &g_master_thread_last_error_code && data != NULL) {
dx_free(data);
}
}
static void dx_key_remover(void *data) {
if (g_initialization_attempted) {
dx_thread_data_key_destroy(g_last_error_data_key);
g_initialization_attempted = false;
}
}
/* -------------------------------------------------------------------------- */
/*
* Internal error handling helpers
*/
/* -------------------------------------------------------------------------- */
dx_error_function_result_t dx_check_error_code (dx_error_code_t error_code) {
if (error_code < 0 || error_code >= dx_ec_count) {
return dx_efr_invalid_error_code;
}
return dx_efr_success;
}
/* -------------------------------------------------------------------------- */
/*
* Error manipulation functions implementation
*/
/* -------------------------------------------------------------------------- */
dx_error_function_result_t dx_set_last_error(dx_error_code_t error_code) {
return dx_set_last_error_impl(error_code, true);
}
dx_error_function_result_t dx_set_last_error_impl(dx_error_code_t error_code, int with_logging) {
dx_error_code_t* error_data = NULL;
dx_error_function_result_t res;
if (error_code == dx_ec_success) {
/* that's a special case used to prune the previously stored error */
res = dx_efr_success;
} else {
res = dx_check_error_code(error_code);
}
if (res != dx_efr_success) {
return res;
}
if (!dx_init_error_subsystem()) {
return dx_efr_error_subsys_init_failure;
}
if (dx_is_thread_master()) {
error_data = &g_master_thread_last_error_code;
} else {
error_data = dx_get_thread_data(g_last_error_data_key);
if (error_data == NULL) {
return dx_efr_error_subsys_init_failure;
}
}
*error_data = error_code;
if (with_logging && error_code != dx_ec_success) {
dx_logging_error_by_code(error_code);
}
return dx_efr_success;
}
/* -------------------------------------------------------------------------- */
dx_error_function_result_t dx_get_last_error (int* error_code) {
dx_error_code_t* error_data = NULL;
if (error_code == NULL) {
return dx_efr_invalid_error_code;
}
if (!dx_init_error_subsystem()) {
return dx_efr_error_subsys_init_failure;
}
if (dx_is_thread_master()) {
error_data = &g_master_thread_last_error_code;
} else {
error_data = dx_get_thread_data(g_last_error_data_key);
if (error_data == NULL) {
return dx_efr_error_subsys_init_failure;
}
}
if (error_code == NULL) {
return dx_efr_invalid_error_code;
}
*error_code = *error_data;
return dx_efr_success;
}
/* -------------------------------------------------------------------------- */
int dx_pop_last_error () {
return (dx_set_last_error(dx_ec_success) == dx_efr_success);
}
/* -------------------------------------------------------------------------- */
int dx_init_error_subsystem (void) {
dx_error_code_t* error_data = NULL;
if (!g_initialization_attempted) {
g_initialization_attempted = true;
dx_mark_thread_master();
if (!dx_thread_data_key_create(&g_last_error_data_key, dx_key_data_destructor)) {
return false;
}
if (!dx_set_thread_data(g_last_error_data_key, &g_master_thread_last_error_code)) {
return false;
}
dx_register_process_destructor(&dx_key_remover, NULL);
return true;
} else if (dx_is_thread_master()) {
return true;
}
/* only the secondary threads reach here */
if (dx_get_thread_data(g_last_error_data_key) != NULL) {
return true;
}
error_data = (dx_error_code_t*)dx_calloc_no_ehm(1, sizeof(dx_error_code_t));
if (error_data == NULL) {
return false;
}
*error_data = dx_ec_success;
if (!dx_set_thread_data_no_ehm(g_last_error_data_key, (const void*)error_data)) {
dx_free(error_data);
return false;
}
return true;
}
/* -------------------------------------------------------------------------- */
/*
* Convenient helper functions implementation
*/
/* -------------------------------------------------------------------------- */
dx_error_code_t dx_get_error_code (void) {
int code;
if (dx_get_last_error(&code) == dx_efr_error_subsys_init_failure) {
return dx_ec_internal_assert_violation;
} else {
return code;
}
}
/* -------------------------------------------------------------------------- */
int dx_set_error_code (dx_error_code_t code) {
return dx_set_error_code_impl(code, true);
}
int dx_set_error_code_impl(dx_error_code_t code, int with_logging) {
dx_error_function_result_t res = dx_set_last_error_impl(code, with_logging);
if (res != dx_efr_success) {
dx_logging_info(L"Setting error code failed: code = %d, return value = %d", code, res);
}
return false;
}