File: auto_unlock_extension.h

package info (click to toggle)
concurrentqueue 1.0.3%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 2,648 kB
  • sloc: cpp: 37,303; makefile: 88; ansic: 67; python: 46; sh: 18
file content (116 lines) | stat: -rw-r--r-- 2,886 bytes parent folder | download | duplicates (13)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_AUTO_UNLOCK_EXTENSIOn_
#define DLIB_AUTO_UNLOCK_EXTENSIOn_

#include "threads_kernel.h"
#include "rmutex_extension.h"
#include "read_write_mutex_extension.h"
#include "auto_unlock_extension_abstract.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    class auto_unlock
    {
        /*!
            INITIAL VALUE
                - if (m != 0) then
                    - the mutex pointed to by m is locked
                - if (r != 0) then
                    - the mutex pointed to by r is locked
                - if (rw != 0) then
                    - the mutex pointed to by rw is locked
                - exactly one of r, m, or rw is not 0.

            CONVENTION
                - if (m != 0) then
                    - the mutex pointed to by m is locked
                - if (r != 0) then
                    - the mutex pointed to by r is locked
                - if (rw != 0) then
                    - the mutex pointed to by rw is locked
                - exactly one of r, m, or rw is not 0.
        !*/
    public:

        explicit auto_unlock (
            const mutex& m_
        ) : m(&m_),
            r(0),
            rw(0)
        {}

        explicit auto_unlock (
            const rmutex& r_
        ) : m(0),
            r(&r_),
            rw(0)
        {}

        explicit auto_unlock (
            const read_write_mutex& rw_
        ) : m(0),
            r(0),
            rw(&rw_)
        {}

        ~auto_unlock (
        )
        {
            if (m != 0)
                m->unlock();
            else if (r != 0)
                r->unlock();
            else
                rw->unlock();
        }

    private:

        const mutex* m;
        const rmutex* r;
        const read_write_mutex* rw;

        // restricted functions
        auto_unlock(auto_unlock&);        // copy constructor
        auto_unlock& operator=(auto_unlock&);    // assignment operator
    };

// ----------------------------------------------------------------------------------------

    class auto_unlock_readonly
    {

    public:

        explicit auto_unlock_readonly (
            const read_write_mutex& rw_
        ) :
            rw(rw_)
        {}

        ~auto_unlock_readonly (
        )
        {
            rw.unlock_readonly();
        }

    private:

        const read_write_mutex& rw;

        // restricted functions
        auto_unlock_readonly(auto_unlock_readonly&);        // copy constructor
        auto_unlock_readonly& operator=(auto_unlock_readonly&);    // assignment operator
    };

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_AUTO_UNLOCK_EXTENSIOn_