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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1999-2001
* Sleepycat Software. All rights reserved.
*/
#include "db_config.h"
#ifndef lint
static const char revid[] = "$Id: tcl_util.c,v 11.35 2002/08/06 06:21:42 bostic Exp $";
#endif /* not lint */
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <tcl.h>
#endif
#include "db_int.h"
#include "dbinc/tcl_db.h"
/*
* Prototypes for procedures defined later in this file:
*/
static int mutex_Cmd __P((ClientData, Tcl_Interp *, int, Tcl_Obj * CONST*));
/*
* bdb_RandCommand --
* Implements rand* functions.
*
* PUBLIC: int bdb_RandCommand __P((Tcl_Interp *, int, Tcl_Obj * CONST*));
*/
int
bdb_RandCommand(interp, objc, objv)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
{
static char *rcmds[] = {
"rand", "random_int", "srand",
NULL
};
enum rcmds {
RRAND, RRAND_INT, RSRAND
};
long t;
int cmdindex, hi, lo, result, ret;
Tcl_Obj *res;
char msg[MSG_SIZE];
result = TCL_OK;
/*
* Get the command name index from the object based on the cmds
* defined above. This SHOULD NOT fail because we already checked
* in the 'berkdb' command.
*/
if (Tcl_GetIndexFromObj(interp,
objv[1], rcmds, "command", TCL_EXACT, &cmdindex) != TCL_OK)
return (IS_HELP(objv[1]));
res = NULL;
switch ((enum rcmds)cmdindex) {
case RRAND:
/*
* Must be 0 args. Error if different.
*/
if (objc != 2) {
Tcl_WrongNumArgs(interp, 2, objv, NULL);
return (TCL_ERROR);
}
ret = rand();
res = Tcl_NewIntObj(ret);
break;
case RRAND_INT:
/*
* Must be 4 args. Error if different.
*/
if (objc != 4) {
Tcl_WrongNumArgs(interp, 2, objv, "lo hi");
return (TCL_ERROR);
}
result = Tcl_GetIntFromObj(interp, objv[2], &lo);
if (result != TCL_OK)
break;
result = Tcl_GetIntFromObj(interp, objv[3], &hi);
if (result == TCL_OK) {
#ifndef RAND_MAX
#define RAND_MAX 0x7fffffff
#endif
t = rand();
if (t > RAND_MAX) {
snprintf(msg, MSG_SIZE,
"Max random is higher than %ld\n",
(long)RAND_MAX);
Tcl_SetResult(interp, msg, TCL_VOLATILE);
result = TCL_ERROR;
break;
}
_debug_check();
ret = (int)(((double)t / ((double)(RAND_MAX) + 1)) *
(hi - lo + 1));
ret += lo;
res = Tcl_NewIntObj(ret);
}
break;
case RSRAND:
/*
* Must be 1 arg. Error if different.
*/
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "seed");
return (TCL_ERROR);
}
result = Tcl_GetIntFromObj(interp, objv[2], &lo);
if (result == TCL_OK) {
srand((u_int)lo);
res = Tcl_NewIntObj(0);
}
break;
}
/*
* Only set result if we have a res. Otherwise, lower
* functions have already done so.
*/
if (result == TCL_OK && res)
Tcl_SetObjResult(interp, res);
return (result);
}
/*
*
* tcl_Mutex --
* Opens an env mutex.
*
* PUBLIC: int tcl_Mutex __P((Tcl_Interp *, int, Tcl_Obj * CONST*, DB_ENV *,
* PUBLIC: DBTCL_INFO *));
*/
int
tcl_Mutex(interp, objc, objv, envp, envip)
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
DB_ENV *envp; /* Environment pointer */
DBTCL_INFO *envip; /* Info pointer */
{
DBTCL_INFO *ip;
Tcl_Obj *res;
_MUTEX_DATA *md;
int i, mode, nitems, result, ret;
char newname[MSG_SIZE];
md = NULL;
result = TCL_OK;
mode = nitems = ret = 0;
memset(newname, 0, MSG_SIZE);
if (objc != 4) {
Tcl_WrongNumArgs(interp, 2, objv, "mode nitems");
return (TCL_ERROR);
}
result = Tcl_GetIntFromObj(interp, objv[2], &mode);
if (result != TCL_OK)
return (TCL_ERROR);
result = Tcl_GetIntFromObj(interp, objv[3], &nitems);
if (result != TCL_OK)
return (TCL_ERROR);
snprintf(newname, sizeof(newname),
"%s.mutex%d", envip->i_name, envip->i_envmutexid);
ip = _NewInfo(interp, NULL, newname, I_MUTEX);
if (ip == NULL) {
Tcl_SetResult(interp, "Could not set up info",
TCL_STATIC);
return (TCL_ERROR);
}
/*
* Set up mutex.
*/
/*
* Map in the region.
*
* XXX
* We don't bother doing this "right", i.e., using the shalloc
* functions, just grab some memory knowing that it's correctly
* aligned.
*/
_debug_check();
if (__os_calloc(NULL, 1, sizeof(_MUTEX_DATA), &md) != 0)
goto posixout;
md->env = envp;
md->n_mutex = nitems;
md->size = sizeof(_MUTEX_ENTRY) * nitems;
md->reginfo.type = REGION_TYPE_MUTEX;
md->reginfo.id = INVALID_REGION_TYPE;
md->reginfo.mode = mode;
md->reginfo.flags = REGION_CREATE_OK | REGION_JOIN_OK;
if ((ret = __db_r_attach(envp, &md->reginfo, md->size)) != 0)
goto posixout;
md->marray = md->reginfo.addr;
/* Initialize a created region. */
if (F_ISSET(&md->reginfo, REGION_CREATE))
for (i = 0; i < nitems; i++) {
md->marray[i].val = 0;
if ((ret = __db_mutex_init_int(envp,
&md->marray[i].m, i, 0)) != 0)
goto posixout;
}
R_UNLOCK(envp, &md->reginfo);
/*
* Success. Set up return. Set up new info
* and command widget for this mutex.
*/
envip->i_envmutexid++;
ip->i_parent = envip;
_SetInfoData(ip, md);
Tcl_CreateObjCommand(interp, newname,
(Tcl_ObjCmdProc *)mutex_Cmd, (ClientData)md, NULL);
res = Tcl_NewStringObj(newname, strlen(newname));
Tcl_SetObjResult(interp, res);
return (TCL_OK);
posixout:
if (ret > 0)
Tcl_PosixError(interp);
result = _ReturnSetup(interp, ret, DB_RETOK_STD(ret), "mutex");
_DeleteInfo(ip);
if (md != NULL) {
if (md->reginfo.addr != NULL)
(void)__db_r_detach(md->env,
&md->reginfo, F_ISSET(&md->reginfo, REGION_CREATE));
__os_free(md->env, md);
}
return (result);
}
/*
* mutex_Cmd --
* Implements the "mutex" widget.
*/
static int
mutex_Cmd(clientData, interp, objc, objv)
ClientData clientData; /* Mutex handle */
Tcl_Interp *interp; /* Interpreter */
int objc; /* How many arguments? */
Tcl_Obj *CONST objv[]; /* The argument objects */
{
static char *mxcmds[] = {
"close",
"get",
"getval",
"release",
"setval",
NULL
};
enum mxcmds {
MXCLOSE,
MXGET,
MXGETVAL,
MXRELE,
MXSETVAL
};
DB_ENV *dbenv;
DBTCL_INFO *envip, *mpip;
_MUTEX_DATA *mp;
Tcl_Obj *res;
int cmdindex, id, result, newval;
Tcl_ResetResult(interp);
mp = (_MUTEX_DATA *)clientData;
mpip = _PtrToInfo((void *)mp);
envip = mpip->i_parent;
dbenv = envip->i_envp;
result = TCL_OK;
if (mp == NULL) {
Tcl_SetResult(interp, "NULL mp pointer", TCL_STATIC);
return (TCL_ERROR);
}
if (mpip == NULL) {
Tcl_SetResult(interp, "NULL mp info pointer", TCL_STATIC);
return (TCL_ERROR);
}
/*
* Get the command name index from the object based on the dbcmds
* defined above.
*/
if (Tcl_GetIndexFromObj(interp,
objv[1], mxcmds, "command", TCL_EXACT, &cmdindex) != TCL_OK)
return (IS_HELP(objv[1]));
res = NULL;
switch ((enum mxcmds)cmdindex) {
case MXCLOSE:
if (objc != 2) {
Tcl_WrongNumArgs(interp, 1, objv, NULL);
return (TCL_ERROR);
}
_debug_check();
(void)__db_r_detach(mp->env, &mp->reginfo, 0);
res = Tcl_NewIntObj(0);
(void)Tcl_DeleteCommand(interp, mpip->i_name);
_DeleteInfo(mpip);
__os_free(mp->env, mp);
break;
case MXRELE:
/*
* Check for 1 arg. Error if different.
*/
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "id");
return (TCL_ERROR);
}
result = Tcl_GetIntFromObj(interp, objv[2], &id);
if (result != TCL_OK)
break;
MUTEX_UNLOCK(dbenv, &mp->marray[id].m);
res = Tcl_NewIntObj(0);
break;
case MXGET:
/*
* Check for 1 arg. Error if different.
*/
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "id");
return (TCL_ERROR);
}
result = Tcl_GetIntFromObj(interp, objv[2], &id);
if (result != TCL_OK)
break;
MUTEX_LOCK(dbenv, &mp->marray[id].m);
res = Tcl_NewIntObj(0);
break;
case MXGETVAL:
/*
* Check for 1 arg. Error if different.
*/
if (objc != 3) {
Tcl_WrongNumArgs(interp, 2, objv, "id");
return (TCL_ERROR);
}
result = Tcl_GetIntFromObj(interp, objv[2], &id);
if (result != TCL_OK)
break;
res = Tcl_NewLongObj((long)mp->marray[id].val);
break;
case MXSETVAL:
/*
* Check for 2 args. Error if different.
*/
if (objc != 4) {
Tcl_WrongNumArgs(interp, 2, objv, "id val");
return (TCL_ERROR);
}
result = Tcl_GetIntFromObj(interp, objv[2], &id);
if (result != TCL_OK)
break;
result = Tcl_GetIntFromObj(interp, objv[3], &newval);
if (result != TCL_OK)
break;
mp->marray[id].val = newval;
res = Tcl_NewIntObj(0);
break;
}
/*
* Only set result if we have a res. Otherwise, lower
* functions have already done so.
*/
if (result == TCL_OK && res)
Tcl_SetObjResult(interp, res);
return (result);
}
|