summaryrefslogtreecommitdiffstats
path: root/src/fiber.h
blob: b7bbcb1ab8cefad0f8f039db2275f10a6872a9fc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef INCLUDE_FIBER_H
#define INCLUDE_FIBER_H

class Fiber
{
public:
    typedef void(*StartFunction)();

    enum Status
    {
        NotStarted,
        Running,
        Stopped,
        Terminated
    };

public:
    explicit Fiber(StartFunction startFunction, int stackSize = 32768);
    ~Fiber();
    
    bool cont();
    static void yield();
    
    Status status()
    { return _status; }

    static Fiber *currentFiber();

private:
    // for the original fiber
    Fiber();

    static void yieldHelper(Status stopStatus);
    static void entryPoint();

    StartFunction _startFunction;
    void *_stackData;
    void *_stackPointer;
    Fiber *_previousFiber;
    Status _status;
};

#endif // INCLUDE_FIBER_H