PageRenderTime 6ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/test-suite/swig_examples_lock.h

#
C++ Header | 70 lines | 55 code | 14 blank | 1 comment | 2 complexity | 81c54ce8755f1fcecd301b3408ed8e16 MD5 | raw file
 1
 2# if defined(_WIN32) || defined(__WIN32__)
 3
 4#include <windows.h>
 5
 6namespace SwigExamples {
 7
 8class CriticalSection {
 9public:
10  CriticalSection() {
11    InitializeCriticalSection(&mutex_);
12  }
13  ~CriticalSection() {
14    DeleteCriticalSection(&mutex_);
15  }
16  CRITICAL_SECTION mutex_;
17};
18
19struct Lock {
20  Lock(CriticalSection &cs) : critical_section(cs) {
21    EnterCriticalSection(&critical_section.mutex_);
22  }
23  ~Lock() {
24    LeaveCriticalSection(&critical_section.mutex_);
25  }
26private:
27  CriticalSection &critical_section;
28};
29
30}
31
32#else
33
34#include <pthread.h>
35#ifndef PTHREAD_MUTEX_RECURSIVE_NP
36  // For Cygwin and possibly other OSs: _NP is "non-portable"
37  #define PTHREAD_MUTEX_RECURSIVE_NP PTHREAD_MUTEX_RECURSIVE
38#endif
39
40namespace SwigExamples {
41
42class CriticalSection {
43public:
44  CriticalSection() {
45    pthread_mutexattr_t mutexattr;
46    pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
47    pthread_mutex_init(&mutex_, &mutexattr);
48    pthread_mutexattr_destroy(&mutexattr);
49  }
50  ~CriticalSection() {
51    pthread_mutex_destroy (&mutex_);
52  }
53  pthread_mutex_t mutex_;
54};
55
56struct Lock {
57  Lock(CriticalSection &cs) : critical_section(cs) {
58    pthread_mutex_lock (&critical_section.mutex_);
59  }
60  ~Lock() {
61    pthread_mutex_unlock (&critical_section.mutex_);
62  }
63private:
64  CriticalSection &critical_section;
65};
66
67}
68
69#endif
70