/*
 * Copyright (c) 2025-2026 IB-Robot Group & openEuler Embedded SIG & openharmony-robot sig_RoboFrame.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// CO-17 module: loaded with dlopen (RTLD_LOCAL | RTLD_DEEPBIND), or linked
// with -Bsymbolic / a version script. Awaits a Task defined in another DSO
// in a long synchronous loop: with a per-DSO slot every completion would
// nest one more resume() and the loop would overflow the stack.

#include "ibmw_selfdev/coro/sync_wait.h"
#include "ibmw_selfdev/coro/task.h"

#define DSO_EXPORT __attribute__((visibility("default")))

namespace coro_dso {

DSO_EXPORT ibmw::selfdev::coro::Task<int> HostChild();

namespace {

ibmw::selfdev::coro::Task<long> Loop(int n)
{
    long sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += co_await HostChild();
    }
    co_return sum;
}

} // namespace

} // namespace coro_dso

extern "C" DSO_EXPORT void* CoroDsoModuleSlot()
{
    return &ibmw::selfdev::coro::detail::CurrentStartRecord();
}

extern "C" DSO_EXPORT long CoroDsoModuleLoop(int n)
{
    return *ibmw::selfdev::coro::SyncWait(coro_dso::Loop(n));
}