ossl_rcu_lock_new.pod 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. =pod
  2. =head1 NAME
  3. ossl_rcu_lock_new,
  4. ossl_rcu_lock_free, ossl_rcu_read_lock,
  5. ossl_rcu_read_unlock, ossl_rcu_write_lock,
  6. ossl_rcu_write_unlock, ossl_synchronize_rcu,
  7. ossl_rcu_call, ossl_rcu_deref,
  8. ossl_rcu_assign_ptr, ossl_rcu_uptr_deref,
  9. ossl_rcu_assign_uptr
  10. - perform read-copy-update locking
  11. =head1 SYNOPSIS
  12. CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers);
  13. void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock);
  14. void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock);
  15. void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock);
  16. void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock);
  17. void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock);
  18. void ossl_rcu_call(CRYPTO_RCU_LOCK *lock, rcu_cb_fn cb, void *data);
  19. void *ossl_rcu_deref(void **p);
  20. void ossl_rcu_uptr_deref(void **p);
  21. void ossl_rcu_assign_ptr(void **p, void **v);
  22. void ossl_rcu_assign_uptr(void **p, void **v);
  23. void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock);
  24. =head1 DESCRIPTION
  25. OpenSSL can be safely used in multi-threaded applications provided that
  26. support for the underlying OS threading API is built-in. Currently, OpenSSL
  27. supports the pthread and Windows APIs. OpenSSL can also be built without
  28. any multi-threading support, for example on platforms that don't provide
  29. any threading support or that provide a threading API that is not yet
  30. supported by OpenSSL.
  31. In addition to more traditional Read/Write locks, OpenSSL provides
  32. Read-Copy-Update (RCU) locks, which allow for always nonblocking read paths.
  33. The following multi-threading functions are provided:
  34. =over 2
  35. =item *
  36. ossl_rcu_assign_uptr() assigns the value pointed to by v to the
  37. location pointed to by p. This function should typically not be used, rely
  38. instead on the ossl_rcu_assign_ptr() macro.
  39. =item *
  40. ossl_rcu_uptr_deref() returns the value stored at the
  41. location pointed to by p. This function should typically not be used, rely
  42. instead on the ossl_rcu_deref() macro.
  43. =item *
  44. ossl_rcu_assign_ptr() assigns the value pointed to by v to
  45. location pointed to by p.
  46. =item *
  47. ossl_rcu_lock_new() allocates a new RCU lock. The I<num_writers> param
  48. indicates the number of write side threads which may execute
  49. ossl_synchronize_rcu() in parallel. The value must be at least 1, but may be
  50. larger to obtain increased write side throughput at the cost of additional
  51. internal memory usage. A value of 1 is generally recommended.
  52. =item *
  53. ossl_rcu_read_lock() acquires a read side hold on data protected by
  54. the lock.
  55. =item *
  56. ossl_rcu_read_unlock() releases a read side hold on data protected by
  57. the lock.
  58. =item *
  59. ossl_rcu_write_lock() acquires a write side hold on data protected by
  60. the lock. Note only one writer per lock is permitted, as with read/write locks.
  61. =item *
  62. ossl_rcu_write_unlock() releases a write side hold on data protected
  63. by the lock.
  64. =item *
  65. ossl_synchronize_rcu() blocks the calling thread until all read side
  66. holds on the lock have been released, guaranteeing that any old data updated by
  67. the write side thread is safe to free.
  68. =item *
  69. ossl_rcu_call() enqueues a callback function to the lock, to be called
  70. when the next synchronization completes. Note: It is not guaranteed that the
  71. thread which enqueued the callback will be the thread which executes the
  72. callback
  73. =item *
  74. ossl_rcu_deref(p) atomically reads a pointer under an RCU locks
  75. protection
  76. =item *
  77. ossl_rcu_assign_ptr(p,v) atomically writes to a pointer under an
  78. RCU locks protection
  79. =item *
  80. ossl_rcu_lock_free() frees an allocated RCU lock
  81. =back
  82. =head1 RETURN VALUES
  83. ossl_rcu_lock_new() returns a pointer to a newly created RCU lock structure.
  84. ossl_rcu_deref() and ossl_rcu_uptr_deref() return the value pointed
  85. to by the passed in value v.
  86. All other functions return no value.
  87. =head1 EXAMPLES
  88. You can find out if OpenSSL was configured with thread support:
  89. #include <openssl/opensslconf.h>
  90. #if defined(OPENSSL_THREADS)
  91. /* thread support enabled */
  92. #else
  93. /* no thread support */
  94. #endif
  95. This example safely initializes and uses a lock.
  96. #include "internal/rcu.h"
  97. struct foo {
  98. int aval;
  99. char *name;
  100. };
  101. static CRYPTO_ONCE once = CRYPTO_ONCE_STATIC_INIT;
  102. static CRYPTO_RCU_LOCK *lock;
  103. static struct foo *fooptr = NULL;
  104. static void myinit(void)
  105. {
  106. lock = ossl_rcu_lock_new(1);
  107. }
  108. static int initlock(void)
  109. {
  110. if (!RUN_ONCE(&once, myinit) || lock == NULL)
  111. return 0;
  112. return 1;
  113. }
  114. static void writer_thread()
  115. {
  116. struct foo *newfoo;
  117. struct foo *oldfoo;
  118. initlock();
  119. /*
  120. * update steps in an rcu model
  121. */
  122. /*
  123. * 1) create a new shared object
  124. */
  125. newfoo = OPENSSL_zalloc(sizeof(struct foo));
  126. /*
  127. * acquire the write side lock
  128. */
  129. ossl_rcu_write_lock(lock);
  130. /*
  131. * 2) read the old pointer
  132. */
  133. oldfoo = ossl_rcu_deref(&fooptr);
  134. /*
  135. * 3) Copy the old pointer to the new object, and
  136. * make any needed adjustments
  137. */
  138. memcpy(newfoo, oldfoo, sizeof(struct foo));
  139. newfoo->aval++;
  140. /*
  141. * 4) Update the shared pointer to the new value
  142. */
  143. ossl_rcu_assign_ptr(&fooptr, &newfoo);
  144. /*
  145. * 5) Release the write side lock
  146. */
  147. ossl_rcu_write_unlock(lock);
  148. /*
  149. * 6) wait for any read side holds on the old data
  150. * to be released
  151. */
  152. ossl_synchronize_rcu(lock);
  153. /*
  154. * 7) free the old pointer, now that there are no
  155. * further readers
  156. */
  157. OPENSSL_free(oldfoo);
  158. }
  159. static void reader_thread()
  160. {
  161. struct foo *myfoo = NULL;
  162. int a;
  163. /*
  164. * 1) Acquire a read side hold on the shared data
  165. */
  166. ossl_rcu_read_lock(lock);
  167. /*
  168. * 2) Access the shared data pointer
  169. */
  170. myfoo = ossl_rcu_deref(&fooptr);
  171. /*
  172. * 3) Read the data from the pointer
  173. */
  174. a = myfoo->aval;
  175. /*
  176. * 4) Indicate our hold on the shared data is complete
  177. */
  178. ossl_rcu_read_unlock(lock);
  179. }
  180. =head1 SEE ALSO
  181. L<crypto(7)>, L<openssl-threads(7)>.
  182. =head1 COPYRIGHT
  183. Copyright 2023-2024 The OpenSSL Project Authors. All Rights Reserved.
  184. Licensed under the Apache License 2.0 (the "License"). You may not use
  185. this file except in compliance with the License. You can obtain a copy
  186. in the file LICENSE in the source distribution or at
  187. L<https://www.openssl.org/source/license.html>.
  188. =cut