CRYPTO_THREAD_run_once.pod 5.2 KB

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