Code

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