comparison src/core/ngx_cycle.c @ 972:6e7a20529f53

ngx_shared_memory_add()
author Igor Sysoev <igor@sysoev.ru>
date Tue, 02 Jan 2007 23:54:14 +0000
parents 7ddc5345fb4f
children e2d358129eb6
comparison
equal deleted inserted replaced
971:948acd940145 972:6e7a20529f53
47 ngx_uint_t i, n; 47 ngx_uint_t i, n;
48 ngx_log_t *log; 48 ngx_log_t *log;
49 ngx_conf_t conf; 49 ngx_conf_t conf;
50 ngx_pool_t *pool; 50 ngx_pool_t *pool;
51 ngx_cycle_t *cycle, **old; 51 ngx_cycle_t *cycle, **old;
52 ngx_shm_zone_t *shm, *oshm; 52 ngx_shm_zone_t *shm_zone, *oshm_zone;
53 ngx_slab_pool_t *shpool; 53 ngx_slab_pool_t *shpool;
54 ngx_list_part_t *part, *opart; 54 ngx_list_part_t *part, *opart;
55 ngx_open_file_t *file; 55 ngx_open_file_t *file;
56 ngx_listening_t *ls, *nls; 56 ngx_listening_t *ls, *nls;
57 ngx_core_conf_t *ccf; 57 ngx_core_conf_t *ccf;
351 351
352 352
353 /* create shared memory */ 353 /* create shared memory */
354 354
355 part = &cycle->shared_memory.part; 355 part = &cycle->shared_memory.part;
356 shm = part->elts; 356 shm_zone = part->elts;
357 357
358 for (i = 0; /* void */ ; i++) { 358 for (i = 0; /* void */ ; i++) {
359 359
360 if (i >= part->nelts) { 360 if (i >= part->nelts) {
361 if (part->next == NULL) { 361 if (part->next == NULL) {
362 break; 362 break;
363 } 363 }
364 part = part->next; 364 part = part->next;
365 shm = part->elts; 365 shm_zone = part->elts;
366 i = 0; 366 i = 0;
367 } 367 }
368 368
369 shm[i].shm.log = cycle->log; 369 shm_zone[i].shm.log = cycle->log;
370 370
371 opart = &old_cycle->shared_memory.part; 371 opart = &old_cycle->shared_memory.part;
372 oshm = opart->elts; 372 oshm_zone = opart->elts;
373 373
374 for (n = 0; /* void */ ; n++) { 374 for (n = 0; /* void */ ; n++) {
375 375
376 if (n >= opart->nelts) { 376 if (n >= opart->nelts) {
377 if (opart->next == NULL) { 377 if (opart->next == NULL) {
378 break; 378 break;
379 } 379 }
380 opart = opart->next; 380 opart = opart->next;
381 oshm = opart->elts; 381 oshm_zone = opart->elts;
382 n = 0; 382 n = 0;
383 } 383 }
384 384
385 if (ngx_strcmp(shm[i].name.data, oshm[n].name.data) != 0) { 385 if (shm_zone[i].name.len != oshm_zone[n].name.len) {
386 continue; 386 continue;
387 } 387 }
388 388
389 if (shm[i].shm.size == oshm[n].shm.size) { 389 if (ngx_strncmp(shm_zone[i].name.data, oshm_zone[n].name.data,
390 shm[i].shm.addr = oshm[n].shm.addr; 390 shm_zone[i].name.len)
391 != 0)
392 {
393 continue;
394 }
395
396 if (shm_zone[i].shm.size == oshm_zone[n].shm.size) {
397 shm_zone[i].shm.addr = oshm_zone[n].shm.addr;
391 goto found; 398 goto found;
392 } 399 }
393 400
394 ngx_shm_free(&oshm[n].shm); 401 ngx_shm_free(&oshm_zone[n].shm);
395 402
396 break; 403 break;
397 } 404 }
398 405
399 if (ngx_shm_alloc(&shm[i].shm) != NGX_OK) { 406 if (ngx_shm_alloc(&shm_zone[i].shm) != NGX_OK) {
400 goto failed; 407 goto failed;
401 } 408 }
402 409
403 shpool = (ngx_slab_pool_t *) shm[i].shm.addr; 410 shpool = (ngx_slab_pool_t *) shm_zone[i].shm.addr;
404 411
405 shpool->end = shm[i].shm.addr + shm[i].shm.size; 412 shpool->end = shm_zone[i].shm.addr + shm_zone[i].shm.size;
406 shpool->min_shift = 3; 413 shpool->min_shift = 3;
407 414
408 #if (NGX_HAVE_ATOMIC_OPS) 415 #if (NGX_HAVE_ATOMIC_OPS)
409 416
410 lock_file = NULL; 417 lock_file = NULL;
411 418
412 #else 419 #else
413 420
414 lock_file = ngx_palloc(cycle->pool, 421 lock_file = ngx_palloc(cycle->pool,
415 cycle->lock_file.len + shm[i].name.len); 422 cycle->lock_file.len + shm_zone[i].name.len);
416 423
417 if (lock_file == NULL) { 424 if (lock_file == NULL) {
418 goto failed; 425 goto failed;
419 } 426 }
420 427
421 (void) ngx_cpystrn(ngx_cpymem(lock_file, cycle->lock_file.data, 428 (void) ngx_cpystrn(ngx_cpymem(lock_file, cycle->lock_file.data,
422 cycle->lock_file.len), 429 cycle->lock_file.len),
423 shm[i].name.data, shm[i].name.len + 1); 430 shm_zone[i].name.data, shm_zone[i].name.len + 1);
424 431
425 #endif 432 #endif
426 433
427 if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file) 434 if (ngx_shmtx_create(&shpool->mutex, (void *) &shpool->lock, lock_file)
428 != NGX_OK) 435 != NGX_OK)
429 { 436 {
430 goto failed; 437 goto failed;
431 } 438 }
432 439
433 ngx_slab_init(shpool); 440 ngx_slab_init(shpool);
441
442 if (shm_zone[i].init(&shm_zone[i]) != NGX_OK) {
443 goto failed;
444 }
434 445
435 found: 446 found:
436 447
437 continue; 448 continue;
438 } 449 }
986 997
987 #endif 998 #endif
988 } 999 }
989 1000
990 1001
1002 ngx_shm_zone_t *
1003 ngx_shared_memory_add(ngx_conf_t *cf, ngx_str_t *name, size_t size, void *tag)
1004 {
1005 ngx_uint_t i;
1006 ngx_shm_zone_t *shm_zone;
1007 ngx_list_part_t *part;
1008
1009 part = &cf->cycle->shared_memory.part;
1010 shm_zone = part->elts;
1011
1012 for (i = 0; /* void */ ; i++) {
1013
1014 if (i >= part->nelts) {
1015 if (part->next == NULL) {
1016 break;
1017 }
1018 part = part->next;
1019 shm_zone = part->elts;
1020 i = 0;
1021 }
1022
1023 if (name->len != shm_zone[i].name.len) {
1024 continue;
1025 }
1026
1027 if (ngx_strncmp(name->data, shm_zone[i].name.data, name->len) != 0) {
1028 continue;
1029 }
1030
1031 if (size && size != shm_zone[i].shm.size) {
1032 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1033 "the size %uz of shared memory zone \"%V\" "
1034 "conflicts with already declared size %uz");
1035 return NULL;
1036 }
1037
1038 if (tag != shm_zone[i].tag) {
1039 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
1040 "the shared memory zone \"%V\" is "
1041 "already declared for a different use");
1042 return NULL;
1043 }
1044
1045 return &shm_zone[i];
1046 }
1047
1048 shm_zone = ngx_list_push(&cf->cycle->shared_memory);
1049
1050 if (shm_zone == NULL) {
1051 return NULL;
1052 }
1053
1054 shm_zone->data = NULL;
1055 shm_zone->shm.log = cf->cycle->log;
1056 shm_zone->shm.size = size;
1057 shm_zone->init = NULL;
1058 shm_zone->name = *name;
1059 shm_zone->tag = tag;
1060
1061 return shm_zone;
1062 }
1063
1064
991 static void 1065 static void
992 ngx_clean_old_cycles(ngx_event_t *ev) 1066 ngx_clean_old_cycles(ngx_event_t *ev)
993 { 1067 {
994 ngx_uint_t i, n, found, live; 1068 ngx_uint_t i, n, found, live;
995 ngx_log_t *log; 1069 ngx_log_t *log;