xRedis API  1.1
The C++ Client API for Redis server
xLock.h
1 /*
2  * ----------------------------------------------------------------------------
3  * Copyright (c) 2012-2013, xSky <guozhw at gmail dot com>
4  * All rights reserved.
5  * Distributed under GPL license.
6  * ----------------------------------------------------------------------------
7  */
8 
9 
10 #ifndef _XLOCK_H_
11 #define _XLOCK_H_
12 
13 #ifdef WIN32
14 #include <Windows.h>
15 #else
16 #include <stdio.h>
17 #include <pthread.h>
18 #include <unistd.h>
19 #endif
20 
21 
22 class xLock {
23 private:
24 #ifdef WIN32
25  CRITICAL_SECTION mSection;
26 #else
27  pthread_mutex_t mMutex;
28 #endif
29 
30 public:
31  inline xLock() {
32 #ifdef WIN32
33  InitializeCriticalSection(&mSection);
34 #else
35  pthread_mutexattr_t attr;
36  pthread_mutexattr_init(&attr);
37  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
38  int ret = pthread_mutex_init(&mMutex, &attr);
39  if(ret != 0 ){
40  printf("pthread_mutex_init error %d \n\r",ret);
41  }
42 #endif
43  };
44  inline ~xLock() {
45 #ifdef WIN32
46  DeleteCriticalSection(&mSection);
47 #else
48  pthread_mutex_destroy(&mMutex);
49 #endif
50  }
51  inline void Enter() {
52 #ifdef WIN32
53  EnterCriticalSection(&mSection);
54 #else
55  pthread_mutex_lock(&mMutex);
56 #endif
57  }
58  inline void Leave() {
59 #ifdef WIN32
60  LeaveCriticalSection(&mSection);
61 #else
62  pthread_mutex_unlock(&mMutex);
63 #endif
64  };
65 };
66 
67 class CLockUser {
68 public:
69  inline CLockUser(xLock& lock):mlock(lock) {
70  mlock.Enter();
71  };
72  inline ~CLockUser(){
73  mlock.Leave();
74  }
75 private:
76  xLock& mlock;
77 };
78 
79 #define XLOCK(T) CLockUser lock(T)
80 
81 #endif
82 
Definition: xLock.h:67
Definition: xLock.h:22

Generated on Fri Oct 23 2015 13:13:56 for xRedis API version 1.1.