home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / lxapi32.zip / Include / asm / semaphor.h < prev    next >
C/C++ Source or Header  |  2002-04-26  |  4KB  |  110 lines

  1. /* $Id: semaphor.h,v 1.2 2002/04/26 23:09:21 smilcke Exp $ */
  2.  
  3. #ifndef _I386_SEMAPHORE_H
  4. #define _I386_SEMAPHORE_H
  5.  
  6. /*
  7.  * SMP- and interrupt-safe semaphores..
  8.  *
  9.  * (C) Copyright 1996 Linus Torvalds
  10.  *
  11.  * Modified 1996-12-23 by Dave Grothe <dave@gcom.com> to fix bugs in
  12.  *                     the original code and to make semaphore waits
  13.  *                     interruptible so that processes waiting on
  14.  *                     semaphores can be killed.
  15.  * Modified 1999-02-14 by Andrea Arcangeli, split the sched.c helper
  16.  *               functions in asm/sempahore-helper.h while fixing a
  17.  *               potential and subtle race discovered by Ulrich Schmid
  18.  *               in down_interruptible(). Since I started to play here I
  19.  *               also implemented the `trylock' semaphore operation.
  20.  *          1999-07-02 Artur Skawina <skawina@geocities.com>
  21.  *                     Optimized "0(ecx)" -> "(ecx)" (the assembler does not
  22.  *                     do this). Changed calling sequences from push/jmp to
  23.  *                     traditional call/ret.
  24.  *
  25.  * If you would like to see an analysis of this implementation, please
  26.  * ftp to gcom.com and download the file
  27.  * /pub/linux/src/semaphore/semaphore-2.0.24.tar.gz.
  28.  *
  29.  */
  30.  
  31. #include <asm/atomic.h>
  32. #include <linux/spinlock.h>
  33.  
  34. struct semaphore {
  35.     atomic_t count;
  36.     int sleepers;
  37.     void * wait;
  38. #if WAITQUEUE_DEBUG
  39.     long __magic;
  40. #endif
  41. };
  42.  
  43. #if WAITQUEUE_DEBUG
  44. # define __SEM_DEBUG_INIT(name) \
  45.         , (int)&(name).__magic
  46. #else
  47. # define __SEM_DEBUG_INIT(name)
  48. #endif
  49.  
  50. #define __SEMAPHORE_INITIALIZER(name,count) \
  51. { ATOMIC_INIT(count), 0, __WAIT_QUEUE_HEAD_INITIALIZER((name).wait) \
  52.     __SEM_DEBUG_INIT(name) }
  53.  
  54. #define __MUTEX_INITIALIZER(name) \
  55.     __SEMAPHORE_INITIALIZER(name,1)
  56.  
  57. #define __DECLARE_SEMAPHORE_GENERIC(name,count) \
  58.     struct semaphore name = __SEMAPHORE_INITIALIZER(name,count)
  59.  
  60. #define DECLARE_MUTEX(name) __DECLARE_SEMAPHORE_GENERIC(name,1)
  61. #define DECLARE_MUTEX_LOCKED(name) __DECLARE_SEMAPHORE_GENERIC(name,0)
  62.  
  63. extern void sema_init (struct semaphore *sem, int val);
  64. #if (defined(TARGET_OS2) && !defined(NOOS2LXAPI))
  65. extern void (*init_MUTEX)(struct semaphore *sem);
  66. extern void (*init_MUTEX_LOCKED)(struct semaphore *sem);
  67. #else
  68. void init_MUTEX (struct semaphore *sem);
  69. void init_MUTEX_LOCKED (struct semaphore *sem);
  70. #endif
  71.  
  72.  void __down_failed(void /* special register calling convention */);
  73.  int  __down_failed_interruptible(void  /* params in registers */);
  74.  int  __down_failed_trylock(void  /* params in registers */);
  75.  void __up_wakeup(void /* special register calling convention */);
  76.  
  77.  void __down(struct semaphore * sem);
  78.  int  __down_interruptible(struct semaphore * sem);
  79.  int  __down_trylock(struct semaphore * sem);
  80.  void __up(struct semaphore * sem);
  81.  
  82. /*
  83.  * This is ugly, but we want the default case to fall through.
  84.  * "down_failed" is a special asm handler that calls the C
  85.  * routine that actually waits. See arch/i386/lib/semaphore.S
  86.  */
  87. #if (defined(TARGET_OS2) && !defined(NOOS2LXAPI))
  88. extern void (*down)(struct semaphore *sem);
  89. #else
  90. extern void down(struct semaphore * sem);
  91. #endif
  92.  
  93. extern int down_interruptible(struct semaphore * sem);
  94.  
  95. extern int down_trylock(struct semaphore * sem);
  96.  
  97. /*
  98.  * Note! This is subtle. We jump to wake people up only if
  99.  * the semaphore was negative (== somebody was waiting on it).
  100.  * The default case (no contention) will result in NO
  101.  * jumps for both down() and up().
  102.  */
  103. #if (defined(TARGET_OS2) && !defined(NOOS2LXAPI))
  104. extern void (*up)(struct semaphore *sem);
  105. #else
  106. extern void up(struct semaphore * sem);
  107. #endif
  108.  
  109. #endif
  110.