Code

21ed7927095c35df76f18ebdd053e4f11c3e2b2f
[pkg-rrdtool.git] / doc / rrdthreads.txt
1 RRDTHREADS(1)                       rrdtool                      RRDTHREADS(1)
5 N\bNA\bAM\bME\bE
6        rrdthreads - Provisions for linking the RRD library to use in
7        multi-threaded programs
9 S\bSY\bYN\bNO\bOP\bPS\bSI\bIS\bS
10        Using librrd in multi-threaded programs requires some extra precau-
11        tions, as the RRD library in its original form was not thread-safe at
12        all. This document describes requirements and pitfalls on the way to
13        use the multi-threaded version of librrd in your own programs. It also
14        gives hints for future RRD development to keep the library thread-safe.
16        Currently only some RRD operations are implemented in a thread-safe
17        way. They all end in the usual ""_r"" suffix.
19 D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
20        In order to use librrd in multi-threaded programs you must:
22        ·   Link with _\bl_\bi_\bb_\br_\br_\bd_\b__\bt_\bh instead of _\bl_\bi_\bb_\br_\br_\bd (use "-lrrd_th" when linking)
24        ·   Use the ""_r"" functions instead of the normal API-functions
26        ·   Do not use any at-style time specifications. Parsing of such time
27            specifications is terribly non-thread-safe.
29        ·   Never use non *"_r" functions unless it is explicitly documented
30            that the function is tread-safe.
32        ·   Every thread SHOULD call "rrd_get_context()" before its first call
33            to any "librrd_th" function in order to set up thread specific
34            data. This is not strictly required, but it is the only way to test
35            if memory allocation can be done by this function. Otherwise the
36            program may die with a SIGSEGV in a low-memory situation.
38        ·   Always call "rrd_error_clear()" before any call to the library.
39            Otherwise the call might fail due to some earlier error.
41        N\bNO\bOT\bTE\bES\bS F\bFO\bOR\bR R\bRR\bRD\bD C\bCO\bON\bNT\bTR\bRI\bIB\bBU\bUT\bTO\bOR\bRS\bS
43        Some precautions must be followed when developing RRD from now on:
45        ·   Only use thread-safe functions in library code. Many often used
46            libc functions aren't thread-safe. Take care in the following situ-
47            ations or when using the following library functions:
49            ·   Direct calls to "strerror()" must be avoided: use "rrd_str-
50                error()" instead, it provides a per-thread error message.
52            ·   The "getpw*", "getgr*", "gethost*" function families (and some
53                more "get*" functions) are not thread-safe: use the *"_r" vari-
54                ants
56            ·   Time functions: "asctime", "ctime", "gmtime", "localtime": use
57                *"_r" variants
59            ·   "strtok": use "strtok_r"
61            ·   "tmpnam": use "tmpnam_r"
63            ·   Many others (lookup documentation)
65        ·   A header file named _\br_\br_\bd_\b__\bi_\bs_\b__\bt_\bh_\br_\be_\ba_\bd_\b__\bs_\ba_\bf_\be_\b._\bh is provided that works
66            with the GNU C-preprocessor to "poison" some of the most common
67            non-thread-safe functions using the "#pragma GCC poison" directive.
68            Just include this header in source files you want to keep
69            thread-safe.
71        ·   Do not introduce global variables!
73            If you really, really have to use a global variable you may add a
74            new field to the "rrd_context" structure and modify _\br_\br_\bd_\b__\be_\br_\br_\bo_\br_\b._\bc,
75            _\br_\br_\bd_\b__\bt_\bh_\br_\be_\ba_\bd_\b__\bs_\ba_\bf_\be_\b._\bc and _\br_\br_\bd_\b__\bn_\bo_\bn_\b__\bt_\bh_\br_\be_\ba_\bd_\b__\bs_\ba_\bf_\be_\b._\bc
77        ·   Do not use "getopt" or "getopt_long" in *"_r" (neither directly nor
78            indirectly).
80            "getopt" uses global variables and behaves badly in a multi-
81            threaded application when called concurrently. Instead provide a
82            *_r function taking all options as function parameters. You may
83            provide argc and **argv arguments for variable length argument
84            lists. See "rrd_update_r" as an example.
86        ·   Do not use the "rrd_parsetime" function!
88            It uses lots of global variables. You may use it in functions not
89            designed to be thread-safe, like in functions wrapping the "_r"
90            version of some operation (e.g., "rrd_create", but not in "rrd_cre-
91            ate_r")
93        C\bCU\bUR\bRR\bRE\bEN\bNT\bTL\bLY\bY I\bIM\bMP\bPL\bLE\bEM\bME\bEN\bNT\bTE\bED\bD T\bTH\bHR\bRE\bEA\bAD\bD S\bSA\bAF\bFE\bE F\bFU\bUN\bNC\bCT\bTI\bIO\bON\bNS\bS
95        Currently there exist thread-safe variants of "rrd_update", "rrd_cre-
96        ate", "rrd_dump", "rrd_info", "rrd_last", and "rrd_fetch".
98 A\bAU\bUT\bTH\bHO\bOR\bR
99        Peter Stamfest <peter@stamfest.at>
103 1.3.99909060808                   2008-06-08                     RRDTHREADS(1)