CRYPTO_THREAD_run_once.pod 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. =pod
  2. =head1 NAME
  3. CRYPTO_THREAD_run_once,
  4. CRYPTO_THREAD_lock_new, CRYPTO_THREAD_read_lock, CRYPTO_THREAD_write_lock,
  5. CRYPTO_THREAD_unlock, CRYPTO_THREAD_lock_free,
  6. CRYPTO_atomic_add, CRYPTO_atomic_or, CRYPTO_atomic_load,
  7. OSSL_set_max_threads, OSSL_get_max_threads,
  8. OSSL_get_thread_support_flags - OpenSSL thread support
  9. =head1 SYNOPSIS
  10. #include <openssl/crypto.h>
  11. CRYPTO_ONCE CRYPTO_ONCE_STATIC_INIT;
  12. int CRYPTO_THREAD_run_once(CRYPTO_ONCE *once, void (*init)(void));
  13. CRYPTO_RWLOCK *CRYPTO_THREAD_lock_new(void);
  14. int CRYPTO_THREAD_read_lock(CRYPTO_RWLOCK *lock);
  15. int CRYPTO_THREAD_write_lock(CRYPTO_RWLOCK *lock);
  16. int CRYPTO_THREAD_unlock(CRYPTO_RWLOCK *lock);
  17. void CRYPTO_THREAD_lock_free(CRYPTO_RWLOCK *lock);
  18. int CRYPTO_atomic_add(int *val, int amount, int *ret, CRYPTO_RWLOCK *lock);
  19. int CRYPTO_atomic_or(uint64_t *val, uint64_t op, uint64_t *ret,
  20. CRYPTO_RWLOCK *lock);
  21. int CRYPTO_atomic_load(uint64_t *val, uint64_t *ret, CRYPTO_RWLOCK *lock);
  22. int OSSL_set_max_threads(OSSL_LIB_CTX *ctx, uint64_t max_threads);
  23. uint64_t OSSL_get_max_threads(OSSL_LIB_CTX *ctx);
  24. uint32_t OSSL_get_thread_support_flags(void);
  25. =head1 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, 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. The following multi-threading function are provided:
  33. =over 2
  34. =item *
  35. CRYPTO_THREAD_run_once() can be used to perform one-time initialization.
  36. The I<once> argument must be a pointer to a static object of type
  37. B<CRYPTO_ONCE> that was statically initialized to the value
  38. B<CRYPTO_ONCE_STATIC_INIT>.
  39. The I<init> argument is a pointer to a function that performs the desired
  40. exactly once initialization.
  41. In particular, this can be used to allocate locks in a thread-safe manner,
  42. which can then be used with the locking functions below.
  43. =item *
  44. CRYPTO_THREAD_lock_new() allocates, initializes and returns a new read/write
  45. lock.
  46. =item *
  47. CRYPTO_THREAD_read_lock() locks the provided I<lock> for reading.
  48. =item *
  49. CRYPTO_THREAD_write_lock() locks the provided I<lock> for writing.
  50. =item *
  51. CRYPTO_THREAD_unlock() unlocks the previously locked I<lock>.
  52. =item *
  53. CRYPTO_THREAD_lock_free() frees the provided I<lock>.
  54. =item *
  55. CRYPTO_atomic_add() atomically adds I<amount> to I<*val> and returns the
  56. result of the operation in I<*ret>. I<lock> will be locked, unless atomic
  57. operations are supported on the specific platform. Because of this, if a
  58. variable is modified by CRYPTO_atomic_add() then CRYPTO_atomic_add() must
  59. be the only way that the variable is modified. If atomic operations are not
  60. supported and I<lock> is NULL, then the function will fail.
  61. =item *
  62. CRYPTO_atomic_or() performs an atomic bitwise or of I<op> and I<*val> and stores
  63. the result back in I<*val>. It also returns the result of the operation in
  64. I<*ret>. I<lock> will be locked, unless atomic operations are supported on the
  65. specific platform. Because of this, if a variable is modified by
  66. CRYPTO_atomic_or() or read by CRYPTO_atomic_load() then CRYPTO_atomic_or() must
  67. be the only way that the variable is modified. If atomic operations are not
  68. supported and I<lock> is NULL, then the function will fail.
  69. =item *
  70. CRYPTO_atomic_load() atomically loads the contents of I<*val> into I<*ret>.
  71. I<lock> will be locked, unless atomic operations are supported on the specific
  72. platform. Because of this, if a variable is modified by CRYPTO_atomic_or() or
  73. read by CRYPTO_atomic_load() then CRYPTO_atomic_load() must be the only way that
  74. the variable is read. If atomic operations are not supported and I<lock> is
  75. NULL, then the function will fail.
  76. =item *
  77. OSSL_set_max_threads() sets the maximum number of threads to be used by the
  78. thread pool. If the argument is 0, thread pooling is disabled. OpenSSL will
  79. not create any threads and existing threads in the thread pool will be torn
  80. down. The maximum thread count is a limit, not a target. Threads will not be
  81. spawned unless (and until) there is demand. Thread polling is disabled by
  82. default. To enable threading you must call OSSL_set_max_threads() explicitly.
  83. Under no circumstances is this done for you.
  84. =back
  85. =head1 RETURN VALUES
  86. CRYPTO_THREAD_run_once() returns 1 on success, or 0 on error.
  87. CRYPTO_THREAD_lock_new() returns the allocated lock, or NULL on error.
  88. CRYPTO_THREAD_lock_free() returns no value.
  89. OSSL_set_max_threads() returns 1 on success and 0 on failure. Returns failure
  90. if OpenSSL-managed thread pooling is not supported (for example, if it is not
  91. supported on the current platform, or because OpenSSL is not built with the
  92. necessary support).
  93. OSSL_get_max_threads() returns the maximum number of threads currently allowed
  94. to be used by the thread pool. If thread pooling is disabled or not available,
  95. returns 0.
  96. The other functions return 1 on success, or 0 on error.
  97. =head1 NOTES
  98. On Windows platforms the CRYPTO_THREAD_* types and functions in the
  99. F<< <openssl/crypto.h> >> header are dependent on some of the types
  100. customarily made available by including F<< <windows.h> >>. The application
  101. developer is likely to require control over when the latter is included,
  102. commonly as one of the first included headers. Therefore, it is defined as an
  103. application developer's responsibility to include F<< <windows.h> >> prior to
  104. F<< <openssl/crypto.h> >> where use of CRYPTO_THREAD_* types and functions is
  105. required.
  106. =head1 EXAMPLES
  107. You can find out if OpenSSL was configured with thread support:
  108. #include <openssl/opensslconf.h>
  109. #if defined(OPENSSL_THREADS)
  110. /* thread support enabled */
  111. #else
  112. /* no thread support */
  113. #endif
  114. This example safely initializes and uses a lock.
  115. #ifdef _WIN32
  116. # include <windows.h>
  117. #endif
  118. #include <openssl/crypto.h>
  119. static CRYPTO_ONCE once = CRYPTO_ONCE_STATIC_INIT;
  120. static CRYPTO_RWLOCK *lock;
  121. static void myinit(void)
  122. {
  123. lock = CRYPTO_THREAD_lock_new();
  124. }
  125. static int mylock(void)
  126. {
  127. if (!CRYPTO_THREAD_run_once(&once, void init) || lock == NULL)
  128. return 0;
  129. return CRYPTO_THREAD_write_lock(lock);
  130. }
  131. static int myunlock(void)
  132. {
  133. return CRYPTO_THREAD_unlock(lock);
  134. }
  135. int serialized(void)
  136. {
  137. int ret = 0;
  138. if (mylock()) {
  139. /* Your code here, do not return without releasing the lock! */
  140. ret = ... ;
  141. }
  142. myunlock();
  143. return ret;
  144. }
  145. Finalization of locks is an advanced topic, not covered in this example.
  146. This can only be done at process exit or when a dynamically loaded library is
  147. no longer in use and is unloaded.
  148. The simplest solution is to just "leak" the lock in applications and not
  149. repeatedly load/unload shared libraries that allocate locks.
  150. =head1 SEE ALSO
  151. L<crypto(7)>, L<openssl-threads(7)>.
  152. =head1 COPYRIGHT
  153. Copyright 2000-2021 The OpenSSL Project Authors. All Rights Reserved.
  154. Licensed under the Apache License 2.0 (the "License"). You may not use
  155. this file except in compliance with the License. You can obtain a copy
  156. in the file LICENSE in the source distribution or at
  157. L<https://www.openssl.org/source/license.html>.
  158. =cut