Skip to content

Commit

Permalink
Faster neco_start
Browse files Browse the repository at this point in the history
This commit alters the behavior of neco_start in order to shorten
the time it takes to start a coroutine and switching back to the
parent coroutine. Making for faster neco_start.

See #12
  • Loading branch information
tidwall committed May 13, 2024
1 parent 5f48ee0 commit d808a81
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 53 deletions.
2 changes: 1 addition & 1 deletion deps/.package
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import github.com/tidwall/sco v0.1.1
import github.com/tidwall/sco v0.2.0

sum ad1e36373245380b0f5588c11f1a69369171436a sco.c
sum 92baa4119d14dbb2b1effcd2eaeb4adcba8befc1 sco.h
10 changes: 5 additions & 5 deletions deps/sco.c
Original file line number Diff line number Diff line change
Expand Up @@ -1854,11 +1854,11 @@ static void sco_entry(void *udata) {
co->prev = co;
co->next = co;
if (sco_cur) {
// Reschedule the coroutine that started this one
sco_list_push_back(&sco_yielders, co);
sco_list_push_back(&sco_yielders, sco_cur);
sco_nyielders += 2;
sco_switch(false, false);
// Reschedule the coroutine that started this one immediately after
// all running coroutines, but before any yielding coroutines, and
// continue running the started coroutine.
sco_list_push_back(&sco_runners, sco_cur);
sco_nrunners++;
}
sco_cur = co;
if (sco_user_entry) {
Expand Down
10 changes: 5 additions & 5 deletions neco.c
Original file line number Diff line number Diff line change
Expand Up @@ -2000,11 +2000,11 @@ static void sco_entry(void *udata) {
co->prev = co;
co->next = co;
if (sco_cur) {
// Reschedule the coroutine that started this one
sco_list_push_back(&sco_yielders, co);
sco_list_push_back(&sco_yielders, sco_cur);
sco_nyielders += 2;
sco_switch(false, false);
// Reschedule the coroutine that started this one immediately after
// all running coroutines, but before any yielding coroutines, and
// continue running the started coroutine.
sco_list_push_back(&sco_runners, sco_cur);
sco_nrunners++;
}
sco_cur = co;
if (sco_user_entry) {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ void co_sched1(int argc, void *argv[]) {
int *i = argv[1];
a[(*i)++] = 'B';
assert(neco_yield() == NECO_OK);
a[(*i)++] = 'D';
a[(*i)++] = 'F';
}

void co_sched2(int argc, void *argv[]) {
assert(argc == 2);
char *a = argv[0];
int *i = argv[1];
a[(*i)++] = 'E';
a[(*i)++] = 'D';
assert(neco_yield() == NECO_OK);
a[(*i)++] = 'G';
}
Expand All @@ -116,7 +116,7 @@ void co_sched(int argc, void *argv[]) {
assert(neco_start(co_sched1, 2, a, i) == NECO_OK);
a[(*i)++] = 'C';
assert(neco_start(co_sched2, 2, a, i) == NECO_OK);
a[(*i)++] = 'F';
a[(*i)++] = 'E';
assert(neco_yield() == NECO_OK);
a[(*i)++] = 'H';
}
Expand Down
39 changes: 0 additions & 39 deletions tests/test_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,30 +343,6 @@ void test_sync_cond_deadline(void) {
expect(neco_start(co_sync_cond_deadline, 0), NECO_OK);
}

void co_sync_mutex_rw_order_child2(int argc, void *argv[]) {
assert(argc == 2);
neco_mutex *mu = argv[0];
struct order *order = argv[1];
order_add(order, 17);
expect(neco_mutex_rdlock(mu), NECO_OK);
order_add(order, 23);
expect(neco_mutex_unlock(mu), NECO_OK);
order_add(order, 26);
}

void co_sync_mutex_rw_order_child3(int argc, void *argv[]) {
assert(argc == 2);
neco_mutex *mu = argv[0];
struct order *order = argv[1];
order_add(order, 19);
expect(neco_mutex_trylock(mu), NECO_BUSY);
order_add(order, 20);
expect(neco_mutex_rdlock(mu), NECO_OK);
order_add(order, 24);
expect(neco_mutex_unlock(mu), NECO_OK);
order_add(order, 27);
}

void co_sync_mutex_rw_order_child(int argc, void *argv[]) {
assert(argc == 2);
neco_mutex *mu = argv[0];
Expand All @@ -382,12 +358,6 @@ void co_sync_mutex_rw_order_child(int argc, void *argv[]) {
order_add(order, 13);
expect(neco_yield(), NECO_OK);
order_add(order, 14);
expect(neco_start(co_sync_mutex_rw_order_child2, 2, mu, order), NECO_OK);
order_add(order, 18);
expect(neco_start(co_sync_mutex_rw_order_child3, 2, mu, order), NECO_OK);
order_add(order, 21);
expect(neco_mutex_unlock(mu), NECO_OK);
order_add(order, 28);
}

void co_sync_mutex_rw_order(int argc, void *argv[]) {
Expand All @@ -414,15 +384,6 @@ void co_sync_mutex_rw_order(int argc, void *argv[]) {
order_add(&order, 12);
expect(neco_mutex_unlock(mu), NECO_OK);
order_add(&order, 15);
expect(neco_mutex_tryrdlock(mu), NECO_BUSY);
order_add(&order, 16);
expect(neco_mutex_rdlock(mu), NECO_OK);
order_add(&order, 22);
expect(neco_mutex_unlock(mu), NECO_OK);
order_add(&order, 25);
expect(neco_yield(), NECO_OK);
order_add(&order, 29);
expect(neco_mutex_destroy(mu), NECO_OK);
order_check(&order);
}

Expand Down

0 comments on commit d808a81

Please sign in to comment.