| 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 200 // 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]; |
| 74 |
}; |
}; |
| 75 |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
| 76 |
|
|
| 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; |
| 81 |
|
static int section_list_pool_shmid; |
| 82 |
static SECTION_LIST *p_section_list_pool = NULL; |
static SECTION_LIST *p_section_list_pool = NULL; |
| 83 |
|
|
| 84 |
static int section_list_count = 0; |
static int section_list_count = 0; |
| 85 |
static TRIE_NODE *p_trie_dict_section_list = NULL; |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
| 86 |
|
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
| 87 |
|
|
| 88 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 89 |
{ |
{ |
| 109 |
return -2; |
return -2; |
| 110 |
} |
} |
| 111 |
|
|
| 112 |
p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL)); |
// Allocate shared memory |
| 113 |
if (p_article_block_pool == NULL) |
proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm |
| 114 |
|
key = ftok(filename, proj_id); |
| 115 |
|
if (key == -1) |
| 116 |
{ |
{ |
| 117 |
log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n"); |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 118 |
return -2; |
return -3; |
| 119 |
} |
} |
| 120 |
|
|
| 121 |
// Allocate shared memory |
size = sizeof(ARTICLE_BLOCK_POOL); |
| 122 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 123 |
|
if (shmid == -1) |
| 124 |
|
{ |
| 125 |
|
log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno); |
| 126 |
|
return -3; |
| 127 |
|
} |
| 128 |
|
p_shm = shmat(shmid, NULL, 0); |
| 129 |
|
if (p_shm == (void *)-1) |
| 130 |
|
{ |
| 131 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 132 |
|
return -3; |
| 133 |
|
} |
| 134 |
|
|
| 135 |
|
article_block_pool_shmid = shmid; |
| 136 |
|
p_article_block_pool = p_shm; |
| 137 |
|
|
| 138 |
p_article_block_pool->shm_count = 0; |
p_article_block_pool->shm_count = 0; |
| 139 |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
| 140 |
|
|
| 143 |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
| 144 |
block_count -= block_count_in_shm; |
block_count -= block_count_in_shm; |
| 145 |
|
|
| 146 |
proj_id = getpid() + p_article_block_pool->shm_count; |
proj_id = p_article_block_pool->shm_count; |
| 147 |
key = ftok(filename, proj_id); |
key = ftok(filename, proj_id); |
| 148 |
if (key == -1) |
if (key == -1) |
| 149 |
{ |
{ |
| 196 |
|
|
| 197 |
void article_block_cleanup(void) |
void article_block_cleanup(void) |
| 198 |
{ |
{ |
| 199 |
if (p_article_block_pool != NULL) |
if (p_article_block_pool == NULL) |
| 200 |
{ |
{ |
| 201 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
return; |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 205 |
|
{ |
| 206 |
|
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 207 |
{ |
{ |
| 208 |
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 209 |
{ |
} |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
|
|
} |
|
| 210 |
|
|
| 211 |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
if (shmctl((p_article_block_pool->shm_pool + i)->shmid, IPC_RMID, NULL) == -1) |
| 212 |
{ |
{ |
| 213 |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
|
} |
|
| 214 |
} |
} |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
if (shmdt(p_article_block_pool) == -1) |
| 218 |
|
{ |
| 219 |
|
log_error("shmdt(shmid = %d) error (%d)\n", article_block_pool_shmid, errno); |
| 220 |
|
} |
| 221 |
|
|
| 222 |
free(p_article_block_pool); |
if (shmctl(article_block_pool_shmid, IPC_RMID, NULL) == -1) |
| 223 |
p_article_block_pool = NULL; |
{ |
| 224 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", article_block_pool_shmid, errno); |
| 225 |
} |
} |
| 226 |
|
|
| 227 |
|
p_article_block_pool = NULL; |
| 228 |
} |
} |
| 229 |
|
|
| 230 |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
| 346 |
|
|
| 347 |
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) |
| 348 |
{ |
{ |
| 349 |
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); |
| 350 |
return NULL; |
return NULL; |
| 351 |
} |
} |
| 352 |
|
|
| 354 |
|
|
| 355 |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
if (index % ARTICLE_PER_BLOCK >= p_block->article_count) |
| 356 |
{ |
{ |
| 357 |
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); |
| 358 |
return NULL; |
return NULL; |
| 359 |
} |
} |
| 360 |
|
|
| 361 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
| 362 |
} |
} |
| 363 |
|
|
| 364 |
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) |
| 365 |
{ |
{ |
| 366 |
SECTION_LIST *p_section; |
int semid; |
| 367 |
|
int shmid; |
| 368 |
|
int proj_id; |
| 369 |
|
key_t key; |
| 370 |
|
size_t size; |
| 371 |
|
void *p_shm; |
| 372 |
|
union semun arg; |
| 373 |
|
int i; |
| 374 |
|
|
| 375 |
if (p_section_list_pool == NULL) |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
| 376 |
{ |
{ |
| 377 |
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
section_list_pool_cleanup(); |
| 378 |
if (p_section_list_pool == NULL) |
} |
|
{ |
|
|
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
|
|
return NULL; |
|
|
} |
|
| 379 |
|
|
| 380 |
section_list_count = 0; |
proj_id = (int)(time(NULL) % getpid()); |
| 381 |
|
key = ftok(filename, proj_id); |
| 382 |
|
if (key == -1) |
| 383 |
|
{ |
| 384 |
|
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 385 |
|
return -3; |
| 386 |
} |
} |
| 387 |
|
|
| 388 |
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 |
| 389 |
|
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 390 |
|
if (semid == -1) |
| 391 |
{ |
{ |
| 392 |
p_trie_dict_section_list = trie_dict_create(); |
log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno); |
| 393 |
if (p_trie_dict_section_list == NULL) |
return -3; |
| 394 |
|
} |
| 395 |
|
|
| 396 |
|
// Initialize sem value to 0 |
| 397 |
|
arg.val = 0; |
| 398 |
|
for (i = 0; i < size; i++) |
| 399 |
|
{ |
| 400 |
|
if (semctl(semid, i, SETVAL, arg) == -1) |
| 401 |
{ |
{ |
| 402 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno); |
| 403 |
return NULL; |
return -3; |
| 404 |
} |
} |
| 405 |
} |
} |
| 406 |
|
|
| 407 |
|
section_list_pool_semid = semid; |
| 408 |
|
|
| 409 |
|
size = sizeof(SECTION_LIST) * BBS_max_section; |
| 410 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 411 |
|
if (shmid == -1) |
| 412 |
|
{ |
| 413 |
|
log_error("shmget(section_list_pool_shm, size = %d) error (%d)\n", size, errno); |
| 414 |
|
return -3; |
| 415 |
|
} |
| 416 |
|
p_shm = shmat(shmid, NULL, 0); |
| 417 |
|
if (p_shm == (void *)-1) |
| 418 |
|
{ |
| 419 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 420 |
|
return -3; |
| 421 |
|
} |
| 422 |
|
|
| 423 |
|
section_list_pool_shmid = shmid; |
| 424 |
|
p_section_list_pool = p_shm; |
| 425 |
|
section_list_count = 0; |
| 426 |
|
|
| 427 |
|
p_trie_dict_section_by_name = trie_dict_create(); |
| 428 |
|
if (p_trie_dict_section_by_name == NULL) |
| 429 |
|
{ |
| 430 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 431 |
|
return -2; |
| 432 |
|
} |
| 433 |
|
|
| 434 |
|
p_trie_dict_section_by_sid = trie_dict_create(); |
| 435 |
|
if (p_trie_dict_section_by_sid == NULL) |
| 436 |
|
{ |
| 437 |
|
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 438 |
|
return -2; |
| 439 |
|
} |
| 440 |
|
|
| 441 |
|
return 0; |
| 442 |
|
} |
| 443 |
|
|
| 444 |
|
inline static void sid_to_str(int32_t sid, char *p_sid_str) |
| 445 |
|
{ |
| 446 |
|
uint32_t u_sid; |
| 447 |
|
int i; |
| 448 |
|
|
| 449 |
|
u_sid = (uint32_t)sid; |
| 450 |
|
for (i = 0; i < SID_STR_LEN - 1; i++) |
| 451 |
|
{ |
| 452 |
|
p_sid_str[i] = (char)(u_sid % 255 + 1); |
| 453 |
|
u_sid /= 255; |
| 454 |
|
} |
| 455 |
|
p_sid_str[i] = '\0'; |
| 456 |
|
} |
| 457 |
|
|
| 458 |
|
SECTION_LIST *section_list_create(int32_t sid, const char *sname, const char *stitle, const char *master_name) |
| 459 |
|
{ |
| 460 |
|
SECTION_LIST *p_section; |
| 461 |
|
char sid_str[SID_STR_LEN]; |
| 462 |
|
|
| 463 |
|
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
| 464 |
|
{ |
| 465 |
|
log_error("session_list_pool not initialized\n"); |
| 466 |
|
return NULL; |
| 467 |
|
} |
| 468 |
|
|
| 469 |
if (section_list_count >= BBS_max_section) |
if (section_list_count >= BBS_max_section) |
| 470 |
{ |
{ |
| 471 |
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); |
| 472 |
return NULL; |
return NULL; |
| 473 |
} |
} |
| 474 |
|
|
| 475 |
|
sid_to_str(sid, sid_str); |
| 476 |
|
|
| 477 |
p_section = p_section_list_pool + section_list_count; |
p_section = p_section_list_pool + section_list_count; |
| 478 |
|
|
| 479 |
p_section->sid = sid; |
p_section->sid = sid; |
| 487 |
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)); |
| 488 |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
| 489 |
|
|
| 490 |
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) |
| 491 |
|
{ |
| 492 |
|
log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count); |
| 493 |
|
return NULL; |
| 494 |
|
} |
| 495 |
|
|
| 496 |
|
if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1) |
| 497 |
{ |
{ |
| 498 |
log_error("trie_dict_set(section_data, %s, %d) error\n", sname, section_list_count); |
log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count); |
| 499 |
|
log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]); |
| 500 |
return NULL; |
return NULL; |
| 501 |
} |
} |
| 502 |
|
|
| 520 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 521 |
} |
} |
| 522 |
|
|
| 523 |
void section_list_cleanup(void) |
void section_list_pool_cleanup(void) |
| 524 |
{ |
{ |
| 525 |
if (p_trie_dict_section_list != NULL) |
if (p_trie_dict_section_by_name != NULL) |
| 526 |
{ |
{ |
| 527 |
trie_dict_destroy(p_trie_dict_section_list); |
trie_dict_destroy(p_trie_dict_section_by_name); |
| 528 |
p_trie_dict_section_list = NULL; |
p_trie_dict_section_by_name = NULL; |
| 529 |
|
} |
| 530 |
|
|
| 531 |
|
if (p_trie_dict_section_by_sid != NULL) |
| 532 |
|
{ |
| 533 |
|
trie_dict_destroy(p_trie_dict_section_by_sid); |
| 534 |
|
p_trie_dict_section_by_sid = NULL; |
| 535 |
} |
} |
| 536 |
|
|
| 537 |
if (p_section_list_pool != NULL) |
if (p_section_list_pool != NULL) |
| 538 |
{ |
{ |
| 539 |
free(p_section_list_pool); |
if (shmdt(p_section_list_pool) == -1) |
| 540 |
|
{ |
| 541 |
|
log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno); |
| 542 |
|
} |
| 543 |
p_section_list_pool = NULL; |
p_section_list_pool = NULL; |
| 544 |
|
|
| 545 |
|
if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1) |
| 546 |
|
{ |
| 547 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
| 548 |
|
} |
| 549 |
|
|
| 550 |
|
if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1) |
| 551 |
|
{ |
| 552 |
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno); |
| 553 |
|
} |
| 554 |
} |
} |
| 555 |
|
|
| 556 |
section_list_count = 0; |
section_list_count = 0; |
| 560 |
{ |
{ |
| 561 |
int64_t index; |
int64_t index; |
| 562 |
|
|
| 563 |
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) |
| 564 |
{ |
{ |
| 565 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 566 |
return NULL; |
return NULL; |
| 567 |
} |
} |
| 568 |
|
|
| 569 |
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) |
| 570 |
{ |
{ |
| 571 |
log_error("trie_dict_get(section_data, %s) error\n", sname); |
log_error("trie_dict_get(section, %s) error\n", sname); |
| 572 |
|
return NULL; |
| 573 |
|
} |
| 574 |
|
|
| 575 |
|
return (p_section_list_pool + index); |
| 576 |
|
} |
| 577 |
|
|
| 578 |
|
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 579 |
|
{ |
| 580 |
|
int64_t index; |
| 581 |
|
char sid_str[SID_STR_LEN]; |
| 582 |
|
|
| 583 |
|
if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL) |
| 584 |
|
{ |
| 585 |
|
log_error("section_list not initialized\n"); |
| 586 |
|
return NULL; |
| 587 |
|
} |
| 588 |
|
|
| 589 |
|
sid_to_str(sid, sid_str); |
| 590 |
|
|
| 591 |
|
if (trie_dict_get(p_trie_dict_section_by_sid, sid_str, &index) != 1) |
| 592 |
|
{ |
| 593 |
|
log_error("trie_dict_get(section, %d) error\n", sid); |
| 594 |
return NULL; |
return NULL; |
| 595 |
} |
} |
| 596 |
|
|
| 617 |
return -1; |
return -1; |
| 618 |
} |
} |
| 619 |
|
|
| 620 |
|
if (p_section->sid != p_article_src->sid) |
| 621 |
|
{ |
| 622 |
|
log_error("section_list_append_article() error: section sid %d != article sid %d\n", p_section->sid, p_article_src->sid); |
| 623 |
|
return -2; |
| 624 |
|
} |
| 625 |
|
|
| 626 |
if (p_section->article_count >= BBS_article_limit_per_section) |
if (p_section->article_count >= BBS_article_limit_per_section) |
| 627 |
{ |
{ |
| 628 |
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); |
| 655 |
// AID of articles should be strictly ascending |
// AID of articles should be strictly ascending |
| 656 |
if (p_article_src->aid <= last_aid) |
if (p_article_src->aid <= last_aid) |
| 657 |
{ |
{ |
| 658 |
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); |
| 659 |
return -3; |
return -3; |
| 660 |
} |
} |
| 661 |
|
|
| 753 |
return -1; // Not found |
return -1; // Not found |
| 754 |
} |
} |
| 755 |
|
|
| 756 |
|
if (p_section->sid != p_article->sid) |
| 757 |
|
{ |
| 758 |
|
log_error("section_list_set_article_visible() error: section sid %d != article sid %d\n", p_section->sid, p_article->sid); |
| 759 |
|
return -2; |
| 760 |
|
} |
| 761 |
|
|
| 762 |
if (p_article->visible == visible) |
if (p_article->visible == visible) |
| 763 |
{ |
{ |
| 764 |
return 0; // Already set |
return 0; // Already set |
| 925 |
|
|
| 926 |
if (start_aid > 0) |
if (start_aid > 0) |
| 927 |
{ |
{ |
| 928 |
|
p_article = article_block_find_by_aid(start_aid); |
| 929 |
|
if (p_article == NULL) |
| 930 |
|
{ |
| 931 |
|
return -1; // Not found |
| 932 |
|
} |
| 933 |
|
|
| 934 |
|
if (p_section->sid != p_article->sid) |
| 935 |
|
{ |
| 936 |
|
log_error("section_list_calculate_page() error: section sid %d != start article sid %d\n", p_section->sid, p_article->sid); |
| 937 |
|
return -2; |
| 938 |
|
} |
| 939 |
|
|
| 940 |
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); |
| 941 |
if (p_article == NULL) |
if (p_article == NULL) |
| 942 |
{ |
{ |
| 1005 |
return 0; |
return 0; |
| 1006 |
} |
} |
| 1007 |
|
|
| 1008 |
int section_list_count_of_topic_articles(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 1009 |
{ |
{ |
| 1010 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1011 |
int article_count; |
int article_count; |
| 1020 |
|
|
| 1021 |
do |
do |
| 1022 |
{ |
{ |
| 1023 |
|
if (p_article->tid != 0 && p_article->tid != aid) |
| 1024 |
|
{ |
| 1025 |
|
log_error("article_count_of_topic(%d) error: article %d not linked to the topic\n", aid, p_article->aid); |
| 1026 |
|
break; |
| 1027 |
|
} |
| 1028 |
|
|
| 1029 |
article_count++; |
article_count++; |
| 1030 |
p_article = p_article->p_topic_next; |
p_article = p_article->p_topic_next; |
| 1031 |
} while (p_article->aid != aid); |
} while (p_article->aid != aid); |
| 1051 |
return -1; |
return -1; |
| 1052 |
} |
} |
| 1053 |
|
|
| 1054 |
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) |
| 1055 |
{ |
{ |
| 1056 |
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: article %d not found in block\n", aid); |
| 1057 |
|
return -2; |
| 1058 |
|
} |
| 1059 |
|
|
| 1060 |
|
if (p_section_src->sid != p_article->sid) |
| 1061 |
|
{ |
| 1062 |
|
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
| 1063 |
|
p_section_src->sid, p_article->aid, p_article->sid); |
| 1064 |
return -2; |
return -2; |
| 1065 |
} |
} |
| 1066 |
|
|
| 1072 |
|
|
| 1073 |
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); |
| 1074 |
|
|
| 1075 |
move_article_count = section_list_count_of_topic_articles(aid); |
move_article_count = article_count_of_topic(aid); |
| 1076 |
if (move_article_count <= 0) |
if (move_article_count <= 0) |
| 1077 |
{ |
{ |
| 1078 |
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); |
| 1092 |
|
|
| 1093 |
do |
do |
| 1094 |
{ |
{ |
| 1095 |
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) |
| 1096 |
{ |
{ |
| 1097 |
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", |
| 1098 |
return -4; |
p_section_src->sid, p_article->aid, p_article->sid); |
| 1099 |
|
return -2; |
| 1100 |
} |
} |
| 1101 |
|
|
| 1102 |
// Remove from bi-directional article list of src section |
// Remove from bi-directional article list of src section |
| 1117 |
p_article->p_prior->p_next = p_article->p_next; |
p_article->p_prior->p_next = p_article->p_next; |
| 1118 |
p_article->p_next->p_prior = p_article->p_prior; |
p_article->p_next->p_prior = p_article->p_prior; |
| 1119 |
|
|
| 1120 |
|
// Update sid of article |
| 1121 |
|
p_article->sid = p_section_dest->sid; |
| 1122 |
|
|
| 1123 |
|
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
| 1124 |
|
{ |
| 1125 |
|
log_error("section_list_move_topic() error: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
| 1126 |
|
return -4; |
| 1127 |
|
} |
| 1128 |
|
|
| 1129 |
// Insert into bi-directional article list of dest section |
// Insert into bi-directional article list of dest section |
| 1130 |
if (p_next == NULL) // empty section |
if (p_next == NULL) // empty section |
| 1131 |
{ |
{ |
| 1191 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1192 |
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) |
| 1193 |
{ |
{ |
| 1194 |
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", |
| 1195 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1196 |
} |
} |
| 1197 |
|
|
| 1208 |
// Re-calculate pages of src section |
// Re-calculate pages of src section |
| 1209 |
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) |
| 1210 |
{ |
{ |
| 1211 |
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", |
| 1212 |
p_section_src->sid, last_unaffected_aid_src, aid); |
p_section_src->sid, last_unaffected_aid_src, aid); |
| 1213 |
} |
} |
| 1214 |
|
|
| 1217 |
// Re-calculate pages of desc section |
// Re-calculate pages of desc section |
| 1218 |
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) |
| 1219 |
{ |
{ |
| 1220 |
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", |
| 1221 |
p_section_dest->sid, first_inserted_aid_dest); |
p_section_dest->sid, first_inserted_aid_dest); |
| 1222 |
} |
} |
| 1223 |
} |
} |
| 1224 |
|
|
| 1225 |
return move_article_count; |
return move_article_count; |
| 1226 |
} |
} |
| 1227 |
|
|
| 1228 |
|
int get_section_index(SECTION_LIST *p_section) |
| 1229 |
|
{ |
| 1230 |
|
int index; |
| 1231 |
|
|
| 1232 |
|
if (p_section_list_pool == NULL) |
| 1233 |
|
{ |
| 1234 |
|
log_error("get_section_index() error: uninitialized\n"); |
| 1235 |
|
return -1; |
| 1236 |
|
} |
| 1237 |
|
|
| 1238 |
|
if (p_section == NULL) |
| 1239 |
|
{ |
| 1240 |
|
index = BBS_max_section; |
| 1241 |
|
} |
| 1242 |
|
else |
| 1243 |
|
{ |
| 1244 |
|
index = (int)(p_section - p_section_list_pool); |
| 1245 |
|
if (index < 0 || index >= BBS_max_section) |
| 1246 |
|
{ |
| 1247 |
|
log_error("get_section_index(%d) error: index out of range\n", index); |
| 1248 |
|
return -2; |
| 1249 |
|
} |
| 1250 |
|
} |
| 1251 |
|
|
| 1252 |
|
return index; |
| 1253 |
|
} |
| 1254 |
|
|
| 1255 |
|
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1256 |
|
{ |
| 1257 |
|
int index; |
| 1258 |
|
struct sembuf sops[4]; |
| 1259 |
|
struct timespec timeout; |
| 1260 |
|
int ret; |
| 1261 |
|
|
| 1262 |
|
index = get_section_index(p_section); |
| 1263 |
|
if (index < 0) |
| 1264 |
|
{ |
| 1265 |
|
return -2; |
| 1266 |
|
} |
| 1267 |
|
|
| 1268 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1269 |
|
sops[0].sem_op = 0; // wait until unlocked |
| 1270 |
|
sops[0].sem_flg = 0; |
| 1271 |
|
|
| 1272 |
|
sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1273 |
|
sops[1].sem_op = 1; // lock |
| 1274 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1275 |
|
|
| 1276 |
|
// Read lock on any specific section will also acquire single read lock on "all section" |
| 1277 |
|
// so that write lock on all section only need to acquire single write on on "all section" |
| 1278 |
|
// rather than to acquire multiple write locks on all the available sections. |
| 1279 |
|
if (index == BBS_max_section) |
| 1280 |
|
{ |
| 1281 |
|
sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section |
| 1282 |
|
sops[2].sem_op = 0; // wait until unlocked |
| 1283 |
|
sops[2].sem_flg = 0; |
| 1284 |
|
|
| 1285 |
|
sops[3].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1286 |
|
sops[3].sem_op = 1; // lock |
| 1287 |
|
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1288 |
|
} |
| 1289 |
|
|
| 1290 |
|
timeout.tv_sec = wait_sec; |
| 1291 |
|
timeout.tv_nsec = 0; |
| 1292 |
|
|
| 1293 |
|
ret = semtimedop(section_list_pool_semid, sops, (index == BBS_max_section ? 4 : 2), &timeout); |
| 1294 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1295 |
|
{ |
| 1296 |
|
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
| 1297 |
|
} |
| 1298 |
|
|
| 1299 |
|
return ret; |
| 1300 |
|
} |
| 1301 |
|
|
| 1302 |
|
int section_list_try_rw_lock(SECTION_LIST *p_section, int wait_sec) |
| 1303 |
|
{ |
| 1304 |
|
int index; |
| 1305 |
|
struct sembuf sops[3]; |
| 1306 |
|
struct timespec timeout; |
| 1307 |
|
int ret; |
| 1308 |
|
|
| 1309 |
|
index = get_section_index(p_section); |
| 1310 |
|
if (index < 0) |
| 1311 |
|
{ |
| 1312 |
|
return -2; |
| 1313 |
|
} |
| 1314 |
|
|
| 1315 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1316 |
|
sops[0].sem_op = 0; // wait until unlocked |
| 1317 |
|
sops[0].sem_flg = 0; |
| 1318 |
|
|
| 1319 |
|
sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1320 |
|
sops[1].sem_op = 1; // lock |
| 1321 |
|
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1322 |
|
|
| 1323 |
|
sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1324 |
|
sops[2].sem_op = 0; // wait until unlocked |
| 1325 |
|
sops[2].sem_flg = 0; |
| 1326 |
|
|
| 1327 |
|
timeout.tv_sec = wait_sec; |
| 1328 |
|
timeout.tv_nsec = 0; |
| 1329 |
|
|
| 1330 |
|
ret = semtimedop(section_list_pool_semid, sops, 3, &timeout); |
| 1331 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1332 |
|
{ |
| 1333 |
|
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
| 1334 |
|
} |
| 1335 |
|
|
| 1336 |
|
return ret; |
| 1337 |
|
} |
| 1338 |
|
|
| 1339 |
|
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1340 |
|
{ |
| 1341 |
|
int index; |
| 1342 |
|
struct sembuf sops[2]; |
| 1343 |
|
int ret; |
| 1344 |
|
|
| 1345 |
|
index = get_section_index(p_section); |
| 1346 |
|
if (index < 0) |
| 1347 |
|
{ |
| 1348 |
|
return -2; |
| 1349 |
|
} |
| 1350 |
|
|
| 1351 |
|
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1352 |
|
sops[0].sem_op = -1; // unlock |
| 1353 |
|
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1354 |
|
|
| 1355 |
|
// The same reason as section_list_try_rd_lock() |
| 1356 |
|
if (index == BBS_max_section) |
| 1357 |
|
{ |
| 1358 |
|
sops[1].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1359 |
|
sops[1].sem_op = -1; // unlock |
| 1360 |
|
sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1361 |
|
} |
| 1362 |
|
|
| 1363 |
|
ret = semop(section_list_pool_semid, sops, (index == BBS_max_section ? 2 : 1)); |
| 1364 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1365 |
|
{ |
| 1366 |
|
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1367 |
|
} |
| 1368 |
|
|
| 1369 |
|
return ret; |
| 1370 |
|
} |
| 1371 |
|
|
| 1372 |
|
int section_list_rw_unlock(SECTION_LIST *p_section) |
| 1373 |
|
{ |
| 1374 |
|
int index; |
| 1375 |
|
struct sembuf sops[1]; |
| 1376 |
|
int ret; |
| 1377 |
|
|
| 1378 |
|
index = get_section_index(p_section); |
| 1379 |
|
if (index < 0) |
| 1380 |
|
{ |
| 1381 |
|
return -2; |
| 1382 |
|
} |
| 1383 |
|
|
| 1384 |
|
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1385 |
|
sops[0].sem_op = -1; // unlock |
| 1386 |
|
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1387 |
|
|
| 1388 |
|
ret = semop(section_list_pool_semid, sops, 1); |
| 1389 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1390 |
|
{ |
| 1391 |
|
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1392 |
|
} |
| 1393 |
|
|
| 1394 |
|
return ret; |
| 1395 |
|
} |