Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / ccoroutine.cc @ 3e29b8a0

History | View | Annotate | Download (3.01 KB)

1 01873262 Georg Kunz
//========================================================================
2
//  CCOROUTINE.CC - part of
3
//
4
//                 OMNeT++/OMNEST
5
//              Discrete System Simulation in C++
6
//
7
//   Member functions of
8
//      cCoroutine : coroutine object
9
//
10
//  Author: Andras Varga
11
//          based on Stig Kofoed's portable coroutines, see the Manual
12
//========================================================================
13
14
/*--------------------------------------------------------------*
15
  Copyright (C) 1992-2008 Andras Varga
16
  Copyright (C) 2006-2008 OpenSim Ltd.
17

18
  This file is distributed WITHOUT ANY WARRANTY. See the file
19
  `license' for details on this and other legal matters.
20
*--------------------------------------------------------------*/
21
22
#include <string.h>
23
#include <stdio.h>
24
#include "ccoroutine.h"
25
26
USING_NAMESPACE
27
28
29
#ifdef USE_WIN32_FIBERS
30
31
LPVOID cCoroutine::lpMainFiber;
32
33
void cCoroutine::init(unsigned total_stack, unsigned main_stack)
34
{
35
    lpMainFiber = ConvertThreadToFiber(0);
36
}
37
38
void cCoroutine::switchTo(cCoroutine *cor)
39
{
40
    SwitchToFiber(cor->lpFiber);
41
}
42
43
void cCoroutine::switchToMain()
44
{
45
    SwitchToFiber(lpMainFiber);
46
}
47
48
cCoroutine::cCoroutine()
49
{
50
    lpFiber = 0;
51
}
52
53
cCoroutine::~cCoroutine()
54
{
55
    if (lpFiber!=0)
56
        DeleteFiber(lpFiber);
57
}
58
59
bool cCoroutine::setup(CoroutineFnp fnp, void *arg, unsigned stack_size )
60
{
61
    // stack_size sets the *committed* stack size; *reserved* (=available)
62
    // stack size is 1MB by default; this allows ~2048 coroutines in a
63
    // 2GB address space
64
    stacksize = stack_size;
65
    //XXX: CreateFiberEx() does not seem to work any better than CreateFiber(),
66
    //it appears to have the same limit for the number of fibers that can be created.
67
    //lpFiber = CreateFiberEx(stacksize, stacksize, 0, (LPFIBER_START_ROUTINE)fnp, arg);
68
    lpFiber = CreateFiber(stack_size, (LPFIBER_START_ROUTINE)fnp, arg);
69
    return lpFiber!=NULL;
70
}
71
72
bool cCoroutine::hasStackOverflow() const
73
{
74
    return false;
75
}
76
77
unsigned cCoroutine::getStackSize() const
78
{
79
    return stacksize;
80
}
81
82
unsigned cCoroutine::getStackUsage() const
83
{
84
    return 0;
85
}
86
87
#endif
88
89
#ifdef USE_PORTABLE_COROUTINES
90
91
#include "task.h"  // Stig Kofoed's "Portable Multitasking" coroutine library
92
93
void cCoroutine::init( unsigned total_stack, unsigned main_stack)
94
{
95
    task_init( total_stack, main_stack );
96
}
97
98
void cCoroutine::switchTo(cCoroutine *cor)
99
{
100
    task_switchto( ((cCoroutine *)cor)->task );
101
}
102
103
void cCoroutine::switchToMain()
104
{
105
    task_switchto( &main_task );
106
}
107
108
cCoroutine::cCoroutine()
109
{
110
    task = NULL;
111
}
112
113
cCoroutine::~cCoroutine()
114
{
115
    if (task) task_free( task );
116
}
117
118
bool cCoroutine::setup(CoroutineFnp fnp, void *arg, unsigned stack_size)
119
{
120
    task = task_create( fnp, arg, stack_size );
121
    return task!=NULL;
122
}
123
124
bool cCoroutine::hasStackOverflow() const
125
{
126
    return task==NULL ? false : task_testoverflow( task );
127
}
128
129
unsigned cCoroutine::getStackSize() const
130
{
131
    return task==NULL ? 0 : task->size;
132
}
133
134
unsigned cCoroutine::getStackUsage() const
135
{
136
    return task==NULL ? 0 : task_stackusage( task );
137
}
138
139
#endif