Mercurial > hg > octave-nkf
annotate liboctave/oct-mutex.h @ 7936:78400fde223e
Support for backend-to-octave event management
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 17 Jul 2008 07:58:50 -0400 |
parents | 5a156ab94dd2 |
children | 7b8aca1cdf0a |
rev | line source |
---|---|
7934 | 1 /* |
2 | |
3 Copyright (C) 2008 Michael Goffioul | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
9 Free Software Foundation; either version 3 of the License, or (at your | |
10 option) any later version. | |
11 | |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
18 along with Octave; see the file COPYING. If not, see | |
19 <http://www.gnu.org/licenses/>. | |
20 | |
21 */ | |
22 | |
23 #if !defined (octave_octave_mutex_h) | |
24 #define octave_octave_mutex_h 1 | |
25 | |
26 class | |
27 OCTAVE_API | |
28 octave_mutex | |
29 { | |
30 public: | |
31 octave_mutex (void); | |
32 | |
33 octave_mutex (const octave_mutex& m) | |
34 { | |
35 rep = m.rep; | |
36 rep->count++; | |
37 } | |
38 | |
39 virtual ~octave_mutex (void) | |
40 { | |
41 if (rep && --rep->count == 0) | |
42 { | |
43 delete rep; | |
44 rep = 0; | |
45 } | |
46 } | |
47 | |
48 octave_mutex& operator = (const octave_mutex& m) | |
49 { | |
50 if (rep != m.rep) | |
51 { | |
52 if (rep && --rep->count == 0) | |
53 delete rep; | |
54 | |
55 rep = m.rep; | |
56 rep->count++; | |
57 } | |
58 | |
59 return *this; | |
60 } | |
61 | |
62 virtual void lock (void) | |
63 { rep->lock (); } | |
64 | |
65 virtual void unlock (void) | |
66 { rep->unlock (); } | |
67 | |
68 protected: | |
69 explicit octave_mutex (int /* dummy */) | |
70 : count (0) { } | |
71 | |
72 protected: | |
73 union | |
74 { | |
75 octave_mutex *rep; | |
76 int count; | |
77 }; | |
78 }; | |
79 | |
80 class | |
81 octave_autolock | |
82 { | |
83 public: | |
84 octave_autolock (const octave_mutex& m) | |
85 : mutex (m) | |
86 { | |
87 mutex.lock (); | |
88 } | |
89 | |
90 ~octave_autolock (void) | |
91 { | |
92 mutex.unlock (); | |
93 } | |
94 | |
95 private: | |
7936
78400fde223e
Support for backend-to-octave event management
John W. Eaton <jwe@octave.org>
parents:
7934
diff
changeset
|
96 |
78400fde223e
Support for backend-to-octave event management
John W. Eaton <jwe@octave.org>
parents:
7934
diff
changeset
|
97 // No copying or default constructor! |
78400fde223e
Support for backend-to-octave event management
John W. Eaton <jwe@octave.org>
parents:
7934
diff
changeset
|
98 octave_autolock (void); |
78400fde223e
Support for backend-to-octave event management
John W. Eaton <jwe@octave.org>
parents:
7934
diff
changeset
|
99 octave_autolock (const octave_autolock&); |
78400fde223e
Support for backend-to-octave event management
John W. Eaton <jwe@octave.org>
parents:
7934
diff
changeset
|
100 octave_autolock& operator = (const octave_autolock&); |
7934 | 101 |
102 private: | |
103 octave_mutex mutex; | |
104 }; | |
105 | |
106 #endif |