Chinaunix首页 | 论坛 | 博客
  • 博客访问: 563407
  • 博文数量: 104
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1559
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-21 00:58
个人简介

锻炼精神,首先要锻炼肉体

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: 数据库开发技术

2015-02-24 12:55:07

源代码链接地址


今天来看的源码文件是位于路径 src/include/ossLatch.hpp 下的文件

// ossLatch.hpp

 在该头文件中定义的是数据库中在进行互斥访问数据库中的对象

点击(此处)折叠或打开

  1. /*******************************************************************************
  2.    Copyright (C) 2013 SequoiaDB Software Inc.

  3.    This program is free software: you can redistribute it and/or modify
  4.    it under the terms of the GNU Affero General Public License, version 3,
  5.    as published by the Free Software Foundation.

  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9.    GNU Affero General Public License for more details.

  10.    You should have received a copy of the GNU Affero General Public License
  11.    along with this program. If not, see <http://www.gnu.org/license/>.
  12. *******************************************************************************/
  13. #ifndef OSSLATCH_HPP__
  14. #define OSSLATCH_HPP__

  15. #include "core.hpp"
  16. // 下面的宏定义的是提高数据库软件的可移植性,使得代码无论是在 windows 下面还是 linux 操作系统中
  17. // 都能够以同样的变量、方法名称来调用
  18. // 主要关注 linux 中的系统编程方法

  19. #ifdef _WINDOWS                // 如果操作系统是 windows 那么定义的宏方法便是 windows 中的函数
  20. #define oss_mutex_t                        CRITICAL_SECTION
  21. #define oss_mutex_init(__lock, __attribute)        InitializeCriticalSection( (__lock) )
  22. #define oss_mutex_destroy                DeleteCriticalSection
  23. #define oss_mutex_lock                    EnterCriticalSection
  24. #define oss_mutex_trylock(__lock)        (TRUE == TryEnterCriticalSection( (__lock) ) )
  25. #define oss_mutex_unlock                LeaveCriticalSection

  26. #define oss_rwlock_t                    SRWLOCK
  27. #define oss_rwlock_init(__lock, __attribute)        InitializeSRWLock( (__lock) )
  28. #define oss_rwlock_destroy(__lock)        (1)    
  29. #define oss_rwlock_rdlock                AcquireSRWLockShared
  30. #define oss_rwlock_rdunlock                ReleaseSRWLockShared
  31. #define oss_rwlock_wrlock                AcquireSRWLockExclusive
  32. #define oss_rwlock_wrunlock                ReleaseSRWLockExclusive
  33. #define oss_rwlock_rdtrylock(__lock)    (false)
  34. #define oss_rwlock_wrtrylock(__lock)    (false)

  35. #else

  36. #define oss_mutex_t                      pthread_mutex_t        // 互斥量变量
  37. #define oss_mutex_init                   pthread_mutex_init     // 互斥量动态初始化方法
  38. #define oss_mutex_destroy                pthread_mutex_destroy  // 互斥量销毁方法
  39. #define oss_mutex_lock                   pthread_mutex_lock     // 互斥量上锁方法
  40. #define oss_mutex_trylock(__lock)        (pthread_mutex_trylock( (__lock) ) == 0 )
  41. #define oss_mutex_unlock                 pthread_mutex_unlock

  42. #define oss_rwlock_t                     pthread_rwlock_t
  43. #define oss_rwlock_init                  pthread_rwlock_init
  44. #define oss_rwlock_destroy               pthread_rwlock_destroy
  45. #define oss_rwlock_rdlock                pthread_rwlock_rdlock
  46. #define oss_rwlock_rdunlock              pthread_rwlock_unlock
  47. #define oss_rwlock_wrlock                pthread_rwlock_wrlock
  48. #define oss_rwlock_wrunlock              pthread_rwlock_unlock
  49. #define oss_rwlock_rdtrylock(__lock)     (pthread_rwlock_tryrdlock( (__lock) ) == 0 )
  50. #define oss_rwlock_wrtrylock(__lock)     (pthread_rwlock_trywrlock ( ( __lock) ) == 0 )

  51. #endif

  52. enum OSS_LATCH_MODE  // 通过定义枚举变量来定义锁的两种类型 
  53. {
  54.    SHARED ,         // SHARED 是共享锁
  55.    EXCLUSIVE        // EXCLUSIVE 是互斥锁/独占锁
  56. } ;

  57. class ossXLatch     //  ossXLatch 类 , 封装了互斥锁/独占锁
  58. {
  59. private :
  60.    oss_mutex_t _lock ;  // ossXLatch 类中封装的互斥量对象 ,
  61.                         // 互斥量每次允许一个运行单位访问其所保护的对象,功能等同于互斥锁
  62. public :
  63.    ossXLatch ()
  64.    {
  65.       oss_mutex_init ( &_lock, 0 ) ;  
  66.    }
  67.    ~ossXLatch ()
  68.    {
  69.         oss_mutex_destroy(&_lock);
  70.    }
  71. // 上面是 ossXLatch 的构建于析构方法

  72.    void get ()
  73.    {
  74.         oss_mutex_lock(&_lock);
  75.    }
  76. //get: 该方法用于获取 ossXLatch 中的私有变量 _lock 

  77.    void release ()
  78.    {
  79.         oss_mutex_unlock(&_lock);
  80.    }
  81. //release: 该方法用于释放 _lock 变量

  82.    bool try_get ()
  83.    {
  84.         return oss_mutex_trylock(&_lock);
  85.    }
  86. //try_get: 该方法用于试图获取 _lock 变量,试图对其上锁,如果有其余的线程占用该互斥量保护的资源
  87. //那么此次获取 _lock 便会失败,如果获取失败返回 false, 如果获取成功返回 true
  88. } ;

  89. class ossSLatch  // 封装了共享锁/读写锁 ,既可以用作读锁,也可以用作写锁来使用
  90. {
  91. private :
  92.    oss_rwlock_t _lock ;  // 使用读写锁作为 ossSLatch 的基本类型
  93. public :
  94.    ossSLatch ()
  95.    {
  96.       oss_rwlock_init ( &_lock, 0 ) ;
  97.    }

  98.    ~ossSLatch ()
  99.    {
  100.       oss_rwlock_destroy ( &_lock ) ;
  101.    }
  102. // ossSLatch 的构造函数和析构函数

  103.    void get ()
  104.    {
  105.       oss_rwlock_wrlock ( &_lock ) ;
  106.    }
  107. //get: 该方法用于获取 ossSLatch 类中的私有变量

  108.    void release ()
  109.    {
  110.       oss_rwlock_wrunlock ( &_lock ) ;
  111.    }
  112. //release : 该方法封装了用于释放 _lock 锁对象的方法 

  113.    bool try_get ()
  114.    {
  115.       return ( oss_rwlock_wrtrylock ( &_lock ) ) ;
  116.    }
  117. // try_get 该方法中封装的 oss_rwlock_wrtrylock 是通过宏定义来实现的
  118. // 通常的获取锁的方法是在多个线程/进程抢占锁的时候,没有抢占到锁的线程/进程会阻塞到该锁的等待队列上面,进行死等
  119. // 这种处理方法比较麻烦,所以有了 try_get 试上锁的方法,即便是线程/进程获取不到锁资源,也不会阻塞死等,而是立即返回。
  120. // 这种操作机制消耗系统资源比较小
  121. // 如果抢占到了锁,那么锁对象便被赋值给 ossSLatch中的 _lock 变量,返回值为 true, 下一步通过调用 get() 方法便可获得该锁;
  122. // 如果没有抢到,那么返回值为 false

  123.    void get_shared ()
  124.    {
  125.       oss_rwlock_rdlock ( &_lock ) ;
  126.    }
  127. //get_shared: 上面的两个方法是获取写锁的,虽然在方法上面没有标识;而下面的这两个方法是与上面的两个方法相呼应的
  128. // 用于获取读锁,读锁常被用于是共享锁也就是在对某个资源进行读取的时,这种锁锁保护的资源对象可以被多个 线程/进程同时访问(读取)

  129.    void release_shared ()
  130.    {
  131.       oss_rwlock_rdunlock ( &_lock ) ;
  132.    }
  133. // release_shared: 该方法用于释放线程/进程占用的锁资源

  134.    bool try_get_shared ()
  135.    {
  136.       return ( oss_rwlock_rdtrylock ( &_lock ) ) ;
  137.    }
  138. //try_get_shared :该方法用户试探性的获取读锁,如果成功获取则返回 true,
  139. // 如果没有成功接收,返回 false
  140. } ;

  141. #endif

在这里主要学习使用源码中定义的宏方法中所调用的 pthread 相关方法与变量
[通过链接可以查看函数原型使用方法]

  1. pthread_mutex_t        
  2. pthread_mutex_init
  3. pthread_mutex_destroy
  4. pthread_mutex_lock
  5. pthread_mutex_trylock
  6. pthread_mutex_unlock
  7. pthread_rwlock_t
  8. pthread_rwlock_init
  9. pthread_rwlock_destroy
  10. pthread_rwlock_rdlock
  11. pthread_rwlock_unlock
  12. pthread_rwlock_wrlock

    end
阅读(721) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~