Code

Imported upstream version 1.4.8
[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
11        precautions, as the RRD library in its original form was not thread-
12        safe at all. This document describes requirements and pitfalls on the
13        way to use the multi-threaded version of librrd in your own programs.
14        It also gives hints for future RRD development to keep the library
15        thread-safe.
17        Currently only some RRD operations are implemented in a thread-safe
18        way. They all end in the usual ""_r"" suffix.
20 D\bDE\bES\bSC\bCR\bRI\bIP\bPT\bTI\bIO\bON\bN
21        In order to use librrd in multi-threaded programs you must:
23        ·   Link with _\bl_\bi_\bb_\br_\br_\bd_\b__\bt_\bh instead of _\bl_\bi_\bb_\br_\br_\bd (use "-lrrd_th" when linking)
25        ·   Use the ""_r"" functions instead of the normal API-functions
27        ·   Do not use any at-style time specifications. Parsing of such time
28            specifications is terribly non-thread-safe.
30        ·   Never use non *"_r" functions unless it is explicitly documented
31            that the function is tread-safe.
33        ·   Every thread SHOULD call "rrd_get_context()" before its first call
34            to any "librrd_th" function in order to set up thread specific
35            data. This is not strictly required, but it is the only way to test
36            if memory allocation can be done by this function. Otherwise the
37            program may die with a SIGSEGV in a low-memory situation.
39        ·   Always call "rrd_error_clear()" before any call to the library.
40            Otherwise the call might fail due to some earlier error.
42    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
47            situations or when using the following library functions:
49            ·   Direct calls to "strerror()" must be avoided: use
50                "rrd_strerror()" instead, it provides a per-thread error
51                message.
53            ·   The "getpw*", "getgr*", "gethost*" function families (and some
54                more "get*" functions) are not thread-safe: use the *"_r"
55                variants
57            ·   Time functions: "asctime", "ctime", "gmtime", "localtime": use
58                *"_r" variants
60            ·   "strtok": use "strtok_r"
62            ·   "tmpnam": use "tmpnam_r"
64            ·   Many others (lookup documentation)
66        ·   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
67            with the GNU C-preprocessor to "poison" some of the most common
68            non-thread-safe functions using the "#pragma GCC poison" directive.
69            Just include this header in source files you want to keep thread-
70            safe.
72        ·   Do not introduce global variables!
74            If you really, really have to use a global variable you may add a
75            new field to the "rrd_context" structure and modify _\br_\br_\bd_\b__\be_\br_\br_\bo_\br_\b._\bc,
76            _\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
78        ·   Do not use "getopt" or "getopt_long" in *"_r" (neither directly nor
79            indirectly).
81            "getopt" uses global variables and behaves badly in a multi-
82            threaded application when called concurrently. Instead provide a
83            *_r function taking all options as function parameters. You may
84            provide argc and **argv arguments for variable length argument
85            lists. See "rrd_update_r" as an example.
87        ·   Do not use the "rrd_parsetime" function!
89            It uses lots of global variables. You may use it in functions not
90            designed to be thread-safe, like in functions wrapping the "_r"
91            version of some operation (e.g., "rrd_create", but not in
92            "rrd_create_r")
94    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",
96        "rrd_create", "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.4.8                             2013-05-23                     RRDTHREADS(1)