]> mj.ucw.cz Git - libucw.git/blob - ucw/trans.h
7ba8e6ff53827df4cf4439402aac964d840e798b
[libucw.git] / ucw / trans.h
1 /*
2  *      The UCW Library -- Transactions
3  *
4  *      (c) 2008 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #ifndef _UCW_TRANS_H
11 #define _UCW_TRANS_H
12
13 #include "ucw/clists.h"
14 #include "ucw/mempool.h"
15
16 #include <jmpbuf.h>
17
18 /* Resource pools */
19
20 struct respool {
21   clist resources;
22   struct mempool *mp;           // If set, resources are allocated from the mempool, otherwise by xmalloc()
23 };
24
25 struct resource {
26   cnode n;
27   struct res_class *rclass;
28   void *priv;                   // Private to the class
29 };
30
31 struct res_class {
32   const char *name;
33   void (*undo)(struct res *t);
34   void (*dump)(struct res *t);
35 };
36
37 struct respool *rp_new(void);
38 void rp_delete(struct respool *rp);
39 void rp_dump(struct respool *rp);
40
41 struct resource *res_alloc(struct respool *rp);
42 void res_fix(struct resource *r);
43 void res_undo(struct resource *r);
44 void res_dump(struct resource *r);
45
46 static inline struct resource *
47 res_new(struct respool *rp, struct res_class *rc, void *priv)
48 {
49   struct resource *r = res_alloc(rp);
50   r->rclass = rc;
51   r->priv = priv;
52   return r;
53 }
54
55 /* Transactions */
56
57 struct trans {
58   struct trans *prev;
59   struct mempool_state trans_pool_state;
60   struct respool res_pool;
61   jmp_buf jmp;
62 };
63
64 void trans_init(void);          // Called automatically on trans_open() if needed
65 void trans_cleanup(void);       // Free memory occupied by the transaction system pools
66
67 struct trans *trans_open(void);
68 struct trans *trans_get_current(void);
69 void trans_commit(void);
70 void trans_rollback(void);
71 void trans_dump(void);
72
73 /* Exceptions */
74
75 #endif