2 * The UCW Library -- Transactions
4 * (c) 2008--2011 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
13 #include <ucw/mempool.h>
17 /** A structure describing a transaction. All fields are for internal use only. **/
19 struct trans *prev_trans;
20 struct mempool_state *trans_pool_state;
21 struct respool *rpool;
22 struct respool *prev_rpool;
23 struct exception *thrown_exc;
27 void trans_init(void); /** Initializes the transaction system for the current thread. Called automatically as needed. **/
28 void trans_cleanup(void); /** Frees memory occupied by the transaction system pools for the current thread. **/
30 struct trans *trans_open(void); /** Creates a new transaction. Used inside `TRANS_TRY`. **/
31 struct trans *trans_get_current(void); /** Get a pointer to the currently running transaction, or NULL if there is none. **/
32 void trans_commit(void); /** Commits the current transaction. **/
33 void trans_rollback(void); /** Rolls back the current transaction. **/
34 void trans_fold(void); /** Folds the current transaction to its parent. **/
35 void trans_dump(void); /** Prints out a debugging dump of the transaction stack to stdout. **/
37 struct mempool *trans_get_pool(void);
40 * Data associated with an exception. Usually, this structure is created
41 * by calling @trans_throw(), but if you want to pass more data, you can
42 * create your own exception and throw it using @trans_throw_exc().
45 const char *id; // Hierarchical identifier of the exception
46 const char *msg; // Error message to present to the user
47 void *object; // Object on which the exception happened
48 // More data specific for the particular `id' can follow
51 /** Creates an exception and throws it. The error message can contain `printf`-like formatting. **/
52 void trans_throw(const char *id, void *object, const char *fmt, ...) FORMAT_CHECK(printf,3,4) NONRET;
54 /** A `va_list` variant of @trans_throw(). **/
55 void trans_vthrow(const char *id, void *object, const char *fmt, va_list args) NONRET;
57 /** Throw an already constructed exception (or re-throw an exception you have caught). **/
58 void trans_throw_exc(struct exception *x) NONRET;
60 /** Declare the current exception caught and roll back the current transaction. Called from `TRANS_END`. **/
61 void trans_caught(void);
63 struct exception *trans_current_exc(void); /** Return the exception in flight, or NULL if there is none. **/
65 #define TRANS_TRY do { \
66 struct trans *_t = trans_open(); \
67 if (!setjmp(_t->jmp)) \
70 #define TRANS_CATCH(x) \
75 struct exception *x UNUSED = trans_current_exc();