File: globals.c

package info (click to toggle)
linux-gpib-user 4.3.7-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,760 kB
  • sloc: ansic: 10,381; perl: 1,120; xml: 375; makefile: 335; yacc: 335; tcl: 308; python: 173; php: 157; lex: 144; sh: 134; lisp: 94
file content (288 lines) | stat: -rw-r--r-- 7,028 bytes parent folder | download | duplicates (2)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
/***************************************************************************
                              lib/globals.c
                             -------------------

    begin                : Oct 2002
    copyright            : (C) 2002 by Frank Mori Hess
    email                : fmhess@users.sourceforge.net
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include "ib_internal.h"
#include <pthread.h>
#include <stdlib.h>


volatile int iberr = 0;
volatile int ibsta = 0;
volatile int ibcnt = 0;
volatile long ibcntl = 0;

static pthread_key_t ibsta_key;
static pthread_key_t iberr_key;
static pthread_key_t ibcntl_key;

static pthread_key_t async_ibsta_key;
static pthread_key_t async_iberr_key;
static pthread_key_t async_ibcntl_key;

static pthread_once_t global_keys_once = PTHREAD_ONCE_INIT;

static void global_keys_alloc()
{
	int retval;

	retval = pthread_key_create(&ibsta_key, free);
	if (retval)
		fprintf(stderr, "libgpib: failed to allocate TSD key!\n");
	retval = pthread_key_create(&iberr_key, free);
	if (retval
	    ) fprintf(stderr, "libgpib: failed to allocate TSD key!\n");
	retval = pthread_key_create(&ibcntl_key, free);
	if (retval)
		fprintf(stderr, "libgpib: failed to allocate TSD key!\n");

	retval = pthread_key_create(&async_ibsta_key, free);
	if (retval)
		fprintf(stderr, "libgpib: failed to allocate TSD key!\n");
	retval = pthread_key_create(&async_iberr_key, free);
	if (retval)
		fprintf(stderr, "libgpib: failed to allocate TSD key!\n");
	retval = pthread_key_create(&async_ibcntl_key, free);
	if (retval)
		fprintf(stderr, "libgpib: failed to allocate TSD key!\n");
}

void globals_alloc(void)
{
	int *ibsta_p, *iberr_p, *ibcntl_p;
	int *async_ibsta_p, *async_iberr_p, *async_ibcntl_p;

	pthread_once(&global_keys_once, global_keys_alloc);
	if (pthread_getspecific(ibsta_key) == NULL) {
		ibsta_p = malloc(sizeof(int));
		if (ibsta_p == NULL)
			fprintf(stderr, "libgpib: failed to allocate ibsta!\n");
		iberr_p = malloc(sizeof(int));
		if (iberr_p == NULL)
			fprintf(stderr, "libgpib: failed to allocate iberr!\n");
		ibcntl_p = malloc(sizeof(long));
		if (ibcntl_p == NULL)
			fprintf(stderr, "libgpib: failed to allocate ibcntl!\n");
		async_ibsta_p = malloc(sizeof(int));
		if (async_ibsta_p == NULL)
			fprintf(stderr, "libgpib: failed to allocate async_ibsta!\n");
		async_iberr_p = malloc(sizeof(int));
		if (async_iberr_p == NULL)
			fprintf(stderr, "libgpib: failed to allocate async_iberr!\n");
		async_ibcntl_p = malloc(sizeof(long));
		if (async_ibcntl_p == NULL)
			fprintf(stderr, "libgpib: failed to allocate async_ibcntl!\n");

		*ibsta_p = 0;
		*iberr_p = 0;
		*ibcntl_p = 0;
		*async_ibsta_p = 0;
		*async_iberr_p = 0;
		*async_ibcntl_p = 0;

		pthread_setspecific(ibsta_key, ibsta_p);
		pthread_setspecific(iberr_key, iberr_p);
		pthread_setspecific(ibcntl_key, ibcntl_p);
		pthread_setspecific(async_ibsta_key, async_ibsta_p);
		pthread_setspecific(async_iberr_key, async_iberr_p);
		pthread_setspecific(async_ibcntl_key, async_ibcntl_p);
	}
}

void setIbsta(int status)
{
	int *thread_ibsta;

	globals_alloc();
	thread_ibsta = pthread_getspecific(ibsta_key);
	if (thread_ibsta == NULL) {
		fprintf(stderr, "libgpib: failed to set ibsta TSD\n");
		return;
	}
	*thread_ibsta = status;
}

void setIberr(int error)
{
	int *thread_iberr;

	globals_alloc();
	thread_iberr = pthread_getspecific(iberr_key);
	if (thread_iberr == NULL) {
		fprintf(stderr, "libgpib: failed to set iberr TSD\n");
		return;
	}
	*thread_iberr = error;
}

void setIbcnt(long count)
{
	int *thread_ibcntl;

	globals_alloc();
	thread_ibcntl = pthread_getspecific(ibcntl_key);
	if (thread_ibcntl == NULL) {
		fprintf(stderr, "libgpib: failed to set ibcntl TSD\n");
		return;
	}
	*thread_ibcntl = count;
}

void setAsyncIbsta(int status)
{
	int *async_thread_ibsta;

	globals_alloc();
	async_thread_ibsta = pthread_getspecific(async_ibsta_key);
	if (async_thread_ibsta == NULL)	{
		fprintf(stderr, "libgpib: failed to set async_ibsta TSD\n");
		return;
	}
	*async_thread_ibsta = status;
}

void setAsyncIberr(int error)
{
	int *async_thread_iberr;

	globals_alloc();
	async_thread_iberr = pthread_getspecific(async_iberr_key);
	if (async_thread_iberr == NULL)	{
		fprintf(stderr, "libgpib: failed to set async_iberr TSD\n");
		return;
	}
	*async_thread_iberr = error;
}

void setAsyncIbcnt(long count)
{
	int *async_thread_ibcntl;

	globals_alloc();
	async_thread_ibcntl = pthread_getspecific(async_ibcntl_key);
	if (async_thread_ibcntl == NULL) {
		fprintf(stderr, "libgpib: failed to set async_ibcntl TSD\n");
		return;
	}
	*async_thread_ibcntl = count;
}

int ThreadIbsta(void)
{
	int *thread_ibsta;

	globals_alloc();

	thread_ibsta = pthread_getspecific(ibsta_key);
	if (thread_ibsta == NULL) {
		fprintf(stderr, "libgpib: failed to get ibsta TSD\n");
		return -1;
	}

	return *thread_ibsta;
}

int ThreadIberr(void)
{
	int *thread_iberr;

	globals_alloc();

	thread_iberr = pthread_getspecific(iberr_key);
	if (thread_iberr == NULL) {
		fprintf(stderr, "libgpib: failed to get iberr TSD\n");
		return -1;
	}

	return *thread_iberr;
}

int ThreadIbcnt(void)
{
	return ThreadIbcntl();
}

long ThreadIbcntl(void)
{
	int *thread_ibcntl;

	globals_alloc();
	thread_ibcntl = pthread_getspecific(ibcntl_key);
	if (thread_ibcntl == NULL) {
		fprintf(stderr, "libgpib: failed to get ibcntl TSD\n");
		return -1;
	}

	return *thread_ibcntl;
}

int AsyncIbsta(void)
{
	int *async_thread_ibsta;

	globals_alloc();

	async_thread_ibsta = pthread_getspecific(async_ibsta_key);
	if (async_thread_ibsta == NULL) {
		fprintf(stderr, "libgpib: failed to get async_ibsta TSD\n");
		return -1;
	}

	return *async_thread_ibsta;
}

int AsyncIberr(void)
{
	int *async_thread_iberr;

	globals_alloc();

	async_thread_iberr = pthread_getspecific(async_iberr_key);
	if (async_thread_iberr == NULL)	{
		fprintf(stderr, "libgpib: failed to get async_iberr TSD\n");
		return -1;
	}

	return *async_thread_iberr;
}

int AsyncIbcnt(void)
{
	return AsyncIbcntl();
}

long AsyncIbcntl(void)
{
	int *async_thread_ibcntl;

	globals_alloc();
	async_thread_ibcntl = pthread_getspecific(async_ibcntl_key);
	if (async_thread_ibcntl == NULL) {
		fprintf(stderr, "libgpib: failed to get async_ibcntl TSD\n");
		return -1;
	}

	return *async_thread_ibcntl;
}

void sync_globals(void)
{
	ibsta = ThreadIbsta();
	iberr = ThreadIberr();
	ibcntl = ThreadIbcntl();
	ibcnt = ibcntl;
}