Ignore:
Timestamp:
Oct 4, 2018, 11:16:13 PM (6 years ago)
Author:
alain
Message:

Complete restructuration of kernel spinlocks.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/kernel/libk/remote_rwlock.h

    r457 r563  
    11/*
    2  * remote_rwlock.h - kernel remote_rwlock definition.
     2 * remote_rwlock.h - kernel remote read/writelock definition.
    33 *
    44 * Authors   Alain Greiner   (2016,2017,2018)
     
    2727#include <kernel_config.h>
    2828#include <hal_kernel_types.h>
     29#include <remote_busylock.h>
    2930#include <xlist.h>
    3031
    31 /***************************************************************************************
    32  * This file defines a remote kernel lock, that supports several simultaneous read
    33  * accesses, but only one write access. It implements a ticket based allocation policy.
    34  * It can be used to synchronize threads running in different clusters, because
    35  * all access functions use remote pointers.
    36  * - A reader take the lock to atomically increments the registered readers count.
    37  *   Then it release the lock and access the protected structure. It atomically
    38  *   decrements the readers count without taking the lock when access is completed.
    39  * - A writer take the lock and keep it, but must wait completion of all current read
    40  *   accesses before starting its own access.
    41  * When the lock is taken by another thread, the new-comers use a busy waiting policy.
    42  **************************************************************************************/
     32/*******************************************************************************************
     33 * This structure defines a kernel, global, read/write lock, supporting several simultaneous
     34 * read accesses, but only one write access to a globally shared object, that can be
     35 * accessed by threads running in any cluster.
     36 * Both readers and writers take the associated busylock before accessing or updating
     37 * the rwlock state, and releases the busylock after rwlock state update.
     38 * - when a reader try to access the object, it increments the readers "count" when the
     39 *   lock is not "taken" by a writer. It registers in the "rd_root" waiting queue, blocks,
     40 *   and deschedules when the lock is taken.
     41 * - when a writer try to take the rwlock, it check the "taken" field. If the lock is already
     42 *   taken, or if the number of readers is non zero, it registers in the "wr_root" waiting
     43 *   queue, blocks, and deschedules. It set "taken" otherwise.
     44 * - when a reader completes its access, it decrement the readers "count", unblock the
     45 *   the first waiting writer if there is no other readers, and unblock all waiting
     46 *   readers if there no write request.
     47 * - when a  writer completes its access, it reset the "taken" field, releases the first
     48 *   waiting writer if queue non empty, or releases all waiting readers if no writer.
     49 ******************************************************************************************/
     50
     51
     52/*******************************************************************************************
     53 * This structure defines a remote rwlock.
     54 ******************************************************************************************/
    4355
    4456typedef struct remote_rwlock_s
    4557{
    46     uint32_t       ticket;          /*! first free ticket index                       */
    47     uint32_t       current;         /*! ticket index of current owner                 */
    48     uint32_t       count;           /*! current number of reader threads              */
     58    remote_busylock_t   lock;        /*! busylock protecting the rwlock state             */
     59        volatile uint32_t   taken;       /*! lock taken by an exclusive writer if non zero    */
     60    volatile uint32_t   count;       /*! current number of simultaneous readers threads   */
     61    xlist_entry_t       rd_xroot;    /*! root of list of waiting readers                  */
     62    xlist_entry_t       wr_xroot;    /*! root of list of waiting writers                  */
     63}
     64remote_rwlock_t;
    4965
    50 #if DEBUG_REMOTE_RWLOCKS
    51     xptr_t         owner;           /*! extended pointer on writer thread             */
    52     xlist_entry_t  list;            /*! member of list of remote locks taken by owner */
    53 #endif
    54 
    55 }
    56 remote_rwlock_t;
    5766
    5867/***************************************************************************************
    5968 * This function initializes a remote rwlock.
     69 * The <type> argument defines the lock usage and is only used for debug.
     70 * This type is actually stored in the associated busylock descriptor.
     71 ***************************************************************************************
     72 * @ lock_xp    : extended pointer on the remote rwlock
     73 * @ type       : lock usage for debug.
     74 **************************************************************************************/
     75void remote_rwlock_init( xptr_t   lock_xp,
     76                         uint32_t type );
     77
     78/***************************************************************************************
     79 * This blocking function get access to a remote rwlock for a reader.
    6080 ***************************************************************************************
    6181 * @ lock_xp    : extended pointer on the remote rwlock
    6282 **************************************************************************************/
    63 void remote_rwlock_init( xptr_t lock_xp );
     83void remote_rwlock_rd_acquire( xptr_t lock_xp );
    6484
    6585/***************************************************************************************
    66  * This blocking function get access to a remote rwlock for a reader.
    67  * It increments the calling thread locks count when the lock has been taken.
     86 * This function releases a remote rwlock for a reader.
    6887 ***************************************************************************************
    6988 * @ lock_xp    : extended pointer on the remote rwlock
    7089 **************************************************************************************/
    71 void remote_rwlock_rd_lock( xptr_t lock_xp );
     90void remote_rwlock_rd_release( xptr_t lock_xp );
    7291
    7392/***************************************************************************************
    74  * This function releases a remote rwlock for a reader.
    75  * It decrements the calling thread locks count when the lock has been released.
     93 * This blocking function get access to a remote rwlock for a writer.
    7694 ***************************************************************************************
    7795 * @ lock_xp    : extended pointer on the remote rwlock
    7896 **************************************************************************************/
    79 void remote_rwlock_rd_unlock( xptr_t lock_xp );
     97void remote_rwlock_wr_acquire( xptr_t lock_xp );
    8098
    8199/***************************************************************************************
    82  * This blocking function get access to a remote rwlock for a writer.
    83  * It increments the calling thread locks count when the lock has been taken.
     100 * This function releases a remote rwlock for a writer.
    84101 ***************************************************************************************
    85102 * @ lock_xp    : extended pointer on the remote rwlock
    86103 **************************************************************************************/
    87 void remote_rwlock_wr_lock( xptr_t lock_xp );
    88 
    89 /***************************************************************************************
    90  * This function releases a remote rwlock for a writer.
    91  * It decrements the calling thread locks count when the lock has been released.
    92  ***************************************************************************************
    93  * @ lock_xp    : extended pointer on the remote rwlock
    94  **************************************************************************************/
    95 void remote_rwlock_wr_unlock( xptr_t lock_xp );
    96 
    97 /***************************************************************************************
    98  * Display the lock state on kernel TTY.
    99  ***************************************************************************************
    100  * @ lock_xp    : extended pointer on the remote rwlock
    101  * @ comment    : comment to be printed.
    102  **************************************************************************************/
    103 void remote_rwlock_print( xptr_t   lock_xp,
    104                           char   * comment );
     104void remote_rwlock_wr_release( xptr_t lock_xp );
    105105
    106106#endif
Note: See TracChangeset for help on using the changeset viewer.