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
- Postgres-XC源码: GTM lock
- Postgres-XC源码:Utilities for GTM standby global values
- postgres-xc
- 优化Postgres-x2 GTM
- postgres-xc介绍
- Postgres-xc介绍
- postgres-xc介绍
- 转载:Postgres-XC: online data redistribution
- Postgres-XC/XL/X2分布式数据库安装
- 转载:Implement Postgres-XC 0.9.7 with 8 servers
- 转载:Postgres-XC: Waiting for online data redistribution
- Complex DML queries and clause push-down in Postgres-XC
- 使用postgres-xc报错Unsupported binary encoding of timestamp
- centos6.5下postgres-XC集群安装与配置
- Postgres-XC 1.0.2 install in 8 KVM host enviroment
- 在linux (CentOs7)上安装Postgres-xc
- Postgres-XC集群部署实例(单机调试)
- XC
- Kalman 滤波器
- Ubuntu 设置环境变量
- Android开发学习笔记
- 迷你MVVM框架 avalonjs 入门教程
- Java基本功练习十五(关于对象的思考【ATM机模拟、贷款类、堆栈类、选课类的实现】)
- Postgres-XC源码: GTM lock
- android设置图片为圆角
- Postgres-XC源码:Utilities for GTM standby global values
- 法制教育对未成年人的作用和实施字体
- C#调用SAPI实现语音识别的两种方法
- Subsets -- Leetcode
- 创建型模式--(再论)单例模式
- C#调用SAPI实现语音合成的两种方法
- 三星 wifi 登录至 无线网络 解决