This commit is contained in:
Hongze Cheng 2022-03-04 09:32:31 +00:00
parent c4e86a7339
commit ec3f723d33
2 changed files with 22 additions and 7 deletions

View File

@ -291,10 +291,11 @@ static int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno) {
static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg) { static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage *, void *), void *arg) {
int ret; int ret;
int lcode;
int nLoops; int nLoops;
ret = TDB_TRY_LOCK_PAGE(pPage); lcode = TDB_TRY_LOCK_PAGE(pPage);
if (ret == 0) { if (lcode == P_LOCK_SUCC) {
if (TDB_PAGE_INITIALIZED(pPage)) { if (TDB_PAGE_INITIALIZED(pPage)) {
TDB_UNLOCK_PAGE(pPage); TDB_UNLOCK_PAGE(pPage);
return 0; return 0;
@ -309,10 +310,7 @@ static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage
pPage->pPager = pPager; pPage->pPager = pPager;
TDB_UNLOCK_PAGE(pPage); TDB_UNLOCK_PAGE(pPage);
} else { } else if (lcode == P_LOCK_BUSY) {
// TODO: Here, we still use the pthread API here
if (errno != EBUSY) return -1;
nLoops = 0; nLoops = 0;
for (;;) { for (;;) {
if (TDB_PAGE_INITIALIZED(pPage)) break; if (TDB_PAGE_INITIALIZED(pPage)) break;
@ -322,6 +320,8 @@ static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage
nLoops = 0; nLoops = 0;
} }
} }
} else {
return -1;
} }
return 0; return 0;

View File

@ -55,11 +55,26 @@ struct SPage {
}; };
// For page lock // For page lock
#define P_LOCK_SUCC 0
#define P_LOCK_BUSY 1
#define P_LOCK_FAIL -1
#define TDB_INIT_PAGE_LOCK(pPage) pthread_spin_init(&((pPage)->lock), 0) #define TDB_INIT_PAGE_LOCK(pPage) pthread_spin_init(&((pPage)->lock), 0)
#define TDB_DESTROY_PAGE_LOCK(pPage) pthread_spin_destroy(&((pPage)->lock)) #define TDB_DESTROY_PAGE_LOCK(pPage) pthread_spin_destroy(&((pPage)->lock))
#define TDB_LOCK_PAGE(pPage) pthread_spin_lock(&((pPage)->lock)) #define TDB_LOCK_PAGE(pPage) pthread_spin_lock(&((pPage)->lock))
#define TDB_TRY_LOCK_PAGE(pPage) pthread_spin_trylock(&((pPage)->lock))
#define TDB_UNLOCK_PAGE(pPage) pthread_spin_unlock(&((pPage)->lock)) #define TDB_UNLOCK_PAGE(pPage) pthread_spin_unlock(&((pPage)->lock))
#define TDB_TRY_LOCK_PAGE(pPage) \
({ \
int ret; \
if (pthread_spin_trylock(&((pPage)->lock)) == 0) { \
ret = P_LOCK_SUCC; \
} else if (errno == EBUSY) { \
ret = P_LOCK_BUSY; \
} else { \
ret = P_LOCK_FAIL; \
} \
ret; \
})
// For page ref (TODO: Need atomic operation) // For page ref (TODO: Need atomic operation)
#define TDB_INIT_PAGE_REF(pPage) ((pPage)->nRef = 0) #define TDB_INIT_PAGE_REF(pPage) ((pPage)->nRef = 0)