| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 16 |
|
|
|
#define _GNU_SOURCE |
|
|
|
|
| 17 |
#include "section_list.h" |
#include "section_list.h" |
| 18 |
#include "log.h" |
#include "log.h" |
| 19 |
#include "trie_dict.h" |
#include "trie_dict.h" |
| 28 |
#include <sys/shm.h> |
#include <sys/shm.h> |
| 29 |
#include <sys/ipc.h> |
#include <sys/ipc.h> |
| 30 |
|
|
| 31 |
|
#ifdef _SEM_SEMUN_UNDEFINED |
| 32 |
|
union semun |
| 33 |
|
{ |
| 34 |
|
int val; /* Value for SETVAL */ |
| 35 |
|
struct semid_ds *buf; /* Buffer for IPC_STAT, IPC_SET */ |
| 36 |
|
unsigned short *array; /* Array for GETALL, SETALL */ |
| 37 |
|
struct seminfo *__buf; /* Buffer for IPC_INFO |
| 38 |
|
(Linux-specific) */ |
| 39 |
|
}; |
| 40 |
|
#endif // #ifdef _SEM_SEMUN_UNDEFINED |
| 41 |
|
|
| 42 |
|
#define SECTION_TRY_LOCK_WAIT_TIME 1 // second |
| 43 |
|
#define SECTION_TRY_LOCK_TIMES 10 |
| 44 |
|
|
| 45 |
#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 |
| 46 |
#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) |
| 47 |
#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) |
| 48 |
|
|
| 49 |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of move topic |
#define CALCULATE_PAGE_THRESHOLD 100 // Adjust to tune performance of moving topic between sections |
| 50 |
|
|
| 51 |
#define SID_STR_LEN 5 // 32-bit + NULL |
#define SID_STR_LEN 5 // 32-bit + NULL |
| 52 |
|
|
| 53 |
struct article_block_t |
struct article_block_t |
| 54 |
{ |
{ |
| 55 |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
ARTICLE articles[ARTICLE_PER_BLOCK]; |
| 56 |
int32_t article_count; |
int article_count; |
| 57 |
struct article_block_t *p_next_block; |
struct article_block_t *p_next_block; |
| 58 |
}; |
}; |
| 59 |
typedef struct article_block_t ARTICLE_BLOCK; |
typedef struct article_block_t ARTICLE_BLOCK; |
| 60 |
|
|
|
struct article_block_shm_t |
|
|
{ |
|
|
int shmid; |
|
|
void *p_shm; |
|
|
}; |
|
|
typedef struct article_block_shm_t ARTICLE_BLOCK_SHM; |
|
|
|
|
| 61 |
struct article_block_pool_t |
struct article_block_pool_t |
| 62 |
{ |
{ |
| 63 |
ARTICLE_BLOCK_SHM shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
int shmid; |
| 64 |
|
struct |
| 65 |
|
{ |
| 66 |
|
int shmid; |
| 67 |
|
void *p_shm; |
| 68 |
|
} shm_pool[ARTICLE_BLOCK_SHM_COUNT_LIMIT]; |
| 69 |
int shm_count; |
int shm_count; |
| 70 |
ARTICLE_BLOCK *p_block_free_list; |
ARTICLE_BLOCK *p_block_free_list; |
| 71 |
ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL]; |
ARTICLE_BLOCK *p_block[ARTICLE_BLOCK_PER_POOL]; |
| 72 |
int32_t block_count; |
int block_count; |
| 73 |
}; |
}; |
| 74 |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
typedef struct article_block_pool_t ARTICLE_BLOCK_POOL; |
| 75 |
|
|
| 76 |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
static ARTICLE_BLOCK_POOL *p_article_block_pool = NULL; |
| 77 |
|
|
| 78 |
static int section_list_pool_shmid; |
struct section_list_pool_t |
| 79 |
static int section_list_pool_semid; |
{ |
| 80 |
static SECTION_LIST *p_section_list_pool = NULL; |
int shmid; |
| 81 |
static int section_list_count = 0; |
SECTION_LIST sections[BBS_max_section]; |
| 82 |
static TRIE_NODE *p_trie_dict_section_by_name = NULL; |
int section_count; |
| 83 |
static TRIE_NODE *p_trie_dict_section_by_sid = NULL; |
int semid; |
| 84 |
|
TRIE_NODE *p_trie_dict_section_by_name; |
| 85 |
|
TRIE_NODE *p_trie_dict_section_by_sid; |
| 86 |
|
}; |
| 87 |
|
typedef struct section_list_pool_t SECTION_LIST_POOL; |
| 88 |
|
|
| 89 |
|
static SECTION_LIST_POOL *p_section_list_pool = NULL; |
| 90 |
|
|
| 91 |
int article_block_init(const char *filename, int block_count) |
int article_block_init(const char *filename, int block_count) |
| 92 |
{ |
{ |
| 106 |
return -1; |
return -1; |
| 107 |
} |
} |
| 108 |
|
|
| 109 |
if (block_count > ARTICLE_BLOCK_PER_POOL) |
if (block_count <= 0 || block_count > ARTICLE_BLOCK_PER_POOL) |
| 110 |
{ |
{ |
| 111 |
log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL); |
log_error("article_block_count exceed limit %d\n", ARTICLE_BLOCK_PER_POOL); |
| 112 |
return -2; |
return -2; |
| 113 |
} |
} |
| 114 |
|
|
| 115 |
p_article_block_pool = calloc(1, sizeof(ARTICLE_BLOCK_POOL)); |
// Allocate shared memory |
| 116 |
if (p_article_block_pool == NULL) |
proj_id = ARTICLE_BLOCK_SHM_COUNT_LIMIT; // keep different from proj_id used to create block shm |
| 117 |
|
key = ftok(filename, proj_id); |
| 118 |
|
if (key == -1) |
| 119 |
{ |
{ |
| 120 |
log_error("calloc(ARTICLE_BLOCK_POOL) OOM\n"); |
log_error("ftok(%s, %d) error (%d)\n", filename, proj_id, errno); |
| 121 |
return -2; |
return -3; |
| 122 |
} |
} |
| 123 |
|
|
| 124 |
// Allocate shared memory |
size = sizeof(ARTICLE_BLOCK_POOL); |
| 125 |
|
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 126 |
|
if (shmid == -1) |
| 127 |
|
{ |
| 128 |
|
log_error("shmget(article_block_pool_shm, size = %d) error (%d)\n", size, errno); |
| 129 |
|
return -3; |
| 130 |
|
} |
| 131 |
|
p_shm = shmat(shmid, NULL, 0); |
| 132 |
|
if (p_shm == (void *)-1) |
| 133 |
|
{ |
| 134 |
|
log_error("shmat(shmid = %d) error (%d)\n", shmid, errno); |
| 135 |
|
return -3; |
| 136 |
|
} |
| 137 |
|
|
| 138 |
|
p_article_block_pool = p_shm; |
| 139 |
|
p_article_block_pool->shmid = shmid; |
| 140 |
|
|
| 141 |
p_article_block_pool->shm_count = 0; |
p_article_block_pool->shm_count = 0; |
| 142 |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
pp_block_next = &(p_article_block_pool->p_block_free_list); |
| 143 |
|
|
| 146 |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
block_count_in_shm = MIN(block_count, ARTICLE_BLOCK_PER_SHM); |
| 147 |
block_count -= block_count_in_shm; |
block_count -= block_count_in_shm; |
| 148 |
|
|
| 149 |
proj_id = getpid() + p_article_block_pool->shm_count; |
proj_id = p_article_block_pool->shm_count; |
| 150 |
key = ftok(filename, proj_id); |
key = ftok(filename, proj_id); |
| 151 |
if (key == -1) |
if (key == -1) |
| 152 |
{ |
{ |
| 155 |
return -3; |
return -3; |
| 156 |
} |
} |
| 157 |
|
|
| 158 |
size = sizeof(shmid) + sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
size = sizeof(ARTICLE_BLOCK) * (size_t)block_count_in_shm; |
| 159 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 160 |
if (shmid == -1) |
if (shmid == -1) |
| 161 |
{ |
{ |
| 199 |
|
|
| 200 |
void article_block_cleanup(void) |
void article_block_cleanup(void) |
| 201 |
{ |
{ |
| 202 |
if (p_article_block_pool != NULL) |
int shmid; |
| 203 |
|
|
| 204 |
|
if (p_article_block_pool == NULL) |
| 205 |
{ |
{ |
| 206 |
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
return; |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 210 |
|
{ |
| 211 |
|
if (shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 212 |
{ |
{ |
| 213 |
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); |
| 214 |
{ |
} |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
|
|
} |
|
| 215 |
|
|
| 216 |
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) |
| 217 |
{ |
{ |
| 218 |
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); |
| 219 |
} |
} |
| 220 |
|
} |
| 221 |
|
|
| 222 |
|
shmid = p_article_block_pool->shmid; |
| 223 |
|
|
| 224 |
|
if (shmdt(p_article_block_pool) == -1) |
| 225 |
|
{ |
| 226 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 227 |
|
} |
| 228 |
|
|
| 229 |
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 230 |
|
{ |
| 231 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 232 |
|
} |
| 233 |
|
|
| 234 |
|
p_article_block_pool = NULL; |
| 235 |
|
} |
| 236 |
|
|
| 237 |
|
int set_article_block_shm_readonly(void) |
| 238 |
|
{ |
| 239 |
|
int shmid; |
| 240 |
|
void *p_shm; |
| 241 |
|
int i; |
| 242 |
|
|
| 243 |
|
if (p_article_block_pool == NULL) |
| 244 |
|
{ |
| 245 |
|
log_error("article_block_pool not initialized\n"); |
| 246 |
|
return -1; |
| 247 |
|
} |
| 248 |
|
|
| 249 |
|
for (i = 0; i < p_article_block_pool->shm_count; i++) |
| 250 |
|
{ |
| 251 |
|
shmid = (p_article_block_pool->shm_pool + i)->shmid; |
| 252 |
|
|
| 253 |
|
// Remap shared memory in read-only mode |
| 254 |
|
p_shm = shmat(shmid, (p_article_block_pool->shm_pool + i)->p_shm, SHM_RDONLY | SHM_REMAP); |
| 255 |
|
if (p_shm == (void *)-1) |
| 256 |
|
{ |
| 257 |
|
log_error("shmat(article_block_pool shmid = %d) error (%d)\n", shmid, errno); |
| 258 |
|
return -2; |
| 259 |
} |
} |
| 260 |
|
} |
| 261 |
|
|
| 262 |
|
return 0; |
| 263 |
|
} |
| 264 |
|
|
| 265 |
free(p_article_block_pool); |
int detach_article_block_shm(void) |
| 266 |
p_article_block_pool = NULL; |
{ |
| 267 |
|
int shmid; |
| 268 |
|
|
| 269 |
|
if (p_article_block_pool == NULL) |
| 270 |
|
{ |
| 271 |
|
return -1; |
| 272 |
} |
} |
| 273 |
|
|
| 274 |
|
for (int i = 0; i < p_article_block_pool->shm_count; i++) |
| 275 |
|
{ |
| 276 |
|
if ((p_article_block_pool->shm_pool + i)->p_shm != NULL && shmdt((p_article_block_pool->shm_pool + i)->p_shm) == -1) |
| 277 |
|
{ |
| 278 |
|
log_error("shmdt(shmid = %d) error (%d)\n", (p_article_block_pool->shm_pool + i)->shmid, errno); |
| 279 |
|
return -2; |
| 280 |
|
} |
| 281 |
|
} |
| 282 |
|
|
| 283 |
|
shmid = p_article_block_pool->shmid; |
| 284 |
|
|
| 285 |
|
if (shmdt(p_article_block_pool) == -1) |
| 286 |
|
{ |
| 287 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 288 |
|
return -3; |
| 289 |
|
} |
| 290 |
|
|
| 291 |
|
p_article_block_pool = NULL; |
| 292 |
|
|
| 293 |
|
return 0; |
| 294 |
} |
} |
| 295 |
|
|
| 296 |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
inline static ARTICLE_BLOCK *pop_free_article_block(void) |
| 397 |
} |
} |
| 398 |
} |
} |
| 399 |
|
|
| 400 |
return (p_block->articles + left); |
if (aid != p_block->articles[left].aid) // not found |
| 401 |
|
{ |
| 402 |
|
return NULL; |
| 403 |
|
} |
| 404 |
|
|
| 405 |
|
return (p_block->articles + left); // found |
| 406 |
} |
} |
| 407 |
|
|
| 408 |
ARTICLE *article_block_find_by_index(int index) |
ARTICLE *article_block_find_by_index(int index) |
| 432 |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
return (p_block->articles + (index % ARTICLE_PER_BLOCK)); |
| 433 |
} |
} |
| 434 |
|
|
| 435 |
extern int section_list_pool_init(const char *filename) |
extern int section_list_init(const char *filename) |
| 436 |
{ |
{ |
| 437 |
int semid; |
int semid; |
| 438 |
int shmid; |
int shmid; |
| 440 |
key_t key; |
key_t key; |
| 441 |
size_t size; |
size_t size; |
| 442 |
void *p_shm; |
void *p_shm; |
| 443 |
|
union semun arg; |
| 444 |
|
int i; |
| 445 |
|
|
| 446 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
if (p_section_list_pool != NULL) |
|
{ |
|
|
section_list_pool_cleanup(); |
|
|
} |
|
|
|
|
|
p_section_list_pool = calloc(BBS_max_section, sizeof(SECTION_LIST)); |
|
|
if (p_section_list_pool == NULL) |
|
| 447 |
{ |
{ |
| 448 |
log_error("calloc(%d SECTION_LIST) OOM\n", BBS_max_section); |
section_list_cleanup(); |
|
return -1; |
|
| 449 |
} |
} |
| 450 |
|
|
| 451 |
proj_id = (int)(time(NULL) % getpid()); |
proj_id = (int)(time(NULL) % getpid()); |
| 456 |
return -3; |
return -3; |
| 457 |
} |
} |
| 458 |
|
|
| 459 |
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
// Allocate shared memory |
| 460 |
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
size = sizeof(SECTION_LIST_POOL); |
|
if (semid == -1) |
|
|
{ |
|
|
log_error("semget(section_list_pool, size = %d) error (%d)\n", size, errno); |
|
|
return -3; |
|
|
} |
|
|
|
|
|
section_list_pool_semid = semid; |
|
|
|
|
|
size = sizeof(shmid) + sizeof(SECTION_LIST) * BBS_max_section; |
|
| 461 |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
shmid = shmget(key, size, IPC_CREAT | IPC_EXCL | 0600); |
| 462 |
if (shmid == -1) |
if (shmid == -1) |
| 463 |
{ |
{ |
| 464 |
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); |
| 465 |
return -3; |
return -3; |
| 466 |
} |
} |
| 467 |
p_shm = shmat(shmid, NULL, 0); |
p_shm = shmat(shmid, NULL, 0); |
| 471 |
return -3; |
return -3; |
| 472 |
} |
} |
| 473 |
|
|
|
section_list_pool_shmid = shmid; |
|
| 474 |
p_section_list_pool = p_shm; |
p_section_list_pool = p_shm; |
| 475 |
section_list_count = 0; |
p_section_list_pool->shmid = shmid; |
| 476 |
|
p_section_list_pool->section_count = 0; |
| 477 |
|
|
| 478 |
p_trie_dict_section_by_name = trie_dict_create(); |
// Allocate semaphore as section locks |
| 479 |
if (p_trie_dict_section_by_name == NULL) |
size = 2 * (BBS_max_section + 1); // r_sem and w_sem per section, the last pair for all sections |
| 480 |
|
semid = semget(key, (int)size, IPC_CREAT | IPC_EXCL | 0600); |
| 481 |
|
if (semid == -1) |
| 482 |
|
{ |
| 483 |
|
log_error("semget(section_list_pool_sem, size = %d) error (%d)\n", size, errno); |
| 484 |
|
return -3; |
| 485 |
|
} |
| 486 |
|
|
| 487 |
|
// Initialize sem value to 0 |
| 488 |
|
arg.val = 0; |
| 489 |
|
for (i = 0; i < size; i++) |
| 490 |
|
{ |
| 491 |
|
if (semctl(semid, i, SETVAL, arg) == -1) |
| 492 |
|
{ |
| 493 |
|
log_error("semctl(section_list_pool_sem, SETVAL) error (%d)\n", errno); |
| 494 |
|
return -3; |
| 495 |
|
} |
| 496 |
|
} |
| 497 |
|
|
| 498 |
|
p_section_list_pool->semid = semid; |
| 499 |
|
|
| 500 |
|
p_section_list_pool->p_trie_dict_section_by_name = trie_dict_create(); |
| 501 |
|
if (p_section_list_pool->p_trie_dict_section_by_name == NULL) |
| 502 |
{ |
{ |
| 503 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 504 |
return -2; |
return -2; |
| 505 |
} |
} |
| 506 |
|
|
| 507 |
p_trie_dict_section_by_sid = trie_dict_create(); |
p_section_list_pool->p_trie_dict_section_by_sid = trie_dict_create(); |
| 508 |
if (p_trie_dict_section_by_sid == NULL) |
if (p_section_list_pool->p_trie_dict_section_by_sid == NULL) |
| 509 |
{ |
{ |
| 510 |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
log_error("trie_dict_create() OOM\n", BBS_max_section); |
| 511 |
return -2; |
return -2; |
| 514 |
return 0; |
return 0; |
| 515 |
} |
} |
| 516 |
|
|
| 517 |
|
void section_list_cleanup(void) |
| 518 |
|
{ |
| 519 |
|
int shmid; |
| 520 |
|
|
| 521 |
|
if (p_section_list_pool == NULL) |
| 522 |
|
{ |
| 523 |
|
return; |
| 524 |
|
} |
| 525 |
|
|
| 526 |
|
if (p_section_list_pool->p_trie_dict_section_by_name != NULL) |
| 527 |
|
{ |
| 528 |
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_name); |
| 529 |
|
p_section_list_pool->p_trie_dict_section_by_name = NULL; |
| 530 |
|
} |
| 531 |
|
|
| 532 |
|
if (p_section_list_pool->p_trie_dict_section_by_sid != NULL) |
| 533 |
|
{ |
| 534 |
|
trie_dict_destroy(p_section_list_pool->p_trie_dict_section_by_sid); |
| 535 |
|
p_section_list_pool->p_trie_dict_section_by_sid = NULL; |
| 536 |
|
} |
| 537 |
|
|
| 538 |
|
shmid = p_section_list_pool->shmid; |
| 539 |
|
|
| 540 |
|
if (semctl(p_section_list_pool->semid, 0, IPC_RMID) == -1) |
| 541 |
|
{ |
| 542 |
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", p_section_list_pool->semid, errno); |
| 543 |
|
} |
| 544 |
|
|
| 545 |
|
if (shmdt(p_section_list_pool) == -1) |
| 546 |
|
{ |
| 547 |
|
log_error("shmdt(shmid = %d) error (%d)\n", shmid, errno); |
| 548 |
|
} |
| 549 |
|
|
| 550 |
|
if (shmctl(shmid, IPC_RMID, NULL) == -1) |
| 551 |
|
{ |
| 552 |
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", shmid, errno); |
| 553 |
|
} |
| 554 |
|
|
| 555 |
|
p_section_list_pool = NULL; |
| 556 |
|
} |
| 557 |
|
|
| 558 |
|
int set_section_list_shm_readonly(void) |
| 559 |
|
{ |
| 560 |
|
int shmid; |
| 561 |
|
void *p_shm; |
| 562 |
|
|
| 563 |
|
if (p_section_list_pool == NULL) |
| 564 |
|
{ |
| 565 |
|
log_error("p_section_list_pool not initialized\n"); |
| 566 |
|
return -1; |
| 567 |
|
} |
| 568 |
|
|
| 569 |
|
shmid = p_section_list_pool->shmid; |
| 570 |
|
|
| 571 |
|
// Remap shared memory in read-only mode |
| 572 |
|
p_shm = shmat(shmid, p_section_list_pool, SHM_RDONLY | SHM_REMAP); |
| 573 |
|
if (p_shm == (void *)-1) |
| 574 |
|
{ |
| 575 |
|
log_error("shmat(section_list_pool shmid = %d) error (%d)\n", shmid, errno); |
| 576 |
|
return -3; |
| 577 |
|
} |
| 578 |
|
|
| 579 |
|
p_section_list_pool = p_shm; |
| 580 |
|
|
| 581 |
|
return 0; |
| 582 |
|
} |
| 583 |
|
|
| 584 |
|
int detach_section_list_shm(void) |
| 585 |
|
{ |
| 586 |
|
if (p_section_list_pool != NULL && shmdt(p_section_list_pool) == -1) |
| 587 |
|
{ |
| 588 |
|
log_error("shmdt(section_list_pool) error (%d)\n", errno); |
| 589 |
|
return -1; |
| 590 |
|
} |
| 591 |
|
|
| 592 |
|
p_section_list_pool = NULL; |
| 593 |
|
|
| 594 |
|
return 0; |
| 595 |
|
} |
| 596 |
|
|
| 597 |
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) |
| 598 |
{ |
{ |
| 599 |
uint32_t u_sid; |
uint32_t u_sid; |
| 608 |
p_sid_str[i] = '\0'; |
p_sid_str[i] = '\0'; |
| 609 |
} |
} |
| 610 |
|
|
| 611 |
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_list) |
| 612 |
{ |
{ |
| 613 |
SECTION_LIST *p_section; |
SECTION_LIST *p_section; |
| 614 |
char sid_str[SID_STR_LEN]; |
char sid_str[SID_STR_LEN]; |
| 615 |
|
|
| 616 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL || p_trie_dict_section_by_sid == NULL) |
if (p_section_list_pool == NULL) |
| 617 |
{ |
{ |
| 618 |
log_error("session_list_pool not initialized\n"); |
log_error("session_list_pool not initialized\n"); |
| 619 |
return NULL; |
return NULL; |
| 620 |
} |
} |
| 621 |
|
|
| 622 |
if (section_list_count >= BBS_max_section) |
if (p_section_list_pool->section_count >= BBS_max_section) |
| 623 |
{ |
{ |
| 624 |
log_error("section_list_count exceed limit %d >= %d\n", section_list_count, BBS_max_section); |
log_error("section_count reach limit %d >= %d\n", p_section_list_pool->section_count, BBS_max_section); |
| 625 |
return NULL; |
return NULL; |
| 626 |
} |
} |
| 627 |
|
|
| 628 |
sid_to_str(sid, sid_str); |
sid_to_str(sid, sid_str); |
| 629 |
|
|
| 630 |
p_section = p_section_list_pool + section_list_count; |
p_section = p_section_list_pool->sections + p_section_list_pool->section_count; |
| 631 |
|
|
| 632 |
p_section->sid = sid; |
p_section->sid = sid; |
| 633 |
|
|
| 634 |
strncpy(p_section->sname, sname, sizeof(p_section->sname - 1)); |
strncpy(p_section->sname, sname, sizeof(p_section->sname) - 1); |
| 635 |
p_section->sname[sizeof(p_section->sname - 1)] = '\0'; |
p_section->sname[sizeof(p_section->sname) - 1] = '\0'; |
| 636 |
|
|
| 637 |
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle - 1)); |
strncpy(p_section->stitle, stitle, sizeof(p_section->stitle) - 1); |
| 638 |
p_section->stitle[sizeof(p_section->stitle - 1)] = '\0'; |
p_section->stitle[sizeof(p_section->stitle) - 1] = '\0'; |
| 639 |
|
|
| 640 |
strncpy(p_section->master_name, master_name, sizeof(p_section->master_name - 1)); |
strncpy(p_section->master_list, master_list, sizeof(p_section->master_list) - 1); |
| 641 |
p_section->master_name[sizeof(p_section->master_name - 1)] = '\0'; |
p_section->master_list[sizeof(p_section->master_list) - 1] = '\0'; |
| 642 |
|
|
| 643 |
if (trie_dict_set(p_trie_dict_section_by_name, sname, section_list_count) != 1) |
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_name, sname, p_section_list_pool->section_count) != 1) |
| 644 |
{ |
{ |
| 645 |
log_error("trie_dict_set(section, %s, %d) error\n", sname, section_list_count); |
log_error("trie_dict_set(section, %s, %d) error\n", sname, p_section_list_pool->section_count); |
| 646 |
return NULL; |
return NULL; |
| 647 |
} |
} |
| 648 |
|
|
| 649 |
if (trie_dict_set(p_trie_dict_section_by_sid, sid_str, section_list_count) != 1) |
if (trie_dict_set(p_section_list_pool->p_trie_dict_section_by_sid, sid_str, p_section_list_pool->section_count) != 1) |
| 650 |
{ |
{ |
| 651 |
log_error("trie_dict_set(section, %d, %d) error\n", sid, section_list_count); |
log_error("trie_dict_set(section, %d, %d) error\n", sid, p_section_list_pool->section_count); |
|
log_std("Debug %x %x %x %x\n", sid_str[0], sid_str[1], sid_str[2], sid_str[3]); |
|
| 652 |
return NULL; |
return NULL; |
| 653 |
} |
} |
| 654 |
|
|
| 655 |
section_list_reset_articles(p_section); |
section_list_reset_articles(p_section); |
| 656 |
|
|
| 657 |
section_list_count++; |
p_section_list_pool->section_count++; |
| 658 |
|
|
| 659 |
return p_section; |
return p_section; |
| 660 |
} |
} |
| 672 |
p_section->last_page_visible_article_count = 0; |
p_section->last_page_visible_article_count = 0; |
| 673 |
} |
} |
| 674 |
|
|
|
void section_list_pool_cleanup(void) |
|
|
{ |
|
|
if (p_trie_dict_section_by_name != NULL) |
|
|
{ |
|
|
trie_dict_destroy(p_trie_dict_section_by_name); |
|
|
p_trie_dict_section_by_name = NULL; |
|
|
} |
|
|
|
|
|
if (p_trie_dict_section_by_sid != NULL) |
|
|
{ |
|
|
trie_dict_destroy(p_trie_dict_section_by_sid); |
|
|
p_trie_dict_section_by_sid = NULL; |
|
|
} |
|
|
|
|
|
if (p_section_list_pool != NULL) |
|
|
{ |
|
|
if (shmdt(p_section_list_pool) == -1) |
|
|
{ |
|
|
log_error("shmdt(shmid = %d) error (%d)\n", section_list_pool_shmid, errno); |
|
|
} |
|
|
p_section_list_pool = NULL; |
|
|
|
|
|
if (shmctl(section_list_pool_shmid, IPC_RMID, NULL) == -1) |
|
|
{ |
|
|
log_error("shmctl(shmid = %d, IPC_RMID) error (%d)\n", section_list_pool_shmid, errno); |
|
|
} |
|
|
|
|
|
if (semctl(section_list_pool_semid, 0, IPC_RMID) == -1) |
|
|
{ |
|
|
log_error("semctl(semid = %d, IPC_RMID) error (%d)\n", section_list_pool_semid, errno); |
|
|
} |
|
|
} |
|
|
|
|
|
section_list_count = 0; |
|
|
} |
|
|
|
|
| 675 |
SECTION_LIST *section_list_find_by_name(const char *sname) |
SECTION_LIST *section_list_find_by_name(const char *sname) |
| 676 |
{ |
{ |
| 677 |
int64_t index; |
int64_t index; |
| 678 |
|
int ret; |
| 679 |
|
|
| 680 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_name == NULL) |
if (p_section_list_pool == NULL) |
| 681 |
{ |
{ |
| 682 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 683 |
return NULL; |
return NULL; |
| 684 |
} |
} |
| 685 |
|
|
| 686 |
if (trie_dict_get(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); |
| 687 |
|
if (ret < 0) |
| 688 |
{ |
{ |
| 689 |
log_error("trie_dict_get(section, %s) error\n", sname); |
log_error("trie_dict_get(section, %s) error\n", sname); |
| 690 |
return NULL; |
return NULL; |
| 691 |
} |
} |
| 692 |
|
else if (ret == 0) |
| 693 |
|
{ |
| 694 |
|
return NULL; |
| 695 |
|
} |
| 696 |
|
|
| 697 |
return (p_section_list_pool + index); |
return (p_section_list_pool->sections + index); |
| 698 |
} |
} |
| 699 |
|
|
| 700 |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
SECTION_LIST *section_list_find_by_sid(int32_t sid) |
| 701 |
{ |
{ |
| 702 |
int64_t index; |
int64_t index; |
| 703 |
|
int ret; |
| 704 |
char sid_str[SID_STR_LEN]; |
char sid_str[SID_STR_LEN]; |
| 705 |
|
|
| 706 |
if (p_section_list_pool == NULL || p_trie_dict_section_by_sid == NULL) |
if (p_section_list_pool == NULL) |
| 707 |
{ |
{ |
| 708 |
log_error("section_list not initialized\n"); |
log_error("section_list not initialized\n"); |
| 709 |
return NULL; |
return NULL; |
| 711 |
|
|
| 712 |
sid_to_str(sid, sid_str); |
sid_to_str(sid, sid_str); |
| 713 |
|
|
| 714 |
if (trie_dict_get(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); |
| 715 |
|
if (ret < 0) |
| 716 |
{ |
{ |
| 717 |
log_error("trie_dict_get(section, %d) error\n", sid); |
log_error("trie_dict_get(section, %d) error\n", sid); |
| 718 |
return NULL; |
return NULL; |
| 719 |
} |
} |
| 720 |
|
else if (ret == 0) |
| 721 |
|
{ |
| 722 |
|
return NULL; |
| 723 |
|
} |
| 724 |
|
|
| 725 |
return (p_section_list_pool + index); |
return (p_section_list_pool->sections + index); |
| 726 |
} |
} |
| 727 |
|
|
| 728 |
int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src) |
int section_list_append_article(SECTION_LIST *p_section, const ARTICLE *p_article_src) |
| 1133 |
return 0; |
return 0; |
| 1134 |
} |
} |
| 1135 |
|
|
| 1136 |
|
int32_t article_block_last_aid(void) |
| 1137 |
|
{ |
| 1138 |
|
ARTICLE_BLOCK *p_block = p_article_block_pool->p_block[p_article_block_pool->block_count - 1]; |
| 1139 |
|
int32_t last_aid = p_block->articles[p_block->article_count - 1].aid; |
| 1140 |
|
|
| 1141 |
|
return last_aid; |
| 1142 |
|
} |
| 1143 |
|
|
| 1144 |
|
int article_block_article_count(void) |
| 1145 |
|
{ |
| 1146 |
|
int ret; |
| 1147 |
|
|
| 1148 |
|
if (p_article_block_pool == NULL || p_article_block_pool->block_count <= 0) |
| 1149 |
|
{ |
| 1150 |
|
return -1; |
| 1151 |
|
} |
| 1152 |
|
|
| 1153 |
|
ret = (p_article_block_pool->block_count - 1) * ARTICLE_PER_BLOCK + |
| 1154 |
|
p_article_block_pool->p_block[p_article_block_pool->block_count - 1]->article_count; |
| 1155 |
|
|
| 1156 |
|
return ret; |
| 1157 |
|
} |
| 1158 |
|
|
| 1159 |
int article_count_of_topic(int32_t aid) |
int article_count_of_topic(int32_t aid) |
| 1160 |
{ |
{ |
| 1161 |
ARTICLE *p_article; |
ARTICLE *p_article; |
| 1196 |
int32_t first_inserted_aid_dest; |
int32_t first_inserted_aid_dest; |
| 1197 |
int move_counter; |
int move_counter; |
| 1198 |
|
|
| 1199 |
if (p_section_dest == NULL) |
if (p_section_src == NULL || p_section_dest == NULL) |
| 1200 |
{ |
{ |
| 1201 |
log_error("section_list_move_topic() NULL pointer error\n"); |
log_error("section_list_move_topic() NULL pointer error\n"); |
| 1202 |
return -1; |
return -1; |
| 1203 |
} |
} |
| 1204 |
|
|
| 1205 |
|
if (p_section_src->sid == p_section_dest->sid) |
| 1206 |
|
{ |
| 1207 |
|
log_error("section_list_move_topic() src and dest section are the same\n"); |
| 1208 |
|
return -1; |
| 1209 |
|
} |
| 1210 |
|
|
| 1211 |
if ((p_article = article_block_find_by_aid(aid)) == NULL) |
if ((p_article = article_block_find_by_aid(aid)) == NULL) |
| 1212 |
{ |
{ |
| 1213 |
log_error("section_list_move_topic() error: article %d not found in block\n", aid); |
log_error("article_block_find_by_aid(aid = %d) error: article not found\n", aid); |
| 1214 |
return -2; |
return -2; |
| 1215 |
} |
} |
| 1216 |
|
|
| 1232 |
move_article_count = article_count_of_topic(aid); |
move_article_count = article_count_of_topic(aid); |
| 1233 |
if (move_article_count <= 0) |
if (move_article_count <= 0) |
| 1234 |
{ |
{ |
| 1235 |
log_error("section_list_count_of_topic_articles(aid = %d) <= 0\n", aid); |
log_error("article_count_of_topic(aid = %d) <= 0\n", aid); |
| 1236 |
return -2; |
return -2; |
| 1237 |
} |
} |
| 1238 |
|
|
| 1251 |
{ |
{ |
| 1252 |
if (p_section_src->sid != p_article->sid) |
if (p_section_src->sid != p_article->sid) |
| 1253 |
{ |
{ |
| 1254 |
log_error("section_list_move_topic() error: src section sid %d != article %d sid %d\n", |
log_error("section_list_move_topic() warning: src section sid %d != article %d sid %d\n", |
| 1255 |
p_section_src->sid, p_article->aid, p_article->sid); |
p_section_src->sid, p_article->aid, p_article->sid); |
| 1256 |
return -2; |
p_article = p_article->p_topic_next; |
| 1257 |
|
continue; |
| 1258 |
} |
} |
| 1259 |
|
|
| 1260 |
// Remove from bi-directional article list of src section |
// Remove from bi-directional article list of src section |
| 1280 |
|
|
| 1281 |
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
if (section_list_find_article_with_offset(p_section_dest, p_article->aid, &page, &offset, &p_next) != NULL) |
| 1282 |
{ |
{ |
| 1283 |
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() warning: article %d already in section %d\n", p_article->aid, p_section_dest->sid); |
| 1284 |
return -4; |
p_article = p_article->p_topic_next; |
| 1285 |
|
continue; |
| 1286 |
} |
} |
| 1287 |
|
|
| 1288 |
// Insert into bi-directional article list of dest section |
// Insert into bi-directional article list of dest section |
| 1360 |
|
|
| 1361 |
if (p_section_dest->article_count - dest_article_count_old != move_article_count) |
if (p_section_dest->article_count - dest_article_count_old != move_article_count) |
| 1362 |
{ |
{ |
| 1363 |
log_error("section_list_move_topic() error: count of moved articles %d != %d\n", |
log_error("section_list_move_topic() warning: count of moved articles %d != %d\n", |
| 1364 |
p_section_dest->article_count - dest_article_count_old, move_article_count); |
p_section_dest->article_count - dest_article_count_old, move_article_count); |
| 1365 |
} |
} |
| 1366 |
|
|
| 1400 |
} |
} |
| 1401 |
else |
else |
| 1402 |
{ |
{ |
| 1403 |
index = (int)(p_section - p_section_list_pool); |
index = (int)(p_section - p_section_list_pool->sections); |
| 1404 |
if (index < 0 || index >= BBS_max_section) |
if (index < 0 || index >= BBS_max_section) |
| 1405 |
{ |
{ |
| 1406 |
log_error("get_section_index(%d) error: index out of range\n", index); |
log_error("get_section_index(%d) error: index out of range\n", index); |
| 1414 |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
int section_list_try_rd_lock(SECTION_LIST *p_section, int wait_sec) |
| 1415 |
{ |
{ |
| 1416 |
int index; |
int index; |
| 1417 |
struct sembuf sops[2]; |
struct sembuf sops[4]; |
| 1418 |
struct timespec timeout; |
struct timespec timeout; |
| 1419 |
int ret; |
int ret; |
| 1420 |
|
|
| 1424 |
return -2; |
return -2; |
| 1425 |
} |
} |
| 1426 |
|
|
| 1427 |
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1428 |
sops[0].sem_op = 0; // check if unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1429 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1430 |
|
|
| 1431 |
sops[1].sem_num = (unsigned short)index; // r_sem of section index |
sops[1].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1432 |
sops[1].sem_op = 1; // lock |
sops[1].sem_op = 1; // lock |
| 1433 |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1434 |
|
|
| 1435 |
|
// Read lock on any specific section will also acquire single read lock on "all section" |
| 1436 |
|
// so that write lock on all section only need to acquire single write on on "all section" |
| 1437 |
|
// rather than to acquire multiple write locks on all the available sections. |
| 1438 |
|
if (index != BBS_max_section) |
| 1439 |
|
{ |
| 1440 |
|
sops[2].sem_num = BBS_max_section * 2 + 1; // w_sem of all section |
| 1441 |
|
sops[2].sem_op = 0; // wait until unlocked |
| 1442 |
|
sops[2].sem_flg = 0; |
| 1443 |
|
|
| 1444 |
|
sops[3].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1445 |
|
sops[3].sem_op = 1; // lock |
| 1446 |
|
sops[3].sem_flg = SEM_UNDO; // undo on terminate |
| 1447 |
|
} |
| 1448 |
|
|
| 1449 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1450 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1451 |
|
|
| 1452 |
ret = semtimedop(section_list_pool_semid, sops, 2, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 2 : 4), &timeout); |
| 1453 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1454 |
|
{ |
| 1455 |
|
log_error("semtimedop(index = %d, lock read) error %d\n", index, errno); |
| 1456 |
|
} |
| 1457 |
|
|
| 1458 |
return ret; |
return ret; |
| 1459 |
} |
} |
| 1471 |
return -2; |
return -2; |
| 1472 |
} |
} |
| 1473 |
|
|
| 1474 |
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1475 |
sops[0].sem_op = 0; // check if unlocked |
sops[0].sem_op = 0; // wait until unlocked |
| 1476 |
sops[0].sem_flg = 0; |
sops[0].sem_flg = 0; |
| 1477 |
|
|
| 1478 |
sops[1].sem_num = (unsigned short)index + 1; // w_sem of section index |
sops[1].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1479 |
sops[1].sem_op = 1; // lock |
sops[1].sem_op = 1; // lock |
| 1480 |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
sops[1].sem_flg = SEM_UNDO; // undo on terminate |
| 1481 |
|
|
| 1482 |
sops[2].sem_num = (unsigned short)index; // r_sem of section index |
sops[2].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1483 |
sops[1].sem_op = 0; // wait until unlocked |
sops[2].sem_op = 0; // wait until unlocked |
| 1484 |
sops[1].sem_flg = 0; |
sops[2].sem_flg = 0; |
| 1485 |
|
|
| 1486 |
timeout.tv_sec = wait_sec; |
timeout.tv_sec = wait_sec; |
| 1487 |
timeout.tv_nsec = 0; |
timeout.tv_nsec = 0; |
| 1488 |
|
|
| 1489 |
ret = semtimedop(section_list_pool_semid, sops, 3, &timeout); |
ret = semtimedop(p_section_list_pool->semid, sops, 3, &timeout); |
| 1490 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1491 |
|
{ |
| 1492 |
|
log_error("semtimedop(index = %d, lock write) error %d\n", index, errno); |
| 1493 |
|
} |
| 1494 |
|
|
| 1495 |
return ret; |
return ret; |
| 1496 |
} |
} |
| 1498 |
int section_list_rd_unlock(SECTION_LIST *p_section) |
int section_list_rd_unlock(SECTION_LIST *p_section) |
| 1499 |
{ |
{ |
| 1500 |
int index; |
int index; |
| 1501 |
struct sembuf sops[1]; |
struct sembuf sops[2]; |
| 1502 |
int ret; |
int ret; |
| 1503 |
|
|
| 1504 |
index = get_section_index(p_section); |
index = get_section_index(p_section); |
| 1507 |
return -2; |
return -2; |
| 1508 |
} |
} |
| 1509 |
|
|
| 1510 |
sops[0].sem_num = (unsigned short)index; // r_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2); // r_sem of section index |
| 1511 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1512 |
sops[0].sem_flg = IPC_NOWAIT; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1513 |
|
|
| 1514 |
ret = semop(section_list_pool_semid, sops, 1); |
if (index != BBS_max_section) |
| 1515 |
|
{ |
| 1516 |
|
sops[1].sem_num = BBS_max_section * 2; // r_sem of all section |
| 1517 |
|
sops[1].sem_op = -1; // unlock |
| 1518 |
|
sops[1].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1519 |
|
} |
| 1520 |
|
|
| 1521 |
|
ret = semop(p_section_list_pool->semid, sops, (index == BBS_max_section ? 1 : 2)); |
| 1522 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1523 |
|
{ |
| 1524 |
|
log_error("semop(index = %d, unlock read) error %d\n", index, errno); |
| 1525 |
|
} |
| 1526 |
|
|
| 1527 |
return ret; |
return ret; |
| 1528 |
} |
} |
| 1539 |
return -2; |
return -2; |
| 1540 |
} |
} |
| 1541 |
|
|
| 1542 |
sops[0].sem_num = (unsigned short)index + 1; // w_sem of section index |
sops[0].sem_num = (unsigned short)(index * 2 + 1); // w_sem of section index |
| 1543 |
sops[0].sem_op = -1; // unlock |
sops[0].sem_op = -1; // unlock |
| 1544 |
sops[0].sem_flg = IPC_NOWAIT; // no wait |
sops[0].sem_flg = IPC_NOWAIT | SEM_UNDO; // no wait |
| 1545 |
|
|
| 1546 |
|
ret = semop(p_section_list_pool->semid, sops, 1); |
| 1547 |
|
if (ret == -1 && errno != EAGAIN && errno != EINTR) |
| 1548 |
|
{ |
| 1549 |
|
log_error("semop(index = %d, unlock write) error %d\n", index, errno); |
| 1550 |
|
} |
| 1551 |
|
|
| 1552 |
|
return ret; |
| 1553 |
|
} |
| 1554 |
|
|
| 1555 |
|
int section_list_rd_lock(SECTION_LIST *p_section) |
| 1556 |
|
{ |
| 1557 |
|
int timer = 0; |
| 1558 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1559 |
|
int ret = -1; |
| 1560 |
|
|
| 1561 |
ret = semop(section_list_pool_semid, sops, 1); |
while (!SYS_server_exit) |
| 1562 |
|
{ |
| 1563 |
|
ret = section_list_try_rd_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1564 |
|
if (ret == 0) // success |
| 1565 |
|
{ |
| 1566 |
|
break; |
| 1567 |
|
} |
| 1568 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1569 |
|
{ |
| 1570 |
|
timer++; |
| 1571 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1572 |
|
{ |
| 1573 |
|
log_error("section_list_rd_lock() tried %d times on section %d\n", sid, timer); |
| 1574 |
|
} |
| 1575 |
|
} |
| 1576 |
|
else // failed |
| 1577 |
|
{ |
| 1578 |
|
log_error("section_list_rd_lock() failed on section %d\n", sid); |
| 1579 |
|
break; |
| 1580 |
|
} |
| 1581 |
|
} |
| 1582 |
|
|
| 1583 |
|
return ret; |
| 1584 |
|
} |
| 1585 |
|
|
| 1586 |
|
int section_list_rw_lock(SECTION_LIST *p_section) |
| 1587 |
|
{ |
| 1588 |
|
int timer = 0; |
| 1589 |
|
int sid = (p_section == NULL ? 0 : p_section->sid); |
| 1590 |
|
int ret = -1; |
| 1591 |
|
|
| 1592 |
|
while (!SYS_server_exit) |
| 1593 |
|
{ |
| 1594 |
|
ret = section_list_try_rw_lock(p_section, SECTION_TRY_LOCK_WAIT_TIME); |
| 1595 |
|
if (ret == 0) // success |
| 1596 |
|
{ |
| 1597 |
|
break; |
| 1598 |
|
} |
| 1599 |
|
else if (errno == EAGAIN || errno == EINTR) // retry |
| 1600 |
|
{ |
| 1601 |
|
timer++; |
| 1602 |
|
if (timer % SECTION_TRY_LOCK_TIMES == 0) |
| 1603 |
|
{ |
| 1604 |
|
log_error("acquire_section_rw_lock() tried %d times on section %d\n", sid, timer); |
| 1605 |
|
} |
| 1606 |
|
} |
| 1607 |
|
else // failed |
| 1608 |
|
{ |
| 1609 |
|
log_error("acquire_section_rw_lock() failed on section %d\n", sid); |
| 1610 |
|
break; |
| 1611 |
|
} |
| 1612 |
|
} |
| 1613 |
|
|
| 1614 |
return ret; |
return ret; |
| 1615 |
} |
} |