| 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 |
|
|
| 36 |
|
|
| 37 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
| 38 |
|
|
| 39 |
|
#define SID_STR_LEN 5 // 32-bit + NULL |
| 40 |
|
|
| 41 |
struct article_block_t |
struct article_block_t |
| 42 |
{ |
{ |
| 43 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
| 65 |
|
|
| 66 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 67 |
|
|
| 68 |
|
static int section_list_pool_shmid; |
| 69 |
|
static int section_list_pool_semid; |
| 70 |
static SECTION_LIST *p_section_list_pool = NULL; |
static SECTION_LIST *p_section_list_pool = NULL; |
| 71 |
static int section_list_count = 0; |
static int section_list_count = 0; |
| 72 |
static TRIE_NODE *p_trie_dict_section_list = NULL; |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
| 73 |
|
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
| 74 |
|
|
| 75 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 76 |
{ |
{ |
| 304 |
|
|
| 305 |
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) |
| 306 |
{ |
{ |
| 307 |
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); |
| 308 |
return NULL; |
return NULL; |
| 309 |
} |
} |
| 310 |
|
|
| 312 |
|
|
| 313 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
| 314 |
{ |
{ |
| 315 |
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); |
| 316 |
return NULL; |
return NULL; |
| 317 |
} |
} |
| 318 |
|
|
| 319 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
| 320 |
} |
} |
| 321 |
|
|
| 322 |
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
extern int section_list_pool_init(const char *filename) |
| 323 |
{ |
{ |
| 324 |
SECTION_LIST *p_section; |
int semid; |
| 325 |
|
int shmid; |
| 326 |
|
int proj_id; |
| 327 |
|
key_t key; |
| 328 |
|
size_t size; |
| 329 |
|
void *p_shm; |
| 330 |
|
|
| 331 |
|
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
| 332 |
|
{ |
| 333 |
|
section_list_pool_cleanup(); |
| 334 |
|
} |
| 335 |
|
|
| 336 |
|
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
| 337 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL) |
| 338 |
{ |
{ |
| 339 |
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
| 340 |
if (p_section_list_pool == NULL) |
return -1; |
| 341 |
{ |
} |
|
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
|
|
return NULL; |
|
|
} |
|
| 342 |
|
|
| 343 |
section_list_count = 0; |
proj_id = (int)(time(NULL) % getpid()); |
| 344 |
|
key = ftok(filename, proj_id); |
| 345 |
|
if (key == -1) |
| 346 |
|
{ |
| 347 |
|
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 348 |
|
return -3; |
| 349 |
} |
} |
| 350 |
|
|
| 351 |
if (p_trie_dict_section_list == NULL) |
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
| 352 |
|
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 353 |
|
if (semid == -1) |
| 354 |
{ |
{ |
| 355 |
p_trie_dict_section_list = trie_dict_create(); |
log_error("semget(section_list_pool, size = %d) error (%d)\n", size, errno); |
| 356 |
if (p_trie_dict_section_list == NULL) |
return -3; |
| 357 |
{ |
} |
| 358 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
|
| 359 |
return NULL; |
section_list_pool_semid = semid; |
| 360 |
} |
|
| 361 |
|
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
| 362 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 363 |
|
if (shmid == -1) |
| 364 |
|
{ |
| 365 |
|
log_error("shmget(section_list_pool, size = %d) error (%d)\n", size, errno); |
| 366 |
|
return -3; |
| 367 |
|
} |
| 368 |
|
p_shm = shmat(shmid, NULL, 0); |
| 369 |
|
if (p_shm == (void *)-1) |
| 370 |
|
{ |
| 371 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 372 |
|
return -3; |
| 373 |
|
} |
| 374 |
|
|
| 375 |
|
section_list_pool_shmid = shmid; |
| 376 |
|
p_section_list_pool = p_shm; |
| 377 |
|
section_list_count = 0; |
| 378 |
|
|
| 379 |
|
p_trie_dict_section_by_name = trie_dict_create(); |
| 380 |
|
if (p_trie_dict_section_by_name == NULL) |
| 381 |
|
{ |
| 382 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 383 |
|
return -2; |
| 384 |
|
} |
| 385 |
|
|
| 386 |
|
p_trie_dict_section_by_sid = trie_dict_create(); |
| 387 |
|
if (p_trie_dict_section_by_sid == NULL) |
| 388 |
|
{ |
| 389 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 390 |
|
return -2; |
| 391 |
|
} |
| 392 |
|
|
| 393 |
|
return 0; |
| 394 |
|
} |
| 395 |
|
|
| 396 |
|
inline static void sid_to_str(int32_t sid, char *p_sid_str) |
| 397 |
|
{ |
| 398 |
|
uint32_t u_sid; |
| 399 |
|
int i; |
| 400 |
|
|
| 401 |
|
u_sid = (uint32_t)sid; |
| 402 |
|
for (i = 0; i < SID_STR_LEN - 1; i++) |
| 403 |
|
{ |
| 404 |
|
p_sid_str[i] = (char)(u_sid % 255 + 1); |
| 405 |
|
u_sid /= 255; |
| 406 |
|
} |
| 407 |
|
p_sid_str[i] = '\0'; |
| 408 |
|
} |
| 409 |
|
|
| 410 |
|
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
| 411 |
|
{ |
| 412 |
|
SECTION_LIST *p_section; |
| 413 |
|
char sid_str[SID_STR_LEN]; |
| 414 |
|
|
| 415 |
|
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
| 416 |
|
{ |
| 417 |
|
log_error("session_list_pool not initialized\n"); |
| 418 |
|
return NULL; |
| 419 |
} |
} |
| 420 |
|
|
| 421 |
if (section_list_count >= BBS_max_section) |
if (section_list_count >= BBS_max_section) |
| 422 |
{ |
{ |
| 423 |
log_error("section_list_count exceed limit %d\n", BBS_max_section); |
log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section); |
| 424 |
return NULL; |
return NULL; |
| 425 |
} |
} |
| 426 |
|
|
| 427 |
|
sid_to_str(sid, sid_str); |
| 428 |
|
|
| 429 |
p_section = p_section_list_pool + section_list_count; |
p_section = p_section_list_pool + section_list_count; |
| 430 |
|
|
| 431 |
p_section->sid = sid; |
p_section->sid = sid; |
| 439 |
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)); |
| 440 |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
| 441 |
|
|
| 442 |
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) |
| 443 |
{ |
{ |
| 444 |
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); |
| 445 |
|
return NULL; |
| 446 |
|
} |
| 447 |
|
|
| 448 |
|
if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1) |
| 449 |
|
{ |
| 450 |
|
log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count); |
| 451 |
|
log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]); |
| 452 |
return NULL; |
return NULL; |
| 453 |
} |
} |
| 454 |
|
|
| 472 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 473 |
} |
} |
| 474 |
|
|
| 475 |
void section_list_cleanup(void) |
void section_list_pool_cleanup(void) |
| 476 |
{ |
{ |
| 477 |
if (p_trie_dict_section_list != NULL) |
if (p_trie_dict_section_by_name != NULL) |
| 478 |
|
{ |
| 479 |
|
trie_dict_destroy(p_trie_dict_section_by_name); |
| 480 |
|
p_trie_dict_section_by_name = NULL; |
| 481 |
|
} |
| 482 |
|
|
| 483 |
|
if (p_trie_dict_section_by_sid != NULL) |
| 484 |
{ |
{ |
| 485 |
trie_dict_destroy(p_trie_dict_section_list); |
trie_dict_destroy(p_trie_dict_section_by_sid); |
| 486 |
p_trie_dict_section_list = NULL; |
p_trie_dict_section_by_sid = NULL; |
| 487 |
} |
} |
| 488 |
|
|
| 489 |
if (p_section_list_pool != NULL) |
if (p_section_list_pool != NULL) |
| 490 |
{ |
{ |
| 491 |
free(p_section_list_pool); |
if (shmdt(p_section_list_pool) == -1) |
| 492 |
|
{ |
| 493 |
|
log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno); |
| 494 |
|
} |
| 495 |
p_section_list_pool = NULL; |
p_section_list_pool = NULL; |
| 496 |
|
|
| 497 |
|
if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1) |
| 498 |
|
{ |
| 499 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
| 500 |
|
} |
| 501 |
|
|
| 502 |
|
if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1) |
| 503 |
|
{ |
| 504 |
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno); |
| 505 |
|
} |
| 506 |
} |
} |
| 507 |
|
|
| 508 |
section_list_count = 0; |
section_list_count = 0; |
| 512 |
{ |
{ |
| 513 |
int64_t index; |
int64_t index; |
| 514 |
|
|
| 515 |
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) |
| 516 |
{ |
{ |
| 517 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 518 |
return NULL; |
return NULL; |
| 519 |
} |
} |
| 520 |
|
|
| 521 |
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) |
| 522 |
{ |
{ |
| 523 |
log_error("trie_dict_get(section_data, %s) error\n", sname); |
log_error("trie_dict_get(section, %s) error\n", sname); |
| 524 |
|
return NULL; |
| 525 |
|
} |
| 526 |
|
|
| 527 |
|
return (p_section_list_pool + index); |
| 528 |
|
} |
| 529 |
|
|
| 530 |
|
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 531 |
|
{ |
| 532 |
|
int64_t index; |
| 533 |
|
char sid_str[SID_STR_LEN]; |
| 534 |
|
|
| 535 |
|
if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL) |
| 536 |
|
{ |
| 537 |
|
log_error("section_list not initialized\n"); |
| 538 |
|
return NULL; |
| 539 |
|
} |
| 540 |
|
|
| 541 |
|
sid_to_str(sid, sid_str); |
| 542 |
|
|
| 543 |
|
if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1) |
| 544 |
|
{ |
| 545 |
|
log_error("trie_dict_get(section, %d) error\n", sid); |
| 546 |
return NULL; |
return NULL; |
| 547 |
} |
} |
| 548 |
|
|
| 569 |
return -1; |
return -1; |
| 570 |
} |
} |
| 571 |
|
|
| 572 |
|
if (p_section->sid != p_article_src->sid) |
| 573 |
|
{ |
| 574 |
|
log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid); |
| 575 |
|
return -2; |
| 576 |
|
} |
| 577 |
|
|
| 578 |
if (p_section->article_count >= BBS_article_limit_per_section) |
if (p_section->article_count >= BBS_article_limit_per_section) |
| 579 |
{ |
{ |
| 580 |
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); |
| 607 |
// AID of articles should be strictly ascending |
// AID of articles should be strictly ascending |
| 608 |
if (p_article_src->aid <= last_aid) |
if (p_article_src->aid <= last_aid) |
| 609 |
{ |
{ |
| 610 |
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); |
| 611 |
return -3; |
return -3; |
| 612 |
} |
} |
| 613 |
|
|
| 705 |
return -1; // Not found |
return -1; // Not found |
| 706 |
} |
} |
| 707 |
|
|
| 708 |
|
if (p_section->sid != p_article->sid) |
| 709 |
|
{ |
| 710 |
|
log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 711 |
|
return -2; |
| 712 |
|
} |
| 713 |
|
|
| 714 |
if (p_article->visible == visible) |
if (p_article->visible == visible) |
| 715 |
{ |
{ |
| 716 |
return 0; // Already set |
return 0; // Already set |
| 877 |
|
|
| 878 |
if (start_aid > 0) |
if (start_aid > 0) |
| 879 |
{ |
{ |
| 880 |
|
p_article = article_block_find_by_aid(start_aid); |
| 881 |
|
if (p_article == NULL) |
| 882 |
|
{ |
| 883 |
|
return -1; // Not found |
| 884 |
|
} |
| 885 |
|
|
| 886 |
|
if (p_section->sid != p_article->sid) |
| 887 |
|
{ |
| 888 |
|
log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid); |
| 889 |
|
return -2; |
| 890 |
|
} |
| 891 |
|
|
| 892 |
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); |
| 893 |
if (p_article == NULL) |
if (p_article == NULL) |
| 894 |
{ |
{ |
| 957 |
return 0; |
return 0; |
| 958 |
} |
} |
| 959 |
|
|
| 960 |
int section_list_count_of_topic_articles(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 961 |
{ |
{ |
| 962 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 963 |
int article_count; |
int article_count; |
| 972 |
|
|
| 973 |
do |
do |
| 974 |
{ |
{ |
| 975 |
|
if (p_article->tid != 0 && p_article->tid != aid) |
| 976 |
|
{ |
| 977 |
|
log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid); |
| 978 |
|
break; |
| 979 |
|
} |
| 980 |
|
|
| 981 |
article_count++; |
article_count++; |
| 982 |
p_article = p_article->p_topic_next; |
p_article = p_article->p_topic_next; |
| 983 |
} while (p_article->aid != aid); |
} while (p_article->aid != aid); |
| 1003 |
return -1; |
return -1; |
| 1004 |
} |
} |
| 1005 |
|
|
| 1006 |
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) |
| 1007 |
|
{ |
| 1008 |
|
log_error("section_list_move_topic() error: article %d not found in block\n", aid); |
| 1009 |
|
return -2; |
| 1010 |
|
} |
| 1011 |
|
|
| 1012 |
|
if (p_section_src->sid != p_article->sid) |
| 1013 |
{ |
{ |
| 1014 |
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", |
| 1015 |
|
p_section_src->sid, p_article->aid, p_article->sid); |
| 1016 |
return -2; |
return -2; |
| 1017 |
} |
} |
| 1018 |
|
|
| 1024 |
|
|
| 1025 |
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); |
| 1026 |
|
|
| 1027 |
move_article_count = section_list_count_of_topic_articles(aid); |
move_article_count = article_count_of_topic(aid); |
| 1028 |
if (move_article_count <= 0) |
if (move_article_count <= 0) |
| 1029 |
{ |
{ |
| 1030 |
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); |
| 1044 |
|
|
| 1045 |
do |
do |
| 1046 |
{ |
{ |
| 1047 |
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) |
| 1048 |
{ |
{ |
| 1049 |
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", |
| 1050 |
return -4; |
p_section_src->sid, p_article->aid, p_article->sid); |
| 1051 |
|
return -2; |
| 1052 |
} |
} |
| 1053 |
|
|
| 1054 |
// Remove from bi-directional article list of src section |
// Remove from bi-directional article list of src section |
| 1069 |
p_article->p_prior->p_next = p_article->p_next; |
p_article->p_prior->p_next = p_article->p_next; |
| 1070 |
p_article->p_next->p_prior = p_article->p_prior; |
p_article->p_next->p_prior = p_article->p_prior; |
| 1071 |
|
|
| 1072 |
|
// Update sid of article |
| 1073 |
|
p_article->sid = p_section_dest->sid; |
| 1074 |
|
|
| 1075 |
|
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
| 1076 |
|
{ |
| 1077 |
|
log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
| 1078 |
|
return -4; |
| 1079 |
|
} |
| 1080 |
|
|
| 1081 |
// Insert into bi-directional article list of dest section |
// Insert into bi-directional article list of dest section |
| 1082 |
if (p_next == NULL) // empty section |
if (p_next == NULL) // empty section |
| 1083 |
{ |
{ |
| 1143 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1144 |
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) |
| 1145 |
{ |
{ |
| 1146 |
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", |
| 1147 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1148 |
} |
} |
| 1149 |
|
|
| 1160 |
// Re-calculate pages of src section |
// Re-calculate pages of src section |
| 1161 |
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) |
| 1162 |
{ |
{ |
| 1163 |
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", |
| 1164 |
p_section_src->sid, last_unaffected_aid_src, aid); |
p_section_src->sid, last_unaffected_aid_src, aid); |
| 1165 |
} |
} |
| 1166 |
|
|
| 1169 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1170 |
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) |
| 1171 |
{ |
{ |
| 1172 |
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", |
| 1173 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1174 |
} |
} |
| 1175 |
} |
} |
| 1176 |
|
|
| 1177 |
return move_article_count; |
return move_article_count; |
| 1178 |
} |
} |
| 1179 |
|
|
| 1180 |
|
int get_section_index(SECTION_LIST *p_section) |
| 1181 |
|
{ |
| 1182 |
|
int index; |
| 1183 |
|
|
| 1184 |
|
if (p_section_list_pool == NULL) |
| 1185 |
|
{ |
| 1186 |
|
log_error("get_section_index() error: uninitialized\n"); |
| 1187 |
|
return -1; |
| 1188 |
|
} |
| 1189 |
|
|
| 1190 |
|
if (p_section == NULL) |
| 1191 |
|
{ |
| 1192 |
|
index = BBS_max_section; |
| 1193 |
|
} |
| 1194 |
|
else |
| 1195 |
|
{ |
| 1196 |
|
index = (int)(p_section - p_section_list_pool); |
| 1197 |
|
if (index < 0 || index >= BBS_max_section) |
| 1198 |
|
{ |
| 1199 |
|
log_error("get_section_index(%d) error: index out of range\n", index); |
| 1200 |
|
return -2; |
| 1201 |
|
} |
| 1202 |
|
} |
| 1203 |
|
|
| 1204 |
|
return index; |
| 1205 |
|
} |
| 1206 |
|
|
| 1207 |
|
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1208 |
|
{ |
| 1209 |
|
int index; |
| 1210 |
|
struct sembuf sops[2]; |
| 1211 |
|
struct timespec timeout; |
| 1212 |
|
int ret; |
| 1213 |
|
|
| 1214 |
|
index = get_section_index(p_section); |
| 1215 |
|
if (index < 0) |
| 1216 |
|
{ |
| 1217 |
|
return -2; |
| 1218 |
|
} |
| 1219 |
|
|
| 1220 |
|
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1221 |
|
sops[0].sem_op = 0; // check if unlocked |
| 1222 |
|
sops[0].sem_flg = 0; |
| 1223 |
|
|
| 1224 |
|
sops[1].sem_num = (unsigned short)index; // r_sem of section index |
| 1225 |
|
sops[1].sem_op = 1; // lock |
| 1226 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1227 |
|
|
| 1228 |
|
timeout.tv_sec = wait_sec; |
| 1229 |
|
timeout.tv_nsec = 0; |
| 1230 |
|
|
| 1231 |
|
ret = semtimedop(section_list_pool_semid, sops, 2, &timeout); |
| 1232 |
|
|
| 1233 |
|
return ret; |
| 1234 |
|
} |
| 1235 |
|
|
| 1236 |
|
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
| 1237 |
|
{ |
| 1238 |
|
int index; |
| 1239 |
|
struct sembuf sops[3]; |
| 1240 |
|
struct timespec timeout; |
| 1241 |
|
int ret; |
| 1242 |
|
|
| 1243 |
|
index = get_section_index(p_section); |
| 1244 |
|
if (index < 0) |
| 1245 |
|
{ |
| 1246 |
|
return -2; |
| 1247 |
|
} |
| 1248 |
|
|
| 1249 |
|
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1250 |
|
sops[0].sem_op = 0; // check if unlocked |
| 1251 |
|
sops[0].sem_flg = 0; |
| 1252 |
|
|
| 1253 |
|
sops[1].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1254 |
|
sops[1].sem_op = 1; // lock |
| 1255 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1256 |
|
|
| 1257 |
|
sops[2].sem_num = (unsigned short)index; // r_sem of section index |
| 1258 |
|
sops[1].sem_op = 0; // wait until unlocked |
| 1259 |
|
sops[1].sem_flg = 0; |
| 1260 |
|
|
| 1261 |
|
timeout.tv_sec = wait_sec; |
| 1262 |
|
timeout.tv_nsec = 0; |
| 1263 |
|
|
| 1264 |
|
ret = semtimedop(section_list_pool_semid, sops, 3, &timeout); |
| 1265 |
|
|
| 1266 |
|
return ret; |
| 1267 |
|
} |
| 1268 |
|
|
| 1269 |
|
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1270 |
|
{ |
| 1271 |
|
int index; |
| 1272 |
|
struct sembuf sops[1]; |
| 1273 |
|
int ret; |
| 1274 |
|
|
| 1275 |
|
index = get_section_index(p_section); |
| 1276 |
|
if (index < 0) |
| 1277 |
|
{ |
| 1278 |
|
return -2; |
| 1279 |
|
} |
| 1280 |
|
|
| 1281 |
|
sops[0].sem_num = (unsigned short)index; // r_sem of section index |
| 1282 |
|
sops[0].sem_op = -1; // unlock |
| 1283 |
|
sops[0].sem_flg = IPC_NOWAIT; // no wait |
| 1284 |
|
|
| 1285 |
|
ret = semop(section_list_pool_semid, sops, 1); |
| 1286 |
|
|
| 1287 |
|
return ret; |
| 1288 |
|
} |
| 1289 |
|
|
| 1290 |
|
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1291 |
|
{ |
| 1292 |
|
int index; |
| 1293 |
|
struct sembuf sops[1]; |
| 1294 |
|
int ret; |
| 1295 |
|
|
| 1296 |
|
index = get_section_index(p_section); |
| 1297 |
|
if (index < 0) |
| 1298 |
|
{ |
| 1299 |
|
return -2; |
| 1300 |
|
} |
| 1301 |
|
|
| 1302 |
|
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
| 1303 |
|
sops[0].sem_op = -1; // unlock |
| 1304 |
|
sops[0].sem_flg = IPC_NOWAIT; // no wait |
| 1305 |
|
|
| 1306 |
|
ret = semop(section_list_pool_semid, sops, 1); |
| 1307 |
|
|
| 1308 |
|
return ret; |
| 1309 |
|
} |