1 // Copyright (C) 2006  Davis E. King (davis@dlib.net)
2 // License: Boost Software License   See LICENSE.txt for the full license.
3 #undef DLIB_AUTO_UNLOCK_EXTENSIOn_ABSTRACT_
4 #ifdef DLIB_AUTO_UNLOCK_EXTENSIOn_ABSTRACT_
5 
6 #include "threads_kernel_abstract.h"
7 #include "rmutex_extension_abstract.h"
8 #include "read_write_mutex_extension_abstract.h"
9 
10 namespace dlib
11 {
12 
13 // ----------------------------------------------------------------------------------------
14 
15     class auto_unlock
16     {
17         /*!
18             INITIAL VALUE
19                 The mutex given in the constructor is associated with this object.
20 
21             WHAT THIS OBJECT REPRESENTS
22                 This object represents a mechanism for automatically unlocking
23                 a mutex object.  It is useful when you already have a locked mutex
24                 and want to make sure it gets unlocked even if an exception is thrown
25                 or you quit the function at a weird spot.
26         !*/
27     public:
28 
29         explicit auto_unlock (
30             const mutex& m
31         );
32         /*!
33             ensures
34                 - #*this is properly initialized
35                 - does not modify m in any way
36         !*/
37 
38         explicit auto_unlock (
39             const rmutex& m
40         );
41         /*!
42             ensures
43                 - #*this is properly initialized
44                 - does not modify m in any way
45         !*/
46 
47         explicit auto_unlock (
48             const read_write_mutex& m
49         );
50         /*!
51             ensures
52                 - #*this is properly initialized
53                 - does not modify m in any way
54         !*/
55 
56         ~auto_unlock (
57         );
58         /*!
59             ensures
60                 - all resources allocated by *this have been freed
61                 - calls unlock() on the mutex associated with *this
62         !*/
63 
64     private:
65         // restricted functions
66         auto_unlock(auto_unlock&);        // copy constructor
67         auto_unlock& operator=(auto_unlock&);    // assignment operator
68     };
69 
70 // ----------------------------------------------------------------------------------------
71 
72     class auto_unlock_readonly
73     {
74         /*!
75             INITIAL VALUE
76                 The mutex given in the constructor is associated with this object.
77 
78             WHAT THIS OBJECT REPRESENTS
79                 This object represents a mechanism for automatically unlocking
80                 a read_write_mutex object.  It is useful when you already have a locked mutex
81                 and want to make sure it gets unlocked even if an exception is thrown
82                 or you quit the function at a weird spot.  Note that the mutex
83                 is unlocked by calling unlock_readonly() on it.
84         !*/
85     public:
86 
87         explicit auto_unlock_readonly (
88             const read_write_mutex& m
89         );
90         /*!
91             ensures
92                 - #*this is properly initialized
93                 - does not modify m in any way
94         !*/
95 
96         ~auto_unlock_readonly (
97         );
98         /*!
99             ensures
100                 - all resources allocated by *this have been freed
101                 - calls unlock_readonly() on the mutex associated with *this
102         !*/
103 
104     private:
105         // restricted functions
106         auto_unlock_readonly(auto_unlock_readonly&);        // copy constructor
107         auto_unlock_readonly& operator=(auto_unlock_readonly&);    // assignment operator
108     };
109 
110 // ----------------------------------------------------------------------------------------
111 
112 }
113 
114 #endif // DLIB_AUTO_UNLOCK_EXTENSIOn_ABSTRACT_
115 
116 
117