/* Copyright 2019-2020 National Research Foundation (SARAO)
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
 * details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * @file
 *
 * This file is automatically generated. Do not edit.
 */

#ifndef _GNU_SOURCE
# define _GNU_SOURCE
#endif
#include <spead2/common_features.h>

#if SPEAD2_USE_IBV

#include <spead2/common_loader_rdmacm.h>
#include <spead2/common_loader_utils.h>
#include <spead2/common_logging.h>
#include <mutex>
#include <exception>

namespace spead2
{

static std::once_flag init_once;
static std::exception_ptr init_result;

static void init();

int rdma_bind_addr_stub(struct rdma_cm_id *id, struct sockaddr *addr)
{
    (void) id;
    (void) addr;
    std::rethrow_exception(init_result);
}

int rdma_bind_addr_first(struct rdma_cm_id *id, struct sockaddr *addr)
{
    std::call_once(init_once, init);
    return rdma_bind_addr(id, addr);
}

struct rdma_event_channel *rdma_create_event_channel_stub(void)
{
    std::rethrow_exception(init_result);
}

struct rdma_event_channel *rdma_create_event_channel_first(void)
{
    std::call_once(init_once, init);
    return rdma_create_event_channel();
}

int rdma_create_id_stub(struct rdma_event_channel *channel, struct rdma_cm_id **id, void *context, enum rdma_port_space ps)
{
    (void) channel;
    (void) id;
    (void) context;
    (void) ps;
    std::rethrow_exception(init_result);
}

int rdma_create_id_first(struct rdma_event_channel *channel, struct rdma_cm_id **id, void *context, enum rdma_port_space ps)
{
    std::call_once(init_once, init);
    return rdma_create_id(channel, id, context, ps);
}

void rdma_destroy_event_channel_stub(struct rdma_event_channel *channel)
{
    (void) channel;
    std::rethrow_exception(init_result);
}

void rdma_destroy_event_channel_first(struct rdma_event_channel *channel)
{
    std::call_once(init_once, init);
    return rdma_destroy_event_channel(channel);
}

int rdma_destroy_id_stub(struct rdma_cm_id *id)
{
    (void) id;
    std::rethrow_exception(init_result);
}

int rdma_destroy_id_first(struct rdma_cm_id *id)
{
    std::call_once(init_once, init);
    return rdma_destroy_id(id);
}


int (*rdma_bind_addr)(struct rdma_cm_id *id, struct sockaddr *addr) = rdma_bind_addr_first;
struct rdma_event_channel *(*rdma_create_event_channel)(void) = rdma_create_event_channel_first;
int (*rdma_create_id)(struct rdma_event_channel *channel, struct rdma_cm_id **id, void *context, enum rdma_port_space ps) = rdma_create_id_first;
void (*rdma_destroy_event_channel)(struct rdma_event_channel *channel) = rdma_destroy_event_channel_first;
int (*rdma_destroy_id)(struct rdma_cm_id *id) = rdma_destroy_id_first;

static void reset_stubs()
{
    rdma_bind_addr = rdma_bind_addr_stub;
    rdma_create_event_channel = rdma_create_event_channel_stub;
    rdma_create_id = rdma_create_id_stub;
    rdma_destroy_event_channel = rdma_destroy_event_channel_stub;
    rdma_destroy_id = rdma_destroy_id_stub;
}

static void init()
{
    try
    {
        dl_handle lib("librdmacm.so.1");
        rdma_bind_addr = reinterpret_cast<int (*)(struct rdma_cm_id *id, struct sockaddr *addr)>(
            lib.sym("rdma_bind_addr"));
        rdma_create_event_channel = reinterpret_cast<struct rdma_event_channel *(*)(void)>(
            lib.sym("rdma_create_event_channel"));
        rdma_create_id = reinterpret_cast<int (*)(struct rdma_event_channel *channel, struct rdma_cm_id **id, void *context, enum rdma_port_space ps)>(
            lib.sym("rdma_create_id"));
        rdma_destroy_event_channel = reinterpret_cast<void (*)(struct rdma_event_channel *channel)>(
            lib.sym("rdma_destroy_event_channel"));
        rdma_destroy_id = reinterpret_cast<int (*)(struct rdma_cm_id *id)>(
            lib.sym("rdma_destroy_id"));
        // Prevent the library being closed, so that the symbols stay valid
        lib.release();
    }
    catch (std::system_error &e)
    {
        init_result = std::current_exception();
        reset_stubs();
        log_warning("could not load librdmacm.so.1: %s", e.what());
    }
}

} // namespace spead2


#endif // SPEAD2_USE_IBV
