summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/threads/rsignaler_extension.h
blob: bfb5a7ecb89d97ed6c36141b9ac171b1cf9d825a (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
// Copyright (C) 2006  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#ifndef DLIB_RSIGNALER_EXTENSIOn_
#define DLIB_RSIGNALER_EXTENSIOn_ 

#include "rsignaler_extension_abstract.h"
#include "rmutex_extension.h"

namespace dlib
{

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

    class rsignaler
    {
    public:
        rsignaler (
            const rmutex& associated_mutex
        ) : 
            assoc_mutex(associated_mutex),
            s(m)
        {}

        ~rsignaler (
        )
        {}

        void wait (
        ) const
        {
            m.lock();
            const unsigned long lock_count = assoc_mutex.lock_count();
            assoc_mutex.unlock(lock_count);
            s.wait();
            m.unlock();
            assoc_mutex.lock(lock_count);
        }

        bool wait_or_timeout (
            unsigned long milliseconds
        ) const
        {
            m.lock();
            const unsigned long lock_count = assoc_mutex.lock_count();
            assoc_mutex.unlock(lock_count);
            bool res = s.wait_or_timeout(milliseconds);
            m.unlock();
            assoc_mutex.lock(lock_count);
            return res;
        }

        void signal (
        ) const 
        { 
            m.lock();
            s.signal(); 
            m.unlock();
        }

        void broadcast (
        ) const 
        { 
            m.lock();
            s.broadcast(); 
            m.unlock();
        }

        const rmutex& get_mutex (
        ) const { return assoc_mutex; }

    private:

        const rmutex& assoc_mutex;
        mutex m;
        signaler s;


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

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

}

#endif // DLIB_RSIGNALER_EXTENSIOn_