Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

62 lines (52 sloc) 1.451 kb
#include <atomic>
#include <cstring>
#include <cstdint>
#include <iostream>
#include <type_traits>
#include "test.hpp"
#include "cppa/util/fiber.hpp"
#include "cppa/detail/yield_interface.hpp"
using namespace cppa;
using namespace cppa::util;
using namespace cppa::detail;
struct pseudo_worker {
int m_count;
bool m_blocked;
pseudo_worker() : m_count(0), m_blocked(true) { }
void operator()() {
for (;;) {
if (m_blocked) {
yield(yield_state::blocked);
}
else {
++m_count;
yield(m_count < 10 ? yield_state::ready : yield_state::done);
}
}
}
};
void coroutine(void* worker) { (*reinterpret_cast<pseudo_worker*>(worker))(); }
int main() {
CPPA_TEST(test__yield_interface);
# ifdef CPPA_DISABLE_CONTEXT_SWITCHING
cout << "WARNING: context switching was explicitly disabled using "
"CPPA_DISABLE_CONTEXT_SWITCHING"
<< endl;
# else
fiber fself;
pseudo_worker worker;
fiber fcoroutine(coroutine, &worker);
yield_state ys;
int i = 0;
do {
if (i == 2) worker.m_blocked = false;
ys = call(&fcoroutine, &fself);
++i;
}
while (ys != yield_state::done && i < 12);
CPPA_CHECK_EQUAL("yield_state::done", to_string(ys));
CPPA_CHECK_EQUAL(10, worker.m_count);
CPPA_CHECK_EQUAL(12, i);
# endif
return CPPA_TEST_RESULT;
}
Jump to Line
Something went wrong with that request. Please try again.