]> git.proxmox.com Git - libgit2.git/blame - docs/threading.md
Drop Debian revision from symbols file
[libgit2.git] / docs / threading.md
CommitLineData
0c9c969a 1Threading in libgit2
15bea02c
CMN
2==================
3
0c9c969a
UG
4Unless otherwise specified, libgit2 objects cannot be safely accessed by
5multiple threads simultaneously.
6
7There are also caveats on the cryptographic libraries libgit2 or its
15bea02c
CMN
8dependencies link to (more on this later). For libgit2 itself,
9provided you take the following into consideration you won't run into
10issues:
11
12Sharing objects
13---------------
14
15Use an object from a single thread at a time. Most data structures do
16not guard against concurrent access themselves. This is because they
17are rarely used in isolation and it makes more sense to synchronize
18access via a larger lock or similar mechanism.
19
20There are some objects which are read-only/immutable and are thus safe
21to share across threads, such as references and configuration
22snapshots.
23
24Error messages
25--------------
26
ac3d33df 27The error message is thread-local. The `git_error_last()` call must
15bea02c
CMN
28happen on the same thread as the error in order to get the
29message. Often this will be the case regardless, but if you use
d698712b 30something like the [GCD](http://en.wikipedia.org/wiki/Grand_Central_Dispatch)
0c9c969a 31on macOS (where code is executed on an arbitrary thread), the code
d698712b
BC
32must make sure to retrieve the error code on the thread where the error
33happened.
15bea02c 34
0c9c969a 35Threading and cryptographic libraries
15bea02c
CMN
36=======================================
37
38On Windows
39----------
40
41When built as a native Windows DLL, libgit2 uses WinCNG and WinHTTP,
42both of which are thread-safe. You do not need to do anything special.
43
44When using libssh2 which itself uses WinCNG, there are no special
45steps necessary. If you are using a MinGW or similar environment where
85247df0 46libssh2 uses OpenSSL or libgcrypt, then the general case affects
15bea02c
CMN
47you.
48
0c9c969a 49On macOS
15bea02c
CMN
50-----------
51
0c9c969a
UG
52By default we make use of CommonCrypto and SecureTransport for cryptographic
53support. These are thread-safe and you do not need to do anything special.
85247df0
CMN
54
55Note that libssh2 may still use OpenSSL itself. In that case, the
56general case still affects you if you use ssh.
57
58General Case
59------------
60
0c9c969a
UG
61libgit2 will default to OpenSSL for HTTPS transport (except on Windows and
62macOS, as mentioned above). On any system, mbedTLS _may_ be optionally
63enabled as the security provider. OpenSSL is thread-safe starting at
64version 1.1.0. If your copy of libgit2 is linked against that version,
65you do not need to take any further steps.
f7d316ed
CMN
66
67Older versions of OpenSSL are made to be thread-implementation agnostic, and the
68users of the library must set which locking function it should use. libgit2
69cannot know what to set as the user of libgit2 may also be using OpenSSL independently and
70the locking settings must then live outside the lifetime of libgit2.
71
72Even if libgit2 doesn't use OpenSSL directly, OpenSSL can still be used by
0c9c969a 73libssh2 depending on the configuration. If OpenSSL is used by
f7d316ed
CMN
74more than one library, you only need to set up threading for OpenSSL once.
75
0c9c969a 76If libgit2 is linked against OpenSSL < 1.1.0, it provides a last-resort convenience function
263b1d6e 77`git_openssl_set_locking()` (available in `sys/openssl.h`) to use the
f7d316ed
CMN
78platform-native mutex mechanisms to perform the locking, which you can use
79if you do not want to use OpenSSL outside of libgit2, or you
80know that libgit2 will outlive the rest of the operations. It is then not
263b1d6e 81safe to use OpenSSL multi-threaded after libgit2's shutdown function
09db7fd8
SR
82has been called. Note `git_openssl_set_locking()` only works if
83libgit2 uses OpenSSL directly - if OpenSSL is only used as a dependency
0c9c969a 84of libssh2 as described above, `git_openssl_set_locking()` is a no-op.
263b1d6e
CMN
85
86If your programming language offers a package/bindings for OpenSSL,
87you should very strongly prefer to use that in order to set up
ea9ea6ac 88locking, as they provide a level of coordination which is impossible
263b1d6e 89when using this function.
15bea02c
CMN
90
91See the
92[OpenSSL documentation](https://www.openssl.org/docs/crypto/threads.html)
09db7fd8
SR
93on threading for more details, and http://trac.libssh2.org/wiki/MultiThreading
94for a specific example of providing the threading callbacks.
15bea02c 95
09db7fd8
SR
96libssh2 may be linked against OpenSSL or libgcrypt. If it uses OpenSSL,
97see the above paragraphs. If it uses libgcrypt, then you need to
15bea02c 98set up its locking before using it multi-threaded. libgit2 has no
ea9ea6ac 99direct connection to libgcrypt and thus has no convenience functions for
15bea02c
CMN
100it (but libgcrypt has macros). Read libgcrypt's
101[threading documentation for more information](http://www.gnupg.org/documentation/manuals/gcrypt/Multi_002dThreading.html)
102
103It is your responsibility as an application author or packager to know
104what your dependencies are linked against and to take the appropriate
105steps to ensure the cryptographic libraries are thread-safe. We agree
106that this situation is far from ideal but at this time it is something
107the application authors need to deal with.