CRYPTO_THREAD_run_once.pod 4.7 KB

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