Wiki source code of What is the lock order for Mib objects in a multi-threaded agent?
Last modified by Frank Fock on 2024/05/25 10:09
Hide last authors
author | version | line-number | content |
---|---|---|---|
![]() |
1.1 | 1 | By default AGENT++ is a multi-threaded agent. Therefore access to each MibEntry in the a Mib has to be synchronized. |
2 | |||
3 | In order to reduce blocking of concurrent requests, AGENT++ uses a two level lockin: | ||
4 | |||
5 | 1. Mib instance level - locks the whole MIB | ||
6 | 1. MibEntry level - locks a scalar, sub-tree, or table | ||
7 | |||
8 | As tables and complex (sub-tree) entries may contain also MibEntry objects, for example scalars, additional levels can be implemented by the user. | ||
9 | |||
10 | In any case, the locking procedure boundary must be implemented according to the following schema: | ||
11 | |||
12 | {{code}} | ||
13 | Mib* mib; | ||
14 | ... | ||
15 | |||
16 | mib->lock_mib(); | ||
17 | |||
18 | // code to lookup a MibEntry (replace "my context" with "" or your context and the OID by the table entry OID, for example): | ||
19 | MibTable* table = (MibTable*) mib->get("my context", "1.3.6.1.4.1.????.1"); | ||
20 | |||
21 | // enter protected region: | ||
22 | table->start_synch(); | ||
23 | |||
24 | // now you can drop the Mib lock | ||
25 | mib->unlock_mib(); | ||
26 | |||
27 | // do the real work on table | ||
28 | ... | ||
29 | |||
30 | table->end_synch(); | ||
31 | {{/code}} | ||
32 | |||
33 | If you need to add/remove objects from the Mib, the locking schema looks sligtly different: | ||
34 | |||
35 | {{code}} | ||
36 | Mib* mib; | ||
37 | ... | ||
38 | |||
39 | mib->lock_mib(); | ||
40 | |||
41 | // code to lookup a MibEntry: | ||
42 | MibTable table = ...; | ||
43 | |||
44 | // enter protected region: | ||
45 | table->start_synch(); | ||
46 | |||
47 | // remove table from Mib: | ||
48 | MibContext* ctx = mib->get_context(DEFAULT_CONTEXT); | ||
49 | ctx->remove(*table->key()); | ||
50 | table->end_synch(); | ||
51 | delete table; | ||
52 | // now you can drop the Mib lock | ||
53 | mib->unlock_mib(); | ||
54 | {{/code}} |