CRYPTO_THREAD_run_once.pod 4.7 KB

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