2190
|
1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
|
2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
3 |
|
|
4 |
|
|
5 |
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
6 |
<head>
|
|
7 |
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
|
8 |
|
|
9 |
<title>Portability — Jansson 2.7 documentation</title>
|
|
10 |
|
|
11 |
<link rel="stylesheet" href="_static/default.css" type="text/css" />
|
|
12 |
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
|
|
13 |
|
|
14 |
<script type="text/javascript">
|
|
15 |
var DOCUMENTATION_OPTIONS = {
|
|
16 |
URL_ROOT: './',
|
|
17 |
VERSION: '2.7',
|
|
18 |
COLLAPSE_INDEX: false,
|
|
19 |
FILE_SUFFIX: '.html',
|
|
20 |
HAS_SOURCE: true
|
|
21 |
};
|
|
22 |
</script>
|
|
23 |
<script type="text/javascript" src="_static/jquery.js"></script>
|
|
24 |
<script type="text/javascript" src="_static/underscore.js"></script>
|
|
25 |
<script type="text/javascript" src="_static/doctools.js"></script>
|
|
26 |
<link rel="top" title="Jansson 2.7 documentation" href="index.html" />
|
|
27 |
<link rel="next" title="API Reference" href="apiref.html" />
|
|
28 |
<link rel="prev" title="RFC Conformance" href="conformance.html" />
|
|
29 |
</head>
|
|
30 |
<body>
|
|
31 |
<div class="related">
|
|
32 |
<h3>Navigation</h3>
|
|
33 |
<ul>
|
|
34 |
<li class="right" style="margin-right: 10px">
|
|
35 |
<a href="genindex.html" title="General Index"
|
|
36 |
accesskey="I">index</a></li>
|
|
37 |
<li class="right" >
|
|
38 |
<a href="apiref.html" title="API Reference"
|
|
39 |
accesskey="N">next</a> |</li>
|
|
40 |
<li class="right" >
|
|
41 |
<a href="conformance.html" title="RFC Conformance"
|
|
42 |
accesskey="P">previous</a> |</li>
|
|
43 |
<li><a href="index.html">Jansson 2.7 documentation</a> »</li>
|
|
44 |
</ul>
|
|
45 |
</div>
|
|
46 |
|
|
47 |
<div class="document">
|
|
48 |
<div class="documentwrapper">
|
|
49 |
<div class="bodywrapper">
|
|
50 |
<div class="body">
|
|
51 |
|
|
52 |
<div class="section" id="portability">
|
|
53 |
<h1>Portability<a class="headerlink" href="#portability" title="Permalink to this headline">¶</a></h1>
|
|
54 |
<div class="section" id="thread-safety">
|
|
55 |
<span id="portability-thread-safety"></span><h2>Thread safety<a class="headerlink" href="#thread-safety" title="Permalink to this headline">¶</a></h2>
|
|
56 |
<p>Jansson is thread safe and has no mutable global state. The only
|
|
57 |
exceptions are the hash function seed and memory allocation functions,
|
|
58 |
see below.</p>
|
|
59 |
<p>There’s no locking performed inside Jansson’s code, so a multithreaded
|
|
60 |
program must perform its own locking if JSON values are shared by
|
|
61 |
multiple threads. Jansson’s reference counting semantics may make this
|
|
62 |
a bit harder than it seems, as it’s possible to have a reference to a
|
|
63 |
value that’s also stored inside a list or object. Modifying the
|
|
64 |
container (adding or removing values) may trigger concurrent access to
|
|
65 |
such values, as containers manage the reference count of their
|
|
66 |
contained values. Bugs involving concurrent incrementing or
|
|
67 |
decrementing of deference counts may be hard to track.</p>
|
|
68 |
<p>The encoding functions (<a class="reference internal" href="apiref.html#c.json_dumps" title="json_dumps"><tt class="xref c c-func docutils literal"><span class="pre">json_dumps()</span></tt></a> and friends) track
|
|
69 |
reference loops by modifying the internal state of objects and arrays.
|
|
70 |
For this reason, encoding functions must not be run on the same JSON
|
|
71 |
values in two separate threads at the same time. As already noted
|
|
72 |
above, be especially careful if two arrays or objects share their
|
|
73 |
contained values with another array or object.</p>
|
|
74 |
<p>If you want to make sure that two JSON value hierarchies do not
|
|
75 |
contain shared values, use <a class="reference internal" href="apiref.html#c.json_deep_copy" title="json_deep_copy"><tt class="xref c c-func docutils literal"><span class="pre">json_deep_copy()</span></tt></a> to make copies.</p>
|
|
76 |
<div class="section" id="hash-function-seed">
|
|
77 |
<h3>Hash function seed<a class="headerlink" href="#hash-function-seed" title="Permalink to this headline">¶</a></h3>
|
|
78 |
<p>To prevent an attacker from intentionally causing large JSON objects
|
|
79 |
with specially crafted keys to perform very slow, the hash function
|
|
80 |
used by Jansson is randomized using a seed value. The seed is
|
|
81 |
automatically generated on the first explicit or implicit call to
|
|
82 |
<a class="reference internal" href="apiref.html#c.json_object" title="json_object"><tt class="xref c c-func docutils literal"><span class="pre">json_object()</span></tt></a>, if <a class="reference internal" href="apiref.html#c.json_object_seed" title="json_object_seed"><tt class="xref c c-func docutils literal"><span class="pre">json_object_seed()</span></tt></a> has not been
|
|
83 |
called beforehand.</p>
|
|
84 |
<p>The seed is generated by using operating system’s entropy sources if
|
|
85 |
they are available (<tt class="docutils literal"><span class="pre">/dev/urandom</span></tt>, <tt class="docutils literal"><span class="pre">CryptGenRandom()</span></tt>). The
|
|
86 |
initialization is done in as thread safe manner as possible, by using
|
|
87 |
architecture specific lockless operations if provided by the platform
|
|
88 |
or the compiler.</p>
|
|
89 |
<p>If you’re using threads, it’s recommended to autoseed the hashtable
|
|
90 |
explicitly before spawning any threads by calling
|
|
91 |
<tt class="docutils literal"><span class="pre">json_object_seed(0)</span></tt> , especially if you’re unsure whether the
|
|
92 |
initialization is thread safe on your platform.</p>
|
|
93 |
</div>
|
|
94 |
<div class="section" id="memory-allocation-functions">
|
|
95 |
<h3>Memory allocation functions<a class="headerlink" href="#memory-allocation-functions" title="Permalink to this headline">¶</a></h3>
|
|
96 |
<p>Memory allocation functions should be set at most once, and only on
|
|
97 |
program startup. See <a class="reference internal" href="apiref.html#apiref-custom-memory-allocation"><em>Custom Memory Allocation</em></a>.</p>
|
|
98 |
</div>
|
|
99 |
</div>
|
|
100 |
<div class="section" id="locale">
|
|
101 |
<h2>Locale<a class="headerlink" href="#locale" title="Permalink to this headline">¶</a></h2>
|
|
102 |
<p>Jansson works fine under any locale.</p>
|
|
103 |
<p>However, if the host program is multithreaded and uses <tt class="docutils literal"><span class="pre">setlocale()</span></tt>
|
|
104 |
to switch the locale in one thread while Jansson is currently encoding
|
|
105 |
or decoding JSON data in another thread, the result may be wrong or
|
|
106 |
the program may even crash.</p>
|
|
107 |
<p>Jansson uses locale specific functions for certain string conversions
|
|
108 |
in the encoder and decoder, and then converts the locale specific
|
|
109 |
values to/from the JSON representation. This fails if the locale
|
|
110 |
changes between the string conversion and the locale-to-JSON
|
|
111 |
conversion. This can only happen in multithreaded programs that use
|
|
112 |
<tt class="docutils literal"><span class="pre">setlocale()</span></tt>, because <tt class="docutils literal"><span class="pre">setlocale()</span></tt> switches the locale for all
|
|
113 |
running threads, not only the thread that calls <tt class="docutils literal"><span class="pre">setlocale()</span></tt>.</p>
|
|
114 |
<p>If your program uses <tt class="docutils literal"><span class="pre">setlocale()</span></tt> as described above, consider
|
|
115 |
using the thread-safe <tt class="docutils literal"><span class="pre">uselocale()</span></tt> instead.</p>
|
|
116 |
</div>
|
|
117 |
</div>
|
|
118 |
|
|
119 |
|
|
120 |
</div>
|
|
121 |
</div>
|
|
122 |
</div>
|
|
123 |
<div class="sphinxsidebar">
|
|
124 |
<div class="sphinxsidebarwrapper">
|
|
125 |
<h3><a href="index.html">Table Of Contents</a></h3>
|
|
126 |
<ul>
|
|
127 |
<li><a class="reference internal" href="#">Portability</a><ul>
|
|
128 |
<li><a class="reference internal" href="#thread-safety">Thread safety</a><ul>
|
|
129 |
<li><a class="reference internal" href="#hash-function-seed">Hash function seed</a></li>
|
|
130 |
<li><a class="reference internal" href="#memory-allocation-functions">Memory allocation functions</a></li>
|
|
131 |
</ul>
|
|
132 |
</li>
|
|
133 |
<li><a class="reference internal" href="#locale">Locale</a></li>
|
|
134 |
</ul>
|
|
135 |
</li>
|
|
136 |
</ul>
|
|
137 |
|
|
138 |
<h4>Previous topic</h4>
|
|
139 |
<p class="topless"><a href="conformance.html"
|
|
140 |
title="previous chapter">RFC Conformance</a></p>
|
|
141 |
<h4>Next topic</h4>
|
|
142 |
<p class="topless"><a href="apiref.html"
|
|
143 |
title="next chapter">API Reference</a></p>
|
|
144 |
<h3>This Page</h3>
|
|
145 |
<ul class="this-page-menu">
|
|
146 |
<li><a href="_sources/portability.txt"
|
|
147 |
rel="nofollow">Show Source</a></li>
|
|
148 |
</ul>
|
|
149 |
<div id="searchbox" style="display: none">
|
|
150 |
<h3>Quick search</h3>
|
|
151 |
<form class="search" action="search.html" method="get">
|
|
152 |
<input type="text" name="q" />
|
|
153 |
<input type="submit" value="Go" />
|
|
154 |
<input type="hidden" name="check_keywords" value="yes" />
|
|
155 |
<input type="hidden" name="area" value="default" />
|
|
156 |
</form>
|
|
157 |
<p class="searchtip" style="font-size: 90%">
|
|
158 |
Enter search terms or a module, class or function name.
|
|
159 |
</p>
|
|
160 |
</div>
|
|
161 |
<script type="text/javascript">$('#searchbox').show(0);</script>
|
|
162 |
</div>
|
|
163 |
</div>
|
|
164 |
<div class="clearer"></div>
|
|
165 |
</div>
|
|
166 |
<div class="related">
|
|
167 |
<h3>Navigation</h3>
|
|
168 |
<ul>
|
|
169 |
<li class="right" style="margin-right: 10px">
|
|
170 |
<a href="genindex.html" title="General Index"
|
|
171 |
>index</a></li>
|
|
172 |
<li class="right" >
|
|
173 |
<a href="apiref.html" title="API Reference"
|
|
174 |
>next</a> |</li>
|
|
175 |
<li class="right" >
|
|
176 |
<a href="conformance.html" title="RFC Conformance"
|
|
177 |
>previous</a> |</li>
|
|
178 |
<li><a href="index.html">Jansson 2.7 documentation</a> »</li>
|
|
179 |
</ul>
|
|
180 |
</div>
|
|
181 |
<div class="footer">
|
|
182 |
© Copyright 2009-2014, Petri Lehtinen.
|
|
183 |
Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.2.
|
|
184 |
</div>
|
|
185 |
</body>
|
|
186 |
</html> |