Simple coroutine library / objects in plain C Snapshot
Defines | Typedefs | Functions | Variables
cthread.c File Reference
#include "cthread.h"
#include <pthread.h>
#include <stdio.h>

Go to the source code of this file.

Defines

#define GET_TLS()   tls_thread
#define SET_TLS(val)   do { tls_thread = val; } while( 0 );

Typedefs

typedef void(* MK_CTX_FUNC )(void)

Functions

int CTHREAD_libinit ()
 library initialisation
void cthread_init (CTHREAD *arg)
CTHREADCTHREAD_init (STACKS *stacks, CTHREAD_PROC proc)
 initialises a new co-routine thread.
static int do_start (CTHREAD *thread)
int CTHREAD_start (CTHREAD *thread, VALUES **rvalue, const char *format,...)
 start a new thread - the thread procedure is invoked.
int CTHREAD_join (CTHREAD *thread, VALUES **rvalue)
 waits till the thread has finished Calls CTHREAD_resume repeatedly unntil the thread finishes.
int CTHREAD_resume (CTHREAD *thread, VALUES **rvalue, const char *format,...)
 resume a suspended thread
int CTHREAD_yield (VALUES **rvalue, const char *format,...)
 a running thread temporarily suspends execution
int CTHREAD_free (CTHREAD *thread)
uint32_t CTHREAD_get_tid ()
 get thread id
int CTHREAD_set_return_value (const char *format,...)
 a running thread sets it return values

Variables

__thread CTHREADtls_thread
static uint32_t next_tid

Define Documentation

#define GET_TLS ( )    tls_thread

Definition at line 15 of file cthread.c.

#define SET_TLS (   val)    do { tls_thread = val; } while( 0 );

Definition at line 16 of file cthread.c.


Typedef Documentation

typedef void(* MK_CTX_FUNC)(void)

Definition at line 46 of file cthread.c.


Function Documentation

void cthread_init ( CTHREAD arg)

Definition at line 30 of file cthread.c.

{
  arg->proc( &arg->caller_to_thread_value );
  
  arg->state = CTHREAD_STATE_EXIT; 

  //fprintf( stderr,"relase stack %p\n",arg->stack_entry->stack_start);


  STACKS_release( arg->stack_entry );

#if 0
  setcontext( &arg->context_caller );
#endif
}
static int do_start ( CTHREAD thread) [static]

Definition at line 116 of file cthread.c.

{
  thread->state = CTHREAD_STATE_RUNNING;
  
  thread->thread_id = ++next_tid;
  
  thread->prev_thread = GET_TLS();  
  SET_TLS( thread );

#if 0
  thread->context_coroutine.uc_link = 0;
#else 
  thread->context_coroutine.uc_link = &thread->context_caller;
#endif
  makecontext( & thread->context_coroutine, (MK_CTX_FUNC) cthread_init, 1, thread);
  
  setcontext( &thread->context_coroutine );

 
  // should not get here.
  return -1;
}

Variable Documentation

uint32_t next_tid [static]

Definition at line 20 of file cthread.c.

__thread CTHREAD* tls_thread

Definition at line 8 of file cthread.c.