]> mj.ucw.cz Git - libucw.git/commitdiff
First bones of the transaction system's skeleton.
authorMartin Mares <mj@ucw.cz>
Sat, 30 Aug 2008 17:24:59 +0000 (19:24 +0200)
committerMartin Mares <mj@ucw.cz>
Tue, 29 Mar 2011 10:55:04 +0000 (12:55 +0200)
ucw/Makefile
ucw/trans.h [new file with mode: 0644]

index 174e51b27d1bc44d947bd550986f77d8031fb2d7..f7835977e8a6fcc73848d67443e51ae991108c40 100644 (file)
@@ -33,7 +33,8 @@ LIBUCW_MODS= \
        string str-esc str-split str-match str-imatch str-hex \
        bbuf gary \
        getopt \
-       strtonum
+       strtonum \
+       trans
 
 LIBUCW_MAIN_INCLUDES= \
        lib.h log.h threads.h \
@@ -59,7 +60,8 @@ LIBUCW_MAIN_INCLUDES= \
        qache.h \
        kmp.h kmp-search.h binsearch.h \
        partmap.h \
-       strtonum.h
+       strtonum.h \
+       trans.h
 
 ifdef CONFIG_UCW_THREADS
 # Some modules require threading
diff --git a/ucw/trans.h b/ucw/trans.h
new file mode 100644 (file)
index 0000000..7ba8e6f
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ *     The UCW Library -- Transactions
+ *
+ *     (c) 2008 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
+ */
+
+#ifndef _UCW_TRANS_H
+#define _UCW_TRANS_H
+
+#include "ucw/clists.h"
+#include "ucw/mempool.h"
+
+#include <jmpbuf.h>
+
+/* Resource pools */
+
+struct respool {
+  clist resources;
+  struct mempool *mp;          // If set, resources are allocated from the mempool, otherwise by xmalloc()
+};
+
+struct resource {
+  cnode n;
+  struct res_class *rclass;
+  void *priv;                  // Private to the class
+};
+
+struct res_class {
+  const char *name;
+  void (*undo)(struct res *t);
+  void (*dump)(struct res *t);
+};
+
+struct respool *rp_new(void);
+void rp_delete(struct respool *rp);
+void rp_dump(struct respool *rp);
+
+struct resource *res_alloc(struct respool *rp);
+void res_fix(struct resource *r);
+void res_undo(struct resource *r);
+void res_dump(struct resource *r);
+
+static inline struct resource *
+res_new(struct respool *rp, struct res_class *rc, void *priv)
+{
+  struct resource *r = res_alloc(rp);
+  r->rclass = rc;
+  r->priv = priv;
+  return r;
+}
+
+/* Transactions */
+
+struct trans {
+  struct trans *prev;
+  struct mempool_state trans_pool_state;
+  struct respool res_pool;
+  jmp_buf jmp;
+};
+
+void trans_init(void);         // Called automatically on trans_open() if needed
+void trans_cleanup(void);      // Free memory occupied by the transaction system pools
+
+struct trans *trans_open(void);
+struct trans *trans_get_current(void);
+void trans_commit(void);
+void trans_rollback(void);
+void trans_dump(void);
+
+/* Exceptions */
+
+#endif