| 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) |
| 577 |
SECTION_LIST *section_list_find_by_name(const char *sname) |
SECTION_LIST *section_list_find_by_name(const char *sname) |
| 578 |
{ |
{ |
| 579 |
int64_t index; |
int64_t index; |
| 580 |
|
int ret; |
| 581 |
|
|
| 582 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 583 |
{ |
{ |
| 585 |
return NULL; |
return NULL; |
| 586 |
} |
} |
| 587 |
|
|
| 588 |
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); |
| 589 |
|
if (ret < 0) |
| 590 |
{ |
{ |
| 591 |
log_error("trie_dict_get(section, %s) error\n", sname); |
log_error("trie_dict_get(section, %s) error\n", sname); |
| 592 |
return NULL; |
return NULL; |
| 593 |
} |
} |
| 594 |
|
else if (ret == 0) |
| 595 |
|
{ |
| 596 |
|
return NULL; |
| 597 |
|
} |
| 598 |
|
|
| 599 |
return (p_section_list_pool->sections + index); |
return (p_section_list_pool->sections + index); |
| 600 |
} |
} |
| 602 |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 603 |
{ |
{ |
| 604 |
int64_t index; |
int64_t index; |
| 605 |
|
int ret; |
| 606 |
char sid_str[SID_STR_LEN]; |
char sid_str[SID_STR_LEN]; |
| 607 |
|
|
| 608 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 613 |
|
|
| 614 |
sid_to_str(sid, sid_str); |
sid_to_str(sid, sid_str); |
| 615 |
|
|
| 616 |
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); |
| 617 |
|
if (ret < 0) |
| 618 |
{ |
{ |
| 619 |
log_error("trie_dict_get(section, %d) error\n", sid); |
log_error("trie_dict_get(section, %d) error\n", sid); |
| 620 |
return NULL; |
return NULL; |
| 621 |
} |
} |
| 622 |
|
else if (ret == 0) |
| 623 |
|
{ |
| 624 |
|
return NULL; |
| 625 |
|
} |
| 626 |
|
|
| 627 |
return (p_section_list_pool->sections + index); |
return (p_section_list_pool->sections + index); |
| 628 |
} |
} |
| 1035 |
return 0; |
return 0; |
| 1036 |
} |
} |
| 1037 |
|
|
| 1038 |
|
int32_t article_block_last_aid(void) |
| 1039 |
|
{ |
| 1040 |
|
ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]; |
| 1041 |
|
int32_t last_aid = p_block->articles[p_block->article_count - 1].aid; |
| 1042 |
|
|
| 1043 |
|
return last_aid; |
| 1044 |
|
} |
| 1045 |
|
|
| 1046 |
int article_count_of_topic(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 1047 |
{ |
{ |
| 1048 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1429 |
} |
} |
| 1430 |
|
|
| 1431 |
return ret; |
return ret; |
| 1432 |
|
} |
| 1433 |
|
|
| 1434 |
|
int section_list_rd_lock(SECTION_LIST *p_section) |
| 1435 |
|
{ |
| 1436 |
|
int timer = 0; |
| 1437 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1438 |
|
int ret = -1; |
| 1439 |
|
|
| 1440 |
|
while (!SYS_server_exit) |
| 1441 |
|
{ |
| 1442 |
|
ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1443 |
|
if (ret == 0) // success |
| 1444 |
|
{ |
| 1445 |
|
break; |
| 1446 |
|
} |
| 1447 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1448 |
|
{ |
| 1449 |
|
timer++; |
| 1450 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1451 |
|
{ |
| 1452 |
|
log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer); |
| 1453 |
|
} |
| 1454 |
|
} |
| 1455 |
|
else // failed |
| 1456 |
|
{ |
| 1457 |
|
log_error("section_list_rd_lock() failed on section %d\n", sid); |
| 1458 |
|
break; |
| 1459 |
|
} |
| 1460 |
|
} |
| 1461 |
|
|
| 1462 |
|
return ret; |
| 1463 |
|
} |
| 1464 |
|
|
| 1465 |
|
int section_list_rw_lock(SECTION_LIST *p_section) |
| 1466 |
|
{ |
| 1467 |
|
int timer = 0; |
| 1468 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1469 |
|
int ret = -1; |
| 1470 |
|
|
| 1471 |
|
while (!SYS_server_exit) |
| 1472 |
|
{ |
| 1473 |
|
ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1474 |
|
if (ret == 0) // success |
| 1475 |
|
{ |
| 1476 |
|
break; |
| 1477 |
|
} |
| 1478 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1479 |
|
{ |
| 1480 |
|
timer++; |
| 1481 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1482 |
|
{ |
| 1483 |
|
log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer); |
| 1484 |
|
} |
| 1485 |
|
} |
| 1486 |
|
else // failed |
| 1487 |
|
{ |
| 1488 |
|
log_error("acquire_section_rw_lock() failed on section %d\n", sid); |
| 1489 |
|
break; |
| 1490 |
|
} |
| 1491 |
|
} |
| 1492 |
|
|
| 1493 |
|
return ret; |
| 1494 |
} |
} |