Debian Codegraph @

lang/cxx/stl/dbstl_resource_manager.h

raw
1 /*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2009, 2013 Oracle and/or its affiliates. All rights reserved.
5 *
6 * $Id$
7 */
8
9 #ifndef _DB_STL_RESOURCE_MANAGER_H__
10 #define _DB_STL_RESOURCE_MANAGER_H__
11
12 #include <map>
13 #include <vector>
14 #include <stack>
15 #include <set>
16
17 #include "dbstl_common.h"
18 #include "dbstl_inner_utility.h"
19
20 START_NS(dbstl)
21
22 class DbCursorBase;
23 using std::map;
24 using std::multimap;
25 using std::set;
26 using std::stack;
27
28 ///////////////////////////////////////////////////////////////////////
29 /////////////////////////////////////////////////////////////////////////
30 //
31 // ResourceManager class definition
32 //
33 // This class manages all the Berkeley DB handles and their mapping
34 // relationship. When it's only thread-specific instance is destructed,
35 // these handles are automatically closed in the correct order (the db and
36 // dbenv handles will be closed when the last thread using the handles in
37 // the process is destructed).
38 //
39 // The Db* and DbEnv* handles are process-wide global, they can be shared
40 // among multithreads, so they are stored into a static stl map, and access
41 // to the two map objects (open_dbs_ & open_envs_) is protected by a process
42 // wide mutex because few stl implementations support multithreaded access.
43 // We use reference counting in the two map objects to make sure each handle
44 // is closed when the last thread using it exits. Each thread sharing the
45 // handle should call ResourceManager::register_db/dbenv to tell DBSTL that
46 // it will use the handle, otherwise the handle may be closed prematurely.
47 //
48 // The transaction and cursor handles are thread specific. They are stored
49 // into stl containers and each instance of the ResourceManager is stored
50 // into thread local storage(TLS) of each thread. Thus the DB STL containers
51 // are thread safe.
52 //
53
54 // This map contains cursors of all open databases opened in the same thread.
55 // We can only duplicate a cursor of the same database; We don't allow sharing
56 // Berkeley DB cursor handles across multiple threads, each thread needs to
57 // open their own cursor handle;
58 //
59 typedef map<Db *, set<dbstl::DbCursorBase *> *> db_csr_map_t;
60
61 // The set of cursors that belong to a db handle or a transaction.
62 typedef set<dbstl::DbCursorBase *> csrset_t;
63 // The cursors opened in each transaction.
64 typedef map<DbTxn *, set<DbCursorBase *> *> txncsr_t;
65
66 // This stack contains the transactions started in current thread. Each
67 // transaction is the child transaction of the one under it in the stack.
68 //
69 // We support nested transactions for those created by the dbstl
70 // users, but still keep reference counting for dbstl internally
71 // created transactions so that the autocommit methods are really
72 // autocommit with least overhead (nested transactions are overheads). The
73 // way we keep both nested transaction and reference counting to internal
74 // transactions in the same stack is:
75 // 1. When creating an external transaction, look at the stack top, if there
76 // is a transaction, it must be an external one too, so use it as the parent
77 // transaction to create the external transaction.
78 // 2. When creating an internal transaction, look at the stack top, if there
79 // is one, call it T, look for its the reference counting, if there is a
80 // reference count for it, T is an internal one, so we increment its
81 // reference count; Otherwise, T is an external one, and according to the DB
82 // autocommit semantics, this function should be in T's context, so we
83 // simply use T and add it to the reference counting structure, and set its
84 // reference count to 2.
85 //
86 // We don't support expanding a transaction across multiple threads,
87 // because there are many restrictions to doing so, making it meaningless.
88 //
89 typedef stack<DbTxn *> txnstk_t;
90 typedef map<DbEnv *, txnstk_t> env_txns_t;
91
92 #ifdef WIN32
93 #pragma warning( push )
94 #pragma warning( disable:4251 )
95 #endif
96 // This class is used to wrap a ResourceManager instance pointer, so that
97 // each thread has its own ResourceManager singleton.
98
99 #ifdef TLS_DECL_MODIFIER
100 template <Typename T>
101 class TlsWrapper
102 {
103 public:
104 static T *get_tls_obj()
105 {
106 return tinst_;
107 }
108
109 static void set_tls_obj(T *objp)
110 {
111 tinst_ = objp;
112 }
113
114 private:
115 TlsWrapper(){}
116
117 // Thread local pointer to the instance of type T.
118 static TLS_DECL_MODIFIER T *tinst_;
119 }; // TlsWrapper<>
120
121 #elif defined(HAVE_PTHREAD_TLS)
122 template <Typename T>
123 class TlsWrapper
124 {
125 public:
126 static T *get_tls_obj()
127 {
128 return static_cast<T*>(pthread_getspecific(tls_key_));
129 }
130
131 static void set_tls_obj(T *objp)
132 {
133 pthread_setspecific(tls_key_, objp);
134 }
135
136 // Friend declarations don't work portably, so we have to declare
137 // tls_key_ public.
138 static pthread_key_t tls_key_;
139 private:
140 TlsWrapper();
141
142 }; // TlsWrapper<>
143
144 #else
145 #error "A multi-threaded build of STL for Berkeley DB requires thread local storage. None is configured."
146 #endif
147
148 class _exported ResourceManager : public DbstlGlobalInnerObject
149 {
150 private:
151
152 ResourceManager(void);
153 // open_dbs_ & open_envs_ are shared among threads, protected by
154 // ghdl_mtx;
155 static map<Db *, size_t> open_dbs_;
156 static map<DbEnv *, size_t>open_envs_;
157
158 // Transaction stack of all environments. Use a stack to allow nested
159 // transactions. The transaction at the top of the stack is the
160 // current active transaction.
161 //
162 env_txns_t env_txns_;
163
164 // Cursors opened in a corresponding transaction context. When
165 // committing or aborting a transaction, first close all open cursors.
166 //
167 txncsr_t txn_csrs_;
168
169 // If in container X, its methods X::A and X::B both call begin and
170 // commit transaction. X::A calls X::B after it's begin transaction
171 // call, then X::B will commit the transaction prematurally. To avoid
172 // will commit the only transaction prematurally, to avoid this, we use
173 // this, we use this map to record each transaction's reference count.
174 // Each begin/commit_txn() will increment/decrement the reference
175 // count, when reference count goes to 0, the transaction is committed.
176 // Abort_txn will unconditionally abort the transaction.
177 //
178 map<DbTxn *, size_t> txn_count_;
179
180 // Contains the cursors opened in the current thread for each database,
181 // So that we can close them in the right way, freeing any Berkeley DB
182 // handles before exiting.
183 //
184 db_csr_map_t all_csrs_;
185
186 // Remove cursors opened in the transaction txn's context, should be
187 // called before commiting or aborting a transaction.
188 //
189 void remove_txn_cursor(DbTxn *txn);
190
191 // Add a cursor to the current transaction's set of open cursors.
192 void add_txn_cursor(DbCursorBase *dcbcsr, DbEnv *env);
193
194 // The environment mtx_env_ and mtx_handle_ are used for synchronizing
195 // multiple threads' access to open_dbs_ and open_envs_ and glob_objs_.
196 // They are discarded when program exits, no deallocation/release
197 // is done.
198 static DbEnv *mtx_env_;
199 static db_mutex_t mtx_handle_;
200 static db_mutex_t mtx_globj_;
201 static set<DbstlGlobalInnerObject *> glob_objs_;
202
203 // This set stores db handles that are new'ed by open_db, and thus
204 // should be deleted after this db is closed automatically by dbstl.
205 // If a db is new'ed and created by user without using open_db, users
206 // should delete it.
207 static set<Db *> deldbs;
208 static set<DbEnv *> delenvs; // Similar to deldbs, works with open_envs.
209 static void set_global_callbacks();
210 public:
211
212 // This function should be called in a single thread inside a process,
213 // before any use of dbstl.
214 static void global_startup();
215 // Delete registered DbstlGlobalInnerObject objects.
216 static void global_exit();
217
218 // Delete the ResourceManager singleton of current thread, and remove it
219 // from glob_objs.
220 static void thread_exit();
221 static void register_global_object(DbstlGlobalInnerObject *gio);
222 static DbEnv *get_mutex_env() { return mtx_env_; }
223 // Lock mtx_handle_, if it is 0, allocate it first.
224 static int global_lock(db_mutex_t dbcontainer_mtx);
225 static int global_unlock(db_mutex_t dbcontainer_mtx);
226 // Close pdb regardless of its reference count, users must make sure
227 // pdb is not used by others before calling this method. We can't
228 // close by reference count in this method, otherwise when the thread
229 // exits pdb's reference count is decremented twice.
230 void close_db(Db *pdb);
231
232 // Close all db handles regardless of reference count, used to clean up
233 // the calling thread's ResourceManager singleton.
234 void close_all_dbs();
235
236 // Close specified db env handle and remove it from resource manager.
237 void close_db_env(DbEnv *penv);
238
239 // Close and remove all db env registered in the resource manager.
240 // Used to clean up the calling thread's ResourceManager singleton.
241 void close_all_db_envs();
242
243 // Begin a new transaction in the specified environment. When outtxn
244 // is non-zero support nested transactions - the new transaction will
245 // be started as a child of the current transaction. If outtxn is 0
246 // we are starting an internal transaction for autocommit, no new
247 // transaction will be started, but the current transaction's
248 // reference count will be incremented if it already has a reference
249 // count; otherwise, it was created by user, and we simply use this
250 // transaction, set its reference count to 2.
251 //
252 // This function is called by both containers to begin an internal
253 // transaction for autocommit, and by db stl users to begin an
254 // external transaction.
255 //
256 DbTxn *begin_txn(u_int32_t flags/* flags for DbEnv::txn_begin */,
257 DbEnv *env, int outtxn);
258
259 // Decrement reference count if it exists and if it goes to 0, commit
260 // current transaction T opened in env;
261 // If T has no reference count(outside transaction), simply find
262 // it by popping the stack and commit it.
263 //
264 // This function is called by db_container to commit a transaction
265 // for auto commit, also can be called by db stl user to commit
266 // an external explicit transaction.
267 //
268 void commit_txn(DbEnv *env, u_int32_t flags = 0);
269
270 // Commit specified transaction txn: find it by popping the stack,
271 // discard all its child transactions and commit it.
272 //
273 // This function is called by dbstl user to commit an external
274 // explicit transaction.
275 //
276 void commit_txn(DbEnv *env, DbTxn *txn, u_int32_t flags = 0);
277
278 // Abort current transaction of the environment.
279 //
280 void abort_txn(DbEnv *env);
281
282 // Abort specified transaction: find it by popping the stack, discard
283 // all its child transactions and abort it.
284 //
285 // This function is called by dbstl user to abort an external
286 // explicit transaction.
287 //
288 void abort_txn(DbEnv *env, DbTxn *txn);
289
290 // Set env's current transaction handle. The original transaction
291 // handle is returned without aborting or commiting. This API can be
292 // used to share a transaction handle among multiple threads.
293 DbTxn* set_current_txn_handle(DbEnv *env, DbTxn *newtxn);
294
295 // Register a Db handle. This handle and handles opened in it
296 // will be closed by ResourceManager, so application code must not
297 // try to close or delete it. Users can configure the handle before
298 // opening the Db and then register it via this function.
299 //
300 void register_db(Db *pdb1);
301
302 // Register a DbEnv handle. This handle and handles opened in it
303 // will be closed by ResourceManager, so application code must not try
304 // to close or delete it. Users can configure the handle before
305 // opening the Environment and then register it via this function.
306 //
307 void register_db_env(DbEnv *env1);
308
309 // Helper function to open a database and register it into
310 // ResourceManager.
311 // Users can set the create flags, open flags, db type, and flags
312 // needing to be set before open.
313 //
314 Db* open_db (DbEnv *penv, const char *filename, DBTYPE dbtype,
315 u_int32_t oflags, u_int32_t set_flags, int mode = 0644,
316 DbTxn* txn = NULL, u_int32_t cflags = 0,
317 const char *dbname = NULL);
318
319 // Helper function to open a dbenv and register it into
320 // ResourceManager.
321 // Users can set the create flags, open flags, db type, and flags
322 // needing to be set before open.
323 //
324 DbEnv *open_env(const char *env_home, u_int32_t set_flags,
325 u_int32_t oflags = DB_CREATE | DB_INIT_MPOOL,
326 u_int32_t cachesize = 4 * 1024 * 1024, int mode = 0644,
327 u_int32_t cflags = 0/* Flags for DbEnv constructor. */);
328
329 static ResourceManager *instance();
330
331 // Release all registered resource in the right order.
332 virtual ~ResourceManager(void);
333
334 // Return current transaction of environment env, that is, the one on
335 // the transaction stack top, the active one.
336 //
337 DbTxn *current_txn(DbEnv *env);
338
339 // Open a Berkeley DB cursor.
340 //
341 int open_cursor(DbCursorBase *dcbcsr, Db *pdb, int flags = 0);
342
343 // Add a db-cursor mapping.
344 void add_cursor(Db *dbp, DbCursorBase *csr);
345
346 // Close all cursors opened in the database.
347 size_t close_db_cursors(Db *dbp1);
348
349 // Close and remove a cursor from ResourceManager.
350 //
351 int remove_cursor(DbCursorBase *csr, bool remove_from_txncsrs = true);
352
353 }; // ResourceManager
354
355 END_NS
356 #ifdef WIN32
357 #pragma warning( pop )
358 #endif
359 #endif // !_DB_STL_RESOURCE_MANAGER_H__