File: cldmgr.c

package info (click to toggle)
xfsdump 3.1.9%2B0
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 3,932 kB
  • sloc: ansic: 45,863; sh: 3,227; makefile: 545
file content (254 lines) | stat: -rw-r--r-- 5,050 bytes parent folder | download | duplicates (3)
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
/*
 * 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 <unistd.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/prctl.h>
#include <errno.h>
#include <pthread.h>
#include <assert.h>
#include <string.h>

#include "config.h"

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

extern size_t pgsz;

#define CLD_MAX	(STREAM_SIMMAX * 2)

typedef enum { C_AVAIL, C_ALIVE, C_EXITED } state_t;

struct cld {
	state_t c_state;
	int c_exit_code;
	pthread_t c_tid;
	ix_t c_streamix;
	int (*c_entry)(void *arg1);
	void * c_arg1;
};

typedef struct cld cld_t;

static cld_t cld[CLD_MAX];
static bool_t cldmgr_stopflag;

static cld_t *cldmgr_getcld(void);
static void *cldmgr_entry(void *);
static void cldmgr_cleanup(void *);
/* REFERENCED */
static pthread_t cldmgr_parenttid;

bool_t
cldmgr_init(void)
{
	(void)memset((void *)cld, 0, sizeof(cld));
	cldmgr_stopflag = BOOL_FALSE;
	cldmgr_parenttid = pthread_self();

	return BOOL_TRUE;
}

bool_t
cldmgr_create(int (*entry)(void *arg1),
	       ix_t streamix,
	       char *descstr,
	       void *arg1)
{
	cld_t *cldp;
	int rval;

	assert(pthread_equal(pthread_self(), cldmgr_parenttid));

	cldp = cldmgr_getcld();
	if (!cldp) {
		mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_PROC, _(
		      "cannot create %s thread for stream %u: "
		      "too many child threads (max allowed is %d)\n"),
		      descstr,
		      streamix,
		      CLD_MAX);
		return BOOL_FALSE;
	}

	cldp->c_exit_code = EXIT_INTERRUPT;
	cldp->c_streamix = streamix;
	cldp->c_entry = entry;
	cldp->c_arg1 = arg1;
	rval = pthread_create(&cldp->c_tid, NULL, cldmgr_entry, cldp);
	if (rval) {
		mlog(MLOG_NORMAL | MLOG_ERROR | MLOG_PROC, _(
		      "failed creating %s thread for stream %u: %s\n"),
		      descstr,
		      streamix,
		      strerror(rval));
	} else {
		mlog(MLOG_NITTY | MLOG_PROC,
		      "%s thread created for stream %u: tid %lu\n",
		      descstr,
		      streamix,
		      cldp->c_tid);
	}

	return rval ? BOOL_FALSE : BOOL_TRUE;
}

void
cldmgr_stop(void)
{
	/* must NOT mlog here!
	 * locked up by main loop dialog
	 */
	cldmgr_stopflag = BOOL_TRUE;
}

int
cldmgr_join(void)
{
	cld_t *p = cld;
	cld_t *ep = cld + sizeof(cld) / sizeof(cld[0]);
	int xc = EXIT_NORMAL;

	lock();
	for (; p < ep; p++) {
		if (p->c_state == C_EXITED) {
			if ((int)(p->c_streamix) >= 0) {
				stream_dead(p->c_tid);
			}
			pthread_join(p->c_tid, NULL);
			if (p->c_exit_code != EXIT_NORMAL && xc != EXIT_FAULT)
				xc = p->c_exit_code;
			if (p->c_exit_code != EXIT_NORMAL) {
				mlog(MLOG_DEBUG | MLOG_PROC | MLOG_NOLOCK,
					"child (thread %lu) requested stop: "
					"exit code %d (%s)\n",
					p->c_tid, p->c_exit_code,
					exit_codestring(p->c_exit_code));
			}

			// reinit this child for reuse
			memset((void *)p, 0, sizeof(cld_t));
		}
	}
	unlock();

	return xc;
}

bool_t
cldmgr_stop_requested(void)
{
	return cldmgr_stopflag;
}

size_t
cldmgr_remainingcnt(void)
{
	cld_t *p = cld;
	cld_t *ep = cld + sizeof(cld) / sizeof(cld[0]);
	size_t cnt;

	cnt = 0;
	lock();
	for (; p < ep; p++) {
		if (p->c_state == C_ALIVE) {
			cnt++;
		}
	}
	unlock();

	return cnt;
}

bool_t
cldmgr_otherstreamsremain(ix_t streamix)
{
	cld_t *p = cld;
	cld_t *ep = cld + sizeof(cld) / sizeof(cld[0]);

	lock();
	for (; p < ep; p++) {
		if (p->c_state == C_ALIVE && p->c_streamix != streamix) {
			unlock();
			return BOOL_TRUE;
		}
	}
	unlock();

	return BOOL_FALSE;
}

static cld_t *
cldmgr_getcld(void)
{
	cld_t *p = cld;
	cld_t *ep = cld + sizeof(cld) / sizeof(cld[0]);

	lock();
	for (; p < ep; p++) {
		if (p->c_state == C_AVAIL) {
			p->c_state = C_ALIVE;
			break;
		}
	}
	unlock();

	return (p < ep) ? p : 0;
}

static void *
cldmgr_entry(void *arg1)
{
	cld_t *cldp = (cld_t *)arg1;
	pthread_t tid = pthread_self();

	pthread_cleanup_push(cldmgr_cleanup, arg1);

	if ((int)(cldp->c_streamix) >= 0) {
		stream_register(tid, (int)cldp->c_streamix);
	}
	mlog(MLOG_DEBUG | MLOG_PROC,
	      "thread %lu created for stream %d\n",
	      tid,
	      cldp->c_streamix);
	cldp->c_exit_code = (*cldp->c_entry)(cldp->c_arg1);

	pthread_cleanup_pop(1);

	return NULL;
}

static void
cldmgr_cleanup(void *arg1)
{
	cld_t *cldp = (cld_t *)arg1;

	lock();
	cldp->c_state = C_EXITED;
	// signal the main thread to look for exited threads
	kill(getpid(), SIGUSR1);
	unlock();
}