portability.rst 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. ***********
  2. Portability
  3. ***********
  4. Thread safety
  5. -------------
  6. Jansson is thread safe and has no mutable global state. The only
  7. exception are the memory allocation functions, that should be set at
  8. most once, and only on program startup. See
  9. :ref:`apiref-custom-memory-allocation`.
  10. There's no locking performed inside Jansson's code, so a multithreaded
  11. program must perform its own locking if JSON values are shared by
  12. multiple threads. Jansson's reference counting semantics may make this
  13. a bit harder than it seems, as it's possible to have a reference to a
  14. value that's also stored inside a list or object. Modifying the
  15. container (adding or removing values) may trigger concurrent access to
  16. such values, as containers manage the reference count of their
  17. contained values. Bugs involving concurrent incrementing or
  18. decrementing of deference counts may be hard to track.
  19. The encoding functions (:func:`json_dumps()` and friends) track
  20. reference loops by modifying the internal state of objects and arrays.
  21. For this reason, encoding functions must not be run on the same JSON
  22. values in two separate threads at the same time. As already noted
  23. above, be especially careful if two arrays or objects share their
  24. contained values with another array or object.
  25. If you want to make sure that two JSON value hierarchies do not
  26. contain shared values, use :func:`json_deep_copy()` to make copies.
  27. Locale
  28. ------
  29. Jansson works fine under any locale.
  30. However, if the host program is multithreaded and uses ``setlocale()``
  31. to switch the locale in one thread while Jansson is currently encoding
  32. or decoding JSON data in another thread, the result may be wrong or
  33. the program may even crash.
  34. Jansson uses locale specific functions for certain string conversions
  35. in the encoder and decoder, and then converts the locale specific
  36. values to/from the JSON representation. This fails if the locale
  37. changes between the string conversion and the locale-to-JSON
  38. conversion. This can only happen in multithreaded programs that use
  39. ``setlocale()``, because ``setlocale()`` switches the locale for all
  40. running threads, not only the thread that calls ``setlocale()``.
  41. If your program uses ``setlocale()`` as described above, consider
  42. using the thread-safe ``uselocale()`` instead.