Rearranged cothread_switch a bit to move all the code for error conditions to the end, using goto's to get there. Th...

Original commit message from CVS:
Rearranged cothread_switch a bit to move all the code for error conditions
to the end, using goto's to get there.  This has the presumed advantage of
consolidating all the normally run code into one chunk, reducing jumps
(and the associated penalties in any modern processor) and limiting cache-
line usage.  It may be instructive to look at the generated assembly for
this revision and the previous, to see if gcc is smart enough to do this
for us anyway.

If you want to turn off some of the checks (they are all checks for NULL
pointers, if you're curious) for a speed gain, disable the #define of
COTHREAD_PARANOID at the top.
This commit is contained in:
Erik Walthinsen 2000-11-29 10:05:47 +00:00
parent 1eb3884bc6
commit 64b4ac5cdf

View file

@ -34,6 +34,10 @@
pthread_key_t _cothread_key = -1;
/* Disablig this define allows you to shut off a few checks in
* cothread_switch. This likely will speed things up fractionally */
#define COTHREAD_PARANOID
/**
* cothread_create:
* @ctx: the cothread context
@ -220,25 +224,20 @@ cothread_switch (cothread_state *thread)
cothread_context *ctx;
cothread_state *current;
int enter;
// int i;
if (thread == NULL) {
g_print("cothread: there's no thread, strange...\n");
return;
}
#ifdef COTHREAD_PARANOID
if (thread == NULL) goto nothread;
#endif
ctx = thread->ctx;
#ifdef COTHREAD_PARANOID
if (ctx == NULL) goto nocontext;
#endif
current = ctx->threads[ctx->current];
if (current == NULL) {
g_print("cothread: there's no current thread, help!\n");
exit(2);
}
if (current == thread) {
g_print("cothread: trying to switch to same thread, legal but not necessary\n");
return;
}
#ifdef COTHREAD_PARANOID
if (current == NULL) goto nocurrent;
#endif
if (current == thread) goto selfswitch;
// find the number of the thread to switch to
ctx->current = thread->threadnum;
@ -271,4 +270,21 @@ cothread_switch (cothread_state *thread)
DEBUG("cothread: exit thread \n");
ctx->current = 0;
}
return;
#ifdef COTHREAD_PARANOID
nothread:
g_print("cothread: there's no thread, strange...\n");
return;
nocontext:
g_print("cothread: there's no context, help!\n");
exit(2);
nocurrent:
g_print("cothread: there's no current thread, help!\n");
exit(2);
#endif /* COTHREAD_PARANOID */
selfswitch:
g_print("cothread: trying to switch to same thread, legal but not necessary\n");
return;
}