| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
| 17 |
|
#define _GNU_SOURCE |
| 18 |
|
|
| 19 |
#include "section_list.h" |
#include "section_list.h" |
| 20 |
#include "log.h" |
#include "log.h" |
| 21 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 26 |
#include <stdlib.h> |
#include <stdlib.h> |
| 27 |
#include <errno.h> |
#include <errno.h> |
| 28 |
#include <sys/param.h> |
#include <sys/param.h> |
| 29 |
|
#include <sys/sem.h> |
| 30 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 31 |
#include <sys/ipc.h> |
#include <sys/ipc.h> |
| 32 |
|
|
| 33 |
|
#ifdef _SEM_SEMUN_UNDEFINED |
| 34 |
|
union semun |
| 35 |
|
{ |
| 36 |
|
int val; /* Value for SETVAL */ |
| 37 |
|
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
| 38 |
|
unsigned short *array; /* Array for GETALL, SETALL */ |
| 39 |
|
struct seminfo *__buf; /* Buffer for IPC_INFO |
| 40 |
|
(Linux-specific) */ |
| 41 |
|
}; |
| 42 |
|
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 43 |
|
|
| 44 |
#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 |
| 45 |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
#define ARTICLE_BLOCK_SHM_COUNT_LIMIT 256 // limited by length (8-bit) of proj_id in ftok(path, proj_id) |
| 46 |
#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) |
| 47 |
|
|
| 48 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
| 49 |
|
|
| 50 |
|
#define SID_STR_LEN 5 // 32-bit + NULL |
| 51 |
|
|
| 52 |
struct article_block_t |
struct article_block_t |
| 53 |
{ |
{ |
| 54 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
| 77 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 78 |
|
|
| 79 |
static int section_list_pool_shmid; |
static int section_list_pool_shmid; |
| 80 |
|
static int section_list_pool_semid; |
| 81 |
static SECTION_LIST *p_section_list_pool = NULL; |
static SECTION_LIST *p_section_list_pool = NULL; |
| 82 |
static int section_list_count = 0; |
static int section_list_count = 0; |
| 83 |
static TRIE_NODE *p_trie_dict_section_list = NULL; |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
| 84 |
|
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
| 85 |
|
|
| 86 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 87 |
{ |
{ |
| 315 |
|
|
| 316 |
if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count) |
if (index < 0 || index / ARTICLE_PER_BLOCK >= p_article_block_pool->block_count) |
| 317 |
{ |
{ |
| 318 |
log_error("section_data_find_article_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count); |
log_error("article_block_find_by_index(%d) is out of boundary of block [0, %d)\n", index, p_article_block_pool->block_count); |
| 319 |
return NULL; |
return NULL; |
| 320 |
} |
} |
| 321 |
|
|
| 323 |
|
|
| 324 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
| 325 |
{ |
{ |
| 326 |
log_error("section_data_find_article_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count); |
log_error("article_block_find_by_index(%d) is out of boundary of article [0, %d)\n", index, p_block->article_count); |
| 327 |
return NULL; |
return NULL; |
| 328 |
} |
} |
| 329 |
|
|
| 332 |
|
|
| 333 |
extern int section_list_pool_init(const char *filename) |
extern int section_list_pool_init(const char *filename) |
| 334 |
{ |
{ |
| 335 |
|
int semid; |
| 336 |
int shmid; |
int shmid; |
| 337 |
int proj_id; |
int proj_id; |
| 338 |
key_t key; |
key_t key; |
| 339 |
size_t size; |
size_t size; |
| 340 |
void *p_shm; |
void *p_shm; |
| 341 |
|
union semun arg; |
| 342 |
|
int i; |
| 343 |
|
|
| 344 |
if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL) |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
| 345 |
{ |
{ |
| 346 |
section_list_pool_cleanup(); |
section_list_pool_cleanup(); |
| 347 |
} |
} |
| 361 |
return -3; |
return -3; |
| 362 |
} |
} |
| 363 |
|
|
| 364 |
|
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
| 365 |
|
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 366 |
|
if (semid == -1) |
| 367 |
|
{ |
| 368 |
|
log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno); |
| 369 |
|
return -3; |
| 370 |
|
} |
| 371 |
|
|
| 372 |
|
// Initialize sem value to 0 |
| 373 |
|
arg.val = 0; |
| 374 |
|
for (i = 0; i < size; i++) |
| 375 |
|
{ |
| 376 |
|
if (semctl(semid, i, SETVAL, arg) == -1) |
| 377 |
|
{ |
| 378 |
|
log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno); |
| 379 |
|
return -3; |
| 380 |
|
} |
| 381 |
|
} |
| 382 |
|
|
| 383 |
|
section_list_pool_semid = semid; |
| 384 |
|
|
| 385 |
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
| 386 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 387 |
if (shmid == -1) |
if (shmid == -1) |
| 388 |
{ |
{ |
| 389 |
log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno); |
log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno); |
| 390 |
return -3; |
return -3; |
| 391 |
} |
} |
| 392 |
p_shm = shmat(shmid, NULL, 0); |
p_shm = shmat(shmid, NULL, 0); |
| 400 |
p_section_list_pool = p_shm; |
p_section_list_pool = p_shm; |
| 401 |
section_list_count = 0; |
section_list_count = 0; |
| 402 |
|
|
| 403 |
p_trie_dict_section_list = trie_dict_create(); |
p_trie_dict_section_by_name = trie_dict_create(); |
| 404 |
if (p_trie_dict_section_list == NULL) |
if (p_trie_dict_section_by_name == NULL) |
| 405 |
|
{ |
| 406 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 407 |
|
return -2; |
| 408 |
|
} |
| 409 |
|
|
| 410 |
|
p_trie_dict_section_by_sid = trie_dict_create(); |
| 411 |
|
if (p_trie_dict_section_by_sid == NULL) |
| 412 |
{ |
{ |
| 413 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 414 |
return -2; |
return -2; |
| 417 |
return 0; |
return 0; |
| 418 |
} |
} |
| 419 |
|
|
| 420 |
|
inline static void sid_to_str(int32_t sid, char *p_sid_str) |
| 421 |
|
{ |
| 422 |
|
uint32_t u_sid; |
| 423 |
|
int i; |
| 424 |
|
|
| 425 |
|
u_sid = (uint32_t)sid; |
| 426 |
|
for (i = 0; i < SID_STR_LEN - 1; i++) |
| 427 |
|
{ |
| 428 |
|
p_sid_str[i] = (char)(u_sid % 255 + 1); |
| 429 |
|
u_sid /= 255; |
| 430 |
|
} |
| 431 |
|
p_sid_str[i] = '\0'; |
| 432 |
|
} |
| 433 |
|
|
| 434 |
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
| 435 |
{ |
{ |
| 436 |
SECTION_LIST *p_section; |
SECTION_LIST *p_section; |
| 437 |
|
char sid_str[SID_STR_LEN]; |
| 438 |
|
|
| 439 |
if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL) |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
| 440 |
{ |
{ |
| 441 |
log_error("session_list_pool not initialized\n"); |
log_error("session_list_pool not initialized\n"); |
| 442 |
return NULL; |
return NULL; |
| 448 |
return NULL; |
return NULL; |
| 449 |
} |
} |
| 450 |
|
|
| 451 |
|
sid_to_str(sid, sid_str); |
| 452 |
|
|
| 453 |
p_section = p_section_list_pool + section_list_count; |
p_section = p_section_list_pool + section_list_count; |
| 454 |
|
|
| 455 |
p_section->sid = sid; |
p_section->sid = sid; |
| 463 |
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)); |
| 464 |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
| 465 |
|
|
| 466 |
if (trie_dict_set(p_trie_dict_section_list, sname, section_list_count) != 1) |
if (trie_dict_set(p_trie_dict_section_by_name, sname, section_list_count) != 1) |
| 467 |
{ |
{ |
| 468 |
log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count); |
log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count); |
| 469 |
|
return NULL; |
| 470 |
|
} |
| 471 |
|
|
| 472 |
|
if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1) |
| 473 |
|
{ |
| 474 |
|
log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count); |
| 475 |
|
log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]); |
| 476 |
return NULL; |
return NULL; |
| 477 |
} |
} |
| 478 |
|
|
| 498 |
|
|
| 499 |
void section_list_pool_cleanup(void) |
void section_list_pool_cleanup(void) |
| 500 |
{ |
{ |
| 501 |
if (p_trie_dict_section_list != NULL) |
if (p_trie_dict_section_by_name != NULL) |
| 502 |
{ |
{ |
| 503 |
trie_dict_destroy(p_trie_dict_section_list); |
trie_dict_destroy(p_trie_dict_section_by_name); |
| 504 |
p_trie_dict_section_list = NULL; |
p_trie_dict_section_by_name = NULL; |
| 505 |
|
} |
| 506 |
|
|
| 507 |
|
if (p_trie_dict_section_by_sid != NULL) |
| 508 |
|
{ |
| 509 |
|
trie_dict_destroy(p_trie_dict_section_by_sid); |
| 510 |
|
p_trie_dict_section_by_sid = NULL; |
| 511 |
} |
} |
| 512 |
|
|
| 513 |
if (p_section_list_pool != NULL) |
if (p_section_list_pool != NULL) |
| 522 |
{ |
{ |
| 523 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
| 524 |
} |
} |
| 525 |
|
|
| 526 |
|
if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1) |
| 527 |
|
{ |
| 528 |
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno); |
| 529 |
|
} |
| 530 |
} |
} |
| 531 |
|
|
| 532 |
section_list_count = 0; |
section_list_count = 0; |
| 536 |
{ |
{ |
| 537 |
int64_t index; |
int64_t index; |
| 538 |
|
|
| 539 |
if (p_section_list_pool == NULL || p_trie_dict_section_list == NULL) |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL) |
| 540 |
{ |
{ |
| 541 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 542 |
return NULL; |
return NULL; |
| 543 |
} |
} |
| 544 |
|
|
| 545 |
if (trie_dict_get(p_trie_dict_section_list, sname, &index) != 1) |
if (trie_dict_get(p_trie_dict_section_by_name, sname, &index) != 1) |
| 546 |
{ |
{ |
| 547 |
log_error("trie_dict_get(section_data, %s) error\n", sname); |
log_error("trie_dict_get(section, %s) error\n", sname); |
| 548 |
|
return NULL; |
| 549 |
|
} |
| 550 |
|
|
| 551 |
|
return (p_section_list_pool + index); |
| 552 |
|
} |
| 553 |
|
|
| 554 |
|
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 555 |
|
{ |
| 556 |
|
int64_t index; |
| 557 |
|
char sid_str[SID_STR_LEN]; |
| 558 |
|
|
| 559 |
|
if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL) |
| 560 |
|
{ |
| 561 |
|
log_error("section_list not initialized\n"); |
| 562 |
|
return NULL; |
| 563 |
|
} |
| 564 |
|
|
| 565 |
|
sid_to_str(sid, sid_str); |
| 566 |
|
|
| 567 |
|
if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1) |
| 568 |
|
{ |
| 569 |
|
log_error("trie_dict_get(section, %d) error\n", sid); |
| 570 |
return NULL; |
return NULL; |
| 571 |
} |
} |
| 572 |
|
|
| 593 |
return -1; |
return -1; |
| 594 |
} |
} |
| 595 |
|
|
| 596 |
|
if (p_section->sid != p_article_src->sid) |
| 597 |
|
{ |
| 598 |
|
log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid); |
| 599 |
|
return -2; |
| 600 |
|
} |
| 601 |
|
|
| 602 |
if (p_section->article_count >= BBS_article_limit_per_section) |
if (p_section->article_count >= BBS_article_limit_per_section) |
| 603 |
{ |
{ |
| 604 |
log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid); |
log_error("section_list_append_article() error: article_count reach limit in section %d\n", p_section->sid); |
| 631 |
// AID of articles should be strictly ascending |
// AID of articles should be strictly ascending |
| 632 |
if (p_article_src->aid <= last_aid) |
if (p_article_src->aid <= last_aid) |
| 633 |
{ |
{ |
| 634 |
log_error("section_data_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid); |
log_error("section_list_append_article(aid=%d) error: last_aid=%d\n", p_article_src->aid, last_aid); |
| 635 |
return -3; |
return -3; |
| 636 |
} |
} |
| 637 |
|
|
| 729 |
return -1; // Not found |
return -1; // Not found |
| 730 |
} |
} |
| 731 |
|
|
| 732 |
|
if (p_section->sid != p_article->sid) |
| 733 |
|
{ |
| 734 |
|
log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 735 |
|
return -2; |
| 736 |
|
} |
| 737 |
|
|
| 738 |
if (p_article->visible == visible) |
if (p_article->visible == visible) |
| 739 |
{ |
{ |
| 740 |
return 0; // Already set |
return 0; // Already set |
| 901 |
|
|
| 902 |
if (start_aid > 0) |
if (start_aid > 0) |
| 903 |
{ |
{ |
| 904 |
|
p_article = article_block_find_by_aid(start_aid); |
| 905 |
|
if (p_article == NULL) |
| 906 |
|
{ |
| 907 |
|
return -1; // Not found |
| 908 |
|
} |
| 909 |
|
|
| 910 |
|
if (p_section->sid != p_article->sid) |
| 911 |
|
{ |
| 912 |
|
log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid); |
| 913 |
|
return -2; |
| 914 |
|
} |
| 915 |
|
|
| 916 |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next); |
p_article = section_list_find_article_with_offset(p_section, start_aid, &page, &offset, &p_next); |
| 917 |
if (p_article == NULL) |
if (p_article == NULL) |
| 918 |
{ |
{ |
| 981 |
return 0; |
return 0; |
| 982 |
} |
} |
| 983 |
|
|
| 984 |
int section_list_count_of_topic_articles(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 985 |
{ |
{ |
| 986 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 987 |
int article_count; |
int article_count; |
| 996 |
|
|
| 997 |
do |
do |
| 998 |
{ |
{ |
| 999 |
|
if (p_article->tid != 0 && p_article->tid != aid) |
| 1000 |
|
{ |
| 1001 |
|
log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid); |
| 1002 |
|
break; |
| 1003 |
|
} |
| 1004 |
|
|
| 1005 |
article_count++; |
article_count++; |
| 1006 |
p_article = p_article->p_topic_next; |
p_article = p_article->p_topic_next; |
| 1007 |
} while (p_article->aid != aid); |
} while (p_article->aid != aid); |
| 1027 |
return -1; |
return -1; |
| 1028 |
} |
} |
| 1029 |
|
|
| 1030 |
if ((p_article = section_list_find_article_with_offset(p_section_src, aid, &page, &offset, &p_next)) == NULL) |
if ((p_article = article_block_find_by_aid(aid)) == NULL) |
| 1031 |
|
{ |
| 1032 |
|
log_error("section_list_move_topic() error: article %d not found in block\n", aid); |
| 1033 |
|
return -2; |
| 1034 |
|
} |
| 1035 |
|
|
| 1036 |
|
if (p_section_src->sid != p_article->sid) |
| 1037 |
{ |
{ |
| 1038 |
log_error("section_list_move_topic() error: article %d not found in section %d\n", aid, p_section_src->sid); |
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
| 1039 |
|
p_section_src->sid, p_article->aid, p_article->sid); |
| 1040 |
return -2; |
return -2; |
| 1041 |
} |
} |
| 1042 |
|
|
| 1048 |
|
|
| 1049 |
last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid); |
last_unaffected_aid_src = (p_article == p_section_src->p_article_head ? 0 : p_article->p_prior->aid); |
| 1050 |
|
|
| 1051 |
move_article_count = section_list_count_of_topic_articles(aid); |
move_article_count = article_count_of_topic(aid); |
| 1052 |
if (move_article_count <= 0) |
if (move_article_count <= 0) |
| 1053 |
{ |
{ |
| 1054 |
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
| 1068 |
|
|
| 1069 |
do |
do |
| 1070 |
{ |
{ |
| 1071 |
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
if (p_section_src->sid != p_article->sid) |
| 1072 |
{ |
{ |
| 1073 |
log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
| 1074 |
return -4; |
p_section_src->sid, p_article->aid, p_article->sid); |
| 1075 |
|
return -2; |
| 1076 |
} |
} |
| 1077 |
|
|
| 1078 |
// Remove from bi-directional article list of src section |
// Remove from bi-directional article list of src section |
| 1093 |
p_article->p_prior->p_next = p_article->p_next; |
p_article->p_prior->p_next = p_article->p_next; |
| 1094 |
p_article->p_next->p_prior = p_article->p_prior; |
p_article->p_next->p_prior = p_article->p_prior; |
| 1095 |
|
|
| 1096 |
|
// Update sid of article |
| 1097 |
|
p_article->sid = p_section_dest->sid; |
| 1098 |
|
|
| 1099 |
|
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
| 1100 |
|
{ |
| 1101 |
|
log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
| 1102 |
|
return -4; |
| 1103 |
|
} |
| 1104 |
|
|
| 1105 |
// Insert into bi-directional article list of dest section |
// Insert into bi-directional article list of dest section |
| 1106 |
if (p_next == NULL) // empty section |
if (p_next == NULL) // empty section |
| 1107 |
{ |
{ |
| 1167 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1168 |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
| 1169 |
{ |
{ |
| 1170 |
log_error("section_list_calculate_page(section = %d, aid = %d) error\n", |
log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n", |
| 1171 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1172 |
} |
} |
| 1173 |
|
|
| 1184 |
// Re-calculate pages of src section |
// Re-calculate pages of src section |
| 1185 |
if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0) |
if (section_list_calculate_page(p_section_src, last_unaffected_aid_src) < 0) |
| 1186 |
{ |
{ |
| 1187 |
log_error("section_list_calculate_page(section = %d, aid = %d) error at aid = %d\n", |
log_error("section_list_calculate_page(src section = %d, aid = %d) error at aid = %d\n", |
| 1188 |
p_section_src->sid, last_unaffected_aid_src, aid); |
p_section_src->sid, last_unaffected_aid_src, aid); |
| 1189 |
} |
} |
| 1190 |
|
|
| 1193 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1194 |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
if (section_list_calculate_page(p_section_dest, first_inserted_aid_dest) < 0) |
| 1195 |
{ |
{ |
| 1196 |
log_error("section_list_calculate_page(section = %d, aid = %d) error\n", |
log_error("section_list_calculate_page(dest section = %d, aid = %d) error\n", |
| 1197 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1198 |
} |
} |
| 1199 |
} |
} |
| 1200 |
|
|
| 1201 |
return move_article_count; |
return move_article_count; |
| 1202 |
} |
} |
| 1203 |
|
|
| 1204 |
|
int get_section_index(SECTION_LIST *p_section) |
| 1205 |
|
{ |
| 1206 |
|
int index; |
| 1207 |
|
|
| 1208 |
|
if (p_section_list_pool == NULL) |
| 1209 |
|
{ |
| 1210 |
|
log_error("get_section_index() error: uninitialized\n"); |
| 1211 |
|
return -1; |
| 1212 |
|
} |
| 1213 |
|
|
| 1214 |
|
if (p_section == NULL) |
| 1215 |
|
{ |
| 1216 |
|
index = BBS_max_section; |
| 1217 |
|
} |
| 1218 |
|
else |
| 1219 |
|
{ |
| 1220 |
|
index = (int)(p_section - p_section_list_pool); |
| 1221 |
|
if (index < 0 || index >= BBS_max_section) |
| 1222 |
|
{ |
| 1223 |
|
log_error("get_section_index(%d) error: index out of range\n", index); |
| 1224 |
|
return -2; |
| 1225 |
|
} |
| 1226 |
|
} |
| 1227 |
|
|
| 1228 |
|
return index; |
| 1229 |
|
} |
| 1230 |
|
|
| 1231 |
|
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1232 |
|
{ |
| 1233 |
|
int index; |
| 1234 |
|
struct sembuf sops[4]; |
| 1235 |
|
struct timespec timeout; |
| 1236 |
|
int ret; |
| 1237 |
|
|
| 1238 |
|
index = get_section_index(p_section); |
| 1239 |
|
if (index < 0) |
| 1240 |
|
{ |
| 1241 |
|
return -2; |
| 1242 |
|
} |
| 1243 |
|
|
| 1244 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1245 |
|
sops[0].sem_op = 0; // wait until unlocked |
| 1246 |
|
sops[0].sem_flg = 0; |
| 1247 |
|
|
| 1248 |
|
sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1249 |
|
sops[1].sem_op = 1; // lock |
| 1250 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1251 |
|
|
| 1252 |
|
// Read lock on any specific section will also acquire single read lock on "all section" |
| 1253 |
|
// so that write lock on all section only need to acquire single write on on "all section" |
| 1254 |
|
// rather than to acquire multiple write locks on all the available sections. |
| 1255 |
|
if (index == BBS_max_section) |
| 1256 |
|
{ |
| 1257 |
|
sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section |
| 1258 |
|
sops[2].sem_op = 0; // wait until unlocked |
| 1259 |
|
sops[2].sem_flg = 0; |
| 1260 |
|
|
| 1261 |
|
sops[3].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1262 |
|
sops[3].sem_op = 1; // lock |
| 1263 |
|
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1264 |
|
} |
| 1265 |
|
|
| 1266 |
|
timeout.tv_sec = wait_sec; |
| 1267 |
|
timeout.tv_nsec = 0; |
| 1268 |
|
|
| 1269 |
|
ret = semtimedop(section_list_pool_semid, sops, (index == BBS_max_section ? 4 : 2), &timeout); |
| 1270 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1271 |
|
{ |
| 1272 |
|
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
| 1273 |
|
} |
| 1274 |
|
|
| 1275 |
|
return ret; |
| 1276 |
|
} |
| 1277 |
|
|
| 1278 |
|
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
| 1279 |
|
{ |
| 1280 |
|
int index; |
| 1281 |
|
struct sembuf sops[3]; |
| 1282 |
|
struct timespec timeout; |
| 1283 |
|
int ret; |
| 1284 |
|
|
| 1285 |
|
index = get_section_index(p_section); |
| 1286 |
|
if (index < 0) |
| 1287 |
|
{ |
| 1288 |
|
return -2; |
| 1289 |
|
} |
| 1290 |
|
|
| 1291 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1292 |
|
sops[0].sem_op = 0; // wait until unlocked |
| 1293 |
|
sops[0].sem_flg = 0; |
| 1294 |
|
|
| 1295 |
|
sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1296 |
|
sops[1].sem_op = 1; // lock |
| 1297 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1298 |
|
|
| 1299 |
|
sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1300 |
|
sops[2].sem_op = 0; // wait until unlocked |
| 1301 |
|
sops[2].sem_flg = 0; |
| 1302 |
|
|
| 1303 |
|
timeout.tv_sec = wait_sec; |
| 1304 |
|
timeout.tv_nsec = 0; |
| 1305 |
|
|
| 1306 |
|
ret = semtimedop(section_list_pool_semid, sops, 3, &timeout); |
| 1307 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1308 |
|
{ |
| 1309 |
|
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
| 1310 |
|
} |
| 1311 |
|
|
| 1312 |
|
return ret; |
| 1313 |
|
} |
| 1314 |
|
|
| 1315 |
|
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1316 |
|
{ |
| 1317 |
|
int index; |
| 1318 |
|
struct sembuf sops[2]; |
| 1319 |
|
int ret; |
| 1320 |
|
|
| 1321 |
|
index = get_section_index(p_section); |
| 1322 |
|
if (index < 0) |
| 1323 |
|
{ |
| 1324 |
|
return -2; |
| 1325 |
|
} |
| 1326 |
|
|
| 1327 |
|
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1328 |
|
sops[0].sem_op = -1; // unlock |
| 1329 |
|
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1330 |
|
|
| 1331 |
|
// The same reason as section_list_try_rd_lock() |
| 1332 |
|
if (index == BBS_max_section) |
| 1333 |
|
{ |
| 1334 |
|
sops[1].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1335 |
|
sops[1].sem_op = -1; // unlock |
| 1336 |
|
sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1337 |
|
} |
| 1338 |
|
|
| 1339 |
|
ret = semop(section_list_pool_semid, sops, (index == BBS_max_section ? 2 : 1)); |
| 1340 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1341 |
|
{ |
| 1342 |
|
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1343 |
|
} |
| 1344 |
|
|
| 1345 |
|
return ret; |
| 1346 |
|
} |
| 1347 |
|
|
| 1348 |
|
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1349 |
|
{ |
| 1350 |
|
int index; |
| 1351 |
|
struct sembuf sops[1]; |
| 1352 |
|
int ret; |
| 1353 |
|
|
| 1354 |
|
index = get_section_index(p_section); |
| 1355 |
|
if (index < 0) |
| 1356 |
|
{ |
| 1357 |
|
return -2; |
| 1358 |
|
} |
| 1359 |
|
|
| 1360 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1361 |
|
sops[0].sem_op = -1; // unlock |
| 1362 |
|
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1363 |
|
|
| 1364 |
|
ret = semop(section_list_pool_semid, sops, 1); |
| 1365 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1366 |
|
{ |
| 1367 |
|
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1368 |
|
} |
| 1369 |
|
|
| 1370 |
|
return ret; |
| 1371 |
|
} |