summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/sync_extension/sync_extension_kernel_abstract.h
blob: 35665d430e03875984ac0c4543981cc988210df0 (plain)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (C) 2003  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_SYNC_EXTENSION_KERNEl_ABSTRACT_
#ifdef DLIB_SYNC_EXTENSION_KERNEl_ABSTRACT_

#include "../threads/threads_kernel_abstract.h"
#include "../threads/rmutex_extension_abstract.h"
#include "../threads/rsignaler_extension_abstract.h"
#include "../algs.h"

namespace dlib
{

    template <
        typename base
        >
    class sync_extension : public base
    {

        /*!
            REQUIREMENTS ON base
                base must have a default constructor
                base must implement swap(base&)


            WHAT THIS OBJECT REPRESENTS
                This object represents a general extension to any object (given the
                restrictions on base).  This object gives any object which it extends 
                an integrated rmutex and rsignaler object.  The extended object will 
                then be able to be treated as if it was also a rmutex and rsignaler.

                NOTE that just like the threading api, this object does not check
                its requires clauses so be careful with it.

                Also note that swap() does not swap the rmutex and rsignaler objects.
                the rmutex and rsignaler are associated with the object instance itself, 
                not with whatever the object represents.  
        !*/


        public:

        sync_extension (
        );
        /*!
            ensures
                - #*this is properly initialized
            throws
                - std::bad_alloc
                    this is thrown if there is a problem gathering memory
                - dlib::thread_error
                    this is thrown if there is a problem creating threading objects
                - any exception thrown by the constructor for the parent class base
        !*/

        template <
            typename T
            >
        sync_extension (
            const T& one
        );
        /*!
            ensures
                - #*this is properly initialized
                - the argument one will be passed on to the constructor for the parent 
                  class base.
            throws
                - std::bad_alloc
                    this is thrown if there is a problem gathering memory
                - dlib::thread_error
                    this is thrown if there is a problem creating threading objects
                - any exception thrown by the constructor for the parent class base
        !*/

        template <
            typename T,
            typename U
            >
        sync_extension (
            const T& one,
            const T& two 
        );
        /*!
            ensures
                - #*this is properly initialized
                - the argument one will be passed on to the constructor for the parent 
                  class base as its first argument.
                - the argument two will be passed on to the constructor for the parent 
                  class base as its second argument.
            throws
                - std::bad_alloc
                    this is thrown if there is a problem gathering memory
                - dlib::thread_error
                    this is thrown if there is a problem creating threading objects
                - any exception thrown by the constructor for the parent class base
        !*/


        const rmutex& get_mutex (
        ) const;
        /*!
            ensures
                - returns the rmutex embedded in this object
        !*/

        void lock (
        ) const;
        /*!
            requires
                - the thread calling lock() does not already have a lock on *this
            ensures
                - if (*this is currently locked by another thread) then 
                    - the thread that called lock() on *this is put to sleep until 
                      it becomes available                  
                - if (*this is currently unlocked) then 
                    - #*this becomes locked and the current thread is NOT put to sleep 
                      but now "owns" #*this
        !*/

        void unlock (
        ) const;
        /*!
            ensures
                - #*this is unlocked (i.e. other threads may now lock this object)
        !*/


        void wait (
        ) const;
        /*!
            requires
                - *this is locked and owned by the calling thread
            ensures
                - atomically unlocks *this and blocks the calling thread
                - calling thread will wake if another thread calls signal() or broadcast() 
                  on *this
                - when wait returns the calling thread again has a lock on #*this
        !*/


        bool wait_or_timeout (
            unsigned long milliseconds
        ) const;
        /*!
            requires
                - *this is locked and owned by the calling thread
            ensures
                - atomically unlocks *this and blocks the calling thread
                - calling thread will wake if another thread calls signal() or broadcast() 
                  on *this
                - after the specified number of milliseconds has elapsed the calling thread
                  will wake once *this is free to be locked
                - when wait returns the calling thread again has a lock on #*this

                - returns false if the call to wait_or_timeout timed out 
                - returns true if the call did not time out
        !*/
        
        void signal (
        ) const;
        /*!
            ensures
                - if (at least one thread is waiting on *this) then
                    - at least one of the waiting threads will wake 
        !*/

        void broadcast (
        ) const;
        /*!
            ensures
                - any and all threads waiting on *this will wake 
        !*/

    };

    template <
        typename base
        >
    inline void swap (
        sync_extension<base>& a, 
        sync_extension<base>& b 
    ) { a.swap(b); }
    /*!
        provides a global swap function
    !*/

}

#endif // DLIB_SYNC_EXTENSION_KERNEl_ABSTRACT_