]> mj.ucw.cz Git - libucw.git/blob - ucw/doc/trans.txt
Trans: more documentation
[libucw.git] / ucw / doc / trans.txt
1 Transactions and resource tracking
2 ==================================
3
4 LibUCW is equipped with a general system for keeping track of resources
5 (allocated memory, open files, ...) and freeing them when requested to.
6
7 The resource tracker can be used either separately (in the form of explicitly
8 managed resource pools) or within a transactional layer, which offers
9 exceptions similar to those in higher-level languages. An exception
10 then rolls back the transaction, freeing all temporary resources allocated
11 within the transaction.
12
13 Resource pools: ucw/respool.h
14 -----------------------------
15
16 A resource pool contains a stack of resources. When a new resource
17 is created, it is pushed onto the stack. When freeing the pool, the
18 resources are freed in the opposite order, which allows a resource
19 refer to data of previously created resources.
20
21 A resource can be also freed separately (which unlinks it from the pool),
22 or *detached* from the pool (which keeps the real resource, but forgets
23 its meta-data, so the resource is no longer tracked).
24
25 For each thread, LibUCW remembers the currently active resource pool.
26 One pool can be used for at most one thread at a time. All functions
27 which create resources do so in the active pool. All other functions
28 operating on resources work on both active and in-active pools.
29
30 !!ucw/respool.h
31
32 Transactions: ucw/trans.h
33 -------------------------
34
35 Upon the resource pools, a transactional mechanism is built. A transaction
36 consists of a piece of code and a resource pool for temporary objects created
37 by the code. Whenever the transaction is running, this pool is set as current.
38 You are allowed to switch to a different pool, but please do so carefully.
39
40 When a transaction ends, the pool is destroyed and the previous active
41 pool is popped off the transaction stack. The fate of the resources
42 inside the pool depends on the operation used to end the transaction:
43
44 * *commit* -- all resources are detached from the pool
45 * *rollback* -- all resources are freed
46 * *fold* -- instead of destroying the pool, it is added as a subpool
47   to the parent transaction (which must exist)
48
49 A transaction is tied to a thread which has created it. A transaction
50 can create a sub-transaction, so every thread keeps a stack of running
51 transactions in its per-thread data. Calling @trans_init() is optional,
52 but @trans_cleanup() should be used before a thread exits in order to
53 free resources used by transaction system.
54
55 Each transaction also includes a memory pool, from which all temporary
56 structures (including all resources created by the transaction) are
57 allocated. Feel free to allocate your temporary data from this pool, too;
58 they will be freed when the transaction is committed or rolled back.
59 When the transaction ends with a fold, this pool gets included inside
60 the parent transaction's pool.
61
62 (More precisely, there is actually a shared transaction pool per thread
63 and the transaction logic uses @mp_push() and @mp_pop() to keep a stack
64 of per-transaction data.)
65
66 === Exceptions ===
67
68 Transactions are commonly used together with exceptions (which are similar
69 to how exceptions work in other languages, but they differ in subtle details,
70 so please read carefully). When a failure condition of some kind is detected,
71 an exception is *raised* ("*thrown*" is also sometimes used). It involves
72 creating an exception object and jumping out of the transaction by
73 a `longjmp()`. The exception object (`struct exception`) contains an
74 identification of the error and possibly additional data.
75
76 Usually, creation of an transaction and handling of exceptions is done
77 using *helper macros* (it is not strictly necessary, but highly recommended):
78
79         TRANS_TRY
80           {
81             // Code that runs inside the transaction.
82           }
83         TRANS_CATCH(x)
84           {
85             // When an exception is raised, execution continues here.
86           }
87         TRANS_END;
88
89 The code inside the transaction ends with an implicit @trans_commit().
90 If you want to end the transaction in a different way, you can do so,
91 but you need to use a `break` statement to skip the implicit commit.
92
93 The exception handling code gets a local variable `x` pointing to the
94 exception object. When the exception is handled (for example, an error
95 message is logged), @trans_caught() is called automatically, which rolls
96 back the transaction and frees all its resources. Again, you can use the
97 `break` statement to skip this.
98
99 Alternatively, when you are in a *nested transaction*, you can throw a different
100 exception or re-throw the original one. This raises an exception in the
101 context of the parent transaction. In this case, the child transaction is
102 not rolled back, but its pools are folded as sub-pools of the parent transaction
103 and kept until @trans_caught() is called finally.
104
105 When an exception is thrown *outside a transaction*, it is converted to
106 a plain @die().
107
108 *Memory management* and lifetime of various objects and pools deserve special
109 attention, as usually when non-local jumps are taking place. When an exception
110 is raised, the exception structure is allocated from the memory pool of the
111 current transaction. When the exception is propagated through the stack of
112 transactions, no transaction is ever rolled back -- all of them are folded
113 and their pools remain accessible until @trans_caught() is called at the end.
114 Therefore exceptions can carry pointers to the objects which have failed
115 without a risk of the object becoming invalid. However, you need to avoid
116 pointing to on-stack data like local variables of functions, because these
117 are of course destroyed during the `longjmp()`.
118
119 === Functions and structures ===
120
121 !!ucw/trans.h
122
123 == FIXME's ==
124
125 - Interaction between exceptions, pools and other libucw modules.
126 - Unit tests
127 - Resourcification of more libucw objects.
128 - Do we want to allow res_alloc() when no pool is active?
129 - Structure of exception identifiers: rethink and document