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