comparison src/os/unix/ngx_freebsd_rfork_thread.c @ 379:73688d5d7fc3

nginx-0.0.7-2004-07-06-20:12:16 import
author Igor Sysoev <igor@sysoev.ru>
date Tue, 06 Jul 2004 16:12:16 +0000
parents 41437e4fd9b4
children 02a511569afb
comparison
equal deleted inserted replaced
378:b7d3625c9336 379:73688d5d7fc3
28 * This threads implementation currently works on i386 (486+) and amd64 28 * This threads implementation currently works on i386 (486+) and amd64
29 * platforms only. 29 * platforms only.
30 */ 30 */
31 31
32 32
33 char *ngx_freebsd_kern_usrstack; 33 char *ngx_freebsd_kern_usrstack;
34 size_t ngx_thread_stack_size; 34 size_t ngx_thread_stack_size;
35 35
36 36
37 static size_t rz_size; 37 static size_t rz_size;
38 static size_t usable_stack_size; 38 static size_t usable_stack_size;
39 static char *last_stack; 39 static char *last_stack;
40 40
41 static ngx_uint_t nthreads; 41 static ngx_uint_t nthreads;
42 static ngx_uint_t max_threads; 42 static ngx_uint_t max_threads;
43 static ngx_tid_t *tids; /* the threads tids array */ 43 static ngx_tid_t *tids; /* the threads tids array */
44 44 void **ngx_tls; /* the threads tls's array */
45 45
46 /* the thread-safe libc errno */ 46 /* the thread-safe libc errno */
47 47
48 static int errno0; /* the main thread's errno */ 48 static int errno0; /* the main thread's errno */
49 static int *errnos; /* the threads errno's array */ 49 static int *errnos; /* the threads errno's array */
218 if (zone != red_zone) { 218 if (zone != red_zone) {
219 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0, 219 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
220 "red zone address was changed"); 220 "red zone address was changed");
221 } 221 }
222 222
223 /* create the threads errno array */ 223 /* create the threads errno's array */
224 224
225 if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) { 225 if (!(errnos = ngx_calloc(n * sizeof(int), cycle->log))) {
226 return NGX_ERROR; 226 return NGX_ERROR;
227 } 227 }
228 228
229 /* create the threads tid array */ 229 /* create the threads tids array */
230 230
231 if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) { 231 if (!(tids = ngx_calloc((n + 1) * sizeof(ngx_tid_t), cycle->log))) {
232 return NGX_ERROR; 232 return NGX_ERROR;
233 } 233 }
234 234
235 tids[0] = ngx_pid; 235 tids[0] = ngx_pid;
236
237 /* create the threads tls's array */
238
239 if (!(ngx_tls = ngx_calloc((n + 1) * sizeof(void *), cycle->log))) {
240 return NGX_ERROR;
241 }
242
236 nthreads = 1; 243 nthreads = 1;
237 244
238 last_stack = zone + rz_size; 245 last_stack = zone + rz_size;
239 usable_stack_size = size; 246 usable_stack_size = size;
240 ngx_thread_stack_size = size + rz_size; 247 ngx_thread_stack_size = size + rz_size;
258 if (tids == NULL) { 265 if (tids == NULL) {
259 return ngx_pid; 266 return ngx_pid;
260 } 267 }
261 268
262 return tids[tid]; 269 return tids[tid];
270 }
271
272
273 ngx_int_t ngx_thread_set_tls(void *value)
274 {
275 ngx_tls[ngx_gettid()] = value;
276
277 return NGX_OK;
263 } 278 }
264 279
265 280
266 ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags) 281 ngx_mutex_t *ngx_mutex_init(ngx_log_t *log, uint flags)
267 { 282 {
324 return NGX_OK; 339 return NGX_OK;
325 } 340 }
326 341
327 #if (NGX_DEBUG) 342 #if (NGX_DEBUG)
328 if (try) { 343 if (try) {
329 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, 344 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
330 "try lock mutex " PTR_FMT " lock:%X", m, m->lock); 345 "try lock mutex " PTR_FMT " lock:%X", m, m->lock);
331 } else { 346 } else {
332 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, 347 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
333 "lock mutex " PTR_FMT " lock:%X", m, m->lock); 348 "lock mutex " PTR_FMT " lock:%X", m, m->lock);
334 } 349 }
335 #endif 350 #endif
336 351
337 old = m->lock; 352 old = m->lock;
358 tries = 0; 373 tries = 0;
359 old = m->lock; 374 old = m->lock;
360 continue; 375 continue;
361 } 376 }
362 377
363 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, 378 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
364 "mutex " PTR_FMT " lock:%X", m, m->lock); 379 "mutex " PTR_FMT " lock:%X", m, m->lock);
365 380
366 /* 381 /*
367 * The mutex is locked so we increase a number 382 * The mutex is locked so we increase a number
368 * of the threads that are waiting on the mutex 383 * of the threads that are waiting on the mutex
378 return NGX_ERROR; 393 return NGX_ERROR;
379 } 394 }
380 395
381 if (ngx_atomic_cmp_set(&m->lock, old, lock)) { 396 if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
382 397
383 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, 398 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
384 "wait mutex " PTR_FMT " lock:%X", m, m->lock); 399 "wait mutex " PTR_FMT " lock:%X", m, m->lock);
385 400
386 /* 401 /*
387 * The number of the waiting threads has been increased 402 * The number of the waiting threads has been increased
388 * and we would wait on the SysV semaphore. 403 * and we would wait on the SysV semaphore.
399 "semop() failed while waiting " 414 "semop() failed while waiting "
400 "on mutex " PTR_FMT, m); 415 "on mutex " PTR_FMT, m);
401 return NGX_ERROR; 416 return NGX_ERROR;
402 } 417 }
403 418
404 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, 419 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
405 "mutex waked up " PTR_FMT " lock:%X", 420 "mutex waked up " PTR_FMT " lock:%X",
406 m, m->lock); 421 m, m->lock);
407 422
408 tries = 0; 423 tries = 0;
409 old = m->lock; 424 old = m->lock;
425 old = m->lock; 440 old = m->lock;
426 } 441 }
427 442
428 if (tries++ > 1000) { 443 if (tries++ > 1000) {
429 444
430 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, 445 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
431 "mutex " PTR_FMT " is contested", m); 446 "mutex " PTR_FMT " is contested", m);
432 447
433 /* the mutex is probably contested so we are giving up now */ 448 /* the mutex is probably contested so we are giving up now */
434 449
435 sched_yield(); 450 sched_yield();
437 tries = 0; 452 tries = 0;
438 old = m->lock; 453 old = m->lock;
439 } 454 }
440 } 455 }
441 456
442 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, 457 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
443 "mutex " PTR_FMT " is locked, lock:%X", m, m->lock); 458 "mutex " PTR_FMT " is locked, lock:%X", m, m->lock);
444 459
445 return NGX_OK; 460 return NGX_OK;
446 } 461 }
447 462
464 } 479 }
465 480
466 /* free the mutex */ 481 /* free the mutex */
467 482
468 #if 0 483 #if 0
469 ngx_log_debug2(NGX_LOG_DEBUG_CORE, m->log, 0, 484 ngx_log_debug2(NGX_LOG_DEBUG_MUTEX, m->log, 0,
470 "unlock mutex " PTR_FMT " lock:%X", m, old); 485 "unlock mutex " PTR_FMT " lock:%X", m, old);
471 #endif 486 #endif
472 487
473 for ( ;; ) { 488 for ( ;; ) {
474 lock = old & ~NGX_MUTEX_LOCK_BUSY; 489 lock = old & ~NGX_MUTEX_LOCK_BUSY;
479 494
480 old = m->lock; 495 old = m->lock;
481 } 496 }
482 497
483 if (m->semid == -1) { 498 if (m->semid == -1) {
484 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, 499 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
485 "mutex " PTR_FMT " is unlocked", m); 500 "mutex " PTR_FMT " is unlocked", m);
486 501
487 return NGX_OK; 502 return NGX_OK;
488 } 503 }
489 504
509 524
510 if (ngx_atomic_cmp_set(&m->lock, old, lock)) { 525 if (ngx_atomic_cmp_set(&m->lock, old, lock)) {
511 526
512 /* wake up the thread that waits on semaphore */ 527 /* wake up the thread that waits on semaphore */
513 528
514 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, 529 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
515 "wake up mutex " PTR_FMT "", m); 530 "wake up mutex " PTR_FMT "", m);
516 531
517 op.sem_num = 0; 532 op.sem_num = 0;
518 op.sem_op = 1; 533 op.sem_op = 1;
519 op.sem_flg = 0; 534 op.sem_flg = 0;
529 } 544 }
530 545
531 old = m->lock; 546 old = m->lock;
532 } 547 }
533 548
534 ngx_log_debug1(NGX_LOG_DEBUG_CORE, m->log, 0, 549 ngx_log_debug1(NGX_LOG_DEBUG_MUTEX, m->log, 0,
535 "mutex " PTR_FMT " is unlocked", m); 550 "mutex " PTR_FMT " is unlocked", m);
536 551
537 return NGX_OK; 552 return NGX_OK;
538 } 553 }
539 554