-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathmutex.h
128 lines (111 loc) · 2.91 KB
/
mutex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/***********************************************************************
* Copyright (c) 2012, Baidu Inc. All rights reserved.
*
* Licensed under the BSD License
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* license.txt
*********************************************************************/
#ifndef _BGCC_MUTEX_H_
#define _BGCC_MUTEX_H_
#ifdef _WIN32
#include <Windows.h>
#else
#include <pthread.h>
#include <limits.h>
#endif
#include "bgcc_stdint.h"
#ifdef _WIN32
#define BGCC_MUTEX_WAIT_INFINITE INFINITE
#define bgcc_mutex_t HANDLE
#else
#define BGCC_MUTEX_WAIT_INFINITE UINT_MAX
#define bgcc_mutex_t pthread_mutex_t
#endif
namespace bgcc {
/**
* @brief 实现线程间的互斥访问
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时23分40秒
*/
class Mutex {
public:
/**
* @brief Mutex 构造函数
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时24分08秒
*/
Mutex();
/**
* @brief ~Mutex 构造函数
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时24分14秒
*/
~Mutex();
/**
* @brief lock 加锁操作
*
* @param millisecond 超时时长(毫秒)
*
* @return 加锁成功返回0;否则返回错误码
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时24分23秒
*/
int32_t lock(uint32_t millisecond = BGCC_MUTEX_WAIT_INFINITE);
/**
* @brief try_lock 尝试加锁操作
*
* @return 加锁成功返回0;否则返回错误码
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时24分57秒
*/
int32_t try_lock();
/**
* @brief unlock 解锁操作
*
* @return 解锁成功返回0;否则返回错误码
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时25分44秒
*/
int32_t unlock();
protected:
/**
* @brief Mutex 禁用拷贝构造函数
*
* @param Mutex
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时28分45秒
*/
Mutex(const Mutex&);
/**
* @brief operator= 禁用赋值运算符
*
* @param Mutex
*
* @return
* @see
* @note
* @author liuxupeng(liuxupeng@baidu.com)
* @date 2012年05月30日 17时29分08秒
*/
Mutex& operator=(const Mutex&);
private:
bgcc_mutex_t _mutex;
}; // end of class Mutex
}
#endif // _BGCC_MUTEX_H_