fiber.cc (13476:04bf50476e37) | fiber.cc (14042:7c548eb5c4c1) |
---|---|
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 --- 74 unchanged lines hidden (view full) --- 83 startingFiber->start(); 84} 85 86Fiber::Fiber(size_t stack_size) : Fiber(primaryFiber(), stack_size) 87{} 88 89Fiber::Fiber(Fiber *link, size_t stack_size) : 90 link(link), stack(nullptr), stackSize(stack_size), guardPage(nullptr), | 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 --- 74 unchanged lines hidden (view full) --- 83 startingFiber->start(); 84} 85 86Fiber::Fiber(size_t stack_size) : Fiber(primaryFiber(), stack_size) 87{} 88 89Fiber::Fiber(Fiber *link, size_t stack_size) : 90 link(link), stack(nullptr), stackSize(stack_size), guardPage(nullptr), |
91 guardPageSize(sysconf(_SC_PAGE_SIZE)), started(false), _finished(false) | 91 guardPageSize(sysconf(_SC_PAGE_SIZE)), _started(false), _finished(false) |
92{ 93 if (stack_size) { 94 guardPage = mmap(nullptr, guardPageSize + stack_size, 95 PROT_READ | PROT_WRITE, 96 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 97 if (guardPage == (void *)MAP_FAILED) { 98 perror("mmap"); 99 fatal("Could not mmap %d byte fiber stack.\n", stack_size); --- 65 unchanged lines hidden (view full) --- 165Fiber::run() 166{ 167 panic_if(_finished, "Fiber has already run to completion."); 168 169 // If we're already running this fiber, we're done. 170 if (_currentFiber == this) 171 return; 172 | 92{ 93 if (stack_size) { 94 guardPage = mmap(nullptr, guardPageSize + stack_size, 95 PROT_READ | PROT_WRITE, 96 MAP_ANONYMOUS | MAP_PRIVATE, -1, 0); 97 if (guardPage == (void *)MAP_FAILED) { 98 perror("mmap"); 99 fatal("Could not mmap %d byte fiber stack.\n", stack_size); --- 65 unchanged lines hidden (view full) --- 165Fiber::run() 166{ 167 panic_if(_finished, "Fiber has already run to completion."); 168 169 // If we're already running this fiber, we're done. 170 if (_currentFiber == this) 171 return; 172 |
173 if (!started) | 173 if (!_started) |
174 createContext(); 175 176 // Switch out of the current Fiber's context and this one's in. 177 Fiber *prev = _currentFiber; 178 Fiber *next = this; 179 _currentFiber = next; 180 swapcontext(&prev->ctx, &next->ctx); 181} 182 183Fiber *Fiber::currentFiber() { return _currentFiber; } 184Fiber *Fiber::primaryFiber() { return &_primaryFiber; } | 174 createContext(); 175 176 // Switch out of the current Fiber's context and this one's in. 177 Fiber *prev = _currentFiber; 178 Fiber *next = this; 179 _currentFiber = next; 180 swapcontext(&prev->ctx, &next->ctx); 181} 182 183Fiber *Fiber::currentFiber() { return _currentFiber; } 184Fiber *Fiber::primaryFiber() { return &_primaryFiber; } |