| 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) |
| 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; |
| 511 |
return 0; |
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) |
| 571 |
|
{ |
| 572 |
|
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
| 573 |
|
return -3; |
| 574 |
|
} |
| 575 |
|
|
| 576 |
|
p_section_list_pool = p_shm; |
| 577 |
|
|
| 578 |
|
return 0; |
| 579 |
|
} |
| 580 |
|
|
| 581 |
|
int detach_section_list_shm(void) |
| 582 |
|
{ |
| 583 |
|
if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1) |
| 584 |
|
{ |
| 585 |
|
log_error("shmdt(section_list_pool) error (%d)\n", errno); |
| 586 |
|
return -1; |
| 587 |
|
} |
| 588 |
|
|
| 589 |
|
p_section_list_pool = NULL; |
| 590 |
|
|
| 591 |
|
return 0; |
| 592 |
|
} |
| 593 |
|
|
| 594 |
inline static void sid_to_str(int32_t sid, char *p_sid_str) |
inline static void sid_to_str(int32_t sid, char *p_sid_str) |
| 595 |
{ |
{ |
| 596 |
uint32_t u_sid; |
uint32_t u_sid; |
| 669 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 670 |
} |
} |
| 671 |
|
|
|
void section_list_cleanup(void) |
|
|
{ |
|
|
int shmid; |
|
|
|
|
|
if (p_section_list_pool == NULL) |
|
|
{ |
|
|
return; |
|
|
} |
|
|
|
|
|
if (p_section_list_pool->p_trie_dict_section_by_name != NULL) |
|
|
{ |
|
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name); |
|
|
p_section_list_pool->p_trie_dict_section_by_name = NULL; |
|
|
} |
|
|
|
|
|
if (p_section_list_pool->p_trie_dict_section_by_sid != NULL) |
|
|
{ |
|
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid); |
|
|
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
|
|
} |
|
|
|
|
|
if (p_section_list_pool != NULL) |
|
|
{ |
|
|
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
|
|
{ |
|
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno); |
|
|
} |
|
|
|
|
|
shmid = p_section_list_pool->shmid; |
|
|
|
|
|
if (shmdt(p_section_list_pool) == -1) |
|
|
{ |
|
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
|
|
} |
|
|
|
|
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
|
|
{ |
|
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
|
|
} |
|
|
|
|
|
p_section_list_pool = NULL; |
|
|
} |
|
|
} |
|
|
|
|
| 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) |
if (p_section_list_pool == NULL) |
| 678 |
{ |
{ |
| 680 |
return NULL; |
return NULL; |
| 681 |
} |
} |
| 682 |
|
|
| 683 |
if (trie_dict_get(p_section_list_pool->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->sections + index); |
return (p_section_list_pool->sections + index); |
| 695 |
} |
} |
| 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) |
if (p_section_list_pool == NULL) |
| 708 |
|
|
| 709 |
sid_to_str(sid, sid_str); |
sid_to_str(sid, sid_str); |
| 710 |
|
|
| 711 |
if (trie_dict_get(p_section_list_pool->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->sections + index); |
return (p_section_list_pool->sections + index); |
| 723 |
} |
} |
| 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_count_of_topic(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 1142 |
{ |
{ |
| 1143 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1524 |
} |
} |
| 1525 |
|
|
| 1526 |
return ret; |
return ret; |
| 1527 |
|
} |
| 1528 |
|
|
| 1529 |
|
int section_list_rd_lock(SECTION_LIST *p_section) |
| 1530 |
|
{ |
| 1531 |
|
int timer = 0; |
| 1532 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1533 |
|
int ret = -1; |
| 1534 |
|
|
| 1535 |
|
while (!SYS_server_exit) |
| 1536 |
|
{ |
| 1537 |
|
ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1538 |
|
if (ret == 0) // success |
| 1539 |
|
{ |
| 1540 |
|
break; |
| 1541 |
|
} |
| 1542 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1543 |
|
{ |
| 1544 |
|
timer++; |
| 1545 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1546 |
|
{ |
| 1547 |
|
log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer); |
| 1548 |
|
} |
| 1549 |
|
} |
| 1550 |
|
else // failed |
| 1551 |
|
{ |
| 1552 |
|
log_error("section_list_rd_lock() failed on section %d\n", sid); |
| 1553 |
|
break; |
| 1554 |
|
} |
| 1555 |
|
} |
| 1556 |
|
|
| 1557 |
|
return ret; |
| 1558 |
|
} |
| 1559 |
|
|
| 1560 |
|
int section_list_rw_lock(SECTION_LIST *p_section) |
| 1561 |
|
{ |
| 1562 |
|
int timer = 0; |
| 1563 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1564 |
|
int ret = -1; |
| 1565 |
|
|
| 1566 |
|
while (!SYS_server_exit) |
| 1567 |
|
{ |
| 1568 |
|
ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1569 |
|
if (ret == 0) // success |
| 1570 |
|
{ |
| 1571 |
|
break; |
| 1572 |
|
} |
| 1573 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1574 |
|
{ |
| 1575 |
|
timer++; |
| 1576 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1577 |
|
{ |
| 1578 |
|
log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer); |
| 1579 |
|
} |
| 1580 |
|
} |
| 1581 |
|
else // failed |
| 1582 |
|
{ |
| 1583 |
|
log_error("acquire_section_rw_lock() failed on section %d\n", sid); |
| 1584 |
|
break; |
| 1585 |
|
} |
| 1586 |
|
} |
| 1587 |
|
|
| 1588 |
|
return ret; |
| 1589 |
} |
} |