edubart/minicoro

Trying to get minicoro working with a C++ wrapper (and smart pointers)

CPStagg opened this issue · 2 comments

Hi there,

Have been experimenting with minicoro for a few hours, with a mixture of successes and failures. (I suspect smart pointers are likely to have some issues.)

I built up a little C++ helper class and some macros to simplify some of the calls:

struct CoroutineManager
{
    mco_coro* m_pCo = nullptr;
    mco_desc desc;

    void Init(void (*func)(mco_coro*))
    {
        desc = mco_desc_init(func, 0);
        mco_result res = mco_create(&m_pCo, &desc);
        WASSERT(res == MCO_SUCCESS);
    }

    void PushParam(const void* src, size_t len)
    {
        mco_push(m_pCo, src, len);
    }

    ~CoroutineManager()
    {
        if (m_pCo != nullptr)
        {
            auto res = mco_destroy(m_pCo);
            WASSERT(res == MCO_SUCCESS);
        }
    }

    bool YieldNext(void* dest, size_t len)
    {
        if (mco_status(m_pCo) == MCO_SUSPENDED)
        {
            auto res = mco_resume(m_pCo);
            WASSERT(res == MCO_SUCCESS);

            res = mco_pop(m_pCo, dest, len);
            WASSERT(res == MCO_SUCCESS);
            return true;
        }
        else
        {
            return false;
        }
    }    
};

This seems to work (with some caveats), but if I move YieldNext(...) out of the header into the associated cpp file I created, it no longer functions properly. Are you able to tell me why this is?

Your test case does not contain enough to reproduce. But that is happening likely due to some misuse, moving implementation to a .cpp is allowed. Minicoro has been used and works with C++, just remember these caveats from main page:

Don't use coroutines with C++ exceptions, this is not supported.
When using C++ RAII (i.e. destructors) you must resume the coroutine until it dies to properly execute all destructors.

Some notes from your code:

  • mco_push can fail, you should check its return.
  • In ~CoroutineManager, take care letting a destructor destroy the coroutine this way, to properly destroy your coroutine and execute all destructors (smart pointers have destructors), you must be sure that everything pushed was poped and the coroutine is in dead state.

To help your development and catch mistakes early define MCO_DEBUG. Also if you are having random/odd issues perhaps you are getting stack overflow, increasing MCO_DEFAULT_STACK_SIZE to 1MB may help in this case.

What pattern would be used to pass e.g. a std::shared_ptr into a coroutine function and use it successfully? The problem I'm having is that when I try to extract the shared_ptr from the stack, it seems to make a bitwise copy of the shared_ptr, rather than a constructed copy (and concomitant ref count increase), which creates two issues:

  1. If you create the original shared_ptr inside one scope, create the coroutine, pass through the shared_ptr on the stack, keep hold of the coroutine but exit the original scope, any shared_ptr you unpack from the stack inside the coroutine later is going to point to dead memory as it never got a chance to add a count to the shared_ptr object
  2. If you don't go out of the original scope but call the coroutine through to completion from within that scope, you might expect that the shared_ptr object still exists in the original scope, but actually it won't, because when you pulled it off the stack inside the coroutine it didn't add an extra ref count to the object, so when the coroutine ends, the shared_ptr essentially believes it is holding the final pointer to the shared object, and deletes it.