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