CRYPTO_THREAD_run_once.pod 6.0 KB

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