GNU Radio Manual and C++ API Reference  3.10.9.1
The Free & Open Software Radio Ecosystem
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 
18 namespace 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  */
27 {
28 public:
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  */
50 {
51 public:
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 
64 private:
66  std::shared_ptr<custom_lock_if> d_locker;
67 };
68 
69 } /* namespace gr */
70 
71 #endif /* INCLUDED_GR_CUSTOM_LOCK_H */
Definition: custom_lock.h:27
virtual void on_unlock()=0
virtual ~custom_lock_if()
Definition: custom_lock.h:29
virtual void on_lock(gr::thread::scoped_lock &lock)=0
Definition: custom_lock.h:50
custom_lock(custom_lock const &)=delete
custom_lock(gr::thread::mutex &mutex, std::shared_ptr< custom_lock_if > locker)
Definition: custom_lock.h:52
~custom_lock()
Definition: custom_lock.h:58
custom_lock & operator=(custom_lock const &)=delete
boost::mutex mutex
Definition: thread.h:37
boost::unique_lock< boost::mutex > scoped_lock
Definition: thread.h:38
GNU Radio logging wrapper.
Definition: basic_block.h:29