Items without a =over and a =back are ignored.
[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 =over 4
61
62 =item *
63 Three additional callback function, dyn_create_function, dyn_lock_function
64 and dyn_destroy_function.
65
66 =item *
67 A structure defined with the data that each lock needs to handle.
68
69 =back
70
71 struct CRYPTO_dynlock_value has to be defined to contain whatever structure
72 is needed to handle locks.
73
74 dyn_create_function(const char *file, int line) is needed to create a
75 lock.  Multi-threaded applications might crash at random if it is not set.
76
77 dyn_lock_function(int mode, CRYPTO_dynlock *l, const char *file, int line)
78 is needed to perform locking off dynamic lock nunmbered n. Multi-threaded
79 applications might crash at random if it is not set.
80
81 dyn_destroy_function(CRYPTO_dynlock *l, const char *file, int line) is
82 needed to destroy the lock l. Multi-threaded applications might crash at
83 random if it is not set.
84
85 CRYPTO_get_new_dynlockid() is used to create locks.  It will call
86 dyn_create_function for the actual creation.
87
88 CRYPTO_destroy_dynlockid() is used to destroy locks.  It will call
89 dyn_destroy_function for the actual destruction.
90
91 CRYPTO_lock() is used to lock and unlock the locks.  mode is a bitfield
92 describing what should be done with the lock.  n is the number of the
93 lock as returned from CRYPTO_get_new_dynlockid().  mode can be combined
94 from the following values.  These values are pairwise exclusive, with
95 undefined behavior if misused (for example, CRYPTO_READ and CRYPTO_WRITE
96 should not be used together):
97
98         CRYPTO_LOCK     0x01
99         CRYPTO_UNLOCK   0x02
100         CRYPTO_READ     0x04
101         CRYPTO_WRITE    0x08
102
103 =head1 RETURN VALUES
104
105 CRYPTO_num_locks() returns the required number of locks.
106
107 CRYPTO_get_new_dynlockid() returns the index to the newly created lock.
108
109 The other functions return no values.
110
111 =head1 NOTE
112
113 You can find out if OpenSSL was configured with thread support:
114
115  #define OPENSSL_THREAD_DEFINES
116  #include <openssl/opensslconf.h>
117  #if defined(THREADS)
118    // thread support enabled
119  #else
120    // no thread support
121  #endif
122
123 Also, dynamic locks are currently not used internally by OpenSSL, but
124 may do so in the future.
125
126 =head1 EXAMPLES
127
128 B<crypto/threads/mttest.c> shows examples of the callback functions on
129 Solaris, Irix and Win32.
130
131 =head1 HISTORY
132
133 CRYPTO_set_locking_callback() and CRYPTO_set_id_callback() are
134 available in all versions of SSLeay and OpenSSL.
135 CRYPTO_num_locks() was added in OpenSSL 0.9.4.
136 All functions dealing with dynamic locks were added in OpenSSL 0.9.5b-dev.
137
138 =head1 SEE ALSO
139
140 L<crypto(3)|crypto(3)>
141
142 =cut