Changeset f43d8ce in mainline for kernel/generic/src/synch/spinlock.c


Ignore:
Timestamp:
2023-02-02T21:58:36Z (15 months ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
2b264c4
Parents:
95658c9
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-01-19 22:40:04)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2023-02-02 21:58:36)
Message:

Make spinlock_lock/unlock into proper functions in all configurations

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/synch/spinlock.c

    r95658c9 rf43d8ce  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
     3 * Copyright (c) 2023 Jiří Zárevúcky
    34 * All rights reserved.
    45 *
     
    4748#include <cpu.h>
    4849
    49 #ifdef CONFIG_SMP
    50 
    5150/** Initialize spinlock
    5251 *
     
    5655void spinlock_initialize(spinlock_t *lock, const char *name)
    5756{
     57#ifdef CONFIG_SMP
    5858        atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
    5959#ifdef CONFIG_DEBUG_SPINLOCK
    6060        lock->name = name;
    6161#endif
     62#endif
    6263}
    6364
    64 #ifdef CONFIG_DEBUG_SPINLOCK
    65 
    6665/** Lock spinlock
    67  *
    68  * Lock spinlock.
    69  * This version has limitted ability to report
    70  * possible occurence of deadlock.
    7166 *
    7267 * @param lock Pointer to spinlock_t structure.
    7368 *
    7469 */
    75 void spinlock_lock_debug(spinlock_t *lock)
     70void spinlock_lock(spinlock_t *lock)
    7671{
     72        preemption_disable();
     73
     74#ifdef CONFIG_SMP
     75        bool deadlock_reported = false;
    7776        size_t i = 0;
    78         bool deadlock_reported = false;
    7977
    80         preemption_disable();
    8178        while (atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire)) {
     79#ifdef CONFIG_DEBUG_SPINLOCK
    8280                /*
    8381                 * We need to be careful about particular locks
     
    111109                        deadlock_reported = true;
    112110                }
     111#endif
    113112        }
     113
     114        /* Avoid compiler warning with debug disabled. */
     115        (void) i;
    114116
    115117        if (deadlock_reported)
    116118                printf("cpu%u: not deadlocked\n", CPU->id);
     119
     120#endif
    117121}
    118122
    119123/** Unlock spinlock
    120124 *
    121  * Unlock spinlock.
    122  *
    123125 * @param sl Pointer to spinlock_t structure.
    124126 */
    125 void spinlock_unlock_debug(spinlock_t *lock)
     127void spinlock_unlock(spinlock_t *lock)
    126128{
     129#ifdef CONFIG_SMP
     130#ifdef CONFIG_DEBUG_SPINLOCK
    127131        ASSERT_SPINLOCK(spinlock_locked(lock), lock);
     132#endif
    128133
    129134        atomic_flag_clear_explicit(&lock->flag, memory_order_release);
     135#endif
     136
    130137        preemption_enable();
    131138}
    132139
    133 #endif
    134 
    135 /** Lock spinlock conditionally
    136  *
     140/**
    137141 * Lock spinlock conditionally. If the spinlock is not available
    138142 * at the moment, signal failure.
     
    140144 * @param lock Pointer to spinlock_t structure.
    141145 *
    142  * @return Zero on failure, non-zero otherwise.
     146 * @return true on success.
    143147 *
    144148 */
     
    146150{
    147151        preemption_disable();
     152
     153#ifdef CONFIG_SMP
    148154        bool ret = !atomic_flag_test_and_set_explicit(&lock->flag, memory_order_acquire);
    149155
     
    152158
    153159        return ret;
     160#else
     161        return true;
     162#endif
    154163}
    155164
     
    161170bool spinlock_locked(spinlock_t *lock)
    162171{
     172#ifdef CONFIG_SMP
    163173        // NOTE: Atomic flag doesn't support simple atomic read (by design),
    164174        //       so instead we test_and_set and then clear if necessary.
     
    170180                atomic_flag_clear_explicit(&lock->flag, memory_order_relaxed);
    171181        return ret;
     182#else
     183        return true;
     184#endif
    172185}
    173 
    174 #endif
    175186
    176187/** @}
Note: See TracChangeset for help on using the changeset viewer.