summaryrefslogtreecommitdiffstats
path: root/library/fibers/fiber.h
blob: 6afc57d018fb027aa423fac4823b5932a739b5d9 (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
44
45
46
47
48
#ifndef INCLUDE_FIBER_H
#define INCLUDE_FIBER_H

class Fiber
{
public:
    enum Status
    {
        NotStarted,
        Running,
        Stopped,
        Terminated
    };

public:
    explicit Fiber(int stackSize = 32768);
    virtual ~Fiber();
    
    bool cont();
    static void yield();
    
    static Fiber *currentFiber();
    
    Status status()
    { return _status; }
    
protected:
    // could be abstract if subclassing for start fiber
    virtual void run() {}
    
private:
    // for the original fiber
    Fiber(bool);

    static void yieldHelper(Status stopStatus);
    
    void *_stackData;
    void *_stackPointer;
    Fiber *_previousFiber;
    Status _status;
    
    // should be thread local
    static Fiber *_currentFiber;
    
    static void entryPoint();
};

#endif // INCLUDE_FIBER_H