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