summaryrefslogtreecommitdiff
path: root/stl/dbstl_resource_manager.h
blob: 6bca540445c0df1218f40baa7614e3f06d851524 (plain)
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
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 2009 Oracle.  All rights reserved.
 *
 * $Id$
 */

#ifndef _DB_STL_RESOURCE_MANAGER_H__
#define _DB_STL_RESOURCE_MANAGER_H__

#include <map>
#include <vector>
#include <stack>
#include <set>

#include "dbstl_common.h"
#include "dbstl_inner_utility.h"

START_NS(dbstl)

class DbCursorBase;
using std::map;
using std::multimap;
using std::set;
using std::stack;

///////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//
// ResourceManager class definition
//
// This class manages all the Berkeley DB handles and their mapping
// relationship. When it's only thread-specific instance is destructed,
// these handles are automatically closed in the correct order (the db and
// dbenv handles will be closed when the last thread using the handles in
// the process is destructed).
//
// The Db* and DbEnv* handles are process-wide global, they can be shared
// among multithreads, so they are stored into a static stl map, and access
// to the two map objects (open_dbs_ & open_envs_) is protected by a process
// wide mutex because few stl implementations support multithreaded access.
// We use reference counting in the two map objects to make sure each handle
// is closed when the last thread using it exits. Each thread sharing the
// handle should call ResourceManager::register_db/dbenv to tell DBSTL that
// it will use the handle, otherwise the handle may be closed prematurely.
//
// The transaction and cursor handles are thread specific. They are stored
// into stl containers and each instance of the ResourceManager is stored
// into thread local storage(TLS) of each thread. Thus the DB STL containers
// are thread safe.
//

// This map contains cursors of all open databases opened in the same thread.
// We can only duplicate a cursor of the same database; We don't allow sharing
// Berkeley DB cursor handles across multiple threads, each thread needs to
// open their own cursor handle;
//
typedef map<Db *, set<dbstl::DbCursorBase *> *> db_csr_map_t;

// The set of cursors that belong to a db handle or a transaction.
typedef set<dbstl::DbCursorBase *> csrset_t;
// The cursors opened in each transaction.
typedef map<DbTxn *, set<DbCursorBase *> *> txncsr_t;

// This stack contains the transactions started in current thread. Each
// transaction is the child transaction of the one under it in the stack.
//
// We support nested transactions for those created by the dbstl
// users, but still keep reference counting for dbstl internally
// created transactions so that the autocommit methods are really
// autocommit with least overhead (nested transactions are overheads). The
// way we keep both nested transaction and reference counting to internal
// transactions in the same stack is:
// 1. When creating an external transaction, look at the stack top, if there
// is a transaction, it must be an external one too, so use it as the parent
// transaction to create the external transaction.
// 2. When creating an internal transaction, look at the stack top, if there
// is one, call it T, look for its the reference counting, if there is a
// reference count for it, T is an internal one, so we increment its
// reference count; Otherwise, T is an external one, and according to the DB
// autocommit semantics, this function should be in T's context, so we
// simply use T and add it to the reference counting structure, and set its
// reference count to 2.
//
// We don't support expanding a transaction across multiple threads,
// because there are many restrictions to doing so, making it meaningless.
//
typedef stack<DbTxn *> txnstk_t;
typedef map<DbEnv *, txnstk_t> env_txns_t;

#ifdef WIN32
#pragma warning( push )
#pragma warning( disable:4251 )
#endif
// This class is used to wrap a ResourceManager instance pointer, so that
// each thread has its own ResourceManager singleton.

#ifdef TLS_DECL_MODIFIER
template <Typename T>
class TlsWrapper
{
public:
	static T *get_tls_obj()
	{
		return tinst_;
	}

	static void set_tls_obj(T *objp)
	{
		tinst_ = objp;
	}

private:
	TlsWrapper(){}

	// Thread local pointer to the instance of type T.
	static TLS_DECL_MODIFIER T *tinst_;
}; // TlsWrapper<>

#elif defined(HAVE_PTHREAD_TLS)
template <Typename T>
class TlsWrapper
{
public:
	static T *get_tls_obj()
	{
		return static_cast<T*>(pthread_getspecific(tls_key_));
	}

	static void set_tls_obj(T *objp)
	{
		pthread_setspecific(tls_key_, objp);
	}

	// Friend declarations don't work portably, so we have to declare 
	// tls_key_ public.
	static pthread_key_t tls_key_;
private:
	TlsWrapper();

}; // TlsWrapper<>

#else
#error "A multi-threaded build of STL for Berkeley DB requires thread local storage.  None is configured."
#endif

class _exported ResourceManager : public DbstlGlobalInnerObject
{
private:

	ResourceManager(void);
	// open_dbs_ & open_envs_ are shared among threads, protected by
	// ghdl_mtx;
	static map<Db *, size_t> open_dbs_;
	static map<DbEnv *, size_t>open_envs_;

	// Transaction stack of all environments. Use a stack to allow nested
	// transactions. The transaction at the top of the stack is the
	// current active transaction.
	//
	env_txns_t env_txns_;

	// Cursors opened in a corresponding transaction context. When
	// committing or aborting a transaction, first close all open cursors.
	//
	txncsr_t txn_csrs_;

	// If in container X, its methods X::A and X::B both call begin and
	// commit transaction. X::A calls X::B after it's begin transaction
	// call, then X::B will commit the transaction prematurally. To avoid
	// will commit the only transaction prematurally, to avoid this, we use
	// this, we use this map to record each transaction's reference count.
	// Each begin/commit_txn() will increment/decrement the reference
	// count, when reference count goes to 0, the transaction is committed.
	// Abort_txn will unconditionally abort the transaction.
	//
	map<DbTxn *, size_t> txn_count_;

	// Contains the cursors opened in the current thread for each database,
	// So that we can close them in the right way, freeing any Berkeley DB
	// handles before exiting.
	//
	db_csr_map_t all_csrs_;

	// Remove cursors opened in the transaction txn's context, should be
	// called before commiting or aborting a transaction.
	//
	void remove_txn_cursor(DbTxn *txn);

	// Add a cursor to the current transaction's set of open cursors.
	void add_txn_cursor(DbCursorBase *dcbcsr, DbEnv *env);

	// The environment mtx_env_ and mtx_handle_ are used for synchronizing
	// multiple threads' access to open_dbs_ and open_envs_ and glob_objs_.
	// They are discarded when program exits, no deallocation/release
	// is done.
	static DbEnv *mtx_env_;
	static db_mutex_t mtx_handle_;
	static db_mutex_t mtx_globj_;
	static set<DbstlGlobalInnerObject *> glob_objs_;

	// This set stores db handles that are new'ed by open_db, and thus 
	// should be deleted after this db is closed automatically by dbstl.
	// If a db is new'ed and created by user without using open_db, users
	// should delete it.
	static set<Db *> deldbs; 
	static set<DbEnv *> delenvs; // Similar to deldbs, works with open_envs.
	static void set_global_callbacks();
public:
	
	// This function should be called in a single thread inside a process, 
	// before any use of dbstl.
	static void global_startup();
	// Delete registered DbstlGlobalInnerObject objects.
	static void global_exit();
	static void register_global_object(DbstlGlobalInnerObject *gio);
	static DbEnv *get_mutex_env() { return mtx_env_; }
	// Lock mtx_handle_, if it is 0, allocate it first.
	static int global_lock(db_mutex_t dbcontainer_mtx);
	static int global_unlock(db_mutex_t dbcontainer_mtx);
	// Close pdb regardless of its reference count, users must make sure
	// pdb is not used by others before calling this method. We can't
	// close by reference count in this method, otherwise when the thread
	// exits pdb's reference count is decremented twice.
	void close_db(Db *pdb);

	// Close all db handles regardless of reference count, used to clean up
	// the calling thread's ResourceManager singleton.
	void close_all_dbs();

	// Close specified db env handle and remove it from resource manager.
	void close_db_env(DbEnv *penv);

	// Close and remove all db env registered in the resource manager.
	// Used to clean up the calling thread's ResourceManager singleton.
	void close_all_db_envs();

	// Begin a new transaction in the specified environment. When outtxn
	// is non-zero support nested transactions - the new transaction will
	// be started as a child of the current transaction. If outtxn is 0
	// we are starting an internal transaction for autocommit, no new
	// transaction will be started, but the current transaction's
	// reference count will be incremented if it already has a reference
	// count; otherwise, it was created by user, and we simply use this
	// transaction, set its reference count to 2.
	//
	// This function is called by both containers to begin an internal
	// transaction for autocommit, and by db stl users to begin an
	// external transaction.
	//
	DbTxn *begin_txn(u_int32_t flags/* flags for DbEnv::txn_begin */,
	    DbEnv *env, int outtxn);

	// Decrement reference count if it exists and if it goes to 0, commit
	// current transaction T opened in env;
	// If T has no reference count(outside transaction), simply find
	// it by popping the stack and commit it.
	//
	// This function is called by db_container to commit a transaction
	// for auto commit, also can be called by db stl user to commit
	// an external explicit transaction.
	//
	void commit_txn(DbEnv *env, u_int32_t flags = 0);

	// Commit specified transaction txn: find it by popping the stack,
	// discard all its child transactions and commit it.
	//
	// This function is called by dbstl user to commit an external
	// explicit transaction.
	//
	void commit_txn(DbEnv *env, DbTxn *txn, u_int32_t flags = 0);

	// Abort current transaction of the environment.
	//
	void abort_txn(DbEnv *env);

	// Abort specified transaction: find it by popping the stack, discard
	// all its child transactions and abort it.
	//
	// This function is called by dbstl user to abort an external
	// explicit transaction.
	//
	void abort_txn(DbEnv *env, DbTxn *txn);

	// Set env's current transaction handle. The original transaction
	// handle is returned without aborting or commiting. This API can be
	// used to share a transaction handle among multiple threads.
	DbTxn* set_current_txn_handle(DbEnv *env, DbTxn *newtxn);

	// Register a Db handle. This handle and handles opened in it
	// will be closed by ResourceManager, so application code must not
	// try to close or delete it. Users can configure the handle before
	// opening the Db and then register it via this function.
	//
	void register_db(Db *pdb1);

	// Register a DbEnv handle. This handle and handles opened in it
	// will be closed by ResourceManager, so application code must not try
	// to close or delete it. Users can configure the handle before
	// opening the Environment and then register it via this function.
	//
	void register_db_env(DbEnv *env1);

	// Helper function to open a database and register it into
	// ResourceManager.
	// Users can set the create flags, open flags, db type, and flags
	// needing to be set before open.
	//
	Db* open_db (DbEnv *penv, const char *filename, DBTYPE dbtype,
	    u_int32_t oflags, u_int32_t set_flags, int mode = 0644,
	    DbTxn* txn = NULL, u_int32_t cflags = 0,
	    const char *dbname = NULL);

	// Helper function to open a dbenv and register it into
	// ResourceManager.
	// Users can set the create flags, open flags, db type, and flags
	// needing to be set before open.
	//
	DbEnv *open_env(const char *env_home, u_int32_t set_flags,
	    u_int32_t oflags = DB_CREATE | DB_INIT_MPOOL,
	    u_int32_t cachesize = 4 * 1024 * 1024, int mode = 0644,
	    u_int32_t cflags = 0/* Flags for DbEnv constructor. */);

	static ResourceManager *instance();

	// Release all registered resource in the right order.
	virtual ~ResourceManager(void);

	// Return current transaction of environment env, that is, the one on
	// the transaction stack top, the active one.
	//
	DbTxn *current_txn(DbEnv *env);

	// Open a Berkeley DB cursor.
	//
	int open_cursor(DbCursorBase *dcbcsr, Db *pdb, int flags = 0);

	// Add a db-cursor mapping.
	void add_cursor(Db *dbp, DbCursorBase *csr);

	// Close all cursors opened in the database.
	size_t close_db_cursors(Db *dbp1);

	// Close and remove a cursor from ResourceManager.
	//
	int remove_cursor(DbCursorBase *csr, bool remove_from_txncsrs = true);

}; // ResourceManager

END_NS
#ifdef WIN32
#pragma warning( pop )
#endif
#endif // !_DB_STL_RESOURCE_MANAGER_H__