synch.h
上传用户:ladybrid91
上传日期:2007-01-04
资源大小:287k
文件大小:3k
源码类别:

Web服务器

开发平台:

Unix_Linux

  1. /*
  2.  * SYNCH.H - Solaris to POSIX Interface Layer for Threads.
  3.  *
  4.  * This code is public domain.  It may be freely distributed provided the
  5.  * Copyright notice is retained. This code is not supported in any way.
  6.  * Usage of the code is solely at the user's/programmer's discretion.
  7.  * Sun Microsystems makes no claim as to the reliabilty, correctness or
  8.  * suitability of this code. Sun Microsystems will not be held liable in
  9.  * any way as a result loss or damage arising from the use this code.
  10.  *
  11.  * Written by: Richard.Marejka@Canada.Sun.COM
  12.  * Copyright (c) 1994, by Sun Microsystems, Inc.
  13.  */
  14. #if !defined(SYNCH_INCLUDED)
  15. #define SYNCH_INCLUDED
  16. #pragma ident "@(#) synch.h.txt 1.1 94/10/06 Richard.Marejka@Canada.Sun.COM"
  17. /* Feature Test Macros */
  18. #define _POSIX_THREADS
  19. #if !defined(SPILT_MACROS)
  20. # define SPILT_MACROS 1 /* use macros where possible */
  21. #endif
  22. /* Include Files */
  23. #include <pthread.h>
  24. #include <sys/types.h>
  25. /* Constants & Macros */
  26. #if defined(SPILT_MACROS)
  27. # define mutex_t pthread_mutex_t
  28. # define cond_t pthread_cond_t
  29. # define mutex_destroy pthread_mutex_destroy
  30. # define mutex_lock pthread_mutex_lock
  31. # define mutex_trylock pthread_mutex_trylock
  32. # define mutex_unlock pthread_mutex_unlock
  33. # define cond_destroy pthread_cond_destroy
  34. # define cond_wait pthread_cond_wait
  35. # define cond_signal pthread_cond_signal
  36. # define cond_broadcast pthread_cond_broadcast
  37. #endif  /* SPILT_MACROS */
  38. #define USYNC_THREAD 0x0001
  39. #define USYNC_PROCESS 0x0002
  40. /* Data Declarations */
  41. typedef struct _timestruc {
  42. time_t tv_sec;
  43. long tv_nsec;
  44. } timestruc_t;
  45. #if !defined(SPILT_MACROS)
  46. typedef pthread_mutex_t mutex_t;
  47. typedef pthread_cond_t cond_t;
  48. #endif /* SPILT_MACROS */
  49. typedef struct _rwlock {
  50. pthread_mutex_t lock; /* lock for structure */
  51. pthread_cond_t readers; /* waiting readers */
  52. pthread_cond_t writers; /* waiting writers */
  53. int state; /* -1:writer,0:free,>0:readers */
  54. int waiters; /* number of waiting writers */
  55. } rwlock_t;
  56. typedef struct _sema {
  57. pthread_mutex_t lock; /* lock for structure */
  58. pthread_cond_t waiters; /* waiters */
  59. unsigned count; /* current value */
  60. } sema_t;
  61. /* External References */
  62. #if defined(__cplusplus)
  63. extern  "C" {
  64. #endif
  65. extern int mutex_init( mutex_t *, int, void * );
  66. #if !defined(SPILT_MACROS)
  67. extern int mutex_destroy( mutex_t * );
  68. extern int mutex_lock( mutex_t * );
  69. extern int mutex_trylock( mutex_t * );
  70. extern int mutex_unlock( mutex_t * );
  71. #endif /* SPILT_MACROS */
  72. extern int cond_init( cond_t *, int type, void * );
  73. #if !defined(SPILT_MACROS)
  74. extern int cond_destroy( cond_t * );
  75. extern int cond_wait( cond_t *, mutex_t * );
  76. #endif /* SPILT_MACROS */
  77. extern int cond_timedwait( cond_t *, mutex_t *, timestruc_t * );
  78. #if !defined(SPILT_MACROS)
  79. extern int cond_signal( cond_t * );
  80. extern int cond_broadcast( cond_t * );
  81. #endif /* SPILT_MACROS */
  82. extern int sema_init( sema_t *, unsigned int, int , void * );
  83. extern int sema_destroy( sema_t * );
  84. extern int sema_wait( sema_t * );
  85. extern int sema_trywait( sema_t * );
  86. extern int sema_post( sema_t * );
  87. extern int rwlock_init( rwlock_t *, int, void * );
  88. extern int rwlock_destroy( rwlock_t * );
  89. extern int rw_rdlock( rwlock_t * );
  90. extern int rw_tryrdlock( rwlock_t * );
  91. extern int rw_wrlock( rwlock_t * );
  92. extern int rw_trywrlock( rwlock_t * );
  93. extern int rw_unlock( rwlock_t * );
  94. #if defined(__cplusplus)
  95. }
  96. #endif
  97. #endif /* SYNCH_INCLUDED */