home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / uccs / root.14 / udk / usr / include / synch.h < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-19  |  7.3 KB  |  229 lines

  1. /*
  2.  * Copyright (c) 1998 The Santa Cruz Operation, Inc.. All Rights Reserved. 
  3.  *                                                                         
  4.  *        THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF THE               
  5.  *                   SANTA CRUZ OPERATION INC.                             
  6.  *                                                                         
  7.  *   The copyright notice above does not evidence any actual or intended   
  8.  *   publication of such source code.                                      
  9.  */
  10.  
  11. #ifndef _SYNCH_H
  12. #define _SYNCH_H
  13. #ident    "@(#)head.usr:synch.h    1.5.1.2"
  14.  
  15. /*
  16.  * This file defines user level synchronization objects,
  17.  * functions, and macros
  18.  */
  19.  
  20. #include <thread.h>
  21. #include <lwpsynch.h>
  22.  
  23. /*
  24.  * The following are used for static allocation
  25.  */
  26. #define DEFAULTMUTEX    {{0},USYNC_THREAD, {0},{0,0},0}
  27. #define SHAREDMUTEX    {{0},USYNC_PROCESS,{0},{0,0},0}
  28. #define DEFAULTCV    {{0},USYNC_THREAD, 0, 0}
  29. #define SHAREDCV    {{0},USYNC_PROCESS,0, 0}
  30. #define DEFAULTSEMA    {0,USYNC_THREAD, 0,{0},{0}}
  31. #define SHAREDSEMA    {0,USYNC_PROCESS,0,{0},{0}}
  32.  
  33. /*
  34.  * Tests on lock states.
  35.  */
  36. #define SEMA_HELD(x)    ((x)->s_count <= 0)
  37. #define MUTEX_HELD(x)    (LOCK_HELD(&(x)->m_lmutex))
  38.  
  39. /*
  40.  * The following values are assigned to the type field of all 
  41.  * synchronization primitives.  
  42.  */
  43. #define    USYNC_THREAD    0    /* known only to threads in a process */
  44. #define    USYNC_PROCESS    1    /* shared by different processes      */
  45. #define USYNC_DESTROYED    2    /* the object has been "destroyed" */
  46.  
  47. typedef struct thrq_elt thrq_elt_t;
  48.  
  49. struct thrq_elt {
  50.     thrq_elt_t *thrq_next;
  51.     thrq_elt_t *thrq_prev;
  52. };
  53.  
  54. /*
  55.  * Mutexes
  56.  */
  57. typedef volatile struct __mutex {
  58.     lwp_mutex_t    m_lmutex;     /* lwp mutex lock */
  59.     long        m_type;       /* type of mutex */
  60.     lwp_mutex_t    m_sync_lock;  /* synchronization lock for sleep */
  61.     thrq_elt_t     m_sleepq;     /* sleep queue */
  62.     long        filler;       /* not currently used */
  63. } mutex_t;
  64.  
  65. /*
  66.  * Condition variables
  67.  */
  68. typedef volatile struct __cond {
  69.     lwp_cond_t    c_lcond;      /* lwp condition variable */
  70.     long        c_type;       /* type of condition variable */
  71.     thrq_elt_t     *c_syncq;     /* sleep queue */
  72.     lwp_mutex_t    c_sync_lock;  /* synchronization lock for sleep */
  73. } cond_t;
  74.  
  75. /*
  76.  * Semaphores
  77.  */
  78. typedef volatile struct __sema {
  79.     mutex_t        s_mutex;
  80.     cond_t        s_cond;
  81.     short        s_count;
  82.     short        s_wakecnt;
  83.     int        s_type;
  84. } sema_t;
  85.  
  86. /*
  87.  * Read-write lock
  88.  */
  89. /*
  90.  * The rwcv_t structure is used when the rwlock_t is of type USYNC_THREAD.
  91.  * The rwcv_t structure is dynamically allocated, one per writer that has to 
  92.  * wait, or one per group of readers that have to wait. This is a FIFO list 
  93.  * pointed to at head by rw_cvqhead and at tail by rw_cvqtail. rwcv_rw 
  94.  * indicates if rwcv_cond is used by a writer or by a group of readers. 
  95.  * rwcv_wakeup indicates a rw_unlock() has been called that unblocks waiters of
  96.  * this condvar. The waiters, when awakened, go back to sleep unless this flag 
  97.  * is set. It may wake up by a signal or for other reasons, which are all 
  98.  * ignored. When a writer is awakened, it deletes its rwcv_t from the list 
  99.  * and deallocates it.  When a reader is awakened, it decrements 
  100.  * rwcv_readerwanted and only the last such reader frees the rwcv_t structure.
  101.  */
  102. typedef volatile struct rwcv rwcv_t;
  103.  
  104. struct rwcv {
  105.     cond_t    rwcv_cond;        /* for waiters to block on */
  106.     rwcv_t    *rwcv_next;        /* to rwcv_t used by next waiter */
  107.     char    rwcv_rw;        /* type of request, reader or writer */
  108. #define    RW_READER    (0)
  109. #define    RW_WRITER    (1)
  110.     char    rwcv_wakeup;        /* set by rw_unlock */
  111.     short    rwcv_readerwanted;    /* number of readers blocked */
  112. };
  113.  
  114. /*
  115.  * read-write locks
  116.  */
  117. typedef volatile struct rwlock rwlock_t;
  118.  
  119. /*
  120.  * rwlock_t is defined for both types, USYNC_THREAD or USYNC_PROCESS.
  121.  * Those members only for USYNC_PROCESS are: rw_lwpcond, rw_wrwakeup,
  122.  * and rw_rdwakecnt; those only for USYNC_THREAD are: rw_cvqhead,
  123.  * and rw_cvqtail.
  124.  */
  125. struct rwlock {
  126.     mutex_t        rw_mutex;    /* controls access to this rwlock */
  127.     lwp_cond_t    rw_lwpcond;    /* for USYNC_PROCESS only */
  128.     int        rw_type;    /* USYNC_THREAD or USYNC_PROCESS */
  129.     short        rw_readers;    /* count of readers holding the lock */
  130.     char        rw_writer;    /* = 1 if held by a writer, or = 0 */
  131.     char        rw_wrwakeup;    /* a wakeup is called for a writer */
  132.     short        rw_writerwanted;/* writers waiting, USYNC_THREAD */
  133.     short        rw_rdwakecnt;    /* readers awakened, USYNC_PROCESS */
  134.     rwcv_t        *rw_cvqhead;    /* FIFO, need only head and tail */
  135.     rwcv_t        *rw_cvqtail;
  136.     long        pad[4];        /* pad out to 64 bytes */
  137. };
  138.  
  139. /*
  140.  * Recursive Mutexes
  141.  */
  142. typedef volatile struct __rmutex {
  143.     mutex_t        rm_mutex;    /* mutex lock                        */
  144.     pid_t        rm_pid;        /* pid of owner -- for USYNC_PROCESS */
  145.     thread_t    rm_owner;    /* thread ID of owner of rmutex      */
  146.     int        rm_depth;    /* number of lock calls made by owner*/
  147.     long        filler;        /* not used for now                  */
  148. } rmutex_t;
  149.  
  150. /*
  151.  * spin locks - the spin_t structure is identical to mutex structure to
  152.  * allow future interoperability.
  153.  */
  154.  
  155. typedef mutex_t spin_t;
  156.  
  157. /*
  158.  * synchronization barriers
  159.  */
  160. typedef volatile struct barrier barrier_t;
  161. typedef volatile struct barrier_spin barrier_spin_t;
  162.  
  163. struct barrier {
  164.     mutex_t        b_lock;        /* controls access to this barrier */
  165.     int        b_type;        /* USYNC_PROCESS or USYNC_THREAD */
  166.     unsigned int    b_count;    /* number of threads to synchronize  */
  167.     unsigned int    b_waiting;    /* number of thread currently waiting */
  168.     unsigned int    b_generation;    /* so we wake up for the right reason */
  169.     cond_t        b_cond;        /* for waiting threads   */
  170. };
  171.  
  172. struct barrier_spin {
  173.     spin_t        bs_lock;    /* controls access to this barrier */
  174.     long        bs_type;    /* USYNC_DESTROYED when destroyed    */
  175.     unsigned int    bs_count;    /* number of threads to synchronize  */
  176.     unsigned int    bs_waiting;    /* number of thread currently waiting*/
  177.     unsigned int    bs_generation;    /* so we wake up for the right reason */
  178. };
  179.  
  180. #ifdef __cplusplus
  181. extern "C" {
  182. #endif
  183.  
  184. extern int    mutex_init(mutex_t *, int, void *);
  185. extern int    mutex_lock(mutex_t *);
  186. extern int    mutex_unlock(mutex_t *);
  187. extern int    mutex_trylock(mutex_t *);
  188. extern int    mutex_destroy(mutex_t *);
  189. extern int    cond_init(cond_t *, int, void *);
  190. extern int    cond_signal(cond_t *);
  191. extern int    cond_broadcast(cond_t *);
  192. extern int    cond_wait(cond_t *,mutex_t *);
  193. extern int    cond_timedwait(cond_t *,mutex_t *,timestruc_t *);
  194. extern int    cond_destroy(cond_t *);
  195. extern int    sema_init(sema_t *, int, int, void *);
  196. extern int    sema_wait(sema_t *);
  197. extern int    sema_trywait(sema_t *);
  198. extern int    sema_post(sema_t *);
  199. extern int    sema_destroy(sema_t *);
  200. extern int    rwlock_init(rwlock_t *, int, void *);
  201. extern int    rw_rdlock(rwlock_t *);
  202. extern int    rw_wrlock(rwlock_t *);
  203. extern int    rw_unlock(rwlock_t *);
  204. extern int    rw_tryrdlock(rwlock_t *);
  205. extern int    rw_trywrlock(rwlock_t *);
  206. extern int    rwlock_destroy(rwlock_t *);
  207. extern int    rmutex_init(rmutex_t *, int, void *);
  208. extern int    rmutex_lock(rmutex_t *);
  209. extern int    rmutex_unlock(rmutex_t *);
  210. extern int    rmutex_trylock(rmutex_t *);
  211. extern int    rmutex_destroy(rmutex_t *);
  212. extern int    _spin_init(spin_t *, void *);
  213. extern void    _spin_unlock(spin_t *);
  214. extern void    _spin_lock(spin_t *);
  215. extern int    _spin_trylock(spin_t *);
  216. extern int    _spin_destroy(spin_t *);
  217. extern int    barrier_init(barrier_t *, int, int, void *);
  218. extern int    barrier_wait(barrier_t *);
  219. extern int    barrier_destroy(barrier_t *);
  220. extern int    _barrier_spin_init(barrier_spin_t *, int, void *);
  221. extern int    _barrier_spin(barrier_spin_t *);
  222. extern int    _barrier_spin_destroy(barrier_spin_t *);
  223.  
  224. #ifdef __cplusplus
  225. }
  226. #endif
  227.  
  228. #endif /*_SYNCH_H*/
  229.