You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

242 lines
7.0 KiB

20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
20 years ago
  1. /******************************************************
  2. The interface to the operating system
  3. synchronization primitives.
  4. (c) 1995 Innobase Oy
  5. Created 9/6/1995 Heikki Tuuri
  6. *******************************************************/
  7. #ifndef os0sync_h
  8. #define os0sync_h
  9. #include "univ.i"
  10. #include "ut0lst.h"
  11. #ifdef __WIN__
  12. #define os_fast_mutex_t CRITICAL_SECTION
  13. typedef HANDLE os_native_event_t;
  14. typedef struct os_event_struct os_event_struct_t;
  15. typedef os_event_struct_t* os_event_t;
  16. struct os_event_struct {
  17. os_native_event_t handle;
  18. /* Windows event */
  19. UT_LIST_NODE_T(os_event_struct_t) os_event_list;
  20. /* list of all created events */
  21. };
  22. #else
  23. typedef pthread_mutex_t os_fast_mutex_t;
  24. typedef struct os_event_struct os_event_struct_t;
  25. typedef os_event_struct_t* os_event_t;
  26. struct os_event_struct {
  27. os_fast_mutex_t os_mutex; /* this mutex protects the next
  28. fields */
  29. ibool is_set; /* this is TRUE when the event is
  30. in the signaled state, i.e., a thread
  31. does not stop if it tries to wait for
  32. this event */
  33. ib_longlong signal_count; /* this is incremented each time
  34. the event becomes signaled */
  35. pthread_cond_t cond_var; /* condition variable is used in
  36. waiting for the event */
  37. UT_LIST_NODE_T(os_event_struct_t) os_event_list;
  38. /* list of all created events */
  39. };
  40. #endif
  41. typedef struct os_mutex_struct os_mutex_str_t;
  42. typedef os_mutex_str_t* os_mutex_t;
  43. #define OS_SYNC_INFINITE_TIME ((ulint)(-1))
  44. #define OS_SYNC_TIME_EXCEEDED 1
  45. /* Mutex protecting counts and the event and OS 'slow' mutex lists */
  46. extern os_mutex_t os_sync_mutex;
  47. /* This is incremented by 1 in os_thread_create and decremented by 1 in
  48. os_thread_exit */
  49. extern ulint os_thread_count;
  50. extern ulint os_event_count;
  51. extern ulint os_mutex_count;
  52. extern ulint os_fast_mutex_count;
  53. /*************************************************************
  54. Initializes global event and OS 'slow' mutex lists. */
  55. void
  56. os_sync_init(void);
  57. /*==============*/
  58. /*************************************************************
  59. Frees created events and OS 'slow' mutexes. */
  60. void
  61. os_sync_free(void);
  62. /*==============*/
  63. /*************************************************************
  64. Creates an event semaphore, i.e., a semaphore which may just have two states:
  65. signaled and nonsignaled. The created event is manual reset: it must be reset
  66. explicitly by calling sync_os_reset_event. */
  67. os_event_t
  68. os_event_create(
  69. /*============*/
  70. /* out: the event handle */
  71. const char* name); /* in: the name of the event, if NULL
  72. the event is created without a name */
  73. #ifdef __WIN__
  74. /*************************************************************
  75. Creates an auto-reset event semaphore, i.e., an event which is automatically
  76. reset when a single thread is released. Works only in Windows. */
  77. os_event_t
  78. os_event_create_auto(
  79. /*=================*/
  80. /* out: the event handle */
  81. const char* name); /* in: the name of the event, if NULL
  82. the event is created without a name */
  83. #endif
  84. /**************************************************************
  85. Sets an event semaphore to the signaled state: lets waiting threads
  86. proceed. */
  87. void
  88. os_event_set(
  89. /*=========*/
  90. os_event_t event); /* in: event to set */
  91. /**************************************************************
  92. Resets an event semaphore to the nonsignaled state. Waiting threads will
  93. stop to wait for the event. */
  94. void
  95. os_event_reset(
  96. /*===========*/
  97. os_event_t event); /* in: event to reset */
  98. /**************************************************************
  99. Frees an event object. */
  100. void
  101. os_event_free(
  102. /*==========*/
  103. os_event_t event); /* in: event to free */
  104. /**************************************************************
  105. Waits for an event object until it is in the signaled state. If
  106. srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS this also exits the
  107. waiting thread when the event becomes signaled (or immediately if the
  108. event is already in the signaled state). */
  109. void
  110. os_event_wait(
  111. /*==========*/
  112. os_event_t event); /* in: event to wait */
  113. /**************************************************************
  114. Waits for an event object until it is in the signaled state or
  115. a timeout is exceeded. In Unix the timeout is always infinite. */
  116. ulint
  117. os_event_wait_time(
  118. /*===============*/
  119. /* out: 0 if success,
  120. OS_SYNC_TIME_EXCEEDED if timeout
  121. was exceeded */
  122. os_event_t event, /* in: event to wait */
  123. ulint time); /* in: timeout in microseconds, or
  124. OS_SYNC_INFINITE_TIME */
  125. #ifdef __WIN__
  126. /**************************************************************
  127. Waits for any event in an OS native event array. Returns if even a single
  128. one is signaled or becomes signaled. */
  129. ulint
  130. os_event_wait_multiple(
  131. /*===================*/
  132. /* out: index of the event
  133. which was signaled */
  134. ulint n, /* in: number of events in the
  135. array */
  136. os_native_event_t* native_event_array);
  137. /* in: pointer to an array of event
  138. handles */
  139. #endif
  140. /*************************************************************
  141. Creates an operating system mutex semaphore. Because these are slow, the
  142. mutex semaphore of InnoDB itself (mutex_t) should be used where possible. */
  143. os_mutex_t
  144. os_mutex_create(
  145. /*============*/
  146. /* out: the mutex handle */
  147. const char* name); /* in: the name of the mutex, if NULL
  148. the mutex is created without a name */
  149. /**************************************************************
  150. Acquires ownership of a mutex semaphore. */
  151. void
  152. os_mutex_enter(
  153. /*===========*/
  154. os_mutex_t mutex); /* in: mutex to acquire */
  155. /**************************************************************
  156. Releases ownership of a mutex. */
  157. void
  158. os_mutex_exit(
  159. /*==========*/
  160. os_mutex_t mutex); /* in: mutex to release */
  161. /**************************************************************
  162. Frees an mutex object. */
  163. void
  164. os_mutex_free(
  165. /*==========*/
  166. os_mutex_t mutex); /* in: mutex to free */
  167. /**************************************************************
  168. Acquires ownership of a fast mutex. Currently in Windows this is the same
  169. as os_fast_mutex_lock! */
  170. UNIV_INLINE
  171. ulint
  172. os_fast_mutex_trylock(
  173. /*==================*/
  174. /* out: 0 if success, != 0 if
  175. was reserved by another
  176. thread */
  177. os_fast_mutex_t* fast_mutex); /* in: mutex to acquire */
  178. /**************************************************************
  179. Releases ownership of a fast mutex. */
  180. void
  181. os_fast_mutex_unlock(
  182. /*=================*/
  183. os_fast_mutex_t* fast_mutex); /* in: mutex to release */
  184. /*************************************************************
  185. Initializes an operating system fast mutex semaphore. */
  186. void
  187. os_fast_mutex_init(
  188. /*===============*/
  189. os_fast_mutex_t* fast_mutex); /* in: fast mutex */
  190. /**************************************************************
  191. Acquires ownership of a fast mutex. */
  192. void
  193. os_fast_mutex_lock(
  194. /*===============*/
  195. os_fast_mutex_t* fast_mutex); /* in: mutex to acquire */
  196. /**************************************************************
  197. Frees an mutex object. */
  198. void
  199. os_fast_mutex_free(
  200. /*===============*/
  201. os_fast_mutex_t* fast_mutex); /* in: mutex to free */
  202. #ifndef UNIV_NONINL
  203. #include "os0sync.ic"
  204. #endif
  205. #endif