2
* See the file LICENSE for redistribution information.
4
* Copyright (c) 1997-2002
5
* Sleepycat Software. All rights reserved.
10
#include <sys/types.h>
23
const char *progname = "LockExample"; // Program name.
26
// An example of a program using DBLock and related classes.
28
class LockExample : public DbEnv
32
int error_code() { return (ecode); }
34
LockExample(const char *home, u_int32_t maxlocks, int do_unlink);
37
static const char FileName[];
40
// no need for copy and assignment
41
LockExample(const LockExample &);
42
void operator = (const LockExample &);
45
static int usage(); // forward
48
main(int argc, char *argv[])
58
for (int argnum = 1; argnum < argc; ++argnum) {
59
if (strcmp(argv[argnum], "-h") == 0) {
64
else if (strcmp(argv[argnum], "-m") == 0) {
67
if ((i = atoi(argv[argnum])) <= 0)
69
maxlocks = (u_int32_t)i; /* XXX: possible overflow. */
71
else if (strcmp(argv[argnum], "-u") == 0) {
83
// Create an environment that immediately
85
LockExample tmp(home, maxlocks, do_unlink);
86
if ((ecode = tmp.error_code()) != 0)
90
LockExample app(home, maxlocks, do_unlink);
91
if ((ecode = app.error_code()) != 0)
95
return (EXIT_SUCCESS);
97
catch (DbException &dbe) {
98
cerr << "LockExample: " << dbe.what() << "\n";
99
return (EXIT_FAILURE);
103
LockExample::LockExample(const char *home, u_int32_t maxlocks, int do_unlink)
110
if ((ret = remove(home, DB_FORCE)) != 0) {
111
cerr << progname << ": DbEnv::remove: "
112
<< strerror(errno) << "\n";
113
ecode = EXIT_FAILURE;
117
set_error_stream(&cerr);
118
set_errpfx("LockExample");
120
set_lk_max_locks(maxlocks);
121
open(home, DB_CREATE | DB_INIT_LOCK, 0);
125
void LockExample::run()
128
u_int32_t len, locker;
136
// Accept lock requests.
140
cout << "Operation get/release [get]> ";
144
cin.getline(opbuf, sizeof(opbuf));
147
if ((len = strlen(opbuf)) <= 1 || strcmp(opbuf, "get") == 0) {
149
cout << "input object (text string) to lock> ";
151
cin.getline(objbuf, sizeof(objbuf));
154
if ((len = strlen(objbuf)) <= 0)
159
cout << "lock type read/write [read]> ";
161
cin.getline(lockbuf, sizeof(lockbuf));
164
len = strlen(lockbuf);
166
strcmp(lockbuf, "read") != 0 &&
167
strcmp(lockbuf, "write") != 0);
169
db_lockmode_t lock_type;
170
if (len <= 1 || strcmp(lockbuf, "read") == 0)
171
lock_type = DB_LOCK_READ;
173
lock_type = DB_LOCK_WRITE;
175
Dbt dbt(objbuf, strlen(objbuf));
178
ret = lock_get(locker, DB_LOCK_NOWAIT, &dbt,
181
lockid = lockcount++;
183
locks = new DbLock[1];
186
DbLock *newlocks = new DbLock[lockcount];
187
for (int lockno = 0; lockno < lockid; lockno++) {
188
newlocks[lockno] = locks[lockno];
193
locks[lockid] = lock;
197
cout << "input lock to release> ";
199
cin.getline(objbuf, sizeof(objbuf));
202
} while ((len = strlen(objbuf)) <= 0);
203
lockid = strtol(objbuf, NULL, 16);
204
if (lockid < 0 || lockid >= lockcount) {
205
cout << "Lock #" << lockid << " out of range\n";
208
DbLock lock = locks[lockid];
209
ret = lock_put(&lock);
215
cout << "Lock #" << lockid << " "
216
<< (did_get ? "granted" : "released")
218
held += did_get ? 1 : -1;
220
case DB_LOCK_NOTGRANTED:
221
cout << "Lock not granted\n";
223
case DB_LOCK_DEADLOCK:
224
cerr << "LockExample: lock_"
225
<< (did_get ? "get" : "put")
226
<< ": " << "returned DEADLOCK";
229
cerr << "LockExample: lock_get: %s",
234
cout << "Closing lock region " << held << " locks held\n";
242
cerr << "usage: LockExample [-u] [-h home] [-m maxlocks]\n";
243
return (EXIT_FAILURE);