Use 'over 2' for bullet lists.
[openssl.git] / doc / man3 / CRYPTO_THREAD_run_once.pod
1 =pod
2
3 =head1 NAME
4
5 CRYPTO_THREAD_run_once,
6 CRYPTO_THREAD_lock_new, CRYPTO_THREAD_read_lock, CRYPTO_THREAD_write_lock,
7 CRYPTO_THREAD_unlock, CRYPTO_THREAD_lock_free, CRYPTO_atomic_add - OpenSSL thread support
8
9 =head1 SYNOPSIS
10
11  #include <openssl/crypto.h>
12
13  CRYPTO_ONCE CRYPTO_ONCE_STATIC_INIT;
14  int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void));
15
16  CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void);
17  int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock);
18  int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock);
19  int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock);
20  void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock);
21
22  int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock);
23
24 =head1 DESCRIPTION
25
26 OpenSSL can be safely used in multi-threaded applications provided that
27 support for the underlying OS threading API is built-in. Currently, OpenSSL
28 supports the pthread and Windows APIs. OpenSSL can also be built without
29 any multi-threading support, for example on platforms that don't provide
30 any threading support or that provide a threading API that is not yet
31 supported by OpenSSL.
32
33 The following multi-threading function are provided:
34
35 =over 2
36
37 =item *
38
39 CRYPTO_THREAD_run_once() can be used to perform one-time initialization.
40 The B<once> argument must be a pointer to a static object of type
41 B<CRYPTO_ONCE> that was statically initialized to the value
42 B<CRYPTO_ONCE_STATIC_INIT>.
43 The B<init> argument is a pointer to a function that performs the desired
44 exactly once initialization.
45 In particular, this can be used to allocate locks in a thread-safe manner,
46 which can then be used with the locking functions below.
47
48 =item *
49
50 CRYPTO_THREAD_lock_new() allocates, initializes and returns a new read/write
51 lock.
52
53 =item *
54
55 CRYPTO_THREAD_read_lock() locks the provided B<lock> for reading.
56
57 =item *
58
59 CRYPTO_THREAD_write_lock() locks the provided B<lock> for writing.
60
61 =item *
62
63 CRYPTO_THREAD_unlock() unlocks the previously locked B<lock>.
64
65 =item *
66
67 CRYPTO_THREAD_lock_frees() frees the provided B<lock>.
68
69 =item *
70
71 CRYPTO_atomic_add() atomically adds B<amount> to B<val> and returns the
72 result of the operation in B<ret>. B<lock> will be locked, unless atomic
73 operations are supported on the specific platform. Because of this, if a
74 variable is modified by CRYPTO_atomic_add() then CRYPTO_atomic_add() must
75 be the only way that the variable is modified.
76
77 =back
78
79 =head1 RETURN VALUES
80
81 CRYPTO_THREAD_run_once() returns 1 on success, or 0 on error.
82
83 CRYPTO_THREAD_lock_new() returns the allocated lock, or NULL on error.
84
85 CRYPTO_THREAD_lock_frees() returns no value.
86
87 The other functions return 1 on success or 0 on error.
88
89 =head1 NOTES
90
91 On Windows platforms the CRYPTO_THREAD_* types and functions in the
92 openssl/crypto.h header are dependent on some of the types customarily
93 made available by including windows.h. The application developer is
94 likely to require control over when the latter is included, commonly as
95 one of the first included headers. Therefore it is defined as an
96 application developer's responsibility to include windows.h prior to
97 crypto.h where use of CRYPTO_THREAD_* types and functions is required.
98
99 =head1 EXAMPLE
100
101 This example safely initializes and uses a lock.
102
103   #ifdef _WIN32
104   # include <windows.h>
105   #endif
106   #include <openssl/crypto.h>
107
108   static CRYPTO_ONCE once = CRYPTO_ONCE_STATIC_INIT;
109   static CRYPTO_RWLOCK *lock;
110
111   static void myinit(void)
112   {
113       lock = CRYPTO_THREAD_lock_new();
114   }
115
116   static int mylock(void)
117   {
118       if (!CRYPTO_THREAD_run_once(&once, void init) || lock == NULL)
119           return 0;
120       return CRYPTO_THREAD_write_lock(lock);
121   }
122
123   static int myunlock(void)
124   {
125       return CRYPTO_THREAD_unlock(lock);
126   }
127
128   int serialized(void)
129   {
130       int ret = 0;
131
132       if (mylock()) {
133           /* Your code here, do not return without releasing the lock! */
134           ret = ... ;
135       }
136       myunlock();
137       return ret;
138   }
139
140 Finalization of locks is an advanced topic, not covered in this example.
141 This can only be done at process exit or when a dynamically loaded library is
142 no longer in use and is unloaded.
143 The simplest solution is to just "leak" the lock in applications and not
144 repeatedly load/unload shared libraries that allocate locks.
145
146 =head1 NOTES
147
148 You can find out if OpenSSL was configured with thread support:
149
150  #include <openssl/opensslconf.h>
151  #if defined(OPENSSL_THREADS)
152    // thread support enabled
153  #else
154    // no thread support
155  #endif
156
157 =head1 SEE ALSO
158
159 L<crypto(7)>
160
161 =head1 COPYRIGHT
162
163 Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
164
165 Licensed under the OpenSSL license (the "License").  You may not use
166 this file except in compliance with the License.  You can obtain a copy
167 in the file LICENSE in the source distribution or at
168 L<https://www.openssl.org/source/license.html>.
169
170 =cut