| 41 |
}; |
}; |
| 42 |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 43 |
|
|
| 44 |
|
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
| 45 |
|
#define SECTION_TRY_LOCK_TIMES 10 |
| 46 |
|
|
| 47 |
#define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
#define ARTICLE_BLOCK_PER_SHM 400 // sizeof(ARTICLE_BLOCK) * ARTICLE_BLOCK_PER_SHM is the size of each shm segment to allocate |
| 48 |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 200 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 49 |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
#define ARTICLE_BLOCK_PER_POOL (ARTICLE_BLOCK_PER_SHM * ARTICLE_BLOCK_SHM_COUNT_LIMIT) |
| 55 |
struct article_block_t |
struct article_block_t |
| 56 |
{ |
{ |
| 57 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
| 58 |
int32_t article_count; |
int article_count; |
| 59 |
struct article_block_t *p_next_block; |
struct article_block_t *p_next_block; |
| 60 |
}; |
}; |
| 61 |
typedef struct article_block_t ARTICLE_BLOCK; |
typedef struct article_block_t ARTICLE_BLOCK; |
| 62 |
|
|
|
struct article_block_shm_t |
|
|
{ |
|
|
int shmid; |
|
|
void *p_shm; |
|
|
}; |
|
|
typedef struct article_block_shm_t ARTICLE_BLOCK_SHM; |
|
|
|
|
| 63 |
struct article_block_pool_t |
struct article_block_pool_t |
| 64 |
{ |
{ |
| 65 |
ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
int shmid; |
| 66 |
|
struct |
| 67 |
|
{ |
| 68 |
|
int shmid; |
| 69 |
|
void *p_shm; |
| 70 |
|
} shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
| 71 |
int shm_count; |
int shm_count; |
| 72 |
ARTICLE_BLOCK *p_block_free_list; |
ARTICLE_BLOCK *p_block_free_list; |
| 73 |
ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL]; |
ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL]; |
| 74 |
int32_t block_count; |
int block_count; |
| 75 |
}; |
}; |
| 76 |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
| 77 |
|
|
|
static int article_block_pool_shmid; |
|
| 78 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 79 |
|
|
| 80 |
static int section_list_pool_semid; |
struct section_list_pool_t |
| 81 |
static int section_list_pool_shmid; |
{ |
| 82 |
static SECTION_LIST *p_section_list_pool = NULL; |
int shmid; |
| 83 |
|
SECTION_LIST sections[BBS_max_section]; |
| 84 |
static int section_list_count = 0; |
int section_count; |
| 85 |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
int semid; |
| 86 |
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
TRIE_NODE *p_trie_dict_section_by_name; |
| 87 |
|
TRIE_NODE *p_trie_dict_section_by_sid; |
| 88 |
|
}; |
| 89 |
|
typedef struct section_list_pool_t SECTION_LIST_POOL; |
| 90 |
|
|
| 91 |
|
static SECTION_LIST_POOL *p_section_list_pool = NULL; |
| 92 |
|
|
| 93 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 94 |
{ |
{ |
| 108 |
return -1; |
return -1; |
| 109 |
} |
} |
| 110 |
|
|
| 111 |
if (block_count > ARTICLE_BLOCK_PER_POOL) |
if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL) |
| 112 |
{ |
{ |
| 113 |
log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL); |
log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL); |
| 114 |
return -2; |
return -2; |
| 137 |
return -3; |
return -3; |
| 138 |
} |
} |
| 139 |
|
|
|
article_block_pool_shmid = shmid; |
|
| 140 |
p_article_block_pool = p_shm; |
p_article_block_pool = p_shm; |
| 141 |
|
p_article_block_pool->shmid = shmid; |
| 142 |
|
|
| 143 |
p_article_block_pool->shm_count = 0; |
p_article_block_pool->shm_count = 0; |
| 144 |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
| 157 |
return -3; |
return -3; |
| 158 |
} |
} |
| 159 |
|
|
| 160 |
size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
| 161 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 162 |
if (shmid == -1) |
if (shmid == -1) |
| 163 |
{ |
{ |
| 201 |
|
|
| 202 |
void article_block_cleanup(void) |
void article_block_cleanup(void) |
| 203 |
{ |
{ |
| 204 |
|
int shmid; |
| 205 |
|
|
| 206 |
if (p_article_block_pool == NULL) |
if (p_article_block_pool == NULL) |
| 207 |
{ |
{ |
| 208 |
return; |
return; |
| 221 |
} |
} |
| 222 |
} |
} |
| 223 |
|
|
| 224 |
|
shmid = p_article_block_pool->shmid; |
| 225 |
|
|
| 226 |
if (shmdt(p_article_block_pool) == -1) |
if (shmdt(p_article_block_pool) == -1) |
| 227 |
{ |
{ |
| 228 |
log_error("shmdt(shmid = %d) error (%d)\n", article_block_pool_shmid, errno); |
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 229 |
} |
} |
| 230 |
|
|
| 231 |
if (shmctl(article_block_pool_shmid, IPC_RMID, NULL) == -1) |
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 232 |
{ |
{ |
| 233 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", article_block_pool_shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 234 |
} |
} |
| 235 |
|
|
| 236 |
p_article_block_pool = NULL; |
p_article_block_pool = NULL; |
| 237 |
} |
} |
| 238 |
|
|
| 239 |
|
int set_article_block_shm_readonly(void) |
| 240 |
|
{ |
| 241 |
|
int shmid; |
| 242 |
|
void *p_shm; |
| 243 |
|
int i; |
| 244 |
|
|
| 245 |
|
if (p_article_block_pool == NULL) |
| 246 |
|
{ |
| 247 |
|
log_error("article_block_pool not initialized\n"); |
| 248 |
|
return -1; |
| 249 |
|
} |
| 250 |
|
|
| 251 |
|
for (i = 0; i < p_article_block_pool->shm_count; i++) |
| 252 |
|
{ |
| 253 |
|
shmid = (p_article_block_pool->shm_pool + i)->shmid; |
| 254 |
|
|
| 255 |
|
// Remap shared memory in read-only mode |
| 256 |
|
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP); |
| 257 |
|
if (p_shm == (void *)-1) |
| 258 |
|
{ |
| 259 |
|
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
| 260 |
|
return -2; |
| 261 |
|
} |
| 262 |
|
} |
| 263 |
|
|
| 264 |
|
return 0; |
| 265 |
|
} |
| 266 |
|
|
| 267 |
|
int detach_article_block_shm(void) |
| 268 |
|
{ |
| 269 |
|
int shmid; |
| 270 |
|
|
| 271 |
|
if (p_article_block_pool == NULL) |
| 272 |
|
{ |
| 273 |
|
return -1; |
| 274 |
|
} |
| 275 |
|
|
| 276 |
|
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 277 |
|
{ |
| 278 |
|
if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 279 |
|
{ |
| 280 |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 281 |
|
return -2; |
| 282 |
|
} |
| 283 |
|
} |
| 284 |
|
|
| 285 |
|
shmid = p_article_block_pool->shmid; |
| 286 |
|
|
| 287 |
|
if (shmdt(p_article_block_pool) == -1) |
| 288 |
|
{ |
| 289 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 290 |
|
return -3; |
| 291 |
|
} |
| 292 |
|
|
| 293 |
|
p_article_block_pool = NULL; |
| 294 |
|
|
| 295 |
|
return 0; |
| 296 |
|
} |
| 297 |
|
|
| 298 |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
| 299 |
{ |
{ |
| 300 |
ARTICLE_BLOCK *p_block = NULL; |
ARTICLE_BLOCK *p_block = NULL; |
| 429 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
| 430 |
} |
} |
| 431 |
|
|
| 432 |
extern int section_list_pool_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 433 |
{ |
{ |
| 434 |
int semid; |
int semid; |
| 435 |
int shmid; |
int shmid; |
| 440 |
union semun arg; |
union semun arg; |
| 441 |
int i; |
int i; |
| 442 |
|
|
| 443 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
if (p_section_list_pool != NULL) |
| 444 |
{ |
{ |
| 445 |
section_list_pool_cleanup(); |
section_list_cleanup(); |
| 446 |
} |
} |
| 447 |
|
|
| 448 |
proj_id = (int)(time(NULL) % getpid()); |
proj_id = (int)(time(NULL) % getpid()); |
| 453 |
return -3; |
return -3; |
| 454 |
} |
} |
| 455 |
|
|
| 456 |
|
// Allocate shared memory |
| 457 |
|
size = sizeof(SECTION_LIST_POOL); |
| 458 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 459 |
|
if (shmid == -1) |
| 460 |
|
{ |
| 461 |
|
log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno); |
| 462 |
|
return -3; |
| 463 |
|
} |
| 464 |
|
p_shm = shmat(shmid, NULL, 0); |
| 465 |
|
if (p_shm == (void *)-1) |
| 466 |
|
{ |
| 467 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 468 |
|
return -3; |
| 469 |
|
} |
| 470 |
|
|
| 471 |
|
p_section_list_pool = p_shm; |
| 472 |
|
p_section_list_pool->shmid = shmid; |
| 473 |
|
p_section_list_pool->section_count = 0; |
| 474 |
|
|
| 475 |
|
// Allocate semaphore as section locks |
| 476 |
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
| 477 |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 478 |
if (semid == -1) |
if (semid == -1) |
| 492 |
} |
} |
| 493 |
} |
} |
| 494 |
|
|
| 495 |
section_list_pool_semid = semid; |
p_section_list_pool->semid = semid; |
| 496 |
|
|
| 497 |
size = sizeof(SECTION_LIST) * BBS_max_section; |
p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create(); |
| 498 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
|
if (shmid == -1) |
|
| 499 |
{ |
{ |
| 500 |
log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno); |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 501 |
return -3; |
return -2; |
| 502 |
} |
} |
| 503 |
p_shm = shmat(shmid, NULL, 0); |
|
| 504 |
|
p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create(); |
| 505 |
|
if (p_section_list_pool->p_trie_dict_section_by_sid == NULL) |
| 506 |
|
{ |
| 507 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 508 |
|
return -2; |
| 509 |
|
} |
| 510 |
|
|
| 511 |
|
return 0; |
| 512 |
|
} |
| 513 |
|
|
| 514 |
|
void section_list_cleanup(void) |
| 515 |
|
{ |
| 516 |
|
int shmid; |
| 517 |
|
|
| 518 |
|
if (p_section_list_pool == NULL) |
| 519 |
|
{ |
| 520 |
|
return; |
| 521 |
|
} |
| 522 |
|
|
| 523 |
|
if (p_section_list_pool->p_trie_dict_section_by_name != NULL) |
| 524 |
|
{ |
| 525 |
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name); |
| 526 |
|
p_section_list_pool->p_trie_dict_section_by_name = NULL; |
| 527 |
|
} |
| 528 |
|
|
| 529 |
|
if (p_section_list_pool->p_trie_dict_section_by_sid != NULL) |
| 530 |
|
{ |
| 531 |
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid); |
| 532 |
|
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
| 533 |
|
} |
| 534 |
|
|
| 535 |
|
shmid = p_section_list_pool->shmid; |
| 536 |
|
|
| 537 |
|
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
| 538 |
|
{ |
| 539 |
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno); |
| 540 |
|
} |
| 541 |
|
|
| 542 |
|
if (shmdt(p_section_list_pool) == -1) |
| 543 |
|
{ |
| 544 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 545 |
|
} |
| 546 |
|
|
| 547 |
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 548 |
|
{ |
| 549 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 550 |
|
} |
| 551 |
|
|
| 552 |
|
p_section_list_pool = NULL; |
| 553 |
|
} |
| 554 |
|
|
| 555 |
|
int set_section_list_shm_readonly(void) |
| 556 |
|
{ |
| 557 |
|
int shmid; |
| 558 |
|
void *p_shm; |
| 559 |
|
|
| 560 |
|
if (p_section_list_pool == NULL) |
| 561 |
|
{ |
| 562 |
|
log_error("p_section_list_pool not initialized\n"); |
| 563 |
|
return -1; |
| 564 |
|
} |
| 565 |
|
|
| 566 |
|
shmid = p_section_list_pool->shmid; |
| 567 |
|
|
| 568 |
|
// Remap shared memory in read-only mode |
| 569 |
|
p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP); |
| 570 |
if (p_shm == (void *)-1) |
if (p_shm == (void *)-1) |
| 571 |
{ |
{ |
| 572 |
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
| 573 |
return -3; |
return -3; |
| 574 |
} |
} |
| 575 |
|
|
|
section_list_pool_shmid = shmid; |
|
| 576 |
p_section_list_pool = p_shm; |
p_section_list_pool = p_shm; |
|
section_list_count = 0; |
|
| 577 |
|
|
| 578 |
p_trie_dict_section_by_name = trie_dict_create(); |
return 0; |
| 579 |
if (p_trie_dict_section_by_name == NULL) |
} |
|
{ |
|
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
|
|
return -2; |
|
|
} |
|
| 580 |
|
|
| 581 |
p_trie_dict_section_by_sid = trie_dict_create(); |
int detach_section_list_shm(void) |
| 582 |
if (p_trie_dict_section_by_sid == NULL) |
{ |
| 583 |
|
if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1) |
| 584 |
{ |
{ |
| 585 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
log_error("shmdt(section_list_pool) error (%d)\n", errno); |
| 586 |
return -2; |
return -1; |
| 587 |
} |
} |
| 588 |
|
|
| 589 |
|
p_section_list_pool = NULL; |
| 590 |
|
|
| 591 |
return 0; |
return 0; |
| 592 |
} |
} |
| 593 |
|
|
| 610 |
SECTION_LIST *p_section; |
SECTION_LIST *p_section; |
| 611 |
char sid_str[SID_STR_LEN]; |
char sid_str[SID_STR_LEN]; |
| 612 |
|
|
| 613 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
if (p_section_list_pool == NULL) |
| 614 |
{ |
{ |
| 615 |
log_error("session_list_pool not initialized\n"); |
log_error("session_list_pool not initialized\n"); |
| 616 |
return NULL; |
return NULL; |
| 617 |
} |
} |
| 618 |
|
|
| 619 |
if (section_list_count >= BBS_max_section) |
if (p_section_list_pool->section_count >= BBS_max_section) |
| 620 |
{ |
{ |
| 621 |
log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section); |
log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section); |
| 622 |
return NULL; |
return NULL; |
| 623 |
} |
} |
| 624 |
|
|
| 625 |
sid_to_str(sid, sid_str); |
sid_to_str(sid, sid_str); |
| 626 |
|
|
| 627 |
p_section = p_section_list_pool + section_list_count; |
p_section = p_section_list_pool->sections + p_section_list_pool->section_count; |
| 628 |
|
|
| 629 |
p_section->sid = sid; |
p_section->sid = sid; |
| 630 |
|
|
| 637 |
strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1)); |
strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1)); |
| 638 |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
| 639 |
|
|
| 640 |
if (trie_dict_set(p_trie_dict_section_by_name, sname, section_list_count) != 1) |
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1) |
| 641 |
{ |
{ |
| 642 |
log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count); |
log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count); |
| 643 |
return NULL; |
return NULL; |
| 644 |
} |
} |
| 645 |
|
|
| 646 |
if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1) |
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1) |
| 647 |
{ |
{ |
| 648 |
log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count); |
log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count); |
|
log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]); |
|
| 649 |
return NULL; |
return NULL; |
| 650 |
} |
} |
| 651 |
|
|
| 652 |
section_list_reset_articles(p_section); |
section_list_reset_articles(p_section); |
| 653 |
|
|
| 654 |
section_list_count++; |
p_section_list_pool->section_count++; |
| 655 |
|
|
| 656 |
return p_section; |
return p_section; |
| 657 |
} |
} |
| 669 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 670 |
} |
} |
| 671 |
|
|
|
void section_list_pool_cleanup(void) |
|
|
{ |
|
|
if (p_trie_dict_section_by_name != NULL) |
|
|
{ |
|
|
trie_dict_destroy(p_trie_dict_section_by_name); |
|
|
p_trie_dict_section_by_name = NULL; |
|
|
} |
|
|
|
|
|
if (p_trie_dict_section_by_sid != NULL) |
|
|
{ |
|
|
trie_dict_destroy(p_trie_dict_section_by_sid); |
|
|
p_trie_dict_section_by_sid = NULL; |
|
|
} |
|
|
|
|
|
if (p_section_list_pool != NULL) |
|
|
{ |
|
|
if (shmdt(p_section_list_pool) == -1) |
|
|
{ |
|
|
log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno); |
|
|
} |
|
|
p_section_list_pool = NULL; |
|
|
|
|
|
if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1) |
|
|
{ |
|
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
|
|
} |
|
|
|
|
|
if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1) |
|
|
{ |
|
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno); |
|
|
} |
|
|
} |
|
|
|
|
|
section_list_count = 0; |
|
|
} |
|
|
|
|
| 672 |
SECTION_LIST *section_list_find_by_name(const char *sname) |
SECTION_LIST *section_list_find_by_name(const char *sname) |
| 673 |
{ |
{ |
| 674 |
int64_t index; |
int64_t index; |
| 675 |
|
int ret; |
| 676 |
|
|
| 677 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL) |
if (p_section_list_pool == NULL) |
| 678 |
{ |
{ |
| 679 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 680 |
return NULL; |
return NULL; |
| 681 |
} |
} |
| 682 |
|
|
| 683 |
if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1) |
ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_name, sname, &index); |
| 684 |
|
if (ret < 0) |
| 685 |
{ |
{ |
| 686 |
log_error("trie_dict_get(section, %s) error\n", sname); |
log_error("trie_dict_get(section, %s) error\n", sname); |
| 687 |
return NULL; |
return NULL; |
| 688 |
} |
} |
| 689 |
|
else if (ret == 0) |
| 690 |
|
{ |
| 691 |
|
return NULL; |
| 692 |
|
} |
| 693 |
|
|
| 694 |
return (p_section_list_pool + index); |
return (p_section_list_pool->sections + index); |
| 695 |
} |
} |
| 696 |
|
|
| 697 |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 698 |
{ |
{ |
| 699 |
int64_t index; |
int64_t index; |
| 700 |
|
int ret; |
| 701 |
char sid_str[SID_STR_LEN]; |
char sid_str[SID_STR_LEN]; |
| 702 |
|
|
| 703 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL) |
if (p_section_list_pool == NULL) |
| 704 |
{ |
{ |
| 705 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 706 |
return NULL; |
return NULL; |
| 708 |
|
|
| 709 |
sid_to_str(sid, sid_str); |
sid_to_str(sid, sid_str); |
| 710 |
|
|
| 711 |
if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1) |
ret = trie_dict_get(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, &index); |
| 712 |
|
if (ret < 0) |
| 713 |
{ |
{ |
| 714 |
log_error("trie_dict_get(section, %d) error\n", sid); |
log_error("trie_dict_get(section, %d) error\n", sid); |
| 715 |
return NULL; |
return NULL; |
| 716 |
} |
} |
| 717 |
|
else if (ret == 0) |
| 718 |
|
{ |
| 719 |
|
return NULL; |
| 720 |
|
} |
| 721 |
|
|
| 722 |
return (p_section_list_pool + index); |
return (p_section_list_pool->sections + index); |
| 723 |
} |
} |
| 724 |
|
|
| 725 |
int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src) |
int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src) |
| 1130 |
return 0; |
return 0; |
| 1131 |
} |
} |
| 1132 |
|
|
| 1133 |
|
int32_t article_block_last_aid(void) |
| 1134 |
|
{ |
| 1135 |
|
ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]; |
| 1136 |
|
int32_t last_aid = p_block->articles[p_block->article_count - 1].aid; |
| 1137 |
|
|
| 1138 |
|
return last_aid; |
| 1139 |
|
} |
| 1140 |
|
|
| 1141 |
|
int article_block_article_count(void) |
| 1142 |
|
{ |
| 1143 |
|
int ret; |
| 1144 |
|
|
| 1145 |
|
if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0) |
| 1146 |
|
{ |
| 1147 |
|
return -1; |
| 1148 |
|
} |
| 1149 |
|
|
| 1150 |
|
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
| 1151 |
|
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count; |
| 1152 |
|
|
| 1153 |
|
return ret; |
| 1154 |
|
} |
| 1155 |
|
|
| 1156 |
int article_count_of_topic(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 1157 |
{ |
{ |
| 1158 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1223 |
move_article_count = article_count_of_topic(aid); |
move_article_count = article_count_of_topic(aid); |
| 1224 |
if (move_article_count <= 0) |
if (move_article_count <= 0) |
| 1225 |
{ |
{ |
| 1226 |
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
log_error("article_count_of_topic(aid = %d) <= 0\n", aid); |
| 1227 |
return -2; |
return -2; |
| 1228 |
} |
} |
| 1229 |
|
|
| 1389 |
} |
} |
| 1390 |
else |
else |
| 1391 |
{ |
{ |
| 1392 |
index = (int)(p_section - p_section_list_pool); |
index = (int)(p_section - p_section_list_pool->sections); |
| 1393 |
if (index < 0 || index >= BBS_max_section) |
if (index < 0 || index >= BBS_max_section) |
| 1394 |
{ |
{ |
| 1395 |
log_error("get_section_index(%d) error: index out of range\n", index); |
log_error("get_section_index(%d) error: index out of range\n", index); |
| 1424 |
// Read lock on any specific section will also acquire single read lock on "all section" |
// Read lock on any specific section will also acquire single read lock on "all section" |
| 1425 |
// so that write lock on all section only need to acquire single write on on "all section" |
// so that write lock on all section only need to acquire single write on on "all section" |
| 1426 |
// rather than to acquire multiple write locks on all the available sections. |
// rather than to acquire multiple write locks on all the available sections. |
| 1427 |
if (index == BBS_max_section) |
if (index != BBS_max_section) |
| 1428 |
{ |
{ |
| 1429 |
sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section |
sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section |
| 1430 |
sops[2].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1438 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1439 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1440 |
|
|
| 1441 |
ret = semtimedop(section_list_pool_semid, sops, (index == BBS_max_section ? 4 : 2), &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout); |
| 1442 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1443 |
{ |
{ |
| 1444 |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
| 1475 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1476 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1477 |
|
|
| 1478 |
ret = semtimedop(section_list_pool_semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1479 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1480 |
{ |
{ |
| 1481 |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
| 1500 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1501 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1502 |
|
|
| 1503 |
// The same reason as section_list_try_rd_lock() |
if (index != BBS_max_section) |
|
if (index == BBS_max_section) |
|
| 1504 |
{ |
{ |
| 1505 |
sops[1].sem_num = BBS_max_section * 2; // r_sem of all section |
sops[1].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1506 |
sops[1].sem_op = -1; // unlock |
sops[1].sem_op = -1; // unlock |
| 1507 |
sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1508 |
} |
} |
| 1509 |
|
|
| 1510 |
ret = semop(section_list_pool_semid, sops, (index == BBS_max_section ? 2 : 1)); |
ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2)); |
| 1511 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1512 |
{ |
{ |
| 1513 |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1532 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1533 |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1534 |
|
|
| 1535 |
ret = semop(section_list_pool_semid, sops, 1); |
ret = semop(p_section_list_pool->semid, sops, 1); |
| 1536 |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1537 |
{ |
{ |
| 1538 |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1539 |
} |
} |
| 1540 |
|
|
| 1541 |
return ret; |
return ret; |
| 1542 |
|
} |
| 1543 |
|
|
| 1544 |
|
int section_list_rd_lock(SECTION_LIST *p_section) |
| 1545 |
|
{ |
| 1546 |
|
int timer = 0; |
| 1547 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1548 |
|
int ret = -1; |
| 1549 |
|
|
| 1550 |
|
while (!SYS_server_exit) |
| 1551 |
|
{ |
| 1552 |
|
ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1553 |
|
if (ret == 0) // success |
| 1554 |
|
{ |
| 1555 |
|
break; |
| 1556 |
|
} |
| 1557 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1558 |
|
{ |
| 1559 |
|
timer++; |
| 1560 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1561 |
|
{ |
| 1562 |
|
log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer); |
| 1563 |
|
} |
| 1564 |
|
} |
| 1565 |
|
else // failed |
| 1566 |
|
{ |
| 1567 |
|
log_error("section_list_rd_lock() failed on section %d\n", sid); |
| 1568 |
|
break; |
| 1569 |
|
} |
| 1570 |
|
} |
| 1571 |
|
|
| 1572 |
|
return ret; |
| 1573 |
|
} |
| 1574 |
|
|
| 1575 |
|
int section_list_rw_lock(SECTION_LIST *p_section) |
| 1576 |
|
{ |
| 1577 |
|
int timer = 0; |
| 1578 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1579 |
|
int ret = -1; |
| 1580 |
|
|
| 1581 |
|
while (!SYS_server_exit) |
| 1582 |
|
{ |
| 1583 |
|
ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1584 |
|
if (ret == 0) // success |
| 1585 |
|
{ |
| 1586 |
|
break; |
| 1587 |
|
} |
| 1588 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1589 |
|
{ |
| 1590 |
|
timer++; |
| 1591 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1592 |
|
{ |
| 1593 |
|
log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer); |
| 1594 |
|
} |
| 1595 |
|
} |
| 1596 |
|
else // failed |
| 1597 |
|
{ |
| 1598 |
|
log_error("acquire_section_rw_lock() failed on section %d\n", sid); |
| 1599 |
|
break; |
| 1600 |
|
} |
| 1601 |
|
} |
| 1602 |
|
|
| 1603 |
|
return ret; |
| 1604 |
} |
} |