~ubuntu-branches/ubuntu/maverick/evolution-data-server/maverick-proposed

« back to all changes in this revision

Viewing changes to libdb/examples_cxx/LockExample.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Didier Roche
  • Date: 2010-05-17 17:02:06 UTC
  • mfrom: (1.1.79 upstream) (1.6.12 experimental)
  • Revision ID: james.westby@ubuntu.com-20100517170206-4ufr52vwrhh26yh0
Tags: 2.30.1-1ubuntu1
* Merge from debian experimental. Remaining change:
  (LP: #42199, #229669, #173703, #360344, #508494)
  + debian/control:
    - add Vcs-Bzr tag
    - don't use libgnome
    - Use Breaks instead of Conflicts against evolution 2.25 and earlier.
  + debian/evolution-data-server.install,
    debian/patches/45_libcamel_providers_version.patch:
    - use the upstream versioning, not a Debian-specific one 
  + debian/libedata-book1.2-dev.install, debian/libebackend-1.2-dev.install,
    debian/libcamel1.2-dev.install, debian/libedataserverui1.2-dev.install:
    - install html documentation
  + debian/rules:
    - don't build documentation it's shipped with the tarball

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*-
2
 
 * See the file LICENSE for redistribution information.
3
 
 *
4
 
 * Copyright (c) 1997-2002
5
 
 *      Sleepycat Software.  All rights reserved.
6
 
 *
7
 
 * $Id$
8
 
 */
9
 
 
10
 
#include <sys/types.h>
11
 
 
12
 
#include <errno.h>
13
 
#include <iostream>
14
 
#include <stdlib.h>
15
 
#include <string.h>
16
 
 
17
 
#include <db_cxx.h>
18
 
 
19
 
using std::cin;
20
 
using std::cout;
21
 
using std::cerr;
22
 
 
23
 
const char *progname = "LockExample";                           // Program name.
24
 
 
25
 
//
26
 
// An example of a program using DBLock and related classes.
27
 
//
28
 
class LockExample : public DbEnv
29
 
{
30
 
public:
31
 
        void run();
32
 
        int error_code() { return (ecode); }
33
 
 
34
 
        LockExample(const char *home, u_int32_t maxlocks, int do_unlink);
35
 
 
36
 
private:
37
 
        static const char FileName[];
38
 
        int ecode;
39
 
 
40
 
        // no need for copy and assignment
41
 
        LockExample(const LockExample &);
42
 
        void operator = (const LockExample &);
43
 
};
44
 
 
45
 
static int usage();          // forward
46
 
 
47
 
int
48
 
main(int argc, char *argv[])
49
 
{
50
 
        const char *home;
51
 
        int do_unlink;
52
 
        u_int32_t maxlocks;
53
 
        int i;
54
 
 
55
 
        home = "TESTDIR";
56
 
        maxlocks = 0;
57
 
        do_unlink = 0;
58
 
        for (int argnum = 1; argnum < argc; ++argnum) {
59
 
                if (strcmp(argv[argnum], "-h") == 0) {
60
 
                        if (++argnum >= argc)
61
 
                                return (usage());
62
 
                        home = argv[argnum];
63
 
                }
64
 
                else if (strcmp(argv[argnum], "-m") == 0) {
65
 
                        if (++argnum >= argc)
66
 
                                return (usage());
67
 
                        if ((i = atoi(argv[argnum])) <= 0)
68
 
                                return (usage());
69
 
                        maxlocks = (u_int32_t)i;  /* XXX: possible overflow. */
70
 
                }
71
 
                else if (strcmp(argv[argnum], "-u") == 0) {
72
 
                        do_unlink = 1;
73
 
                }
74
 
                else {
75
 
                        return (usage());
76
 
                }
77
 
        }
78
 
 
79
 
        try {
80
 
                int ecode;
81
 
 
82
 
                if (do_unlink) {
83
 
                        // Create an environment that immediately
84
 
                        // removes all files.
85
 
                        LockExample tmp(home, maxlocks, do_unlink);
86
 
                        if ((ecode = tmp.error_code()) != 0)
87
 
                                return (ecode);
88
 
                }
89
 
 
90
 
                LockExample app(home, maxlocks, do_unlink);
91
 
                if ((ecode = app.error_code()) != 0)
92
 
                        return (ecode);
93
 
                app.run();
94
 
                app.close(0);
95
 
                return (EXIT_SUCCESS);
96
 
        }
97
 
        catch (DbException &dbe) {
98
 
                cerr << "LockExample: " << dbe.what() << "\n";
99
 
                return (EXIT_FAILURE);
100
 
        }
101
 
}
102
 
 
103
 
LockExample::LockExample(const char *home, u_int32_t maxlocks, int do_unlink)
104
 
:       DbEnv(0)
105
 
,       ecode(0)
106
 
{
107
 
        int ret;
108
 
 
109
 
        if (do_unlink) {
110
 
                if ((ret = remove(home, DB_FORCE)) != 0) {
111
 
                        cerr << progname << ": DbEnv::remove: "
112
 
                             << strerror(errno) << "\n";
113
 
                        ecode = EXIT_FAILURE;
114
 
                }
115
 
        }
116
 
        else {
117
 
                set_error_stream(&cerr);
118
 
                set_errpfx("LockExample");
119
 
                if (maxlocks != 0)
120
 
                        set_lk_max_locks(maxlocks);
121
 
                open(home, DB_CREATE | DB_INIT_LOCK, 0);
122
 
        }
123
 
}
124
 
 
125
 
void LockExample::run()
126
 
{
127
 
        long held;
128
 
        u_int32_t len, locker;
129
 
        int did_get, ret;
130
 
        DbLock *locks = 0;
131
 
        int lockcount = 0;
132
 
        char objbuf[1024];
133
 
        int lockid = 0;
134
 
 
135
 
        //
136
 
        // Accept lock requests.
137
 
        //
138
 
        lock_id(&locker);
139
 
        for (held = 0;;) {
140
 
                cout << "Operation get/release [get]> ";
141
 
                cout.flush();
142
 
 
143
 
                char opbuf[16];
144
 
                cin.getline(opbuf, sizeof(opbuf));
145
 
                if (cin.eof())
146
 
                        break;
147
 
                if ((len = strlen(opbuf)) <= 1 || strcmp(opbuf, "get") == 0) {
148
 
                        // Acquire a lock.
149
 
                        cout << "input object (text string) to lock> ";
150
 
                        cout.flush();
151
 
                        cin.getline(objbuf, sizeof(objbuf));
152
 
                        if (cin.eof())
153
 
                                break;
154
 
                        if ((len = strlen(objbuf)) <= 0)
155
 
                                continue;
156
 
 
157
 
                        char lockbuf[16];
158
 
                        do {
159
 
                                cout << "lock type read/write [read]> ";
160
 
                                cout.flush();
161
 
                                cin.getline(lockbuf, sizeof(lockbuf));
162
 
                                if (cin.eof())
163
 
                                        break;
164
 
                                len = strlen(lockbuf);
165
 
                        } while (len >= 1 &&
166
 
                                 strcmp(lockbuf, "read") != 0 &&
167
 
                                 strcmp(lockbuf, "write") != 0);
168
 
 
169
 
                        db_lockmode_t lock_type;
170
 
                        if (len <= 1 || strcmp(lockbuf, "read") == 0)
171
 
                                lock_type = DB_LOCK_READ;
172
 
                        else
173
 
                                lock_type = DB_LOCK_WRITE;
174
 
 
175
 
                        Dbt dbt(objbuf, strlen(objbuf));
176
 
 
177
 
                        DbLock lock;
178
 
                        ret = lock_get(locker, DB_LOCK_NOWAIT, &dbt,
179
 
                                       lock_type, &lock);
180
 
                        did_get = 1;
181
 
                        lockid = lockcount++;
182
 
                        if (locks == NULL) {
183
 
                                locks = new DbLock[1];
184
 
                        }
185
 
                        else {
186
 
                                DbLock *newlocks = new DbLock[lockcount];
187
 
                                for (int lockno = 0; lockno < lockid; lockno++) {
188
 
                                        newlocks[lockno] = locks[lockno];
189
 
                                }
190
 
                                delete locks;
191
 
                                locks = newlocks;
192
 
                        }
193
 
                        locks[lockid] = lock;
194
 
                } else {
195
 
                        // Release a lock.
196
 
                        do {
197
 
                                cout << "input lock to release> ";
198
 
                                cout.flush();
199
 
                                cin.getline(objbuf, sizeof(objbuf));
200
 
                                if (cin.eof())
201
 
                                        break;
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";
206
 
                                continue;
207
 
                        }
208
 
                        DbLock lock = locks[lockid];
209
 
                        ret = lock_put(&lock);
210
 
                        did_get = 0;
211
 
                }
212
 
 
213
 
                switch (ret) {
214
 
                case 0:
215
 
                        cout << "Lock #" << lockid << " "
216
 
                             <<  (did_get ? "granted" : "released")
217
 
                             << "\n";
218
 
                        held += did_get ? 1 : -1;
219
 
                        break;
220
 
                case DB_LOCK_NOTGRANTED:
221
 
                        cout << "Lock not granted\n";
222
 
                        break;
223
 
                case DB_LOCK_DEADLOCK:
224
 
                        cerr << "LockExample: lock_"
225
 
                             << (did_get ? "get" : "put")
226
 
                             << ": " << "returned DEADLOCK";
227
 
                        break;
228
 
                default:
229
 
                        cerr << "LockExample: lock_get: %s",
230
 
                                strerror(errno);
231
 
                }
232
 
        }
233
 
        cout << "\n";
234
 
        cout << "Closing lock region " << held << " locks held\n";
235
 
        if (locks != 0)
236
 
                delete locks;
237
 
}
238
 
 
239
 
static int
240
 
usage()
241
 
{
242
 
        cerr << "usage: LockExample [-u] [-h home] [-m maxlocks]\n";
243
 
        return (EXIT_FAILURE);
244
 
}