| | 166 | |
| | 167 | #undef XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 168 | #undef XC_LOCK_UNSUED |
| | 169 | |
| | 170 | #ifdef ZEND_WIN32 |
| | 171 | # define XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 172 | # ifndef ZTS |
| | 173 | # define XC_LOCK_UNSUED |
| | 174 | # endif |
| | 175 | #endif |
| | 176 | |
| | 177 | #if defined(PTHREADS) |
| | 178 | # define XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 179 | #endif |
| | 180 | |
| | 181 | struct _xc_lock_t { |
| | 182 | #ifdef XC_LOCK_UNSUED |
| | 183 | int dummy; |
| | 184 | #else |
| | 185 | # ifdef ZTS |
| | 186 | MUTEX_T tsrm_mutex; |
| | 187 | # endif |
| | 188 | |
| | 189 | # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 190 | # ifdef ZTS |
| | 191 | zend_bool use_fcntl; |
| | 192 | # endif |
| | 193 | xc_fcntl_lock_t fcntl_lock; |
| | 194 | # endif |
| | 195 | #endif |
| | 196 | }; |
| | 197 | |
| | 198 | xc_lock_t *xc_lock_init(const char *pathname, int interprocess) /* {{{ */ |
| | 199 | { |
| | 200 | #ifdef XC_LOCK_UNSUED |
| | 201 | return (xc_lock_t *) 1; |
| | 202 | #else |
| | 203 | # ifdef ZTS |
| | 204 | xc_lock_t *lck = malloc(sizeof(xc_lock_t)); |
| | 205 | # if defined(PTHREADS) |
| | 206 | pthread_mutexattr_t psharedm; |
| | 207 | pthread_mutexattr_init(&psharedm); |
| | 208 | pthread_mutexattr_setpshared(&psharedm, PTHREAD_PROCESS_SHARED); |
| | 209 | lck->tsrm_mutex = (pthread_mutex_t *) malloc(sizeof(pthread_mutex_t)); |
| | 210 | pthread_mutex_init(lck->tsrm_mutex, &psharedm); |
| | 211 | # else |
| | 212 | lck->tsrm_mutex = tsrm_mutex_alloc(); |
| | 213 | # endif |
| | 214 | # endif |
| | 215 | # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 216 | # ifdef ZTS |
| | 217 | lck->use_fcntl = interprocess; |
| | 218 | if (lck->use_fcntl) |
| | 219 | # endif |
| | 220 | xc_fcntl_init(&lck->fcntl_lock, pathname); |
| | 221 | # endif |
| | 222 | return lck; |
| | 223 | #endif |
| | 224 | } |
| | 225 | /* }}} */ |
| | 226 | void xc_lock_destroy(xc_lock_t *lck) /* {{{ */ |
| | 227 | { |
| | 228 | #ifdef XC_LOCK_UNSUED |
| | 229 | /* do nothing */ |
| | 230 | #else |
| | 231 | # ifdef ZTS |
| | 232 | tsrm_mutex_free(lck->tsrm_mutex); |
| | 233 | # endif |
| | 234 | # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 235 | # ifdef ZTS |
| | 236 | if (lck->use_fcntl) |
| | 237 | # endif |
| | 238 | xc_fcntl_destroy(&lck->fcntl_lock); |
| | 239 | # endif |
| | 240 | free(lck); |
| | 241 | #endif |
| | 242 | } |
| | 243 | /* }}} */ |
| | 244 | void xc_lock(xc_lock_t *lck) /* {{{ */ |
| | 245 | { |
| | 246 | #ifdef XC_LOCK_UNSUED |
| | 247 | #else |
| | 248 | # ifdef ZTS |
| | 249 | if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) { |
| | 250 | zend_error(E_ERROR, "xc_lock failed errno:%d", errno); |
| | 251 | } |
| | 252 | # endif |
| | 253 | # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 254 | # ifdef ZTS |
| | 255 | if (lck->use_fcntl) |
| | 256 | # endif |
| | 257 | xc_fcntl_lock(&lck->fcntl_lock); |
| | 258 | # endif |
| | 259 | #endif |
| | 260 | } |
| | 261 | /* }}} */ |
| | 262 | void xc_rdlock(xc_lock_t *lck) /* {{{ */ |
| | 263 | { |
| | 264 | #ifdef XC_LOCK_UNSUED |
| | 265 | #else |
| | 266 | # ifdef ZTS |
| | 267 | if (tsrm_mutex_lock(lck->tsrm_mutex) < 0) { |
| | 268 | zend_error(E_ERROR, "xc_rdlock failed errno:%d", errno); |
| | 269 | } |
| | 270 | # endif |
| | 271 | # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 272 | # ifdef ZTS |
| | 273 | if (lck->use_fcntl) |
| | 274 | # endif |
| | 275 | xc_fcntl_lock(&lck->fcntl_lock); |
| | 276 | # endif |
| | 277 | #endif |
| | 278 | } |
| | 279 | /* }}} */ |
| | 280 | void xc_unlock(xc_lock_t *lck) /* {{{ */ |
| | 281 | { |
| | 282 | #ifdef XC_LOCK_UNSUED |
| | 283 | #else |
| | 284 | # ifndef XC_INTERPROCESS_LOCK_IMPLEMENTED |
| | 285 | # ifdef ZTS |
| | 286 | if (lck->use_fcntl) |
| | 287 | # endif |
| | 288 | xc_fcntl_unlock(&lck->fcntl_lock); |
| | 289 | # endif |
| | 290 | #endif |
| | 291 | # ifdef ZTS |
| | 292 | if (tsrm_mutex_unlock(lck->tsrm_mutex) < 0) { |
| | 293 | zend_error(E_ERROR, "xc_unlock failed errno:%d", errno); |
| | 294 | } |
| | 295 | # endif |
| | 296 | } |
| | 297 | /* }}} */ |