patch-2.1.48 linux/include/asm-ppc/semaphore.h

Next file: linux/include/asm-ppc/smp.h
Previous file: linux/include/asm-ppc/ptrace.h
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.1.47/linux/include/asm-ppc/semaphore.h linux/include/asm-ppc/semaphore.h
@@ -1,11 +1,18 @@
 #ifndef _PPC_SEMAPHORE_H
 #define _PPC_SEMAPHORE_H
 
+/*
+ * SMP- and interrupt-safe semaphores..
+ *
+ * (C) Copyright 1996 Linus Torvalds
+ * Adapted for PowerPC by Gary Thomas and Paul Mackerras
+ */
+
 #include <asm/atomic.h>
 
 struct semaphore {
 	atomic_t count;
-	atomic_t waiting;
+	atomic_t waking;
 	struct wait_queue * wait;
 };
 
@@ -13,44 +20,61 @@
 #define MUTEX_LOCKED ((struct semaphore) { ATOMIC_INIT(0), ATOMIC_INIT(0), NULL })
 
 extern void __down(struct semaphore * sem);
+extern int  __down_interruptible(struct semaphore * sem);
 extern void __up(struct semaphore * sem);
 
-extern void atomic_add(int c, int *v);
-extern void atomic_sub(int c, int *v);
+#define sema_init(sem, val)	atomic_set(&((sem)->count), (val))
+
+/*
+ * These two _must_ execute atomically wrt each other.
+ *
+ * This is trivially done with load_locked/store_cond,
+ * i.e. load with reservation and store conditional on the ppc.
+ */
 
-#define sema_init(sem, val)	atomic_set(&((sem)->count), val)
+static inline void wake_one_more(struct semaphore * sem)
+{
+	atomic_inc(&sem->waking);
+}
 
 static inline int waking_non_zero(struct semaphore *sem)
 {
-	unsigned long flags;
-	int ret = 0;
+	int ret, tmp;
+
+	__asm__ __volatile__(
+		"1:	lwarx %1,0,%2\n"
+		"	cmpwi 0,%1,0\n"
+		"	addi %1,%1,-1\n"
+		"	ble- 2f\n"
+		"	stwcx. %1,0,%2\n"
+		"	bne- 1b\n"
+		"	mr %0,%1\n"
+		"2:"
+		: "=r" (ret), "=r" (tmp)
+		: "r" (&sem->waking), "0" (0)
+		: "cr0", "memory");
 
-	save_flags(flags);
-	cli();
-	if (atomic_read(&sem->waking) > 0) {
-		atomic_dec(&sem->waking);
-		ret = 1;
-	}
-	restore_flags(flags);
 	return ret;
 }
 
 extern inline void down(struct semaphore * sem)
 {
-  for (;;)
-  {
-    atomic_dec_return(&sem->count);
-    if ( sem->count >= 0)
-      break;
-    __down(sem);
-  }
+	if (atomic_dec_return(&sem->count) < 0)
+		__down(sem);
+}
+
+extern inline int down_interruptible(struct semaphore * sem)
+{
+	int ret = 0;
+	if (atomic_dec_return(&sem->count) < 0)
+		ret = __down_interruptible(sem);
+	return ret;
 }
 
 extern inline void up(struct semaphore * sem)
 {
-  atomic_inc_return(&sem->count);
-  if ( sem->count <= 0)
-    __up(sem);
+	if (atomic_inc_return(&sem->count) <= 0)
+		__up(sem);
 }	
 
 #endif /* !(_PPC_SEMAPHORE_H) */

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen, slshen@lbl.gov