cothreads

Name

cothreads -- userspace threads

Synopsis


#include <gst/gst.h>


#define     COTHREAD_STACKSIZE
#define     COTHREAD_MAXTHREADS
#define     STACK_SIZE
#define     CURRENT_STACK_FRAME
struct      cothread_state;
struct      cothread_context;
int         (*cothread_func)                (int argc,
                                             char **argv);
#define     COTHREAD_STARTED
cothread_context* cothread_init             ();
cothread_state* cothread_create             (cothread_context *ctx);
void        cothread_setfunc                (cothread_state *thread,
                                             cothread_func func,
                                             int argc,
                                             char **argv);
void        cothread_switch                 (cothread_state *thread);
int         cothread_getcurrent             (void);
cothread_state* cothread_main               (cothread_context *ctx);
void        cothread_set_data               (cothread_state *thread,
                                             gchar *key,
                                             gpointer data);
gpointer    cothread_get_data               (cothread_state *thread,
                                             gchar *key);

Description

Cothreads are a simple user-space method for switching between subtasks. They're based on setjmp()/longjmp() in their current form.

Cothreads are used for loop-based elements that pull data instead of being fed with data. They can also be used to pull a specific region of data out of their src element.

Details

COTHREAD_STACKSIZE

#define COTHREAD_STACKSIZE 8192

The default stack size of a cothread.


COTHREAD_MAXTHREADS

#define COTHREAD_MAXTHREADS 16

The maximum number of cothreads we are going to support.


STACK_SIZE

#define STACK_SIZE 0x200000


CURRENT_STACK_FRAME

#define CURRENT_STACK_FRAME  ({ char __csf; &__csf; })


struct cothread_state

struct cothread_state {
  cothread_context *ctx;
  int threadnum;

  cothread_func func;
  int argc;
  char **argv;

  int flags;
  void *sp;
  /* is this needed any more? */
  void *top_sp;
  void *pc;
  jmp_buf jmp;
};


struct cothread_context

struct cothread_context {
  cothread_state *threads[COTHREAD_MAXTHREADS];
  int nthreads;
  int current;
  GHashTable *data;
};


cothread_func ()

int         (*cothread_func)                (int argc,
                                             char **argv);

the function that will be called when the cothread starts. The function prototype is like a main() function, so you can do whatever you want with it.

argc :a main-like argument count
argv :a main-like array of arguments
Returns :a return code


COTHREAD_STARTED

#define COTHREAD_STARTED	0x01

Indicates the cothread is started.


cothread_init ()

cothread_context* cothread_init             ();

Create and initialize a new cothread context

Returns : the new cothread context


cothread_create ()

cothread_state* cothread_create             (cothread_context *ctx);

Create a new cothread state in the given context

ctx : the cothread context
Returns : the new cothread state


cothread_setfunc ()

void        cothread_setfunc                (cothread_state *thread,
                                             cothread_func func,
                                             int argc,
                                             char **argv);

Set the cothread function

thread : the cothread state
func : the function to call
argc : argument count for the cothread function
argv : arguments for the cothread function


cothread_switch ()

void        cothread_switch                 (cothread_state *thread);

Switches to the given cothread state

thread : cothread state to switch to


cothread_getcurrent ()

int         cothread_getcurrent             (void);

Returns : the current cothread id


cothread_main ()

cothread_state* cothread_main               (cothread_context *ctx);

ctx : cothread context to find main thread of
Returns : the cothread_state of the main (0th) thread


cothread_set_data ()

void        cothread_set_data               (cothread_state *thread,
                                             gchar *key,
                                             gpointer data);

adds data to a cothread

thread : the cothread state
key : a key for the data
data : the data


cothread_get_data ()

gpointer    cothread_get_data               (cothread_state *thread,
                                             gchar *key);

get data from the cothread

thread : the cothread state
key : a key for the data
Returns : the data assiciated with the key