OMNeT++ Simulation Library  5.6.1
ccoroutine.h
1 //==========================================================================
2 // CCOROUTINE.H - header for
3 // OMNeT++/OMNEST
4 // Discrete System Simulation in C++
5 //
6 //==========================================================================
7 
8 /*--------------------------------------------------------------*
9  Copyright (C) 1992-2017 Andras Varga
10  Copyright (C) 2006-2017 OpenSim Ltd.
11 
12  This file is distributed WITHOUT ANY WARRANTY. See the file
13  `license' for details on this and other legal matters.
14 *--------------------------------------------------------------*/
15 
16 #ifndef __OMNETPP_CCOROUTINE_H
17 #define __OMNETPP_CCOROUTINE_H
18 
19 #include "platdep/platmisc.h" // for <windows.h>
20 #include "simkerneldefs.h"
21 
22 #if !defined(USE_WIN32_FIBERS) && !defined(USE_POSIX_COROUTINES) && !defined(USE_PORTABLE_COROUTINES)
23 #error "Coroutine library choice not specified"
24 #endif
25 
26 #ifdef USE_POSIX_COROUTINES
27 #include <ucontext.h>
28 #endif
29 
30 namespace omnetpp {
31 
32 #ifdef USE_PORTABLE_COROUTINES
33 struct _Task;
34 #endif
35 
42 typedef void (*CoroutineFnp)( void * );
43 
44 //--------------------------------------------------------------------------
45 
67 class SIM_API cCoroutine
68 {
69  protected:
70 #ifdef USE_WIN32_FIBERS
71  LPVOID lpFiber;
72  static LPVOID lpMainFiber;
73  unsigned stackSize;
74 #endif
75 #ifdef USE_POSIX_COROUTINES
76  static ucontext_t mainContext;
77  static ucontext_t *curContextPtr;
78  static unsigned totalStackLimit;
79  static unsigned totalStackUsage;
80  unsigned stackSize;
81  char *stackPtr;
82  ucontext_t context;
83 #endif
84 #ifdef USE_PORTABLE_COROUTINES
85  _Task *task;
86 #endif
87 
88  public:
91 
96  static void init(unsigned totalStack, unsigned mainStack);
97 
103  static void switchTo(cCoroutine *cor);
104 
108  static void switchToMain();
110 
113 
119  bool setup(CoroutineFnp fnp, void *arg, unsigned stackSize);
120 
124  cCoroutine();
125 
129  virtual ~cCoroutine();
131 
134 
149  virtual bool hasStackOverflow() const;
150 
154  virtual unsigned getStackSize() const;
155 
164  virtual unsigned getStackUsage() const;
166 };
167 
168 } // namespace omnetpp
169 
170 
171 #endif
172 
173 
void(* CoroutineFnp)(void *)
Prototype for functions that can be used with cCoroutine objects as coroutine bodies.
Definition: ccoroutine.h:42
Low-level coroutine library. Coroutines are used by cSimpleModule.
Definition: ccoroutine.h:67
Definition: cabstracthistogram.h:21