aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/include/uhdlib/transport/frame_reservation_mgr.hpp
blob: f0dd853a4c873c07ea9009756fbc11c90d693f4f (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
//
// Copyright 2019 Ettus Research, a National Instruments brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#ifndef INCLUDED_UHDLIB_TRANSPORT_FRAME_RESERVATION_MGR_HPP
#define INCLUDED_UHDLIB_TRANSPORT_FRAME_RESERVATION_MGR_HPP

#include <uhd/config.hpp>
#include <uhd/exception.hpp>
#include <uhdlib/transport/link_if.hpp>
#include <unordered_map>

namespace uhd { namespace transport {

/*!
 * Helper class to keep track of the number of frames reserved from a pair of links
 */
class frame_reservation_mgr
{
public:
    struct frame_reservation_t
    {
        recv_link_if::sptr recv_link;
        size_t num_recv_frames = 0;
        send_link_if::sptr send_link;
        size_t num_send_frames = 0;
    };

    void register_link(const recv_link_if::sptr& recv_link)
    {
        if (_recv_tbl[recv_link.get()] != 0) {
            throw uhd::runtime_error("Recv link already attached to I/O service");
        }
        _recv_tbl[recv_link.get()] = 0;
    }

    void register_link(const send_link_if::sptr& send_link)
    {
        if (_send_tbl[send_link.get()] != 0) {
            throw uhd::runtime_error("Send link already attached to I/O service");
        }
        _send_tbl[send_link.get()] = 0;
    }

    void unregister_link(const recv_link_if::sptr& recv_link)
    {
        auto link_ptr = recv_link.get();
        UHD_ASSERT_THROW(_recv_tbl.count(link_ptr) != 0);
        _recv_tbl.erase(link_ptr);
    }

    void unregister_link(const send_link_if::sptr& send_link)
    {
        auto link_ptr = send_link.get();
        UHD_ASSERT_THROW(_send_tbl.count(link_ptr) != 0);
        _send_tbl.erase(link_ptr);
    }

    void reserve_frames(const frame_reservation_t& reservation)
    {
        if (reservation.recv_link) {
            const size_t rsvd_frames = _recv_tbl.at(reservation.recv_link.get());
            const size_t capacity    = reservation.recv_link->get_num_recv_frames();
            if (rsvd_frames + reservation.num_recv_frames > capacity) {
                throw uhd::runtime_error(
                    "Number of frames requested exceeds link recv frame capacity");
            }

            recv_link_if* link_ptr = reservation.recv_link.get();
            _recv_tbl[link_ptr]    = rsvd_frames + reservation.num_recv_frames;
        }

        if (reservation.send_link) {
            const size_t rsvd_frames = _send_tbl.at(reservation.send_link.get());
            const size_t capacity    = reservation.send_link->get_num_send_frames();
            if (rsvd_frames + reservation.num_send_frames > capacity) {
                throw uhd::runtime_error(
                    "Number of frames requested exceeds link send frame capacity");
            }

            send_link_if* link_ptr = reservation.send_link.get();
            _send_tbl[link_ptr]    = rsvd_frames + reservation.num_send_frames;
        }
    }

    void unreserve_frames(const frame_reservation_t& reservation)
    {
        if (reservation.recv_link) {
            const size_t rsvd_frames = _recv_tbl.at(reservation.recv_link.get());
            recv_link_if* link_ptr   = reservation.recv_link.get();
            _recv_tbl[link_ptr]      = rsvd_frames - reservation.num_recv_frames;
        }

        if (reservation.send_link) {
            const size_t rsvd_frames = _send_tbl.at(reservation.send_link.get());
            send_link_if* link_ptr   = reservation.send_link.get();
            _send_tbl[link_ptr]      = rsvd_frames - reservation.num_send_frames;
        }
    }

private:
    std::unordered_map<recv_link_if*, size_t> _recv_tbl;
    std::unordered_map<send_link_if*, size_t> _send_tbl;
};

}} // namespace uhd::transport

#endif /* INCLUDED_UHDLIB_TRANSPORT_FRAME_RESERVATION_MGR_HPP */