1CRYPTO_THREAD_RUN_ONCE(3)           OpenSSL          CRYPTO_THREAD_RUN_ONCE(3)
2
3
4

NAME

6       CRYPTO_THREAD_run_once, CRYPTO_THREAD_lock_new,
7       CRYPTO_THREAD_read_lock, CRYPTO_THREAD_write_lock,
8       CRYPTO_THREAD_unlock, CRYPTO_THREAD_lock_free, CRYPTO_atomic_add -
9       OpenSSL thread support
10

SYNOPSIS

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

DESCRIPTION

26       OpenSSL can be safely used in multi-threaded applications provided that
27       support for the underlying OS threading API is built-in. Currently,
28       OpenSSL supports the pthread and Windows APIs. OpenSSL can also be
29       built without any multi-threading support, for example on platforms
30       that don't provide any threading support or that provide a threading
31       API that is not yet supported by OpenSSL.
32
33       The following multi-threading function are provided:
34
35       · CRYPTO_THREAD_run_once() can be used to perform one-time
36         initialization.  The once argument must be a pointer to a static
37         object of type CRYPTO_ONCE that was statically initialized to the
38         value CRYPTO_ONCE_STATIC_INIT.  The init argument is a pointer to a
39         function that performs the desired exactly once initialization.  In
40         particular, this can be used to allocate locks in a thread-safe
41         manner, which can then be used with the locking functions below.
42
43       · CRYPTO_THREAD_lock_new() allocates, initializes and returns a new
44         read/write lock.
45
46       · CRYPTO_THREAD_read_lock() locks the provided lock for reading.
47
48       · CRYPTO_THREAD_write_lock() locks the provided lock for writing.
49
50       · CRYPTO_THREAD_unlock() unlocks the previously locked lock.
51
52       · CRYPTO_THREAD_lock_free() frees the provided lock.
53
54       · CRYPTO_atomic_add() atomically adds amount to val and returns the
55         result of the operation in ret. lock will be locked, unless atomic
56         operations are supported on the specific platform. Because of this,
57         if a variable is modified by CRYPTO_atomic_add() then
58         CRYPTO_atomic_add() must be the only way that the variable is
59         modified.
60

RETURN VALUES

62       CRYPTO_THREAD_run_once() returns 1 on success, or 0 on error.
63
64       CRYPTO_THREAD_lock_new() returns the allocated lock, or NULL on error.
65
66       CRYPTO_THREAD_lock_free() returns no value.
67
68       The other functions return 1 on success, or 0 on error.
69

NOTES

71       On Windows platforms the CRYPTO_THREAD_* types and functions in the
72       openssl/crypto.h header are dependent on some of the types customarily
73       made available by including windows.h. The application developer is
74       likely to require control over when the latter is included, commonly as
75       one of the first included headers. Therefore, it is defined as an
76       application developer's responsibility to include windows.h prior to
77       crypto.h where use of CRYPTO_THREAD_* types and functions is required.
78

EXAMPLES

80       This example safely initializes and uses a lock.
81
82        #ifdef _WIN32
83        # include <windows.h>
84        #endif
85        #include <openssl/crypto.h>
86
87        static CRYPTO_ONCE once = CRYPTO_ONCE_STATIC_INIT;
88        static CRYPTO_RWLOCK *lock;
89
90        static void myinit(void)
91        {
92            lock = CRYPTO_THREAD_lock_new();
93        }
94
95        static int mylock(void)
96        {
97            if (!CRYPTO_THREAD_run_once(&once, void init) || lock == NULL)
98                return 0;
99            return CRYPTO_THREAD_write_lock(lock);
100        }
101
102        static int myunlock(void)
103        {
104            return CRYPTO_THREAD_unlock(lock);
105        }
106
107        int serialized(void)
108        {
109            int ret = 0;
110
111            if (mylock()) {
112                /* Your code here, do not return without releasing the lock! */
113                ret = ... ;
114            }
115            myunlock();
116            return ret;
117        }
118
119       Finalization of locks is an advanced topic, not covered in this
120       example.  This can only be done at process exit or when a dynamically
121       loaded library is no longer in use and is unloaded.  The simplest
122       solution is to just "leak" the lock in applications and not repeatedly
123       load/unload shared libraries that allocate locks.
124

NOTES

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

SEE ALSO

136       crypto(7)
137
139       Copyright 2000-2020 The OpenSSL Project Authors. All Rights Reserved.
140
141       Licensed under the OpenSSL license (the "License").  You may not use
142       this file except in compliance with the License.  You can obtain a copy
143       in the file LICENSE in the source distribution or at
144       <https://www.openssl.org/source/license.html>.
145
146
147
1481.1.1k                            2021-03-26         CRYPTO_THREAD_RUN_ONCE(3)
Impressum