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

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

diff -u --recursive --new-file v2.3.42/linux/include/asm-ppc/semaphore.h linux/include/asm-ppc/semaphore.h
@@ -4,6 +4,9 @@
 /*
  * Swiped from asm-sparc/semaphore.h and modified
  * -- Cort (cort@cs.nmt.edu)
+ *
+ * Stole some rw spinlock-based semaphore stuff from asm-alpha/semaphore.h
+ * -- Ani Joshi (ajoshi@unixbox.com)
  */
 
 #ifdef __KERNEL__
@@ -101,6 +104,99 @@
 	if (atomic_inc_return(&sem->count) <= 0)
 		__up(sem);
 }	
+
+
+/* RW spinlock-based semaphores */
+
+struct rw_semaphore
+{
+	spinlock_t lock;
+	int rd, wr;
+	wait_queue_head_t wait;
+#if WAITQUEUE_DEBUG
+	long __magic;
+#endif
+};
+
+#define __RWSEM_INITIALIZER(name, rd, wr)		\
+{							\
+	SPIN_LOCK_UNLOCKED,				\
+	(rd), (wr),					\
+	__WAIT_QUEUE_HEAD_INITIALIZER((name).wait)	\
+	__SEM_DEBUG_INIT(name)				\
+}
+
+#define __DECLARE_RWSEM_GENERIC(name, rd, wr)		\
+	struct rw_semaphore name = __RWSEM_INITIALIZER(name, rd, wr)
+
+#define DECLARE_RWSEM(name) __DECLARE_RWSEM_GENERIC(name, 0, 0)
+#define DECLARE_RWSEM_READ_LOCKED(name) __DECLARE_RWSEM_GENERIC(name, 1, 0)
+#define DECLAER_RWSEM_WRITE_LOCKED(name) __DECLARE_RWSEM_GENERIC(name, 0, 1)
+
+extern inline void init_rwsem(struct rw_semaphore *sem)
+{
+	spin_lock_init(&sem->lock);
+	sem->rd = sem->wr = 0;
+	init_waitqueue_head(&sem->wait);
+#if WAITQUEUE_DEBUG
+	sem->__magic = (long)&sem->__magic;
+#endif
+}
+
+#ifndef CHECK_MAGIC
+#define CHECK_MAGIC(x)
+#endif
+
+extern void down_read_failed(struct rw_semaphore *);
+extern void down_write_failed(struct rw_semaphore *);
+
+extern inline void down_read(struct rw_semaphore *sem)
+{
+	CHECK_MAGIC(sem->__magic);
+
+	spin_lock_irq(&sem->lock);
+	if (sem->wr)
+		down_read_failed(sem);
+	sem->rd++;
+	spin_unlock_irq(&sem->lock);
+}
+
+extern inline void down_write(struct rw_semaphore *sem)
+{
+	CHECK_MAGIC(sem->__magic);
+
+	spin_lock(&sem->lock);
+	if(sem->rd || sem->wr)
+		down_write_failed(sem);
+	sem->wr = 1;
+	spin_unlock(&sem->lock);
+}
+
+#define up_read(sem)							\
+	do {								\
+		unsigned long flags;					\
+									\
+		CHECK_MAGIC((sem)->__magic);				\
+									\
+		spin_lock_irqsave(&(sem)->lock, flags);			\
+		if (!--(sem)->rd && waitqueue_active(&(sem)->wait))	\
+			wake_up(&(sem)->wait);				\
+		spin_unlock_irqrestore(&(sem)->lock, flags);		\
+	} while (0)
+
+#define up_write(sem)							\
+	do {								\
+		unsigned long flags;					\
+									\
+		CHECK_MAGIC((sem)->__magic);				\
+									\
+		spin_lock_irqsave(&(sem)->lock, flags);			\
+		(sem)->wr = 0;						\
+		if (waitqueue_active(&(sem)->wait))			\
+			wake_up(&(sem)->wait);				\
+		spin_unlock_irqrestore(&(sem)->lock, flags);		\
+	} while (0)
+
 
 #endif /* __KERNEL__ */
 

FUNET's LINUX-ADM group, linux-adm@nic.funet.fi
TCL-scripts by Sam Shen (who was at: slshen@lbl.gov)