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
|
/*-
* See the file LICENSE for redistribution information.
*
* Copyright (c) 1997, 1998
* Sleepycat Software. All rights reserved.
*
* @(#)LockExample.cpp 10.6 (Sleepycat) 10/18/98
*/
#include "config.h"
#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <errno.h>
#include <iostream.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#endif
#include <db_cxx.h>
#define LOCK_HOME "/var/tmp/lock"
char *progname = "LockExample"; // Program name.
//
// An example of a program using DBLock and related classes.
//
class LockExample : public DbEnv
{
public:
void run();
LockExample(const char *home);
private:
static const char FileName[];
// no need for copy and assignment
LockExample(const LockExample &);
operator = (const LockExample &);
};
static void usage(); // forward
int
main(int argc, char *argv[])
{
const char *home = LOCK_HOME;
int do_unlink = 0;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-h") == 0) {
home = argv[++i];
}
else if (strcmp(argv[i], "-u") == 0) {
do_unlink = 1;
}
else {
usage();
}
}
try {
if (do_unlink) {
LockExample temp(home);
DbLockTab::unlink(home, 1, &temp);
}
LockExample app(home);
app.run();
return 0;
}
catch (DbException &dbe) {
cerr << "AccessExample: " << dbe.what() << "\n";
return 1;
}
}
LockExample::LockExample(const char *home)
: DbEnv(home, 0, DB_CREATE|DB_INIT_LOCK)
{
set_error_stream(&cerr);
set_errpfx("LockExample");
}
void LockExample::run()
{
long held;
u_int32_t len, locker;
int did_get, ret;
DbLock *locks = 0;
int lockcount = 0;
char objbuf[1024];
int lockid = 0;
DbLockTab *lockTable = get_lk_info();
if (!lockTable) {
cerr << "LockExample: lock table not initialized\n";
return;
}
//
// Accept lock requests.
//
lockTable->id(&locker);
for (held = 0;;) {
cout << "Operation get/release [get]> ";
cout.flush();
char opbuf[16];
cin.getline(opbuf, sizeof(opbuf));
if (cin.eof())
break;
if ((len = strlen(opbuf)) <= 1 || strcmp(opbuf, "get") == 0) {
// Acquire a lock.
cout << "input object (text string) to lock> ";
cout.flush();
cin.getline(objbuf, sizeof(objbuf));
if (cin.eof())
break;
if ((len = strlen(objbuf)) <= 0)
continue;
char lockbuf[16];
do {
cout << "lock type read/write [read]> ";
cout.flush();
cin.getline(lockbuf, sizeof(lockbuf));
if (cin.eof())
break;
len = strlen(lockbuf);
} while (len >= 1 &&
strcmp(lockbuf, "read") != 0 &&
strcmp(lockbuf, "write") != 0);
db_lockmode_t lock_type;
if (len <= 1 || strcmp(lockbuf, "read") == 0)
lock_type = DB_LOCK_READ;
else
lock_type = DB_LOCK_WRITE;
Dbt dbt(objbuf, strlen(objbuf));
DbLock lock;
ret = lockTable->get(locker,
DB_LOCK_NOWAIT, &dbt, lock_type, &lock);
did_get = 1;
lockid = lockcount++;
if (locks == 0) {
locks = new DbLock[1];
}
else {
DbLock *newlocks = new DbLock[lockcount];
for (int lockno = 0; lockno < lockid; lockno++) {
newlocks[lockno] = locks[lockno];
}
delete locks;
locks = newlocks;
}
locks[lockid] = lock;
} else {
// Release a lock.
do {
cout << "input lock to release> ";
cout.flush();
cin.getline(objbuf, sizeof(objbuf));
if (cin.eof())
break;
} while ((len = strlen(objbuf)) <= 0);
lockid = strtol(objbuf, NULL, 16);
if (lockid < 0 || lockid >= lockcount) {
cout << "Lock #" << lockid << " out of range\n";
continue;
}
DbLock lock = locks[lockid];
ret = lock.put(lockTable);
did_get = 0;
}
switch (ret) {
case 0:
cout << "Lock #" << lockid << " "
<< (did_get ? "granted" : "released")
<< "\n";
held += did_get ? 1 : -1;
break;
case DB_LOCK_NOTHELD:
cout << "You do not hold the lock " << lockid << "\n";
break;
case DB_LOCK_NOTGRANTED:
cout << "Lock not granted\n";
break;
case DB_LOCK_DEADLOCK:
cerr << "LockExample: lock_"
<< (did_get ? "get" : "put")
<< ": " << "returned DEADLOCK";
break;
default:
cerr << "LockExample: lock_get: %s",
strerror(errno);
}
}
cout << "\n";
cout << "Closing lock region " << held << " locks held\n";
if (locks != 0)
delete locks;
}
static void
usage()
{
cerr << "usage: LockExample [-u] [-h home]\n";
exit(1);
}
|