/*
 * SPDX-FileCopyrightText: 2004 Copyright contributors to the cppTango project
 *
 * SPDX-License-Identifier: LGPL-3.0-or-later
 */

#include <tango/client/CallBackThread.h>

#include <tango/client/ApiUtil.h>
#include <tango/common/defines.h>
#include <tango/server/tango_clock.h>

#include <iostream>

namespace Tango
{

//+-------------------------------------------------------------------------
//
// method :         CallBackThread::CallBackThread
//
// description :     Two constructors for the PollObj class. The first one
//            constructs a PollObji nstance with the default polling
//            ring depth
//            The second one create a PollObj instance with a
//            specified polling ring depth
//
// argument : in :
//
//--------------------------------------------------------------------------

void *CallBackThread::run_undetached(TANGO_UNUSED(void *ptr))
{
    while(!asyn_ptr->is_shutdown())
    {
        try
        {
            {
                omni_mutex_lock sync(*asyn_ptr);
                if(asyn_ptr->get_cb_request_nb_i() == 0)
                {
                    asyn_ptr->wait();
                }
            }

            if(asyn_ptr->get_cb_request_nb() != 0)
            {
                ApiUtil::instance()->get_asynch_replies(0);
            }
        }
        catch(omni_thread_fatal &)
        {
            std::cerr << "OUPS !! A omni thread fatal exception !!!!!!!!" << std::endl;
#ifndef _TG_WINDOWS_
            const time_t t = Tango::get_current_system_datetime();
            std::cerr << ctime(&t);
#endif
            std::cerr << "Trying to re-enter the main loop" << std::endl;
        }
    }

    omni_thread::exit();

    return nullptr;
}

} // namespace Tango
