fiber.hh revision 12787
1/* 2 * Copyright 2018 Google, Inc. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: redistributions of source code must retain the above copyright 7 * notice, this list of conditions and the following disclaimer; 8 * redistributions in binary form must reproduce the above copyright 9 * notice, this list of conditions and the following disclaimer in the 10 * documentation and/or other materials provided with the distribution; 11 * neither the name of the copyright holders nor the names of its 12 * contributors may be used to endorse or promote products derived from 13 * this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 18 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 19 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 21 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 * 27 * Authors: Gabe Black 28 */ 29 30#ifndef __BASE_FIBER_HH__ 31#define __BASE_FIBER_HH__ 32 33#include <ucontext.h> 34 35#include <cstddef> 36#include <cstdint> 37 38/** 39 * This class represents a fiber, which is a light weight sort of thread which 40 * is cooperatively scheduled and runs sequentially with other fibers, swapping 41 * in and out of a single actual thread of execution. 42 * 43 * To define your own threads, create a subclass of Fiber and override its 44 * main() function to do what you want your fiber to do. You can start it by 45 * calling its run() method which will stop your execution and start the other 46 * fiber in your place. 47 * 48 * If your main() function ends, that fiber will automatically switch to either 49 * the primary fiber, or to a particular fiber you specified at construction 50 * time, and your fiber is considered finished. 51 */ 52 53class Fiber 54{ 55 public: 56 const static size_t DefaultStackSize = 0x50000; 57 58 /// stack_size is the size of the stack available to this fiber. 59 /// link points to another fiber which will start executing when this 60 /// fiber's main function returns. 61 Fiber(size_t stack_size=DefaultStackSize); 62 Fiber(Fiber *link, size_t stack_size=DefaultStackSize); 63 64 virtual ~Fiber(); 65 66 /// Start executing the fiber represented by this object. This function 67 /// will "return" when the current fiber is switched back to later on. 68 void run(); 69 70 /// Returns whether the "main" function of this fiber has finished. 71 /// 72 bool finished() const { return _finished; }; 73 74 /// Get a pointer to the current running Fiber. 75 /// 76 static Fiber *currentFiber(); 77 /// Get a pointer to the primary Fiber. 78 /// This Fiber represents the thread of execution started by the OS, and 79 /// which has a Fiber attached to it after the fact. 80 static Fiber *primaryFiber(); 81 82 protected: 83 /// This method is called when this fiber is first run. Override it to 84 /// give your fiber something to do. When main returns, the fiber will 85 /// mark itself as finished and switch to its link fiber. 86 virtual void main() = 0; 87 88 void setStarted() { started = true; } 89 90 private: 91 static void entryTrampoline(); 92 void start(); 93 94 ucontext_t ctx; 95 Fiber *link; 96 97 // The stack for this context, or a nullptr if allocated elsewhere. 98 uint8_t *stack; 99 size_t stackSize; 100 101 bool started; 102 bool _finished; 103 void createContext(); 104}; 105 106#endif // __BASE_FIBER_HH__ 107