public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* recursive spinlocks. Shoot.
@ 2003-05-18  9:21 Peter T. Breuer
  2003-05-18 16:30 ` Martin J. Bligh
  2003-05-18 18:13 ` Davide Libenzi
  0 siblings, 2 replies; 58+ messages in thread
From: Peter T. Breuer @ 2003-05-18  9:21 UTC (permalink / raw)
  To: linux-kernel


Here's a before-breakfast implementation of a recursive spinlock. That
is, the same thread can "take" the spinlock repeatedly. This is crude -
I just want to focus some attention on the issue (while I go out and
have breakfast :'E).

The idea is to implement trylock correctly, and then get lock and
unlock from that via the standard algebra. lock is a loop doing
a trylock until it succeeds. We emerge from a successful trylock
with the lock notionally held.

The "spinlock" is a register of the current pid, plus a recursion
counter, with atomic access.  The pid is either -1 (unset, count is
zero) or some decent value (count is positive).

The trylock will succeed and set the pid if it is currently unset.  It
will succeed if the pid matches ours, and increment the count of
holders.

Unlock just decrements the count.  When we've unlocked enough times,
somebody else can take the lock.


The objection to this scheme that I have is

  1) its notion of identity is limited to the pid, which is crude
  2) it doesn't detect dead holders of the lock, which would be nice
  3) it should probably be done in assembler, using whatever trick
     rwlocks use
  4) it doesn't actually use a real spinlock to "hold" the lock, which
     makes me nervous.


typedef struct recursive_spinlock {
        spinlock_t lock;
        int pid;
        int count;
} recursive_spinlock_t;

int recursive_spin_trylock(recursive_spinlock_t *lock) {
        
        spin_lock(&lock->lock);
        if (lock->count <= 0) {
                // greenfield site
                lock->pid = current->pid;
                lock->count = 1;
                spin_unlock(&lock->lock);
                return 0;
        }
        // somebody has the lock
        if (lock->pid == current->pid) {
                // it was us! return ok`
                lock->count++;
                spin_unlock(&lock->lock);
                return 0;
        } 
        // somebody has the lock and it's not us! return fail
        spin_unlock(&lock->lock);
        return 1;
}

void recursive_spin_lock(recursive_spinlock_t *lock) {
        while (recursive_spin_trylock(lock) != 0);
}

void recursive_spin_unlock(recursive_spinlock_t *lock) {
        spin_lock(&lock->lock);
        if (--lock->count <= 0)
                lock->count = 0;
        spin_unlock(&lock->lock);
}

void recursive_spinlock_init(recursive_spinlock_t *lock) {
        spinlock_init(&lock->lock);
        lock->pid = -1;
        lock->count = 0;
}


Peter

^ permalink raw reply	[flat|nested] 58+ messages in thread
[parent not found: <20030518182010$0541@gated-at.bofh.it>]
[parent not found: <20030518202013$5297@gated-at.bofh.it>]
[parent not found: <20030520231013$3d77@gated-at.bofh.it>]

end of thread, other threads:[~2003-05-28 16:29 UTC | newest]

Thread overview: 58+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-05-18  9:21 recursive spinlocks. Shoot Peter T. Breuer
2003-05-18 16:30 ` Martin J. Bligh
2003-05-18 16:35   ` William Lee Irwin III
2003-05-18 16:49     ` Arjan van de Ven
2003-05-18 16:54       ` William Lee Irwin III
2003-05-18 17:14         ` Martin J. Bligh
2003-05-18 17:24     ` Peter T. Breuer
2003-05-18 22:34       ` David Woodhouse
2003-05-19 13:37         ` Peter T. Breuer
2003-05-19 13:45           ` Jens Axboe
2003-05-19 13:47           ` Arjan van de Ven
     [not found]           ` <mailman.1053352200.24653.linux-kernel2news@redhat.com>
2003-05-19 23:54             ` Pete Zaitcev
2003-05-20  0:03               ` viro
2003-05-20  0:03               ` Johannes Erdfelt
2003-05-20  3:12         ` Robert White
2003-05-20 11:59           ` Helge Hafting
2003-05-20 12:23             ` Richard B. Johnson
2003-05-20 21:05               ` Robert White
2003-05-20 21:42                 ` Richard B. Johnson
2003-05-20 23:06                   ` Robert White
2003-05-21 14:01                     ` Richard B. Johnson
2003-05-21 21:56                       ` Robert White
2003-05-22  0:13                         ` viro
2003-05-22  0:32                           ` Robert White
2003-05-22  0:46                         ` Carl-Daniel Hailfinger
2003-05-21  5:48                   ` Nikita Danilov
2003-05-22  1:00           ` Rik van Riel
2003-05-22  3:11             ` Robert White
2003-05-22  4:04               ` Nick Piggin
2003-05-22  4:42                 ` Peter T. Breuer
2003-05-22  5:09                   ` Nick Piggin
2003-05-23  0:19                 ` Robert White
2003-05-23  7:22                   ` Nikita Danilov
2003-05-23  9:07                     ` Helge Hafting
2003-05-23 12:18                     ` William Lee Irwin III
2003-05-24  2:39                       ` Robert White
2003-05-28 16:50                         ` Timothy Miller
2003-05-19  2:05       ` Kevin O'Connor
2003-05-19  6:19       ` Jan Hudec
2003-05-19 10:29       ` Helge Hafting
2003-05-19 11:37         ` Nikita Danilov
2003-05-22  1:21           ` Daniel Phillips
2003-05-19 14:28       ` Martin J. Bligh
2003-05-18 18:13 ` Davide Libenzi
     [not found] <20030518182010$0541@gated-at.bofh.it>
2003-05-18 19:09 ` Peter T. Breuer
2003-05-18 19:31   ` Davide Libenzi
2003-05-18 19:49     ` Peter T. Breuer
2003-05-18 20:13       ` Davide Libenzi
2003-05-19 20:47   ` Jan Hudec
     [not found] <20030518202013$5297@gated-at.bofh.it>
2003-05-18 23:15 ` Peter T. Breuer
2003-05-18 23:26   ` Davide Libenzi
2003-05-19 12:48     ` Peter T. Breuer
2003-05-19 17:15       ` Davide Libenzi
2003-05-19 17:27         ` Peter T. Breuer
2003-05-19 17:57           ` Alan Cox
2003-05-19 19:51         ` Peter T. Breuer
2003-05-19 20:22   ` Robert White
     [not found] <20030520231013$3d77@gated-at.bofh.it>
2003-05-21 14:16 ` Peter T. Breuer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox