File: stream.c

package info (click to toggle)
xfsdump 3.2.0-2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,012 kB
  • sloc: ansic: 45,797; sh: 3,449; makefile: 512
file content (297 lines) | stat: -rw-r--r-- 6,543 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
289
290
291
292
293
294
295
296
297
/*
 * Copyright (c) 2000-2001 Silicon Graphics, Inc.
 * All Rights Reserved.
 *
 * 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.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <pthread.h>
#include <assert.h>
#include <string.h>

#include "config.h"

#include "types.h"
#include "exit.h"
#include "stream.h"
#include "lock.h"
#include "mlog.h"

#define N(a) (sizeof((a)) / sizeof((a)[0]))

struct spm {
	stream_state_t	s_state;
	pthread_t	s_tid;
	int	s_ix;
	int		s_exit_code;
	rv_t		s_exit_return;
	rv_t		s_exit_hint;
};

typedef struct spm spm_t;
static spm_t spm[STREAM_SIMMAX * 3];
static bool_t initialized = BOOL_FALSE;

void
stream_init(void)
{
	(void)memset((void *)spm, 0, sizeof(spm));
	initialized = BOOL_TRUE;
}

/*
 * Note that the stream list structure (updated via the stream_* functions)
 * is indexed by pthread_t (tid). Multiple processes can be registered against
 * the same stream index, typically: the primary content process that does the
 * work; and the drive worker process, which just processes stuff off the ring
 * buffer. In general having multiple tids registered per stream is not an issue
 * for termination status reporting, as the mlog_exit* logging functions only
 * ever get called out of the primary content process.
 */

void
stream_register(pthread_t tid, int streamix)
{
	spm_t *p = spm;
	spm_t *ep = spm + N(spm);

	assert(streamix < STREAM_SIMMAX);

	lock();
	for (; p < ep; p++) {
		if (p->s_state == S_FREE) {
			p->s_state = S_RUNNING;
			break;
		}
	}
	unlock();
	assert(p < ep);

	if (p >= ep) return;

	p->s_tid = tid;
	p->s_ix = streamix;
	p->s_exit_code = -1;
	p->s_exit_return = RV_NONE;
	p->s_exit_hint = RV_NONE;
}

/* NOTE: lock() must be held when calling stream_dead() */
void
stream_dead(pthread_t tid)
{
	spm_t *p = spm;
	spm_t *ep = spm + N(spm);

	for (; p < ep; p++)
		if (pthread_equal(p->s_tid, tid)) {
			p->s_state = S_ZOMBIE;
			break;
		}
	assert(p < ep);
}

void
stream_free(pthread_t tid)
{
	spm_t *p = spm;
	spm_t *ep = spm + N(spm);

	lock();
	for (; p < ep; p++) {
		if (pthread_equal(p->s_tid, tid)) {
			(void) memset((void *) p, 0, sizeof(spm_t));
			p->s_state = S_FREE;
			break;
		}
	}
	unlock();
	assert(p < ep);
}

int
stream_find_all(stream_state_t states[], int nstates,
		 pthread_t tids[], int ntids)
{
	int i, count = 0;
	spm_t *p = spm;
	spm_t *ep = spm + N(spm);
	assert(nstates > 0 && ntids > 0);

	if (!initialized)
		return 0;

	/* lock - make sure we get a consistent snapshot of the stream status */
	lock();
	for (; p < ep && count < ntids; p++)
		for (i = 0; i < nstates; i++)
			if (p->s_state == states[i]) {
				tids[count++] = p->s_tid;
				break;
			}
	unlock();
	return count;
}

static spm_t *
stream_find(pthread_t tid, stream_state_t s[], int nstates)
{
	int i;
	spm_t *p = spm;
	spm_t *ep = spm + N(spm);

	assert(nstates > 0);

	/* note we don't lock the stream array in this function */
	for (; p < ep; p++)
		if (pthread_equal(p->s_tid, tid)) {
			/* check state */
			for (i = 0; i < nstates; i++)
				if (p->s_state == s[i])
					return p;
		}

#ifdef STREAM_DEBUG
	{
		static const char *state_strings[] = { "S_FREE", "S_RUNNING", "S_ZOMBIE" };
		mlog(MLOG_DEBUG | MLOG_ERROR | MLOG_NOLOCK | MLOG_BARE,
		      "stream_find(): no stream with tid: %lu and state%s:",
		      tid, nstates == 1 ? "" : "s");
		for (i = 0; i < nstates; i++)
			mlog(MLOG_DEBUG | MLOG_ERROR | MLOG_NOLOCK | MLOG_BARE,
			      " %s", state_strings[s[i]]);
		mlog(MLOG_DEBUG | MLOG_ERROR | MLOG_NOLOCK | MLOG_BARE, "\n");
	}
#endif /* STREAM_DEBUG */

	return (spm_t *) NULL;
}

/*
 * Note, the following function is called from mlog.c:mlog_va(),
 * where locking may be disabled and we are already protected by
 * another lock. So no locking is done in this function.
 */

int
stream_getix(pthread_t tid)
{
	stream_state_t states[] = { S_RUNNING };
	spm_t *p;
	int ix;
	p = stream_find(tid, states, N(states));
	ix = p ? p->s_ix : -1;
	return ix;
}


/*
 * We don't currently export spm_t and don't really want callers
 * keeping pointers into the stream array, so use explicit access
 * functions. Note we only expect these to be called from running/owner
 * streams.
 */

#define stream_set(field_name, tid, value)				\
	stream_state_t states[] = { S_RUNNING };			\
	spm_t *p;							\
	pthread_t mytid = pthread_self();				\
									\
	if (!pthread_equal(mytid, (tid))) {				\
		mlog(MLOG_DEBUG | MLOG_ERROR | MLOG_NOLOCK,		\
		      "stream_set_" #field_name "(): "			\
		      "foreign stream (tid %lu) "			\
		      "not permitted to update this stream (tid %lu)\n",\
		      mytid, (tid));					\
		return;							\
	}								\
									\
	lock();								\
	p = stream_find((tid), states, N(states));			\
	if (p) p->s_exit_ ## field_name = (value);			\
	unlock();

void stream_set_code(pthread_t tid, int exit_code)
{
	stream_set(code, tid, exit_code);
}

void stream_set_return(pthread_t tid, rv_t rv)
{
	stream_set(return, tid, rv);
}

void stream_set_hint(pthread_t tid, rv_t rv)
{
	stream_set(hint, tid, rv);
}


bool_t
stream_get_exit_status(pthread_t tid,
			stream_state_t states[],
			int nstates,
			stream_state_t *state,
			int *ix,
			int *exit_code,
			rv_t *exit_return,
			rv_t *exit_hint)
{
	bool_t sts = BOOL_FALSE;
	spm_t *p;

	lock();
	p = stream_find(tid, states, nstates);
	if (!p) goto unlock;

	if (state) *state = p->s_state;
	if (ix) *ix = p->s_ix;
	if (exit_code) *exit_code = p->s_exit_code;
	if (exit_return) *exit_return = p->s_exit_return;
	if (exit_hint) *exit_hint = p->s_exit_hint;
	sts = BOOL_TRUE;

 unlock:
	unlock();
	return sts;
}

size_t
stream_cnt(void)
{
	spm_t *p = spm;
	spm_t *ep = spm + N(spm);
	size_t ixmap = 0;
	size_t ixcnt;
	size_t bitix;

	assert(sizeof(ixmap) * NBBY >= STREAM_SIMMAX);

	lock();
	for (; p < ep; p++) {
		if (p->s_state == S_RUNNING) {
			ixmap |= (size_t)1 << p->s_ix;
		}
	}
	unlock();

	ixcnt = 0;
	for (bitix = 0; bitix < STREAM_SIMMAX; bitix++) {
		if (ixmap & ((size_t)1 << bitix)) {
			ixcnt++;
		}
	}

	return ixcnt;
}