Code

7375bc8e247d9f602a75b766f51afc0f9b663ed7
[pkg-rrdtool.git] / doc / rrdthreads.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
2 <html xmlns="http://www.w3.org/1999/xhtml">
3 <head>
4 <title>rrdthreads</title>
5 <link rev="made" href="mailto:root@localhost" />
6 </head>
8 <body style="background-color: white">
10 <p><a name="__index__"></a></p>
11 <!-- INDEX BEGIN -->
12 <!--
14 <ul>
16         <li><a href="#name">NAME</a></li>
17         <li><a href="#synopsis">SYNOPSIS</a></li>
18         <li><a href="#description">DESCRIPTION</a></li>
19         <ul>
21                 <li><a href="#notes_for_rrd_contributors">NOTES FOR RRD CONTRIBUTORS</a></li>
22                 <li><a href="#currently_implemented_thread_safe_functions">CURRENTLY IMPLEMENTED THREAD SAFE FUNCTIONS</a></li>
23         </ul>
25         <li><a href="#author">AUTHOR</a></li>
26 </ul>
27 -->
28 <!-- INDEX END -->
30 <p>
31 </p>
32 <h1><a name="name">NAME</a></h1>
33 <p>rrdthreads - Provisions for linking the RRD library to use in multi-threaded programs</p>
34 <p>
35 </p>
36 <hr />
37 <h1><a name="synopsis">SYNOPSIS</a></h1>
38 <p>Using librrd in multi-threaded programs requires some extra
39 precautions, as the RRD library in its original form was not
40 thread-safe at all. This document describes requirements and pitfalls
41 on the way to use the multi-threaded version of librrd in your own
42 programs. It also gives hints for future RRD development to keep the
43 library thread-safe.</p>
44 <p>Currently only some RRD operations are implemented in a thread-safe
45 way. They all end in the usual ``<code>_r</code>'' suffix.</p>
46 <p>
47 </p>
48 <hr />
49 <h1><a name="description">DESCRIPTION</a></h1>
50 <p>In order to use librrd in multi-threaded programs you must:</p>
51 <ul>
52 <li></li>
53 Link with <em>librrd_th</em> instead of <em>librrd</em> (use <code>-lrrd_th</code> when
54 linking)
55 <p></p>
56 <li></li>
57 Use the ``<code>_r</code>'' functions instead of the normal API-functions
58 <p></p>
59 <li></li>
60 Do not use any at-style time specifications. Parsing of such time
61 specifications is terribly non-thread-safe.
62 <p></p>
63 <li></li>
64 Never use non *<code>_r</code> functions unless it is explicitly documented that
65 the function is tread-safe.
66 <p></p>
67 <li></li>
68 Every thread SHOULD call <code>rrd_get_context()</code> before its first call to
69 any <code>librrd_th</code> function in order to set up thread specific data. This
70 is not strictly required, but it is the only way to test if memory
71 allocation can be done by this function. Otherwise the program may die
72 with a SIGSEGV in a low-memory situation.
73 <p></p>
74 <li></li>
75 Always call <code>rrd_error_clear()</code> before any call to the
76 library. Otherwise the call might fail due to some earlier error.
77 <p></p></ul>
78 <p>
79 </p>
80 <h2><a name="notes_for_rrd_contributors">NOTES FOR RRD CONTRIBUTORS</a></h2>
81 <p>Some precautions must be followed when developing RRD from now on:</p>
82 <ul>
83 <li></li>
84 Only use thread-safe functions in library code. Many often used libc
85 functions aren't thread-safe. Take care in the following
86 situations or when using the following library functions:
87 <ul>
88 <li></li>
89 Direct calls to <code>strerror()</code> must be avoided: use <code>rrd_strerror()</code>
90 instead, it provides a per-thread error message.
91 <p></p>
92 <li></li>
93 The <code>getpw*</code>, <code>getgr*</code>, <code>gethost*</code> function families (and some more
94 <code>get*</code> functions) are not thread-safe: use the *<code>_r</code> variants
95 <p></p>
96 <li></li>
97 Time functions: <code>asctime</code>, <code>ctime</code>, <code>gmtime</code>, <code>localtime</code>: use
98 *<code>_r</code> variants
99 <p></p>
100 <li></li>
101 <code>strtok</code>: use <code>strtok_r</code>
102 <p></p>
103 <li></li>
104 <code>tmpnam</code>: use <code>tmpnam_r</code>
105 <p></p>
106 <li></li>
107 Many others (lookup documentation)
108 <p></p></ul>
109 <li></li>
110 A header file named <em>rrd_is_thread_safe.h</em> is provided
111 that works with the GNU C-preprocessor to ``poison'' some of the most
112 common non-thread-safe functions using the <code>#pragma GCC poison</code>
113 directive. Just include this header in source files you want to keep
114 thread-safe.
115 <p></p>
116 <li></li>
117 Do not introduce global variables!
118 <p>If you really, really have to use a global variable you may add a new
119 field to the <code>rrd_context</code> structure and modify <em>rrd_error.c</em>,
120 <em>rrd_thread_safe.c</em> and <em>rrd_non_thread_safe.c</em></p>
121 <p></p>
122 <li></li>
123 Do not use <code>getopt</code> or <code>getopt_long</code> in *<code>_r</code> (neither directly nor
124 indirectly).
125 <p><code>getopt</code> uses global variables and behaves badly in a multi-threaded
126 application when called concurrently. Instead provide a *_r function
127 taking all options as function parameters. You may provide argc and
128 **argv arguments for variable length argument lists. See
129 <code>rrd_update_r</code> as an example.</p>
130 <p></p>
131 <li></li>
132 Do not use the <code>rrd_parsetime</code> function!
133 <p>It uses lots of global variables. You may use it in functions not designed
134 to be thread-safe, like in functions wrapping the <code>_r</code> version of some
135 operation (e.g., <code>rrd_create</code>, but not in <code>rrd_create_r</code>)</p>
136 <p></p></ul>
137 <p>
138 </p>
139 <h2><a name="currently_implemented_thread_safe_functions">CURRENTLY IMPLEMENTED THREAD SAFE FUNCTIONS</a></h2>
140 <p>Currently there exist thread-safe variants of <code>rrd_update</code>,
141 <code>rrd_create</code>, <code>rrd_dump</code>, <code>rrd_info</code>, <code>rrd_last</code>, and <code>rrd_fetch</code>.</p>
142 <p>
143 </p>
144 <hr />
145 <h1><a name="author">AUTHOR</a></h1>
146 <p>Peter Stamfest &lt;<a href="mailto:peter@stamfest.at">peter@stamfest.at</a>&gt;</p>
148 </body>
150 </html>