Home | History | Annotate | Line # | Download | only in man3
      1 =pod
      2 
      3 =head1 NAME
      4 
      5 ossl_rcu_lock_new,
      6 ossl_rcu_lock_free, ossl_rcu_read_lock,
      7 ossl_rcu_read_unlock, ossl_rcu_write_lock,
      8 ossl_rcu_write_unlock, ossl_synchronize_rcu,
      9 ossl_rcu_cb_item_new, ossl_rcu_cb_item_free,
     10 ossl_rcu_call, ossl_rcu_deref,
     11 ossl_rcu_assign_ptr, ossl_rcu_uptr_deref,
     12 ossl_rcu_assign_uptr
     13 - perform read-copy-update locking
     14 
     15 =head1 SYNOPSIS
     16 
     17  CRYPTO_RCU_LOCK *ossl_rcu_lock_new(int num_writers, OSSL_LIB_CTX *ctx);
     18  void ossl_rcu_read_lock(CRYPTO_RCU_LOCK *lock);
     19  void ossl_rcu_write_lock(CRYPTO_RCU_LOCK *lock);
     20  void ossl_rcu_write_unlock(CRYPTO_RCU_LOCK *lock);
     21  void ossl_rcu_read_unlock(CRYPTO_RCU_LOCK *lock);
     22  void ossl_synchronize_rcu(CRYPTO_RCU_LOCK *lock);
     23  CRYPTO_RCU_CB_ITEM *ossl_rcu_cb_item_new(void);
     24  void ossl_rcu_cb_item_free(CRYPTO_RCU_CB_ITEM *item);
     25  void ossl_rcu_call(CRYPTO_RCU_LOCK *lock, CRYPTO_RCU_CB_ITEM *item,
     26                     rcu_cb_fn cb, void *data);
     27  void *ossl_rcu_deref(void **p);
     28  void ossl_rcu_uptr_deref(void **p);
     29  void ossl_rcu_assign_ptr(void **p, void **v);
     30  void ossl_rcu_assign_uptr(void **p, void **v);
     31  void ossl_rcu_lock_free(CRYPTO_RCU_LOCK *lock);
     32 
     33 =head1 DESCRIPTION
     34 
     35 OpenSSL can be safely used in multi-threaded applications provided that
     36 support for the underlying OS threading API is built-in. Currently, OpenSSL
     37 supports the pthread and Windows APIs. OpenSSL can also be built without
     38 any multi-threading support, for example on platforms that don't provide
     39 any threading support or that provide a threading API that is not yet
     40 supported by OpenSSL.
     41 
     42 In addition to more traditional Read/Write locks, OpenSSL provides
     43 Read-Copy-Update (RCU) locks, which allow for always nonblocking read paths.
     44 
     45 The following multi-threading functions are provided:
     46 
     47 =over 2
     48 
     49 =item *
     50 
     51 ossl_rcu_assign_uptr() assigns the value pointed to by v to the
     52 location pointed to by p.  This function should typically not be used, rely
     53 instead on the ossl_rcu_assign_ptr() macro.
     54 
     55 =item *
     56 
     57 ossl_rcu_uptr_deref() returns the value stored at the
     58 location pointed to by p. This function should typically not be used, rely
     59 instead on the ossl_rcu_deref() macro.
     60 
     61 =item *
     62 
     63 ossl_rcu_assign_ptr() assigns the value pointed to by v to
     64 location pointed to by p.
     65 
     66 =item *
     67 
     68 ossl_rcu_lock_new() allocates a new RCU lock.  The I<num_writers> param
     69 indicates the number of write side threads which may execute
     70 ossl_synchronize_rcu() in parallel.  The value must be at least 1, but may be
     71 larger to obtain increased write side throughput at the cost of additional
     72 internal memory usage.  A value of 1 is generally recommended. The I<ctx>
     73 parameter references the library context in which the lock is allocated.
     74 
     75 =item *
     76 
     77 ossl_rcu_read_lock() acquires a read side hold on data protected by
     78 the lock.
     79 
     80 =item *
     81 
     82 ossl_rcu_read_unlock() releases a read side hold on data protected by
     83 the lock.
     84 
     85 =item *
     86 
     87 ossl_rcu_write_lock() acquires a write side hold on data protected by
     88 the lock.  Note only one writer per lock is permitted, as with read/write locks.
     89 
     90 =item *
     91 
     92 ossl_rcu_write_unlock() releases a write side hold on data protected
     93 by the lock.
     94 
     95 =item *
     96 
     97 ossl_synchronize_rcu() blocks the calling thread until all read side
     98 holds on the lock have been released, guaranteeing that any old data updated by
     99 the write side thread is safe to free.
    100 
    101 =item *
    102 
    103 ossl_rcu_cb_item_new() allocates a callback item suitable for use with
    104 ossl_rcu_call().  Returns NULL on allocation failure.  The item is owned by
    105 the caller until it is passed to ossl_rcu_call(), at which point ownership
    106 transfers to the lock and the item must not be touched again by the caller.
    107 
    108 =item *
    109 
    110 ossl_rcu_cb_item_free() frees a callback item that was allocated by
    111 ossl_rcu_cb_item_new() but never passed to ossl_rcu_call().  Use this to
    112 release the item on the failure path of an operation that decided not to
    113 publish its update.
    114 
    115 =item *
    116 
    117 ossl_rcu_call() enqueues a callback function I<cb> to the lock, to be
    118 called with I<data> when the next synchronization completes.  The caller
    119 must provide a callback item I<item> previously obtained from
    120 ossl_rcu_cb_item_new().  After this call the lock owns the item and will
    121 free it after invoking the callback.  This function does not allocate and
    122 cannot fail, which lets callers allocate the item before performing any
    123 publish (assign_ptr) and bail cleanly if allocation fails.  Note: it is
    124 not guaranteed that the thread which enqueued the callback will be the
    125 thread which executes the callback.
    126 
    127 =item *
    128 
    129 ossl_rcu_deref(p) atomically reads a pointer under an RCU locks
    130 protection
    131 
    132 =item *
    133 
    134 ossl_rcu_assign_ptr(p,v) atomically writes to a pointer under an
    135 RCU locks protection
    136 
    137 =item *
    138 
    139 ossl_rcu_lock_free() frees an allocated RCU lock
    140 
    141 =back
    142 
    143 =head1 RETURN VALUES
    144 
    145 ossl_rcu_lock_new() returns a pointer to a newly created RCU lock structure.
    146 
    147 ossl_rcu_cb_item_new() returns a pointer to a newly created callback item,
    148 or NULL on allocation failure.
    149 
    150 ossl_rcu_deref() and ossl_rcu_uptr_deref() return the value pointed
    151 to by the passed in value v.
    152 
    153 All other functions return no value.
    154 
    155 =head1 EXAMPLES
    156 
    157 You can find out if OpenSSL was configured with thread support:
    158 
    159  #include <openssl/opensslconf.h>
    160  #if defined(OPENSSL_THREADS)
    161      /* thread support enabled */
    162  #else
    163      /* no thread support */
    164  #endif
    165 
    166 This example safely initializes and uses a lock.
    167 
    168  #include "internal/rcu.h"
    169 
    170  struct foo {
    171     int aval;
    172     char *name;
    173  };
    174 
    175  static CRYPTO_ONCE once = CRYPTO_ONCE_STATIC_INIT;
    176  static CRYPTO_RCU_LOCK *lock;
    177  static struct foo *fooptr = NULL;
    178 
    179  static void myinit(void)
    180  {
    181      lock = ossl_rcu_lock_new(1, NULL);
    182  }
    183 
    184  static int initlock(void)
    185  {
    186      if (!RUN_ONCE(&once, myinit) || lock == NULL)
    187          return 0;
    188      return 1;
    189  }
    190 
    191  static void free_old_foo(void *data)
    192  {
    193      OPENSSL_free(data);
    194  }
    195 
    196  static int writer_thread(void)
    197  {
    198     struct foo *newfoo;
    199     struct foo *oldfoo;
    200     CRYPTO_RCU_CB_ITEM *cbi;
    201 
    202     initlock();
    203 
    204     /*
    205      * update steps in an rcu model
    206      */
    207 
    208     /*
    209      * 1) create a new shared object
    210      */
    211     newfoo = OPENSSL_zalloc(sizeof(struct foo));
    212     if (newfoo == NULL)
    213         return 0;
    214 
    215     /*
    216      * 2) Pre allocate the rcu callback item before any publish.
    217      */
    218     cbi = ossl_rcu_cb_item_new();
    219     if (cbi == NULL) {
    220         OPENSSL_free(newfoo);
    221         return 0;
    222     }
    223 
    224     /*
    225      * 3) acquire the write side lock
    226      */
    227     ossl_rcu_write_lock(lock);
    228 
    229     /*
    230      * 4) read the old pointer
    231      */
    232     oldfoo = ossl_rcu_deref(&fooptr);
    233 
    234     /*
    235      * 5) Copy the old pointer to the new object, and
    236      *    make any needed adjustments
    237      */
    238     memcpy(newfoo, oldfoo, sizeof(struct foo));
    239     newfoo->aval++;
    240 
    241     /*
    242      * 6) Update the shared pointer to the new value
    243      */
    244     ossl_rcu_assign_ptr(&fooptr, &newfoo);
    245 
    246     /*
    247      * 7) Schedule the old pointer to be freed when readers are done.
    248      */
    249     ossl_rcu_call(lock, cbi, free_old_foo, oldfoo);
    250 
    251     /*
    252      * 8) Release the write side lock
    253      */
    254     ossl_rcu_write_unlock(lock);
    255 
    256     /*
    257      * 9) wait for any read side holds on the old data
    258      *    to be released, after which free_old_foo will run
    259      */
    260     ossl_synchronize_rcu(lock);
    261 
    262     return 1;
    263  }
    264 
    265  static void reader_thread(void)
    266  {
    267     struct foo *myfoo = NULL;
    268     int a;
    269     /*
    270      * 1) Acquire a read side hold on the shared data
    271      */
    272     ossl_rcu_read_lock(lock);
    273 
    274     /*
    275      * 2) Access the shared data pointer
    276      */
    277     myfoo = ossl_rcu_deref(&fooptr);
    278 
    279     /*
    280      * 3) Read the data from the pointer
    281      */
    282     a = myfoo->aval;
    283 
    284     /*
    285      * 4) Indicate our hold on the shared data is complete
    286      */
    287     ossl_rcu_read_unlock(lock);
    288  }
    289 
    290 =head1 SEE ALSO
    291 
    292 L<crypto(7)>, L<openssl-threads(7)>.
    293 
    294 =head1 COPYRIGHT
    295 
    296 Copyright 2023-2026 The OpenSSL Project Authors. All Rights Reserved.
    297 
    298 Licensed under the Apache License 2.0 (the "License").  You may not use
    299 this file except in compliance with the License.  You can obtain a copy
    300 in the file LICENSE in the source distribution or at
    301 L<https://www.openssl.org/source/license.html>.
    302 
    303 =cut
    304