summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/include/gnuradio/custom_lock.h
blob: b8d9ed6d47c9d2403e23d38efd520a3d4c148657 (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
/* -*- c++ -*- */
/*
 * Copyright 2021 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 *
 */

#ifndef INCLUDED_GR_CUSTOM_LOCK_H
#define INCLUDED_GR_CUSTOM_LOCK_H

#include <gnuradio/api.h>
#include <gnuradio/logger.h>
#include <gnuradio/thread/thread.h>

namespace gr {

/*!
 * Custom lock interface. Objects should implement this interface in order to
 * use the custom_lock object below. The interface defines two functions that,
 * as their names suggest, are called when the lock is locked and unlocked
 * respectively.
 */
class custom_lock_if
{
public:
    /*!
     * This function will be executed on construction of the custom lock.
     */
    virtual void on_lock(gr::thread::scoped_lock& lock) = 0;

    /*!
     * This function will be executed on destruction of the custom lock.
     */
    virtual void on_unlock() = 0;
};

/*!
 * Class that defines a lock using a mutex and a "locker" that implements the
 * custom_lock_if interface. The interface defines an on_lock() function that
 * is executed when the lock is locked and an on_unlock() function that the
 * is called when the lock is unlocked. Calls to these two functions are
 * delegated to the locker object.
 */
class custom_lock
{
public:
    explicit custom_lock(gr::thread::mutex& mutex, std::shared_ptr<custom_lock_if> locker)
        : d_mutex(mutex), d_lock(mutex), d_locker(locker)
    {
        d_locker->on_lock(d_lock);
    }

    ~custom_lock() { d_locker->on_unlock(); }

    // Disallow copying and assignment
    custom_lock(custom_lock const&) = delete;
    custom_lock& operator=(custom_lock const&) = delete;

private:
    gr::thread::mutex& d_mutex;
    gr::thread::scoped_lock d_lock;
    std::shared_ptr<custom_lock_if> d_locker;
};

} /* namespace gr */

#endif /* INCLUDED_GR_CUSTOM_LOCK_H */