Main Page   Modules   Class Hierarchy   Compound List   File List   Compound Members  

ccor.h

00001 //==========================================================================
00002 //   CCOR.H  -  header for
00003 //                             OMNeT++
00004 //            Discrete System Simulation in C++
00005 //
00006 //  Declarations:
00007 //    class cCoroutine: coroutines
00008 //
00009 //==========================================================================
00010 
00011 /*--------------------------------------------------------------*
00012   Copyright (C) 1992-2001 Andras Varga
00013   Technical University of Budapest, Dept. of Telecommunications,
00014   Stoczek u.2, H-1111 Budapest, Hungary.
00015 
00016   This file is distributed WITHOUT ANY WARRANTY. See the file
00017   `license' for details on this and other legal matters.
00018 *--------------------------------------------------------------*/
00019 
00020 #ifndef __CCOR_H
00021 #define __CCOR_H
00022 
00023 #include "cobject.h"  // bool (for old C++) -- FIXME remove!
00024 
00025 // The following define selects which coroutine-set to use.
00026 // The non-portable version (module stacks allocated on heap)
00027 // is selected for 16-bit MSDOS (otherwise the total of coroutine
00028 // stacks would be limited to 64K.)
00029 #if !defined(__BORLANDC__) || defined(__FLAT__) || defined(__DPMI16__)
00030 #  define PORTABLE_COROUTINES
00031 #endif
00032 
00033 // Getting around the unwanted behavior of longjmp() under AIX
00034 #if defined(_AIX)
00035    typedef struct {char regblk[240];} my_jmp_buf[1];
00036    extern "C" {
00037      int my_setjmp(my_jmp_buf b);
00038      void my_longjmp(my_jmp_buf b, int retv);
00039    };
00040    #define JMP_BUF my_jmp_buf
00041    #define SETJMP  my_setjmp
00042    #define LONGJMP my_longjmp
00043 #else
00044    #include <setjmp.h>
00045    #define JMP_BUF jmp_buf
00046    #define SETJMP  setjmp
00047    #define LONGJMP longjmp
00048 #endif
00049 
00050 //=== some defines
00051 #define SAFETY_AREA     512
00052 #define MIN_STACKSIZE  1024
00053 
00054 
00060 typedef void (*CoroutineFnp)( void * );
00061 
00062 
00063 #ifdef PORTABLE_COROUTINES
00064 
00065 struct _Task;
00066 
00067 #else /* nonportable coroutines */
00068 
00069 extern "C"
00070 {
00071   void set_sp_reg(int *sp);   // overwrite SS:SP (or equiv.on a diff.platform)
00072   int *get_sp_reg();          // return SS:SP
00073 };
00074 
00075 #endif
00076 
00077 //--------------------------------------------------------------------------
00078 
00094 class SIM_API cCoroutine
00095 {
00096   private:
00097 #ifdef PORTABLE_COROUTINES
00098     _Task *task;
00099     bool started;           // true after 1st stack switch
00100 #else  /* nonportable coroutines */
00101     CoroutineFnp fnp;       // pointer to task function
00102     void *arg;              // argument to task function
00103     int stack_size;         // size of stack
00104     int *stack;             // ptr to stack allocated on heap
00105     int *stkbeg;            // beginning of stack
00106     int *stklow;            // stkbeg + safety area
00107     int *sp;                // stack ptr (not used in task switches)
00108     JMP_BUF jmpbuf;         // task state (incl. stack ptr)
00109     bool started;           // true after 1st stack switch
00110 #endif
00111 
00112   public:
00117     static void init( unsigned total_stack, unsigned main_stack);
00118 
00124     static void switchTo( cCoroutine *cor );
00125 
00129     static void switchtoMain();
00130 
00134     cCoroutine();
00135 
00139     ~cCoroutine();
00140 
00144     cCoroutine& operator=(const cCoroutine& cor);
00145 
00151     bool setup( CoroutineFnp fnp, void *arg, unsigned stack_size );
00152 
00156     void destroy();
00157 
00161     void restart();
00162 
00173     bool stackOverflow() const;
00174 
00179     unsigned stackSize() const;
00180 
00186     unsigned stackUsage() const;
00187 
00191     int stackLeft() const;
00192 
00196     bool stackLow() const;
00197 
00201     static int *getMainSP();
00202 };
00203 
00204 #ifdef PORTABLE_COROUTINES
00205 inline int cCoroutine::stackLeft() const {return 0;}
00206 inline bool cCoroutine::stackLow() const {return false;}
00207 //inline int *cCoroutine::getSP() const    {return NULL;}
00208 #else /* nonportable coroutines */
00209 inline int cCoroutine::stackLeft() const {return (char *)sp - (char *)stkbeg;}
00210 inline bool cCoroutine::stackLow() const {return sp<stklow;}
00211 //inline int *cCoroutine::getSP() const    {return sp;}
00212 #endif
00213 
00214 #endif
00215 

Generated at Sat May 4 15:45:48 2002 for OMNeT++ by doxygen1.2.8.1 written by Dimitri van Heesch, © 1997-2001