     1	#ifndef _I386_SEMAPHORE_H
     2	#define _I386_SEMAPHORE_H
     3	
     4	#include <linux/linkage.h>
     5	
     6	#ifdef __KERNEL__
     7	
     8	/*
     9	 * SMP- and interrupt-safe semaphores..
    10	 *
    11	 * (C) Copyright 1996 Linus Torvalds
    12	 *
    13	 * Modified 1996-12-23 by Dave Grothe <dave@gcom.com> to fix bugs in
    14	 *                     the original code and to make semaphore waits
    15	 *                     interruptible so that processes waiting on
    16	 *                     semaphores can be killed.
    17	 * Modified 1999-02-14 by Andrea Arcangeli, split the sched.c helper
    18	 *		       functions in asm/sempahore-helper.h while fixing a
    19	 *		       potential and subtle race discovered by Ulrich Schmid
    20	 *		       in down_interruptible(). Since I started to play here I
    21	 *		       also implemented the `trylock' semaphore operation.
    22	 *          1999-07-02 Artur Skawina <skawina@geocities.com>
    23	 *                     Optimized "0(ecx)" -> "(ecx)" (the assembler does not
    24	 *                     do this). Changed calling sequences from push/jmp to
    25	 *                     traditional call/ret.
    26	 * Modified 2001-01-01 Andreas Franck <afranck@gmx.de>
    27	 *		       Some hacks to ensure compatibility with recent
    28	 *		       GCC snapshots, to avoid stack corruption when compiling
    29	 *		       with -fomit-frame-pointer. It's not sure if this will
    30	 *		       be fixed in GCC, as our previous implementation was a
    31	 *		       bit dubious.
    32	 *
    33	 * If you would like to see an analysis of this implementation, please
    34	 * ftp to gcom.com and download the file
    35	 * /pub/linux/src/semaphore/semaphore-2.0.24.tar.gz.
    36	 *
    37	 */
    38	
    39	#include <asm/system.h>
    40	#include <asm/atomic.h>
    41	#include <linux/wait.h>
    42	#include <linux/rwsem.h>
    43	#include <linux/stringify.h>
    44	
    45	struct semaphore {
    46		atomic_t count;
    47		int sleepers;
    48		wait_queue_head_t wait;
    49	#if WAITQUEUE_DEBUG
    50		long __magic;
    51	#endif
    52	};
    53	
    54	#if WAITQUEUE_DEBUG
    55	# define __SEM_DEBUG_INIT(name) \
    56			, (int)&(name).__magic
    57	#else
    58	# define __SEM_DEBUG_INIT(name)
    59	#endif
    60	
    61	#define __SEMAPHORE_INITIALIZER(name,count) \
    62	{ ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
    63		__SEM_DEBUG_INIT(name) }
    64	
    65	#define __MUTEX_INITIALIZER(name) \
    66		__SEMAPHORE_INITIALIZER(name,1)
    67	
    68	#define __DECLARE_SEMAPHORE_GENERIC(name,count) \
    69		struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
    70	
    71	#define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
    72	#define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
    73	
    74	static inline void sema_init (struct semaphore *sem, int val)
    75	{
    76	/*
    77	 *	*sem = (struct semaphore)__SEMAPHORE_INITIALIZER((*sem),val);
    78	 *
    79	 * i'd rather use the more flexible initialization above, but sadly
    80	 * GCC 2.7.2.3 emits a bogus warning. EGCS doesnt. Oh well.
    81	 */
    82		atomic_set(&sem->count, val);
    83		sem->sleepers = 0;
    84		init_waitqueue_head(&sem->wait);
    85	#if WAITQUEUE_DEBUG
    86		sem->__magic = (int)&sem->__magic;
    87	#endif
    88	}
    89	
    90	static inline void init_MUTEX (struct semaphore *sem)
    91	{
    92		sema_init(sem, 1);
    93	}
    94	
    95	static inline void init_MUTEX_LOCKED (struct semaphore *sem)
    96	{
    97		sema_init(sem, 0);
    98	}
    99	
   100	asmlinkage void __down_failed(void /* special register calling convention */);
   101	asmlinkage int  __down_failed_interruptible(void  /* params in registers */);
   102	asmlinkage int  __down_failed_trylock(void  /* params in registers */);
   103	asmlinkage void __up_wakeup(void /* special register calling convention */);
   104	
   105	asmlinkage void __down(struct semaphore * sem);
   106	asmlinkage int  __down_interruptible(struct semaphore * sem);
   107	asmlinkage int  __down_trylock(struct semaphore * sem);
   108	asmlinkage void __up(struct semaphore * sem);
   109	
   110	/*
   111	 * This is ugly, but we want the default case to fall through.
   112	 * "__down_failed" is a special asm handler that calls the C
   113	 * routine that actually waits. See arch/i386/kernel/semaphore.c
   114	 */
   115	static inline void down(struct semaphore * sem)
   116	{
   117	#if WAITQUEUE_DEBUG
   118		CHECK_MAGIC(sem->__magic);
   119	#endif
   120	
   121		__asm__ __volatile__(
   122			"# atomic down operation\n\t"
   123			LOCK "decl %0\n\t"     /* --sem->count */
   124			"js 2f\n"
   125			"1:\n"
   126			".subsection 1\n"
   127			".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "\n"
   128			"_text_lock_" __stringify(KBUILD_BASENAME) ":\n"
   129			".endif\n"
   130			"2:\tcall __down_failed\n\t"
   131			"jmp 1b\n"
   132			".subsection 0\n"
   133			:"=m" (sem->count)
   134			:"c" (sem)
   135			:"memory");
   136	}
   137	
   138	/*
   139	 * Interruptible try to acquire a semaphore.  If we obtained
   140	 * it, return zero.  If we were interrupted, returns -EINTR
   141	 */
   142	static inline int down_interruptible(struct semaphore * sem)
   143	{
   144		int result;
   145	
   146	#if WAITQUEUE_DEBUG
   147		CHECK_MAGIC(sem->__magic);
   148	#endif
   149	
   150		__asm__ __volatile__(
   151			"# atomic interruptible down operation\n\t"
   152			LOCK "decl %1\n\t"     /* --sem->count */
   153			"js 2f\n\t"
   154			"xorl %0,%0\n"
   155			"1:\n"
   156			".subsection 1\n"
   157			".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "\n"
   158			"_text_lock_" __stringify(KBUILD_BASENAME) ":\n"
   159			".endif\n"
   160			"2:\tcall __down_failed_interruptible\n\t"
   161			"jmp 1b\n"
   162			".subsection 0\n"
   163			:"=a" (result), "=m" (sem->count)
   164			:"c" (sem)
   165			:"memory");
   166		return result;
   167	}
   168	
   169	/*
   170	 * Non-blockingly attempt to down() a semaphore.
   171	 * Returns zero if we acquired it
   172	 */
   173	static inline int down_trylock(struct semaphore * sem)
   174	{
   175		int result;
   176	
   177	#if WAITQUEUE_DEBUG
   178		CHECK_MAGIC(sem->__magic);
   179	#endif
   180	
   181		__asm__ __volatile__(
   182			"# atomic interruptible down operation\n\t"
   183			LOCK "decl %1\n\t"     /* --sem->count */
   184			"js 2f\n\t"
   185			"xorl %0,%0\n"
   186			"1:\n"
   187			".subsection 1\n"
   188			".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "\n"
   189			"_text_lock_" __stringify(KBUILD_BASENAME) ":\n"
   190			".endif\n"
   191			"2:\tcall __down_failed_trylock\n\t"
   192			"jmp 1b\n"
   193			".subsection 0\n"
   194			:"=a" (result), "=m" (sem->count)
   195			:"c" (sem)
   196			:"memory");
   197		return result;
   198	}
   199	
   200	/*
   201	 * Note! This is subtle. We jump to wake people up only if
   202	 * the semaphore was negative (== somebody was waiting on it).
   203	 * The default case (no contention) will result in NO
   204	 * jumps for both down() and up().
   205	 */
   206	static inline void up(struct semaphore * sem)
   207	{
   208	#if WAITQUEUE_DEBUG
   209		CHECK_MAGIC(sem->__magic);
   210	#endif
   211		__asm__ __volatile__(
   212			"# atomic up operation\n\t"
   213			LOCK "incl %0\n\t"     /* ++sem->count */
   214			"jle 2f\n"
   215			"1:\n"
   216			".subsection 1\n"
   217			".ifndef _text_lock_" __stringify(KBUILD_BASENAME) "\n"
   218			"_text_lock_" __stringify(KBUILD_BASENAME) ":\n"
   219			".endif\n"
   220			"2:\tcall __up_wakeup\n\t"
   221			"jmp 1b\n"
   222			".subsection 0\n"
   223			:"=m" (sem->count)
   224			:"c" (sem)
   225			:"memory");
   226	}
   227	
   228	#endif
   229	#endif
