File: tcl_mutex.c

package info (click to toggle)
db5.3 5.3.28-12+deb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 163,332 kB
  • ctags: 82,990
  • sloc: ansic: 448,411; java: 111,824; tcl: 80,544; sh: 44,326; cs: 33,697; cpp: 21,604; perl: 14,557; xml: 10,799; makefile: 4,106; yacc: 1,003; awk: 965; sql: 801; erlang: 342; python: 216; php: 24; asm: 14
file content (389 lines) | stat: -rw-r--r-- 9,175 bytes parent folder | download | duplicates (4)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2004, 2013 Oracle and/or its affiliates.  All rights reserved.
 *
 * $Id$
 */

#include "db_config.h"

#include "db_int.h"
#ifdef HAVE_SYSTEM_INCLUDE_FILES
#include <tcl.h>
#endif
#include "dbinc/tcl_db.h"

#ifdef CONFIG_TEST
/*
 * PUBLIC: int tcl_Mutex __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
 * PUBLIC:    DB_ENV *));
 *
 * tcl_Mutex --
 *      Implements dbenv->mutex_alloc method.
 */
int
tcl_Mutex(interp, objc, objv, dbenv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DB_ENV *dbenv;			/* Environment */
{
	static const char *which[] = {
		"-process_only",
		"-self_block",
		NULL
	};
	enum which {
		PROCONLY,
		SELFBLOCK
	};
	int arg, i, result, ret;
	u_int32_t flags;
	db_mutex_t indx;
	Tcl_Obj *res;

	result = TCL_OK;
	flags = 0;
	Tcl_ResetResult(interp);
	if (objc < 2) {
		Tcl_WrongNumArgs(interp, 2, objv,
		    "-proccess_only | -self_block");
		return (TCL_ERROR);
	}

	i = 2;
	while (i < objc) {
		/*
		 * If there is an arg, make sure it is the right one.
		 */
		if (Tcl_GetIndexFromObj(interp, objv[i], which, "option",
		    TCL_EXACT, &arg) != TCL_OK)
			return (IS_HELP(objv[i]));
		i++;
		switch ((enum which)arg) {
		case PROCONLY:
			flags |= DB_MUTEX_PROCESS_ONLY;
			break;
		case SELFBLOCK:
			flags |= DB_MUTEX_SELF_BLOCK;
			break;
		}
	}
	res = NULL;
	ret = dbenv->mutex_alloc(dbenv, flags, &indx);
	if (ret != 0) {
		result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
		    "mutex_alloc");
		Tcl_SetResult(interp, "allocation failed", TCL_STATIC);
	} else {
		res = Tcl_NewWideIntObj((Tcl_WideInt)indx);
		Tcl_SetObjResult(interp, res);
	}
	return (result);
}

/*
 * PUBLIC: int tcl_MutFree __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
 * PUBLIC:    DB_ENV *));
 *
 * tcl_MutFree --
 *      Implements dbenv->mutex_free method.
 */
int
tcl_MutFree(interp, objc, objv, dbenv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DB_ENV *dbenv;			/* Environment */
{
	int result, ret;
	Tcl_WideInt tmp;
	db_mutex_t indx;

	if (objc != 3) {
		Tcl_WrongNumArgs(interp, 3, objv, "mutexid");
		return (TCL_ERROR);
	}
	if ((result = Tcl_GetWideIntFromObj(interp, objv[2], &tmp)) != TCL_OK)
		return (result);
	indx = (db_mutex_t)tmp;
	ret = dbenv->mutex_free(dbenv, indx);
	return (_ReturnSetup(interp, ret, DB_RETOK_STD(ret), "env mutex_free"));
}

/*
 * PUBLIC: int tcl_MutGet __P((Tcl_Interp *, DB_ENV *, int));
 *
 * tcl_MutGet --
 *      Implements dbenv->mutex_get_* methods.
 */
int
tcl_MutGet(interp, dbenv, op)
	Tcl_Interp *interp;		/* Interpreter */
	DB_ENV *dbenv;			/* Environment */
	int op;				/* Which item to get */
{
	Tcl_Obj *res;
	u_int32_t val;
	int result, ret;

	res = NULL;
	val = 0;
	ret = 0;

	switch (op) {
	case DBTCL_MUT_ALIGN:
		ret = dbenv->mutex_get_align(dbenv, &val);
		break;
	case DBTCL_MUT_INCR:
		ret = dbenv->mutex_get_increment(dbenv, &val);
		break;
	case DBTCL_MUT_INIT:
		ret = dbenv->mutex_get_init(dbenv, &val);
		break;
	case DBTCL_MUT_MAX:
		ret = dbenv->mutex_get_max(dbenv, &val);
		break;
	case DBTCL_MUT_TAS:
		ret = dbenv->mutex_get_tas_spins(dbenv, &val);
		break;
	default:
		return (TCL_ERROR);
	}
	if ((result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret),
	    "mutex_get")) == TCL_OK) {
		res = Tcl_NewLongObj((long)val);
		Tcl_SetObjResult(interp, res);
	}
	return (result);
}

/*
 * PUBLIC: int tcl_MutLock __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
 * PUBLIC:    DB_ENV *));
 *
 * tcl_MutLock --
 *      Implements dbenv->mutex_lock method.
 */
int
tcl_MutLock(interp, objc, objv, dbenv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DB_ENV *dbenv;			/* Environment */
{
	int result, ret;
	Tcl_WideInt tmp;
	db_mutex_t indx;

	if (objc != 3) {
		Tcl_WrongNumArgs(interp, 3, objv, "mutexid");
		return (TCL_ERROR);
	}
	if ((result = Tcl_GetWideIntFromObj(interp, objv[2], &tmp)) != TCL_OK)
		return (result);
	indx = (db_mutex_t)tmp;
	ret = dbenv->mutex_lock(dbenv, indx);
	return (_ReturnSetup(interp, ret, DB_RETOK_STD(ret), "env mutex_lock"));
}

/*
 * PUBLIC: int tcl_MutSet __P((Tcl_Interp *, Tcl_Obj *,
 * PUBLIC:    DB_ENV *, int));
 *
 * tcl_MutSet --
 *      Implements dbenv->mutex_set methods.
 */
int
tcl_MutSet(interp, obj, dbenv, op)
	Tcl_Interp *interp;		/* Interpreter */
	Tcl_Obj *obj;			/* The argument object */
	DB_ENV *dbenv;			/* Environment */
	int op;				/* Which item to set */
{
	int result, ret;
	u_int32_t val;

	if ((result = _GetUInt32(interp, obj, &val)) != TCL_OK)
		return (result);
	switch (op) {
	case DBTCL_MUT_ALIGN:
		ret = dbenv->mutex_set_align(dbenv, val);
		break;
	case DBTCL_MUT_INCR:
		ret = dbenv->mutex_set_increment(dbenv, val);
		break;
	case DBTCL_MUT_INIT:
		ret = dbenv->mutex_set_init(dbenv, val);
		break;
	case DBTCL_MUT_MAX:
		ret = dbenv->mutex_set_max(dbenv, val);
		break;
	case DBTCL_MUT_TAS:
		ret = dbenv->mutex_set_tas_spins(dbenv, val);
		break;
	default:
		return (TCL_ERROR);
	}
	return (_ReturnSetup(interp, ret, DB_RETOK_STD(ret), "env mutex_set"));
}

/*
 * PUBLIC: int tcl_MutStat __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
 * PUBLIC:    DB_ENV *));
 *
 * tcl_MutStat --
 *      Implements dbenv->mutex_stat method.
 */
int
tcl_MutStat(interp, objc, objv, dbenv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DB_ENV *dbenv;			/* Environment */
{
	DB_MUTEX_STAT *sp;
	Tcl_Obj *res;
	u_int32_t flag;
	int result, ret;
	char *arg;

	result = TCL_OK;
	flag = 0;

	if (objc > 3) {
		Tcl_WrongNumArgs(interp, 2, objv, "?-clear?");
		return (TCL_ERROR);
	}

	if (objc == 3) {
		arg = Tcl_GetStringFromObj(objv[2], NULL);
		if (strcmp(arg, "-clear") == 0)
			flag = DB_STAT_CLEAR;
		else {
			Tcl_SetResult(interp,
			    "db stat: unknown arg", TCL_STATIC);
			return (TCL_ERROR);
		}
	}

	_debug_check();
	ret = dbenv->mutex_stat(dbenv, &sp, flag);
	result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "mutex stat");
	if (result == TCL_ERROR)
		return (result);

	res = Tcl_NewObj();
	MAKE_STAT_LIST("Mutex align", sp->st_mutex_align);
	MAKE_STAT_LIST("Mutex TAS spins", sp->st_mutex_tas_spins);
	MAKE_STAT_LIST("Initial mutex count", sp->st_mutex_init);
	MAKE_STAT_LIST("Mutex count", sp->st_mutex_cnt);
	MAKE_STAT_LIST("Mutex max", sp->st_mutex_max);
	MAKE_STAT_LIST("Free mutexes", sp->st_mutex_free);
	MAKE_STAT_LIST("Mutexes in use", sp->st_mutex_inuse);
	MAKE_STAT_LIST("Max in use", sp->st_mutex_inuse_max);
	MAKE_STAT_LIST("Mutex region size", sp->st_regsize);
	MAKE_STAT_LIST("Mutex region max", sp->st_regmax);
	MAKE_WSTAT_LIST("Number of region waits", sp->st_region_wait);
	MAKE_WSTAT_LIST("Number of region no waits", sp->st_region_nowait);
	Tcl_SetObjResult(interp, res);

	/*
	 * The 'error' label is used by the MAKE_STAT_LIST macro.
	 * Therefore we cannot remove it, and also we know that
	 * sp is allocated at that time.
	 */
error:	__os_ufree(dbenv->env, sp);
	return (result);
}

/*
 * PUBLIC: int tcl_MutStatPrint __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
 * PUBLIC:    DB_ENV *));
 *
 * tcl_MutStat --
 *      Implements dbenv->mutex_stat_print method.
 */
int
tcl_MutStatPrint(interp, objc, objv, dbenv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DB_ENV *dbenv;			/* Environment */
{
	static const char *mutstatprtopts[] = {
		"-all",
		"-clear",
		 NULL
	};
	enum mutstatprtopts {
		MUTSTATPRTALL,
		MUTSTATPRTCLEAR
	};
	u_int32_t flag;
	int i, optindex, result, ret;

	result = TCL_OK;
	flag = 0;
	i = 2;

	while (i < objc) {
		if (Tcl_GetIndexFromObj(interp, objv[i], mutstatprtopts, 
		    "option", TCL_EXACT, &optindex) != TCL_OK) {
			result = IS_HELP(objv[i]);
			goto error;
		}
		i++;
		switch ((enum mutstatprtopts)optindex) {
		case MUTSTATPRTALL:
			flag |= DB_STAT_ALL;
			break;
		case MUTSTATPRTCLEAR:
			flag |= DB_STAT_CLEAR;
			break;
		}
		if (result != TCL_OK)
			break;
	}
	if (result != TCL_OK)
		goto error;

	_debug_check();
	ret = dbenv->mutex_stat_print(dbenv, flag);
	result = _ReturnSetup(interp, 
	    ret, DB_RETOK_STD(ret), "dbenv mutex_stat_print");
error:
	return (result);
}

/*
 * PUBLIC: int tcl_MutUnlock __P((Tcl_Interp *, int, Tcl_Obj * CONST*,
 * PUBLIC:    DB_ENV *));
 *
 * tcl_MutUnlock --
 *      Implements dbenv->mutex_unlock method.
 */
int
tcl_MutUnlock(interp, objc, objv, dbenv)
	Tcl_Interp *interp;		/* Interpreter */
	int objc;			/* How many arguments? */
	Tcl_Obj *CONST objv[];		/* The argument objects */
	DB_ENV *dbenv;			/* Environment */
{
	int result, ret;
	Tcl_WideInt tmp;
	db_mutex_t indx;

	if (objc != 3) {
		Tcl_WrongNumArgs(interp, 3, objv, "mutexid");
		return (TCL_ERROR);
	}
	if ((result = Tcl_GetWideIntFromObj(interp, objv[2], &tmp)) != TCL_OK)
		return (result);
	indx = (db_mutex_t)tmp;
	ret = dbenv->mutex_unlock(dbenv, indx);
	return (_ReturnSetup(interp, ret, DB_RETOK_STD(ret),
	    "env mutex_unlock"));
}
#endif