00001 /****************************************************** 00002 The interface to the operating system 00003 synchronization primitives. 00004 00005 (c) 1995 Innobase Oy 00006 00007 Created 9/6/1995 Heikki Tuuri 00008 *******************************************************/ 00009 #ifndef os0sync_h 00010 #define os0sync_h 00011 00012 #include "univ.i" 00013 #include "ut0lst.h" 00014 00015 #ifdef __WIN__ 00016 00017 #define os_fast_mutex_t CRITICAL_SECTION 00018 00019 typedef HANDLE os_native_event_t; 00020 00021 typedef struct os_event_struct os_event_struct_t; 00022 typedef os_event_struct_t* os_event_t; 00023 00024 struct os_event_struct { 00025 os_native_event_t handle; 00026 /* Windows event */ 00027 UT_LIST_NODE_T(os_event_struct_t) os_event_list; 00028 /* list of all created events */ 00029 }; 00030 #else 00031 typedef pthread_mutex_t os_fast_mutex_t; 00032 00033 typedef struct os_event_struct os_event_struct_t; 00034 typedef os_event_struct_t* os_event_t; 00035 00036 struct os_event_struct { 00037 os_fast_mutex_t os_mutex; /* this mutex protects the next 00038 fields */ 00039 ibool is_set; /* this is TRUE when the event is 00040 in the signaled state, i.e., a thread 00041 does not stop if it tries to wait for 00042 this event */ 00043 ib_longlong signal_count; /* this is incremented each time 00044 the event becomes signaled */ 00045 pthread_cond_t cond_var; /* condition variable is used in 00046 waiting for the event */ 00047 UT_LIST_NODE_T(os_event_struct_t) os_event_list; 00048 /* list of all created events */ 00049 }; 00050 #endif 00051 00052 typedef struct os_mutex_struct os_mutex_str_t; 00053 typedef os_mutex_str_t* os_mutex_t; 00054 00055 #define OS_SYNC_INFINITE_TIME ((ulint)(-1)) 00056 00057 #define OS_SYNC_TIME_EXCEEDED 1 00058 00059 /* Mutex protecting counts and the event and OS 'slow' mutex lists */ 00060 extern os_mutex_t os_sync_mutex; 00061 00062 /* This is incremented by 1 in os_thread_create and decremented by 1 in 00063 os_thread_exit */ 00064 extern ulint os_thread_count; 00065 00066 extern ulint os_event_count; 00067 extern ulint os_mutex_count; 00068 extern ulint os_fast_mutex_count; 00069 00070 /************************************************************* 00071 Initializes global event and OS 'slow' mutex lists. */ 00072 00073 void 00074 os_sync_init(void); 00075 /*==============*/ 00076 /************************************************************* 00077 Frees created events and OS 'slow' mutexes. */ 00078 00079 void 00080 os_sync_free(void); 00081 /*==============*/ 00082 /************************************************************* 00083 Creates an event semaphore, i.e., a semaphore which may just have two states: 00084 signaled and nonsignaled. The created event is manual reset: it must be reset 00085 explicitly by calling sync_os_reset_event. */ 00086 00087 os_event_t 00088 os_event_create( 00089 /*============*/ 00090 /* out: the event handle */ 00091 const char* name); /* in: the name of the event, if NULL 00092 the event is created without a name */ 00093 #ifdef __WIN__ 00094 /************************************************************* 00095 Creates an auto-reset event semaphore, i.e., an event which is automatically 00096 reset when a single thread is released. Works only in Windows. */ 00097 00098 os_event_t 00099 os_event_create_auto( 00100 /*=================*/ 00101 /* out: the event handle */ 00102 const char* name); /* in: the name of the event, if NULL 00103 the event is created without a name */ 00104 #endif 00105 /************************************************************** 00106 Sets an event semaphore to the signaled state: lets waiting threads 00107 proceed. */ 00108 00109 void 00110 os_event_set( 00111 /*=========*/ 00112 os_event_t event); /* in: event to set */ 00113 /************************************************************** 00114 Resets an event semaphore to the nonsignaled state. Waiting threads will 00115 stop to wait for the event. */ 00116 00117 void 00118 os_event_reset( 00119 /*===========*/ 00120 os_event_t event); /* in: event to reset */ 00121 /************************************************************** 00122 Frees an event object. */ 00123 00124 void 00125 os_event_free( 00126 /*==========*/ 00127 os_event_t event); /* in: event to free */ 00128 /************************************************************** 00129 Waits for an event object until it is in the signaled state. If 00130 srv_shutdown_state == SRV_SHUTDOWN_EXIT_THREADS this also exits the 00131 waiting thread when the event becomes signaled (or immediately if the 00132 event is already in the signaled state). */ 00133 00134 void 00135 os_event_wait( 00136 /*==========*/ 00137 os_event_t event); /* in: event to wait */ 00138 /************************************************************** 00139 Waits for an event object until it is in the signaled state or 00140 a timeout is exceeded. In Unix the timeout is always infinite. */ 00141 00142 ulint 00143 os_event_wait_time( 00144 /*===============*/ 00145 /* out: 0 if success, 00146 OS_SYNC_TIME_EXCEEDED if timeout 00147 was exceeded */ 00148 os_event_t event, /* in: event to wait */ 00149 ulint time); /* in: timeout in microseconds, or 00150 OS_SYNC_INFINITE_TIME */ 00151 #ifdef __WIN__ 00152 /************************************************************** 00153 Waits for any event in an OS native event array. Returns if even a single 00154 one is signaled or becomes signaled. */ 00155 00156 ulint 00157 os_event_wait_multiple( 00158 /*===================*/ 00159 /* out: index of the event 00160 which was signaled */ 00161 ulint n, /* in: number of events in the 00162 array */ 00163 os_native_event_t* native_event_array); 00164 /* in: pointer to an array of event 00165 handles */ 00166 #endif 00167 /************************************************************* 00168 Creates an operating system mutex semaphore. Because these are slow, the 00169 mutex semaphore of InnoDB itself (mutex_t) should be used where possible. */ 00170 00171 os_mutex_t 00172 os_mutex_create( 00173 /*============*/ 00174 /* out: the mutex handle */ 00175 const char* name); /* in: the name of the mutex, if NULL 00176 the mutex is created without a name */ 00177 /************************************************************** 00178 Acquires ownership of a mutex semaphore. */ 00179 00180 void 00181 os_mutex_enter( 00182 /*===========*/ 00183 os_mutex_t mutex); /* in: mutex to acquire */ 00184 /************************************************************** 00185 Releases ownership of a mutex. */ 00186 00187 void 00188 os_mutex_exit( 00189 /*==========*/ 00190 os_mutex_t mutex); /* in: mutex to release */ 00191 /************************************************************** 00192 Frees an mutex object. */ 00193 00194 void 00195 os_mutex_free( 00196 /*==========*/ 00197 os_mutex_t mutex); /* in: mutex to free */ 00198 /************************************************************** 00199 Acquires ownership of a fast mutex. Currently in Windows this is the same 00200 as os_fast_mutex_lock! */ 00201 UNIV_INLINE 00202 ulint 00203 os_fast_mutex_trylock( 00204 /*==================*/ 00205 /* out: 0 if success, != 0 if 00206 was reserved by another 00207 thread */ 00208 os_fast_mutex_t* fast_mutex); /* in: mutex to acquire */ 00209 /************************************************************** 00210 Releases ownership of a fast mutex. */ 00211 00212 void 00213 os_fast_mutex_unlock( 00214 /*=================*/ 00215 os_fast_mutex_t* fast_mutex); /* in: mutex to release */ 00216 /************************************************************* 00217 Initializes an operating system fast mutex semaphore. */ 00218 00219 void 00220 os_fast_mutex_init( 00221 /*===============*/ 00222 os_fast_mutex_t* fast_mutex); /* in: fast mutex */ 00223 /************************************************************** 00224 Acquires ownership of a fast mutex. */ 00225 00226 void 00227 os_fast_mutex_lock( 00228 /*===============*/ 00229 os_fast_mutex_t* fast_mutex); /* in: mutex to acquire */ 00230 /************************************************************** 00231 Frees an mutex object. */ 00232 00233 void 00234 os_fast_mutex_free( 00235 /*===============*/ 00236 os_fast_mutex_t* fast_mutex); /* in: mutex to free */ 00237 00238 #ifndef UNIV_NONINL 00239 #include "os0sync.ic" 00240 #endif 00241 00242 #endif
1.4.7

