-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.c
164 lines (145 loc) · 3.64 KB
/
global.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
/**
* Copyright (C) 2015 "Frostsnow" (Wade T. Cline).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "global.h"
G_LOG_DEFINITION(debug)
G_LOG_DEFINITION(info)
G_LOG_DEFINITION(warn)
G_LOG_DEFINITION(error)
void g_flub_destructor(void* flub) {
free(flub);
}
int g_flub_init() {
struct flub* flub;
static int key_created = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int ret;
int ret2;
// Initialize key.
ret = pthread_mutex_lock(&mutex);
if (ret) {
g_log_error("Unable to lock mutex: '%s'", g_serr(ret));
return -1;
}
if (!key_created) {
ret = pthread_key_create(&g_flub_key, g_flub_destructor);
if (ret) {
g_log_error("Unable to create key: '%s'", g_serr(ret));
goto unlock;
}
key_created = 1;
}
unlock:
ret2 = pthread_mutex_unlock(&mutex);
if (ret2) {
g_log_error("Mutex unlock failed: '%s'", g_serr(ret2));
}
if (ret || ret2) {
return -1;
}
// Initialize buffer.
flub = (struct flub*)malloc(sizeof(struct flub));
if (!flub) {
g_log_error("Unable to allocate flub buffer");
return -1;
}
// Set buffer.
ret = pthread_setspecific(g_flub_key, flub);
if (ret) {
g_log_error("Unable to set pthread buffer: '%s'", g_serr(ret));
return -1;
}
return 0;
}
struct flub* g_flub_toss(char* format, ...) {
va_list ap;
struct flub* flub;
// Get the flub.
flub = pthread_getspecific(g_flub_key);
if (!flub) {
// Thread-specific flub not found. This should never happen
// so long as 'g_flub_init' is called first.
g_log_error("Getting thread-specific flub *FAILED*, panicing!");
exit(EXIT_FAILURE);
}
// Initialize the flub.
va_start(ap, format);
flub_tossv(flub, format, ap);
va_end(ap);
// Return the flub.
return (flub);
}
char* g_serr(int err) {
char* buf;
// Get buffer.
buf = pthread_getspecific(g_serr_key);
if (!buf) {
return "THREAD-SPECIFIC BUFFER FAILURE";
}
// Return string.
return strerror_r(errno, buf, G_SERR_SIZE);
}
void g_serr_destructor(void* buffer) {
free(buffer);
}
int g_serr_init() {
char* buffer;
char error[64];
static int key_created = 0;
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
int ret;
int ugh;
// Initialize key.
ugh = 0;
ret = pthread_mutex_lock(&mutex);
if (ret) {
g_log_error("Unable to lock mutex: '%s'",
strerror_r(ret, error, sizeof(error)));
return -1;
}
if (!key_created) {
ugh = pthread_key_create(&g_serr_key, g_serr_destructor);
if (ugh) {
g_log_error("Unable to create key: '%s'",
strerror_r(ret, error, sizeof(error)));
goto unlock;
}
key_created = 1;
}
unlock:
ret = pthread_mutex_unlock(&mutex);
if (ret) {
g_log_error("Mutex unlock failed: '%s'",
strerror_r(ret, error, sizeof(error)));
}
if (ugh || ret) {
return -1;
}
// Initialize buffer.
buffer = (char*)malloc(G_SERR_SIZE);
if (!buffer) {
g_log_error("Unable to allocate buffer");
return -1;
}
// Set buffer.
ret = pthread_setspecific(g_serr_key, buffer);
if (ret) {
g_log_error("Unable to set pthread buffer: '%s'",
strerror_r(ret, error, sizeof(error)));
return -1;
}
return 0;
}