Postgres-XC源码: GTM lock

来源:互联网 发布:mui.js文档 编辑:程序博客网 时间:2024/06/06 11:45

come from :Postgres-XC source code 1.04

 * gtm_lock.c
 * Handling for locks in GTM


#include "gtm/gtm_c.h"#include "gtm/gtm_lock.h"#include "gtm/elog.h"/* * Acquire the request lock. Block if the lock is not available * * TODO We should track the locks acquired in the thread specific context. If an * error is thrown and cought, we don't want to keep holding to those locks * since that would lead to a deadlock. Right now, we assume that the caller * will appropriately catch errors and release the locks sanely. */boolGTM_RWLockAcquire(GTM_RWLock *lock, GTM_LockMode mode){int status;switch (mode){case GTM_LOCKMODE_WRITE:status = pthread_rwlock_wrlock(&lock->lk_lock);break;case GTM_LOCKMODE_READ:status = pthread_rwlock_rdlock(&lock->lk_lock);break;default:elog(ERROR, "Invalid lockmode");break;}return status ? false : true;}/* * Release previously acquired lock */boolGTM_RWLockRelease(GTM_RWLock *lock){int status;status = pthread_rwlock_unlock(&lock->lk_lock);return status ? false : true;}/* * Initialize a lock */intGTM_RWLockInit(GTM_RWLock *lock){return pthread_rwlock_init(&lock->lk_lock, NULL);}/* * Destroy a lock */intGTM_RWLockDestroy(GTM_RWLock *lock){return pthread_rwlock_destroy(&lock->lk_lock);}/* * Conditionally acquire a lock. If the lock is not available, the function * immediately returns without blocking. * * Returns true if lock is successfully acquired. Otherwise returns false */boolGTM_RWLockConditionalAcquire(GTM_RWLock *lock, GTM_LockMode mode){int status;switch (mode){case GTM_LOCKMODE_WRITE:status = pthread_rwlock_trywrlock(&lock->lk_lock);break;case GTM_LOCKMODE_READ:status = pthread_rwlock_tryrdlock(&lock->lk_lock);break;default:elog(ERROR, "Invalid lockmode");break;}return status ? false : true;}/* * Initialize a mutex lock */intGTM_MutexLockInit(GTM_MutexLock *lock){return pthread_mutex_init(&lock->lk_lock, NULL);}/* * Destroy a mutex lock */intGTM_MutexLockDestroy(GTM_MutexLock *lock){return pthread_mutex_destroy(&lock->lk_lock);}/* * Acquire a mutex lock * * Return true if the lock is successfully acquired, else return false. */boolGTM_MutexLockAcquire(GTM_MutexLock *lock){int status = pthread_mutex_lock(&lock->lk_lock);return status ? false : true;}/* * Release previously acquired lock */boolGTM_MutexLockRelease(GTM_MutexLock *lock){return pthread_mutex_unlock(&lock->lk_lock);}/* * Conditionally acquire a lock. If the lock is not available, the function * immediately returns without blocking. * * Returns true if lock is successfully acquired. Otherwise returns false */boolGTM_MutexLockConditionalAcquire(GTM_MutexLock *lock){int status = pthread_mutex_trylock(&lock->lk_lock);return status ? false : true;}/* * Initialize a condition variable */intGTM_CVInit(GTM_CV *cv){return pthread_cond_init(&cv->cv_condvar, NULL);}/* * Destroy the conditional variable */intGTM_CVDestroy(GTM_CV *cv){return pthread_cond_destroy(&cv->cv_condvar);}/* * Wake up all the threads waiting on this conditional variable */intGTM_CVBcast(GTM_CV *cv){return pthread_cond_broadcast(&cv->cv_condvar);}/* * Wake up only one thread waiting on this conditional variable */intGTM_CVSignal(GTM_CV *cv){return pthread_cond_signal(&cv->cv_condvar);}/* * Wait on a conditional variable. The caller must have acquired the mutex lock * already. */intGTM_CVWait(GTM_CV *cv, GTM_MutexLock *lock){return pthread_cond_wait(&cv->cv_condvar, &lock->lk_lock);}

可以看出,GTM对线程锁的使用是非常明了,lock mode 只有两种: GTM_LOCKMODE_WRITE,GTM_LOCKMODE_READ;

作为Postgres-XC调度中心的GTM,提供GXID等全局事务相关处理,有关锁的代码部分太过简洁,估计后面会逐渐完善。



参考:

(1)读写锁pthread_rwlock_t的使用

(2)pthread_rwlock_t基本用法

(3)pthread_cond_signal与pthread_cond_wait详解 


0 0
原创粉丝点击