More BIO docs.
[openssl.git] / doc / crypto / threads.pod
1 =pod
2
3 =head1 NAME
4
5 CRYPTO_set_locking_callback, CRYPTO_set_id_callback - OpenSSL thread support
6
7 =head1 SYNOPSIS
8
9  #include <openssl/crypto.h>
10
11  void CRYPTO_set_locking_callback(void (*locking_function)(int mode,
12         int n, const char *file, int line));
13
14  void CRYPTO_set_id_callback(unsigned long (*id_function)(void));
15
16  int CRYPTO_num_locks(void);
17
18
19  /* struct CRYPTO_dynlock_value needs to be defined by the user */
20  struct CRYPTO_dynlock_value;
21
22  void CRYPTO_set_dynlock_create_callback(struct CRYPTO_dynlock_value *
23         (*dyn_create_function)(char *file, int line));
24  void CRYPTO_set_dynlock_lock_callback(void (*dyn_lock_function)
25         (int mode, struct CRYPTO_dynlock_value *l,
26         const char *file, int line));
27  void CRYPTO_set_dynlock_destroy_callback(void (*dyn_destroy_function)
28         (struct CRYPTO_dynlock_value *l, const char *file, int line));
29
30  int CRYPTO_get_new_dynlockid(void);
31
32  void CRYPTO_destroy_dynlockid(int i);
33
34  void CRYPTO_lock(int mode, int n, const char *file, int line);
35
36 =head1 DESCRIPTION
37
38 OpenSSL can safely be used in multi-threaded applications provided
39 that at least two callback functions are set.
40
41 locking_function(int mode, int n, const char *file, int line) is
42 needed to perform locking on shared data stuctures. Multi-threaded
43 applications will crash at random if it is not set.
44
45 locking_function() must be able to handle up to CRYPTO_num_locks()
46 different mutex locks. It sets the B<n>-th lock if B<mode> &
47 B<CRYPTO_LOCK>, and releases it otherwise.
48
49 B<file> and B<line> are the file number of the function setting the
50 lock. They can be useful for debugging.
51
52 id_function(void) is a function that returns a thread ID. It is not
53 needed on Windows nor on platforms where getpid() returns a different
54 ID for each thread (most notably Linux).
55
56 Additionally, OpenSSL supports dynamic locks, and sometimes, some parts
57 of OpenSSL need it for better performance.  To enable this, the following
58 is required:
59
60 =item *
61 Three additional callback function, dyn_create_function, dyn_lock_function
62 and dyn_destroy_function.
63
64 =item *
65 A structure defined with the data that each lock needs to handle.
66
67 struct CRYPTO_dynlock_value has to be defined to contain whatever structure
68 is needed to handle locks.
69
70 dyn_create_function(const char *file, int line) is needed to create a
71 lock.  Multi-threaded applications might crash at random if it is not set.
72
73 dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
74 is needed to perform locking off dynamic lock nunmbered n. Multi-threaded
75 applications might crash at random if it is not set.
76
77 dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
78 needed to destroy the lock l. Multi-threaded applications might crash at
79 random if it is not set.
80
81 CRYPTO_get_new_dynlockid() is used to create locks.  It will call
82 dyn_create_function for the actual creation.
83
84 CRYPTO_destroy_dynlockid() is used to destroy locks.  It will call
85 dyn_destroy_function for the actual destruction.
86
87 CRYPTO_lock() is used to lock and unlock the locks.  mode is a bitfield
88 describing what should be done with the lock.  n is the number of the
89 lock as returned from CRYPTO_get_new_dynlockid().  mode can be combined
90 from the following values.  These values are pairwise exclusive, with
91 undefined behavior if misused (for example, CRYPTO_READ and CRYPTO_WRITE
92 should not be used together):
93
94         CRYPTO_LOCK     0x01
95         CRYPTO_UNLOCK   0x02
96         CRYPTO_READ     0x04
97         CRYPTO_WRITE    0x08
98
99 =head1 RETURN VALUES
100
101 CRYPTO_num_locks() returns the required number of locks.
102
103 CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
104
105 The other functions return no values.
106
107 =head1 NOTE
108
109 You can find out if OpenSSL was configured with thread support:
110
111  #define OPENSSL_THREAD_DEFINES
112  #include <openssl/opensslconf.h>
113  #if defined(THREADS)
114    // thread support enabled
115  #else
116    // no thread support
117  #endif
118
119 Also, dynamic locks are currently not used internally by OpenSSL, but
120 may do so in the future.
121
122 =head1 EXAMPLES
123
124 B<crypto/threads/mttest.c> shows examples of the callback functions on
125 Solaris, Irix and Win32.
126
127 =head1 HISTORY
128
129 CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() are
130 available in all versions of SSLeay and OpenSSL.
131 CRYPTO_num_locks() was added in OpenSSL 0.9.4.
132 All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev.
133
134 =head1 SEE ALSO
135
136 L<crypto(3)|crypto(3)>
137
138 =cut