patch-2.2.4 linux/include/asm-sparc/semaphore.h

Next file: linux/include/asm-sparc/smp.h
Previous file: linux/include/asm-sparc/semaphore-helper.h
Back to the patch index
Back to the overall index

diff -u --recursive --new-file v2.2.3/linux/include/asm-sparc/semaphore.h linux/include/asm-sparc/semaphore.h
@@ -9,60 +9,148 @@
 
 struct semaphore {
 	atomic_t count;
-	int waking;
+	atomic_t waking;
 	struct wait_queue * wait;
 };
 
-#define MUTEX ((struct semaphore) { ATOMIC_INIT(1), 0, NULL })
-#define MUTEX_LOCKED ((struct semaphore) { ATOMIC_INIT(0), 0, NULL })
+#define MUTEX ((struct semaphore) { ATOMIC_INIT(1), ATOMIC_INIT(0), NULL })
+#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 int __down_trylock(struct semaphore * sem);
 extern void __up(struct semaphore * sem);
 
 #define sema_init(sem, val)	atomic_set(&((sem)->count), val)
 
-#define wake_one_more(sem)	\
-do {				\
-	unsigned long flags;	\
-	save_and_cli(flags);	\
-	sem->waking++;		\
-	restore_flags(flags);	\
-} while(0)
-
-#define waking_non_zero(sem,tsk)\
-({	unsigned long flags;	\
-	int ret = 0;		\
-	save_and_cli(flags);	\
-	if (sem->waking > 0) {	\
-		sem->waking--;	\
-		ret = 1;	\
-	}			\
-	restore_flags(flags);	\
-	ret;			\
-})
-
-/* This isn't quite as clever as the x86 side, I'll be fixing this
- * soon enough.
- */
 extern inline void down(struct semaphore * sem)
 {
-	if (atomic_dec_return(&sem->count) < 0)
-		__down(sem);
+	register atomic_t *ptr asm("g1");
+	register int increment asm("g2");
+
+	ptr = (atomic_t *) __atomic_fool_gcc(sem);
+	increment = 1;
+
+	__asm__ __volatile__("
+	mov	%%o7, %%g4
+	call	___atomic_sub
+	 add	%%o7, 8, %%o7
+	tst	%%g2
+	bl	2f
+	 nop
+1:
+	.subsection 2
+2:	save	%%sp, -64, %%sp
+	mov	%%g1, %%l1
+	mov	%%g5, %%l5
+	call	%3
+	 mov	%%g1, %%o0
+	mov	%%l1, %%g1
+	ba	1b
+	 restore %%l5, %%g0, %%g5
+	.previous\n"
+	: "=&r" (increment)
+	: "0" (increment), "r" (ptr), "i" (__down)
+	: "g3", "g4", "g7", "memory", "cc");
 }
 
 extern inline int down_interruptible(struct semaphore * sem)
 {
-	int ret = 0;
-	if(atomic_dec_return(&sem->count) < 0)
-		ret = __down_interruptible(sem);
-	return ret;
+	register atomic_t *ptr asm("g1");
+	register int increment asm("g2");
+
+	ptr = (atomic_t *) __atomic_fool_gcc(sem);
+	increment = 1;
+
+	__asm__ __volatile__("
+	mov	%%o7, %%g4
+	call	___atomic_sub
+	 add	%%o7, 8, %%o7
+	tst	%%g2
+	bl	2f
+	 clr	%%g2
+1:
+	.subsection 2
+2:	save	%%sp, -64, %%sp
+	mov	%%g1, %%l1
+	mov	%%g5, %%l5
+	call	%3
+	 mov	%%g1, %%o0
+	mov	%%l1, %%g1
+	mov	%%l5, %%g5
+	ba	1b
+	 restore %%o0, %%g0, %%g2
+	.previous\n"
+	: "=&r" (increment)
+	: "0" (increment), "r" (ptr), "i" (__down_interruptible)
+	: "g3", "g4", "g7", "memory", "cc");
+
+	return increment;
+}
+
+extern inline int down_trylock(struct semaphore * sem)
+{
+	register atomic_t *ptr asm("g1");
+	register int increment asm("g2");
+
+	ptr = (atomic_t *) __atomic_fool_gcc(sem);
+	increment = 1;
+
+	__asm__ __volatile__("
+	mov	%%o7, %%g4
+	call	___atomic_sub
+	 add	%%o7, 8, %%o7
+	tst	%%g2
+	bl	2f
+	 clr	%%g2
+1:
+	.subsection 2
+2:	save	%%sp, -64, %%sp
+	mov	%%g1, %%l1
+	mov	%%g5, %%l5
+	call	%3
+	 mov	%%g1, %%o0
+	mov	%%l1, %%g1
+	mov	%%l5, %%g5
+	ba	1b
+	 restore %%o0, %%g0, %%g2
+	.previous\n"
+	: "=&r" (increment)
+	: "0" (increment), "r" (ptr), "i" (__down_trylock)
+	: "g3", "g4", "g7", "memory", "cc");
+
+	return increment;
 }
 
 extern inline void up(struct semaphore * sem)
 {
-	if (atomic_inc_return(&sem->count) <= 0)
-		__up(sem);
+	register atomic_t *ptr asm("g1");
+	register int increment asm("g2");
+
+	ptr = (atomic_t *) __atomic_fool_gcc(sem);
+	increment = 1;
+
+	__asm__ __volatile__("
+	mov	%%o7, %%g4
+	call	___atomic_add
+	 add	%%o7, 8, %%o7
+	tst	%%g2
+	ble	2f
+	 nop
+1:
+	.subsection 2
+2:	save	%%sp, -64, %%sp
+	mov	%%g1, %%l1
+	mov	%%g5, %%l5
+	call	%3
+	 mov	%%g1, %%o0
+	mov	%%l1, %%g1
+	ba	1b
+	 restore %%l5, %%g0, %%g5
+	.previous\n"
+	: "=&r" (increment)
+	: "0" (increment), "r" (ptr), "i" (__up)
+	: "g3", "g4", "g7", "memory", "cc");
 }	
 
 #endif /* __KERNEL__ */

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