Mercurial > hg > octave-lyh
comparison liboctave/oct-mutex.cc @ 7952:2c0a0edae596
reorganize octave_mutex class
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Sat, 19 Jul 2008 22:59:14 -0400 |
parents | 7b8aca1cdf0a |
children | ba2e00a216e8 |
comparison
equal
deleted
inserted
replaced
7951:9d102940bdc7 | 7952:2c0a0edae596 |
---|---|
33 | 33 |
34 #if defined (__WIN32__) && ! defined (__CYGWIN__) | 34 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
35 #include <windows.h> | 35 #include <windows.h> |
36 #endif | 36 #endif |
37 | 37 |
38 class | 38 void |
39 octave_default_mutex : public octave_mutex | 39 octave_base_mutex::lock (void) |
40 { | 40 { |
41 public: | 41 error ("mutex not supported on this platform"); |
42 octave_default_mutex (void) | 42 } |
43 : octave_mutex (-1) { } | |
44 | 43 |
45 void lock (void) | 44 void |
46 { error ("mutex not supported on this platform"); } | 45 octave_base_mutex::unlock (void) |
47 | 46 { |
48 void unlock(void) | 47 error ("mutex not supported on this platform"); |
49 { error ("mutex not supported on this platform"); } | 48 } |
50 }; | |
51 | 49 |
52 #if defined (__WIN32__) && ! defined (__CYGWIN__) | 50 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
53 | 51 |
54 class | 52 class |
55 octave_w32_mutex : public octave_mutex | 53 octave_w32_mutex : public octave_base_mutex |
56 { | 54 { |
57 public: | 55 public: |
58 octave_w32_mutex (void) | 56 octave_w32_mutex (void) |
59 : octave_mutex (-1) | 57 : octave_base_mutex () |
60 { | 58 { |
61 InitializeCriticalSection (&cs); | 59 InitializeCriticalSection (&cs); |
62 } | 60 } |
63 | 61 |
64 ~octave_w32_mutex (void) | 62 ~octave_w32_mutex (void) |
65 { | 63 { |
66 DeleteCriticalSection (&cs); | 64 DeleteCriticalSection (&cs); |
67 } | 65 } |
68 | 66 |
69 void lock (void) | 67 void lock (void) |
70 { | 68 { |
71 EnterCriticalSection (&cs); | 69 EnterCriticalSection (&cs); |
72 } | 70 } |
73 | 71 |
74 void unlock (void) | 72 void unlock (void) |
75 { | 73 { |
76 LeaveCriticalSection (&cs); | 74 LeaveCriticalSection (&cs); |
77 } | 75 } |
78 | 76 |
79 private: | 77 private: |
80 CRITICAL_SECTION cs; | 78 CRITICAL_SECTION cs; |
81 }; | 79 }; |
82 | 80 |
83 #elif defined (HAVE_PTHREAD_H) | 81 #elif defined (HAVE_PTHREAD_H) |
84 | 82 |
85 class | 83 class |
86 octave_pthread_mutex : public octave_mutex | 84 octave_pthread_mutex : public octave_base_mutex |
87 { | 85 { |
88 public: | 86 public: |
89 octave_pthread_mutex (void) | 87 octave_pthread_mutex (void) |
90 : octave_mutex (-1) | 88 : octave_base_mutex () |
91 { | 89 { |
92 pthread_mutexattr_t attr; | 90 pthread_mutexattr_t attr; |
93 | 91 |
94 pthread_mutexattr_init (&attr); | 92 pthread_mutexattr_init (&attr); |
95 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); | 93 pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); |
96 pthread_mutex_init (&pm, &attr); | 94 pthread_mutex_init (&pm, &attr); |
97 pthread_mutexattr_destroy (&attr); | 95 pthread_mutexattr_destroy (&attr); |
98 } | 96 } |
99 | 97 |
100 ~octave_pthread_mutex (void) | 98 ~octave_pthread_mutex (void) |
101 { | 99 { |
102 pthread_mutex_destroy (&pm); | 100 pthread_mutex_destroy (&pm); |
103 } | 101 } |
104 | 102 |
105 void lock (void) | 103 void lock (void) |
106 { | 104 { |
107 pthread_mutex_lock (&pm); | 105 pthread_mutex_lock (&pm); |
108 } | 106 } |
109 | 107 |
110 void unlock (void) | 108 void unlock (void) |
111 { | 109 { |
112 pthread_mutex_unlock (&pm); | 110 pthread_mutex_unlock (&pm); |
113 } | 111 } |
114 | 112 |
115 private: | 113 private: |
116 pthread_mutex_t pm; | 114 pthread_mutex_t pm; |
117 }; | 115 }; |
118 | 116 |
119 #endif | 117 #endif |
120 | 118 |
121 octave_mutex::octave_mutex (void) | 119 octave_mutex::octave_mutex (void) |
123 #if defined (__WIN32__) && ! defined (__CYGWIN__) | 121 #if defined (__WIN32__) && ! defined (__CYGWIN__) |
124 rep = new octave_w32_mutex (); | 122 rep = new octave_w32_mutex (); |
125 #elif defined (HAVE_PTHREAD_H) | 123 #elif defined (HAVE_PTHREAD_H) |
126 rep = new octave_pthread_mutex (); | 124 rep = new octave_pthread_mutex (); |
127 #else | 125 #else |
128 rep = new octave_default_mutex (); | 126 rep = new octave_base_mutex (); |
129 #endif | 127 #endif |
130 rep->count = 1; | |
131 } | 128 } |