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