]> bbs.cooldavid.org Git - net-next-2.6.git/blobdiff - lib/rwsem.c
qlcnic: fix board description
[net-next-2.6.git] / lib / rwsem.c
index a3e68bf5932eaad7c8ea7d6fbf10c53bb8d35189..f236d7cd5cf3e34bab6955613a773aeda8914039 100644 (file)
@@ -67,6 +67,9 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
                goto readers_only;
 
        if (wake_type == RWSEM_WAKE_READ_OWNED)
+               /* Another active reader was observed, so wakeup is not
+                * likely to succeed. Save the atomic op.
+                */
                goto out;
 
        /* There's a writer at the front of the queue - try to grant it the
@@ -111,8 +114,8 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
         * count adjustment pretty soon.
         */
        if (wake_type == RWSEM_WAKE_ANY &&
-           (rwsem_atomic_update(0, sem) & RWSEM_ACTIVE_MASK))
-               /* Someone grabbed the sem already */
+           rwsem_atomic_update(0, sem) < RWSEM_WAITING_BIAS)
+               /* Someone grabbed the sem for write already */
                goto out;
 
        /* Grant an infinite number of read locks to the readers at the front
@@ -168,8 +171,9 @@ __rwsem_do_wake(struct rw_semaphore *sem, int wake_type)
  */
 static struct rw_semaphore __sched *
 rwsem_down_failed_common(struct rw_semaphore *sem,
-                       struct rwsem_waiter *waiter, signed long adjustment)
+                        unsigned int flags, signed long adjustment)
 {
+       struct rwsem_waiter waiter;
        struct task_struct *tsk = current;
        signed long count;
 
@@ -177,25 +181,34 @@ rwsem_down_failed_common(struct rw_semaphore *sem,
 
        /* set up my own style of waitqueue */
        spin_lock_irq(&sem->wait_lock);
-       waiter->task = tsk;
+       waiter.task = tsk;
+       waiter.flags = flags;
        get_task_struct(tsk);
 
        if (list_empty(&sem->wait_list))
                adjustment += RWSEM_WAITING_BIAS;
-       list_add_tail(&waiter->list, &sem->wait_list);
+       list_add_tail(&waiter.list, &sem->wait_list);
 
        /* we're now waiting on the lock, but no longer actively locking */
        count = rwsem_atomic_update(adjustment, sem);
 
-       /* if there are no active locks, wake the front queued process(es) up */
-       if (!(count & RWSEM_ACTIVE_MASK))
+       /* If there are no active locks, wake the front queued process(es) up.
+        *
+        * Alternatively, if we're called from a failed down_write(), there
+        * were already threads queued before us and there are no active
+        * writers, the lock must be read owned; so we try to wake any read
+        * locks that were queued ahead of us. */
+       if (count == RWSEM_WAITING_BIAS)
                sem = __rwsem_do_wake(sem, RWSEM_WAKE_NO_ACTIVE);
+       else if (count > RWSEM_WAITING_BIAS &&
+                adjustment == -RWSEM_ACTIVE_WRITE_BIAS)
+               sem = __rwsem_do_wake(sem, RWSEM_WAKE_READ_OWNED);
 
        spin_unlock_irq(&sem->wait_lock);
 
        /* wait to be given the lock */
        for (;;) {
-               if (!waiter->task)
+               if (!waiter.task)
                        break;
                schedule();
                set_task_state(tsk, TASK_UNINTERRUPTIBLE);
@@ -212,11 +225,8 @@ rwsem_down_failed_common(struct rw_semaphore *sem,
 asmregparm struct rw_semaphore __sched *
 rwsem_down_read_failed(struct rw_semaphore *sem)
 {
-       struct rwsem_waiter waiter;
-
-       waiter.flags = RWSEM_WAITING_FOR_READ;
-       rwsem_down_failed_common(sem, &waiter, -RWSEM_ACTIVE_READ_BIAS);
-       return sem;
+       return rwsem_down_failed_common(sem, RWSEM_WAITING_FOR_READ,
+                                       -RWSEM_ACTIVE_READ_BIAS);
 }
 
 /*
@@ -225,12 +235,8 @@ rwsem_down_read_failed(struct rw_semaphore *sem)
 asmregparm struct rw_semaphore __sched *
 rwsem_down_write_failed(struct rw_semaphore *sem)
 {
-       struct rwsem_waiter waiter;
-
-       waiter.flags = RWSEM_WAITING_FOR_WRITE;
-       rwsem_down_failed_common(sem, &waiter, -RWSEM_ACTIVE_WRITE_BIAS);
-
-       return sem;
+       return rwsem_down_failed_common(sem, RWSEM_WAITING_FOR_WRITE,
+                                       -RWSEM_ACTIVE_WRITE_BIAS);
 }
 
 /*