OMNeT++ Simulation Library  6.0.3
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 
66 class SIM_API cCoroutine
67 {
68  protected:
69 #ifdef USE_WIN32_FIBERS
70  LPVOID lpFiber = 0;
71  static LPVOID lpMainFiber;
72  unsigned stackSize = 0;
73 #endif
74 #ifdef USE_POSIX_COROUTINES
75  static ucontext_t mainContext;
76  static ucontext_t *curContextPtr;
77  static unsigned totalStackLimit;
78  static unsigned totalStackUsage;
79  unsigned stackSize = 0;
80  char *stackPtr = nullptr;
81  ucontext_t context;
82 #endif
83 #ifdef USE_PORTABLE_COROUTINES
84  _Task *task;
85 #endif
86 
87  public:
90 
95  static void init(unsigned totalStack, unsigned mainStack);
96 
102  static void switchTo(cCoroutine *cor);
103 
107  static void switchToMain();
109 
112 
118  bool setup(CoroutineFnp fnp, void *arg, unsigned stackSize);
119 
123  cCoroutine();
124 
128  virtual ~cCoroutine();
130 
133 
148  virtual bool hasStackOverflow() const;
149 
153  virtual unsigned getStackSize() const;
154 
163  virtual unsigned getStackUsage() const;
165 };
166 
167 } // namespace omnetpp
168 
169 
170 #endif
171 
172 
omnetpp::cCoroutine
Low-level coroutine library. Coroutines are used by cSimpleModule.
Definition: ccoroutine.h:66
omnetpp::CoroutineFnp
void(* CoroutineFnp)(void *)
Prototype for functions that can be used with cCoroutine objects as coroutine bodies.
Definition: ccoroutine.h:42