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

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

文章分类

全部博文(104)

文章存档

2018年(1)

2016年(1)

2015年(101)

2014年(1)

我的朋友

分类: 数据库开发技术

2015-02-27 15:51:21

源代码链接地址


今天来看的源码文件是位于路径 src/include/ossUtil.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 OSSUTIL_HPP__
  14. #define OSSUTIL_HPP__
  15. #include "core.hpp"
  16. #include <boost/thread/thread.hpp>
  17. #include <boost/date_time/posix_time/posix_time.hpp>
  18. #include <boost/thread/xtime.hpp>
  19. #if defined _WINDOWS
  20. #include <TlHelp32.h>
  21. #endif

  22. #if defined _WINDOWS
  23. /*
  24. ossSleepmicros 该方法是用来让系统睡眠传入的时间的。
  25. 精准度达到纳秒级别; 如果操作系统是 windows 的话,
  26. 则会调用 boost 中的时间库函数
  27. 将传入的代表时间的数值 s(默认单位为微秒) 除以 10^6
  28. 得到的是以秒为单位的计数数值,将其赋值为 boost::xtime xt 
  29. 用以计数 秒 的变量 xt.sec 
  30. 同时将传入的数值 s 与 10^6 去余操作,得到的数值便是以纳秒
  31. 计数的时间,将其赋值到 boost::xtime xt ; 变量的
  32. xt.nsec 字段上面
  33. ---------
  34. 然后在对 xt.nsec 进行进一步的细化计算,如果该数值 >= 10^6
  35. 那么将其每次 减去数值 10^6 然后对 xt.sec 进行加 1 进位
  36. -----------------
  37. 最后调用 boost::thread::sleep 方法将创建好的时间计数结构体
  38. xt (boost::xtime 类型) 传入,便是当前运行该方法的线程(进程)
  39. 将要睡眠的时间
  40. */

  41. inline void ossSleepmicros ( unsigned int s )
  42. {
  43.    boost::xtime xt ;
  44.    boost::xtime_get ( &xt, boost::TIME_UTC_ ) ;
  45.    xt.sec += (int)(s/1000000) ;
  46.    xt.nsec += (int)((s%1000000)*1000) ;
  47.    if ( xt.nsec >= 1000000000 )
  48.    {
  49.       xt.nsec -= 1000000000 ;
  50.       xt.sec ++ ;
  51.    }
  52.    boost::thread::sleep(xt) ;
  53. }
  54. #else
  55. /*
  56.  下面的这个方法是用在 linux/unix 操作系统中,对当前正在运行的
  57. 进程(线程)执行睡眠操作的方法,其计数精度为纳秒级别
  58.   在该方法中,使用的计数数据结构为 struct timespec  ;
  59. 它的结构体定义代码如下:
  60. // time.h

  61. struct timespec
  62. {
  63.     time_t tv_sec ; // second
  64.     long   tv_nsec ; // nano-second
  65. } ;

  66. 方法 nanosleep 的函数原型定义如下:
  67. int naosleep ( const struct timespec *req , struct timespec *rem ) ;

  68. 其中 传入的是指向 struct timespec 的指针;
  69.  参数一 为需要当前线程睡眠的时间,
  70. 如果,当前的线程(进程) 的状态是可以被其他信号中断的,那么在 req 所指定的时间
  71. 到来之前该线程就会醒来 ;

  72. 在这种情况下,如果参数传入的数值不为 NULL,而同样的是一个 struct timespec 指针,
  73. 那么 nanosleep 将会把剩余的时间(秒+纳秒)按照 struct timespec 赋值给通过 
  74. 值-地址 方式传入的参数 rem (remain - time)变量中,

  75. 这样调用该函数的使用者,可以通过参数1 来设定需要休眠的时间,而通过参数 2 来获知
  76. 按照规定的休眠的时间还剩余的时间

  77. 使用方法见下面的链接
  78. */
  79. inline void ossSleepmicros ( unsigned int s )
  80. {
  81.    struct timespec t ;
  82.    t.tv_sec = (time_t) (s/1000000) ;
  83.    t.tv_nsec = 1000 * (s % 1000000 ) ;
  84.    while ( nanosleep ( &t, &t )==-1 && errno==EINTR ) ;
  85. }
  86. #endif
  87. /*
  88. 下面的方法也是用于为当前运行该方法的线程(进程) 来设定需要睡眠的时间的,
  89. 不过,传入的时间计数单位是 毫秒(milliseconds)
  90. */
  91. inline void ossSleepmillis ( unsigned int s )
  92. {
  93.    ossSleepmicros ( s * 1000 ) ;
  94. }

  95. /*
  96.     下面定义的方法是用来获取当前正在运行的线程(进程) 的ID号码
  97.     以及它的父线程(进程)的 ID号码,
  98.     windows 下面获取父线程(进程)有些麻烦,
  99.     linux/unix 下面只需要一个方法即可
  100. */
  101. #if defined _WINDOWS
  102. typedef DWORD OSSPID ;
  103. typedef DWORD OSSTID ;
  104. #else
  105. typedef pid_t OSSPID ;
  106. typedef pthread_t OSSTID ;
  107. #endif
  108. inline OSSPID ossGetParentProcessID ()
  109. {
  110. #if defined _WINDOWS
  111.    OSSPID pid = -1 ;
  112.    OSSPID ppid = -1 ;
  113.    HANDLE hSnapshot = 0 ;
  114.    PROCESSENTRY32 pe = {0} ;
  115.    pe.dwSize = sizeof(PROCESSENTRY32) ;
  116.    hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 ) ;
  117.    if ( hSnapshot == 0 )
  118.      goto error ;
  119.    pid = GetCurrentProcessId () ;
  120.    if ( Process32First ( hSnapshot, &pe ) )
  121.    {
  122.      do
  123.      {
  124.          if ( pe.th32ProcessID == pid )
  125.          {
  126.              ppid = pe.th32ParentProcessID ;
  127.              goto done ;
  128.          }
  129.      } while ( Process32Next ( hSnapshot, &pe ) ) ;
  130.    }
  131. done:
  132.    return ppid ;
  133. error :
  134.    goto done ;
  135. #else
  136.    return getppid () ;
  137. #endif
  138. }

  139. inline OSSPID ossGetCurrentProcessID ()
  140. {
  141. #if defined _WINDOWS
  142.    return GetCurrentProcessId () ;
  143. #else
  144.    return getpid () ;
  145. #endif
  146. }

  147. inline OSSTID ossGetCurrentThreadID ()
  148. {
  149. #if defined _WINDOWS
  150.    return GetCurrentThreadId () ;
  151. #else
  152.    return syscall(SYS_gettid) ;
  153. #endif
  154. }

  155. #endif


struct timeval 数据结构声明代码及即使用方法举例

struct timespec 数据结构声明代码及使用方法举例




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