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