]> git.proxmox.com Git - libgit2.git/blame - THREADING.md
crlf: do not ignore GIT_PASSTHROUGH error
[libgit2.git] / THREADING.md
CommitLineData
15bea02c
CMN
1Threads in libgit2
2==================
3
4You may safely use any libgit2 object from any thread, though there
5may be issues depending on the cryptographic libraries libgit2 or its
6dependencies link to (more on this later). For libgit2 itself,
7provided you take the following into consideration you won't run into
8issues:
9
10Sharing objects
11---------------
12
13Use an object from a single thread at a time. Most data structures do
14not guard against concurrent access themselves. This is because they
15are rarely used in isolation and it makes more sense to synchronize
16access via a larger lock or similar mechanism.
17
18There are some objects which are read-only/immutable and are thus safe
19to share across threads, such as references and configuration
20snapshots.
21
22Error messages
23--------------
24
25The error message is thread-local. The `giterr_last()` call must
26happen on the same thread as the error in order to get the
27message. Often this will be the case regardless, but if you use
d698712b
BC
28something like the [GCD](http://en.wikipedia.org/wiki/Grand_Central_Dispatch)
29on Mac OS X (where code is executed on an arbitrary thread), the code
30must make sure to retrieve the error code on the thread where the error
31happened.
15bea02c
CMN
32
33Threads and cryptographic libraries
34=======================================
35
36On Windows
37----------
38
39When built as a native Windows DLL, libgit2 uses WinCNG and WinHTTP,
40both of which are thread-safe. You do not need to do anything special.
41
42When using libssh2 which itself uses WinCNG, there are no special
43steps necessary. If you are using a MinGW or similar environment where
85247df0 44libssh2 uses OpenSSL or libgcrypt, then the general case affects
15bea02c
CMN
45you.
46
85247df0 47On Mac OS X
15bea02c
CMN
48-----------
49
ede517bc
CMN
50By default we use libcurl to perform the encryption. The
51system-provided libcurl uses SecureTransport, so no special steps are
52necessary. If you link against another libcurl (e.g. from homebrew)
53refer to the general case.
54
55If the option to use libcurl was deactivated, the library makes use of
56CommonCrypto and SecureTransport for cryptographic support. These are
57thread-safe and you do not need to do anything special.
85247df0
CMN
58
59Note that libssh2 may still use OpenSSL itself. In that case, the
60general case still affects you if you use ssh.
61
62General Case
63------------
64
ede517bc
CMN
65By default we use libcurl, which has its own ![recommendations for
66thread safety](http://curl.haxx.se/libcurl/c/libcurl-tutorial.html#Multi-threading).
67
68If libcurl was not found or was disabled, libgit2 uses OpenSSL to be
69able to use HTTPS as a transport. This library is made to be
70thread-implementation agnostic, and the users of the library must set
71which locking function it should use. This means that libgit2 cannot
72know what to set as the user of libgit2 may use OpenSSL independently
73and the locking settings must survive libgit2 shutting down.
15bea02c 74
263b1d6e
CMN
75libgit2 does provide a last-resort convenience function
76`git_openssl_set_locking()` (available in `sys/openssl.h`) to use the
77platform-native mutex mechanisms to perform the locking, which you may
78rely on if you do not want to use OpenSSL outside of libgit2, or you
79know that libgit2 will outlive the rest of the operations. It is not
80safe to use OpenSSL multi-threaded after libgit2's shutdown function
81has been called.
82
83If your programming language offers a package/bindings for OpenSSL,
84you should very strongly prefer to use that in order to set up
85locking, as they provide a level of coördination which is impossible
86when using this function.
15bea02c
CMN
87
88See the
89[OpenSSL documentation](https://www.openssl.org/docs/crypto/threads.html)
90on threading for more details.
91
85247df0
CMN
92Be also aware that libgit2 does not always link against OpenSSL
93if there are alternatives provided by the system.
263b1d6e 94
15bea02c
CMN
95libssh2 may be linked against OpenSSL or libgcrypt. If it uses
96OpenSSL, you only need to set up threading for OpenSSL once and the
97above paragraphs are enough. If it uses libgcrypt, then you need to
98set up its locking before using it multi-threaded. libgit2 has no
99direct connection to libgcrypt and thus has not convenience functions for
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.