GNU Radio C++ API Reference: custom_lock.h Source File

| GNU Radio C++ API Reference 3.10.12.0 The Free & Open Software Radio Ecosystem |

Loading...
Searching...
No Matches
custom_lock.h
Go to the documentation of this file.
1/* -*- c++ -*- */
2/*
3 * Copyright 2021 Free Software Foundation, Inc.
4 *
5 * This file is part of GNU Radio
6 *
7 * SPDX-License-Identifier: GPL-3.0-or-later
8 *
9 */
10
11#ifndef INCLUDED_GR_CUSTOM_LOCK_H
12#define INCLUDED_GR_CUSTOM_LOCK_H
13
14#include <gnuradio/api.h>
15#include <gnuradio/logger.h>
16#include <gnuradio/thread/thread.h>
17
18namespace gr {
19
20/*!
21 * Custom lock interface. Objects should implement this interface in order to
22 * use the custom_lock object below. The interface defines two functions that,
23 * as their names suggest, are called when the lock is locked and unlocked
24 * respectively.
25 */
26 class custom_lock_if
27{
28public:
29 virtual ~custom_lock_if(){};
30
31 /*!
32 * This function will be executed on construction of the custom lock.
33 */
34 virtual void on_lock(gr::thread::scoped_lock& lock) = 0;
35
36 /*!
37 * This function will be executed on destruction of the custom lock.
38 */
39 virtual void on_unlock() = 0;
40};
41
42/*!
43 * Class that defines a lock using a mutex and a "locker" that implements the
44 * custom_lock_if interface. The interface defines an on_lock() function that
45 * is executed when the lock is locked and an on_unlock() function that the
46 * is called when the lock is unlocked. Calls to these two functions are
47 * delegated to the locker object.
48 */
49 class custom_lock
50{
51public:
52 explicit custom_lock(gr::thread::mutex& mutex, std::shared_ptr<custom_lock_if> locker)
53 : d_lock(mutex), d_locker(locker)
54 {
55 d_locker->on_lock(d_lock);
56 }
57
58 ~custom_lock() { d_locker->on_unlock(); }
59
60 // Disallow copying and assignment
61 custom_lock(custom_lock const&) = delete;
62 custom_lock& operator=(custom_lock const&) = delete;
63
64private:
65 gr::thread::scoped_lock d_lock;
66 std::shared_ptr<custom_lock_if> d_locker;
67};
68
69} /* namespace gr */
70
71#endif /* INCLUDED_GR_CUSTOM_LOCK_H */
gr::custom_lock_if
Definition custom_lock.h:27
gr::custom_lock_if::on_unlock
virtual void on_unlock()=0
gr::custom_lock_if::~custom_lock_if
virtual ~custom_lock_if()
Definition custom_lock.h:29
gr::custom_lock_if::on_lock
virtual void on_lock(gr::thread::scoped_lock &lock)=0
gr::custom_lock::custom_lock
custom_lock(custom_lock const &)=delete
gr::custom_lock::custom_lock
custom_lock(gr::thread::mutex &mutex, std::shared_ptr< custom_lock_if > locker)
Definition custom_lock.h:52
gr::custom_lock::~custom_lock
~custom_lock()
Definition custom_lock.h:58
gr::custom_lock::operator=
custom_lock & operator=(custom_lock const &)=delete
api.h
logger.h
gr::thread::mutex
boost::mutex mutex
Definition thread.h:37
gr::thread::scoped_lock
boost::unique_lock< boost::mutex > scoped_lock
Definition thread.h:38
gr
GNU Radio logging wrapper.
Definition basic_block.h:29
thread.h