<commit_before>#ifndef __INCLUDED_82D14FF80A1611E7AA6EA088B4D1658C
#define __INCLUDED_82D14FF80A1611E7AA6EA088B4D1658C

#include "RObject.h"
#include "RUniquePointer.h"
#include "RCoRoutineRunner.h"
#include "RSpinLocker.h"

#define RCR_BEGIN()                PT_BEGIN(&this->mPt)
#define RCR_END()                  PT_END(&this->mPt)
#define RCR_WAIT_UNITL(condition)  PT_WAIT_UNTIL(&this->mPt, (condition))
#define RCR_WAIT_WHILE(condition)  PT_WAIT_WHILE(&this->mPt, (condition))
#define RCR_YIELD()                PT_YIELD(&this->mPt)
#define RCR_YIELD_UNTIL(condition) PT_YIELD_UNTIL(&this->mPt, (condition))
#define RCR_EXIT()                 PT_EXIT(&this->mPt)
#define RCR_SPAWN(otherCR, args ...) \
  do { \
    if(!PT_SCHEDULE((otherCR)->spawn(args))) \
    { \
      return PT_WAITING; \
    } \
    PT_WAIT_THREAD((&this->mPt), (otherCR)->run()) \
  } while(0);

class RAbstractCoRoutine : public RObject
{
public:
  virtual char
  run() = 0;
};

/**
 * @brief The RCoRoutine class
 *
 * CoRoutine design, not yet implemented.
 */
template <class T>
class RCoRoutine : public RAbstractCoRoutine
{
public:
  template <class ... ParamTypes>
  char
  spawn(ParamTypes ... params)
  {
    {
      R_MAKE_SPINLOCKER();

      mRunner.reset();
      mRunner.reset(new T(params ...));
    }

    thread()->eventLoop()->_attachCR(this);

    return PT_WAITING;
  }

  void
  terminate()
  {
    thread()->eventLoop()->_detachCR(this);
  }

protected:
  char
  run()
  {
    if(!mRunner)
    {<commit_msg>RCoRoutine: Don't use slow detach method to detach the coroutine durint terminate<commit_after>#ifndef __INCLUDED_82D14FF80A1611E7AA6EA088B4D1658C
#define __INCLUDED_82D14FF80A1611E7AA6EA088B4D1658C

#include "RObject.h"
#include "RUniquePointer.h"
#include "RCoRoutineRunner.h"
#include "RSpinLocker.h"

#define RCR_BEGIN()                PT_BEGIN(&this->mPt)
#define RCR_END()                  PT_END(&this->mPt)
#define RCR_WAIT_UNITL(condition)  PT_WAIT_UNTIL(&this->mPt, (condition))
#define RCR_WAIT_WHILE(condition)  PT_WAIT_WHILE(&this->mPt, (condition))
#define RCR_YIELD()                PT_YIELD(&this->mPt)
#define RCR_YIELD_UNTIL(condition) PT_YIELD_UNTIL(&this->mPt, (condition))
#define RCR_EXIT()                 PT_EXIT(&this->mPt)
#define RCR_SPAWN(otherCR, args ...) \
  do { \
    if(!PT_SCHEDULE((otherCR)->spawn(args))) \
    { \
      return PT_WAITING; \
    } \
    PT_WAIT_THREAD((&this->mPt), (otherCR)->run()) \
  } while(0);

class RAbstractCoRoutine : public RObject
{
public:
  virtual char
  run() = 0;
};

/**
 * @brief The RCoRoutine class
 *
 * CoRoutine design, not yet implemented.
 */
template <class T>
class RCoRoutine : public RAbstractCoRoutine
{
public:
  template <class ... ParamTypes>
  char
  spawn(ParamTypes ... params)
  {
    {
      R_MAKE_SPINLOCKER();

      mRunner.reset();
      mRunner.reset(new T(params ...));
    }

    thread()->eventLoop()->_attachCR(this);

    return PT_WAITING;
  }

  void
  terminate()
  {
    // run() will return PT_ENDED lead REventLoop detach this CR.
    mRunner.reset();
  }

protected:
  char
  run()
  {
    if(!mRunner)
    {
{'fraction_non_alphanumeric': 0.14262871762072274, 'fraction_numerical': 0.026862807803006077, 'mean_word_length': 3.3565459610027855, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3464919', 'n_tokens_mistral': 1252, 'n_tokens_neox': 1201, 'n_words': 204}