├── .gitignore ├── LICENSE ├── Makefile ├── README.md ├── log.h ├── main.cpp ├── util.cpp ├── util.h ├── xfiber.cpp ├── xfiber.h ├── xsocket.cpp └── xsocket.h /.gitignore: -------------------------------------------------------------------------------- 1 | bin/* 2 | obj/* 3 | .vscode 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | DIR_INC = ./ 2 | DIR_SRC = ./ 3 | DIR_OBJ = ./obj 4 | DIR_BIN = ./bin 5 | 6 | $(shell if [ ! -e ${DIR_OBJ} ];then mkdir -p ${DIR_OBJ}; fi) 7 | $(shell if [ ! -e ${DIR_BIN} ];then mkdir -p ${DIR_BIN}; fi) 8 | 9 | SRC = $(wildcard ${DIR_SRC}/*.cpp) 10 | OBJ = $(patsubst %.cpp,${DIR_OBJ}/%.o,$(notdir ${SRC})) 11 | 12 | TARGET = main 13 | 14 | BIN_TARGET = ${DIR_BIN}/${TARGET} 15 | 16 | CC = g++ 17 | CFLAGS = -std=c++11 -O2 -g -Wall -I${DIR_INC} 18 | 19 | ${BIN_TARGET}:${OBJ} 20 | $(CC) $(OBJ) -o $@ 21 | 22 | ${DIR_OBJ}/%.o:${DIR_SRC}/%.cpp 23 | $(CC) $(CFLAGS) -c $< -o $@ 24 | 25 | .PHONY:clean 26 | 27 | clean: 28 | # find ${DIR_OBJ} -name "*.o" -exec rm -rf{} 29 | find ${DIR_OBJ} -name "*.o" | xargs rm -rf 30 | rm -rf ${BIN_TARGET} 31 | 32 | 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # XFiber 2 | 3 | #### 介绍 4 | C++实现轻量级的协程 5 | 6 | #### 软件架构 7 | 软件架构说明 8 | 9 | 10 | #### 安装教程 11 | 12 | 1. xxxx 13 | 2. xxxx 14 | 3. xxxx 15 | 16 | #### 使用说明 17 | 18 | 1. xxxx 19 | 2. xxxx 20 | 3. xxxx 21 | 22 | #### 参与贡献 23 | 24 | 1. Fork 本仓库 25 | 2. 新建 Feat_xxx 分支 26 | 3. 提交代码 27 | 4. 新建 Pull Request 28 | 29 | 30 | #### 特技 31 | 32 | 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 33 | 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 34 | 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 35 | 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 36 | 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 37 | 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) 38 | -------------------------------------------------------------------------------- /log.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | 6 | #define DEBUG_ENABLE 1 7 | #define INFO_ENABLE 1 8 | #define WARNING_ENABLE 1 9 | #define ERROR_ENABLE 1 10 | 11 | 12 | static std::string log_date() { 13 | time_t now = time(0); 14 | struct tm tstruct; 15 | char buf[80]; 16 | tstruct = *localtime(&now); 17 | strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct); 18 | return buf; 19 | } 20 | 21 | #ifndef __LOG_DATE__ 22 | #define __LOG_DATE__ log_date().c_str() 23 | #endif 24 | 25 | #if DEBUG_ENABLE 26 | #define LOG_DEBUG(fmt, args...) fprintf(stderr, "[D][%s][%s %d] " fmt"\n", __LOG_DATE__, __FILE__, __LINE__, ##args); 27 | #else 28 | #define LOG_DEBUG(fmt, ...) 29 | #endif 30 | 31 | #if INFO_ENABLE 32 | #define LOG_INFO(fmt, args...) fprintf(stderr, "[I][%s][%s %d] " fmt"\n", __LOG_DATE__, __FILE__, __LINE__, ##args); 33 | #else 34 | #define LOG_INFO(fmt, ...) 35 | #endif 36 | 37 | #if WARNING_ENABLE 38 | #define LOG_WARNING(fmt, args...) fprintf(stderr, "[W][%s][%s %d] " fmt"\n", __LOG_DATE__, __FILE__, __LINE__, ##args); 39 | #else 40 | #define LOG_WARNING(fmt, ...) 41 | #endif 42 | 43 | #if ERROR_ENABLE 44 | #define LOG_ERROR(fmt, args...) fprintf(stderr, "[E][%s][%s %d] " fmt"\n", __LOG_DATE__, __FILE__, __LINE__, ##args); 45 | #else 46 | #define LOG_ERROR(fmt, ...) 47 | #endif 48 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | 8 | #include "xfiber.h" 9 | #include "xsocket.h" 10 | 11 | using namespace std; 12 | 13 | 14 | void sigint_action(int sig) { 15 | std::cout << "exit..." << std::endl; 16 | exit(0); 17 | } 18 | 19 | // #define DEBUG_ENABLE 20 | #define DPRINT(fmt, args...) fprintf(stderr, "[D][%s %d] " fmt"\n", __FILE__, __LINE__, ##args); 21 | 22 | 23 | int main() { 24 | signal(SIGINT, sigint_action); 25 | 26 | XFiber *xfiber = XFiber::xfiber(); 27 | /*xfiber->AddTask([&]() { 28 | cout << "hello world 11" << endl; 29 | xfiber->Yield(); 30 | cout << "hello world 12" << endl; 31 | xfiber->Yield(); 32 | cout << "hello world 13" << endl; 33 | xfiber->Yield(); 34 | cout << "hello world 14" << endl; 35 | cout << "hello world 15" << endl; 36 | 37 | }, 0, "f1"); 38 | 39 | xfiber->AddTask([]() { 40 | cout << "hello world 2" << endl; 41 | }, 0, "f2"); 42 | */ 43 | 44 | // xfiber->CreateFiber([xfiber]{ 45 | // for (int i = 0; i < 10; i++) { 46 | // cout << i << endl; 47 | // xfiber->SleepMs(1000); 48 | // } 49 | // }); 50 | 51 | xfiber->CreateFiber([&]{ 52 | Listener listener = Listener::ListenTCP(7000); 53 | while (true) { 54 | shared_ptr conn1 = listener.Accept(); 55 | //shared_ptr conn2 = Connection::ConnectTCP("127.0.0.1", 6379); 56 | 57 | xfiber->CreateFiber([conn1] { 58 | while (true) { 59 | char recv_buf[512]; 60 | int n = conn1->Read(recv_buf, 512, 50000); 61 | if (n <= 0) { 62 | break; 63 | } 64 | 65 | #if 0 66 | conn2->Write(recv_buf, n); 67 | char rsp[1024]; 68 | int rsp_len = conn2->Read(rsp, 1024); 69 | cout << "recv from remote: " << rsp << endl; 70 | conn1->Write(rsp, rsp_len); 71 | #else 72 | if (conn1->Write("+OK\r\n", 5, 1000) <= 0) { 73 | break; 74 | } 75 | #endif 76 | } 77 | }, 0, "server"); 78 | } 79 | }); 80 | 81 | xfiber->Dispatch(); 82 | 83 | return 0; 84 | } 85 | -------------------------------------------------------------------------------- /util.cpp: -------------------------------------------------------------------------------- 1 | #include "util.h" 2 | 3 | namespace util { 4 | 5 | int64_t NowMs() { 6 | struct timeval tv; 7 | gettimeofday(&tv, nullptr); 8 | return int64_t(tv.tv_sec * 1000) + tv.tv_usec / 1000; 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /util.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H_ 2 | #define UTIL_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace util { 9 | 10 | int64_t NowMs(); 11 | 12 | } 13 | 14 | #endif -------------------------------------------------------------------------------- /xfiber.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include "xfiber.h" 12 | 13 | 14 | XFiber::XFiber() { 15 | curr_fiber_ = nullptr; 16 | efd_ = epoll_create1(0); 17 | if (efd_ < 0) { 18 | LOG_ERROR("epoll_create failed, msg=%s", strerror(errno)); 19 | exit(-1); 20 | } 21 | } 22 | 23 | XFiber::~XFiber() { 24 | close(efd_); 25 | } 26 | 27 | XFiberCtx *XFiber::SchedCtx() { 28 | return &sched_ctx_; 29 | } 30 | 31 | void XFiber::WakeupFiber(Fiber *fiber) { 32 | LOG_DEBUG("try wakeup fiber[%lu] %p", fiber->Seq(), fiber); 33 | // 1. 加入就绪队列 34 | ready_fibers_.push_back(fiber); 35 | 36 | // 2. 从等待队列中删除 37 | WaitingEvents &waiting_events = fiber->GetWaitingEvents(); 38 | for (size_t i = 0; i < waiting_events.waiting_fds_r_.size(); i++) { 39 | int fd = waiting_events.waiting_fds_r_[i]; 40 | auto iter = io_waiting_fibers_.find(fd); 41 | if (iter != io_waiting_fibers_.end()) { 42 | io_waiting_fibers_.erase(iter); 43 | } 44 | } 45 | for (size_t i = 0; i < waiting_events.waiting_fds_w_.size(); i++) { 46 | int fd = waiting_events.waiting_fds_w_[i]; 47 | auto iter = io_waiting_fibers_.find(fd); 48 | if (iter != io_waiting_fibers_.end()) { 49 | io_waiting_fibers_.erase(iter); 50 | } 51 | } 52 | 53 | // 3. 从超时队列中删除 54 | int64_t expire_at = waiting_events.expire_at_; 55 | if (expire_at > 0) { 56 | auto expired_iter = expire_events_.find(expire_at); 57 | if (expired_iter->second.find(fiber) == expired_iter->second.end()) { 58 | LOG_WARNING("not fiber [%lu] in expired events", fiber->Seq()); 59 | } 60 | else { 61 | LOG_DEBUG("remove fiber [%lu] from expire events...", fiber->Seq()); 62 | expired_iter->second.erase(fiber); 63 | } 64 | } 65 | LOG_DEBUG("fiber [%lu] %p has wakeup success, ready to run!", fiber->Seq(), fiber); 66 | } 67 | 68 | void XFiber::CreateFiber(std::function run, size_t stack_size, std::string fiber_name) { 69 | if (stack_size == 0) { 70 | stack_size = 1024 * 1024; 71 | } 72 | Fiber *fiber = new Fiber(run, this, stack_size, fiber_name); 73 | ready_fibers_.push_back(fiber); 74 | LOG_DEBUG("create a new fiber with id[%lu]", fiber->Seq()); 75 | } 76 | 77 | void XFiber::Dispatch() { 78 | while (true) { 79 | if (ready_fibers_.size() > 0) { 80 | running_fibers_ = std::move(ready_fibers_); 81 | ready_fibers_.clear(); 82 | LOG_DEBUG("there are %ld fiber(s) in ready list, ready to run...", running_fibers_.size()); 83 | 84 | for (auto iter = running_fibers_.begin(); iter != running_fibers_.end(); iter++) { 85 | Fiber *fiber = *iter; 86 | curr_fiber_ = fiber; 87 | LOG_DEBUG("switch from sched to fiber[%lu]", fiber->Seq()); 88 | assert(SwitchCtx(SchedCtx(), fiber->Ctx()) == 0); 89 | curr_fiber_ = nullptr; 90 | 91 | if (fiber->IsFinished()) { 92 | LOG_INFO("fiber[%lu] finished, free it!", fiber->Seq()); 93 | delete fiber; 94 | } 95 | } 96 | running_fibers_.clear(); 97 | } 98 | 99 | int64_t now_ms = util::NowMs(); 100 | while (!expire_events_.empty() && expire_events_.begin()->first <= now_ms) { 101 | std::set &expired_fibers = expire_events_.begin()->second; 102 | while (!expired_fibers.empty()) { 103 | std::set::iterator expired_fiber = expired_fibers.begin(); 104 | WakeupFiber(*expired_fiber); 105 | } 106 | expire_events_.erase(expire_events_.begin()); 107 | } 108 | 109 | #define MAX_EVENT_COUNT 512 110 | struct epoll_event evs[MAX_EVENT_COUNT]; 111 | int n = epoll_wait(efd_, evs, MAX_EVENT_COUNT, 2); 112 | if (n < 0) { 113 | LOG_ERROR("epoll_wait error, msg=%s", strerror(errno)); 114 | continue; 115 | } 116 | 117 | for (int i = 0; i < n; i++) { 118 | struct epoll_event &ev = evs[i]; 119 | int fd = ev.data.fd; 120 | 121 | auto fiber_iter = io_waiting_fibers_.find(fd); 122 | if (fiber_iter != io_waiting_fibers_.end()) { 123 | WaitingFibers &waiting_fiber = fiber_iter->second; 124 | if (ev.events & EPOLLIN) { 125 | LOG_DEBUG("waiting fd[%d] has fired IN event, wake up pending fiber[%lu]", fd, waiting_fiber.r_->Seq()); 126 | WakeupFiber(waiting_fiber.r_); 127 | } 128 | else if (ev.events & EPOLLOUT) { 129 | if (waiting_fiber.w_ == nullptr) { 130 | LOG_WARNING("fd[%d] has been fired OUT event, but not found any fiber to handle!", fd); 131 | } 132 | else { 133 | LOG_DEBUG("waiting fd[%d] has fired OUT event, wake up pending fiber[%lu]", fd, waiting_fiber.w_->Seq()); 134 | WakeupFiber(waiting_fiber.w_); 135 | } 136 | } 137 | } 138 | } 139 | } 140 | } 141 | 142 | void XFiber::Yield() { 143 | assert(curr_fiber_ != nullptr); 144 | // 主动切出的后仍然是ready状态,等待下次调度 145 | ready_fibers_.push_back(curr_fiber_); 146 | SwitchToSched(); 147 | } 148 | 149 | void XFiber::SwitchToSched() { 150 | assert(curr_fiber_ != nullptr); 151 | LOG_DEBUG("switch to sched"); 152 | assert(SwitchCtx(curr_fiber_->Ctx(), SchedCtx()) == 0); 153 | } 154 | 155 | void XFiber::SleepMs(int ms) { 156 | if (ms < 0) { 157 | return; 158 | } 159 | 160 | int64_t expired_at = util::NowMs() + ms; 161 | WaitingEvents events; 162 | events.expire_at_ = expired_at; 163 | RegisterWaitingEvents(events); 164 | SwitchToSched(); 165 | } 166 | 167 | void XFiber::TakeOver(int fd) { 168 | struct epoll_event ev; 169 | ev.events = EPOLLIN | EPOLLOUT | EPOLLET; 170 | ev.data.fd = fd; 171 | 172 | if (epoll_ctl(efd_, EPOLL_CTL_ADD, fd, &ev) < 0) { 173 | LOG_ERROR("add fd [%d] into epoll failed, msg=%s", fd, strerror(errno)); 174 | exit(-1); 175 | } 176 | LOG_DEBUG("add fd[%d] into epoll event success", fd); 177 | } 178 | 179 | void XFiber::RegisterWaitingEvents(WaitingEvents &events) { 180 | assert(curr_fiber_ != nullptr); 181 | if (events.expire_at_ > 0) { 182 | expire_events_[events.expire_at_].insert(curr_fiber_); 183 | curr_fiber_->SetWaitingEvent(events); 184 | LOG_DEBUG("register fiber [%lu] with expire event at %ld", curr_fiber_->Seq(), events.expire_at_); 185 | } 186 | 187 | for (size_t i = 0; i < events.waiting_fds_r_.size(); i++) { 188 | int fd = events.waiting_fds_r_[i]; 189 | auto iter = io_waiting_fibers_.find(fd); 190 | if (iter == io_waiting_fibers_.end()) { 191 | io_waiting_fibers_.insert(std::make_pair(fd, WaitingFibers(curr_fiber_, nullptr))); 192 | curr_fiber_->SetWaitingEvent(events); 193 | } 194 | } 195 | 196 | for (size_t i = 0; i < events.waiting_fds_w_.size(); i++) { 197 | int fd = events.waiting_fds_w_[i]; 198 | auto iter = io_waiting_fibers_.find(fd); 199 | if (iter == io_waiting_fibers_.end()) { 200 | io_waiting_fibers_.insert(std::make_pair(fd, WaitingFibers(nullptr, curr_fiber_))); 201 | curr_fiber_->SetWaitingEvent(events); 202 | } 203 | } 204 | } 205 | 206 | bool XFiber::UnregisterFd(int fd) { 207 | LOG_DEBUG("unregister fd[%d] from sheduler", fd); 208 | auto io_waiting_fibers_iter = io_waiting_fibers_.find(fd); 209 | // assert(io_waiting_fibers_iter != io_waiting_fibers_.end()); 210 | 211 | if (io_waiting_fibers_iter != io_waiting_fibers_.end()) { 212 | WaitingFibers &waiting_fibers = io_waiting_fibers_iter->second; 213 | if (waiting_fibers.r_ != nullptr) { 214 | WakeupFiber(waiting_fibers.r_); 215 | } 216 | if (waiting_fibers.w_ != nullptr) { 217 | WakeupFiber(waiting_fibers.w_); 218 | } 219 | 220 | io_waiting_fibers_.erase(io_waiting_fibers_iter); 221 | } 222 | 223 | struct epoll_event ev; 224 | if (epoll_ctl(efd_, EPOLL_CTL_DEL, fd, &ev) < 0) { 225 | LOG_ERROR("unregister fd[%d] from epoll efd[%d] failed, msg=%s", fd, efd_, strerror(errno)); 226 | } 227 | else { 228 | LOG_INFO("unregister fd[%d] from epoll efd[%d] success!", fd, efd_); 229 | } 230 | return true; 231 | } 232 | 233 | 234 | thread_local uint64_t fiber_seq = 0; 235 | 236 | Fiber::Fiber(std::function run, XFiber *xfiber, size_t stack_size, std::string fiber_name) { 237 | run_ = run; 238 | xfiber_ = xfiber; 239 | fiber_name_ = fiber_name; 240 | stack_size_ = stack_size; 241 | stack_ptr_ = new uint8_t[stack_size_]; 242 | 243 | getcontext(&ctx_); 244 | ctx_.uc_stack.ss_sp = stack_ptr_; 245 | ctx_.uc_stack.ss_size = stack_size_; 246 | ctx_.uc_link = xfiber->SchedCtx(); 247 | makecontext(&ctx_, (void (*)())Fiber::Start, 1, this); 248 | 249 | seq_ = fiber_seq++; 250 | status_ = FiberStatus::INIT; 251 | } 252 | 253 | Fiber::~Fiber() { 254 | delete []stack_ptr_; 255 | stack_ptr_ = nullptr; 256 | stack_size_ = 0; 257 | } 258 | 259 | uint64_t Fiber::Seq() { 260 | return seq_; 261 | } 262 | 263 | XFiberCtx *Fiber::Ctx() { 264 | return &ctx_; 265 | } 266 | 267 | void Fiber::Start(Fiber *fiber) { 268 | fiber->run_(); 269 | fiber->status_ = FiberStatus::FINISHED; 270 | LOG_DEBUG("fiber[%lu] finished...", fiber->Seq()); 271 | } 272 | 273 | std::string Fiber::Name() { 274 | return fiber_name_; 275 | } 276 | 277 | bool Fiber::IsFinished() { 278 | return status_ == FiberStatus::FINISHED; 279 | } 280 | 281 | void Fiber::SetWaitingEvent(const WaitingEvents &events) { 282 | for (size_t i = 0; i < events.waiting_fds_r_.size(); i++) { 283 | waiting_events_.waiting_fds_r_.push_back(events.waiting_fds_r_[i]); 284 | } 285 | for (size_t i = 0; i < events.waiting_fds_w_.size(); i++) { 286 | waiting_events_.waiting_fds_w_.push_back(events.waiting_fds_w_[i]); 287 | } 288 | if (events.expire_at_ > 0) { 289 | waiting_events_.expire_at_ = events.expire_at_; 290 | } 291 | } 292 | 293 | -------------------------------------------------------------------------------- /xfiber.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | 12 | #include "log.h" 13 | #include "util.h" 14 | 15 | typedef enum { 16 | INIT = 0, 17 | READYING = 1, 18 | WAITING = 2, 19 | FINISHED = 3 20 | }FiberStatus; 21 | 22 | typedef struct ucontext_t XFiberCtx; 23 | 24 | 25 | #define SwitchCtx(from, to) \ 26 | swapcontext(from, to) 27 | 28 | struct WaitingEvents { 29 | WaitingEvents() { 30 | expire_at_ = -1; 31 | } 32 | void Reset() { 33 | expire_at_ = -1; 34 | waiting_fds_r_.clear(); 35 | waiting_fds_w_.clear(); 36 | } 37 | 38 | // 一个协程中监听的fd不会太多,所以直接用数组 39 | std::vector waiting_fds_r_; 40 | std::vector waiting_fds_w_; 41 | int64_t expire_at_; 42 | }; 43 | 44 | class Fiber; 45 | 46 | class XFiber { 47 | public: 48 | XFiber(); 49 | 50 | ~XFiber(); 51 | 52 | void WakeupFiber(Fiber *fiber); 53 | 54 | void CreateFiber(std::function run, size_t stack_size = 0, std::string fiber_name=""); 55 | 56 | void Dispatch(); 57 | 58 | void Yield(); 59 | 60 | void SwitchToSched(); 61 | 62 | void TakeOver(int fd); 63 | 64 | bool UnregisterFd(int fd); 65 | 66 | void RegisterWaitingEvents(WaitingEvents &events); 67 | 68 | void SleepMs(int ms); 69 | 70 | XFiberCtx *SchedCtx(); 71 | 72 | static XFiber *xfiber() { 73 | static thread_local XFiber xf; 74 | return &xf; 75 | } 76 | 77 | private: 78 | int efd_; 79 | 80 | std::deque ready_fibers_; 81 | 82 | std::deque running_fibers_; 83 | 84 | XFiberCtx sched_ctx_; 85 | 86 | Fiber *curr_fiber_; 87 | 88 | struct WaitingFibers { 89 | Fiber *r_, *w_; 90 | WaitingFibers(Fiber *r = nullptr, Fiber *w = nullptr) { 91 | r_ = r; 92 | w_ = w; 93 | } 94 | }; 95 | 96 | std::map io_waiting_fibers_; 97 | // 会不会出现一个fd的读/写被多个协程监听??不会! 98 | // 但是一个fiber可能会监听多个fd,实际也不存在,一个连接由一个协程处理 99 | 100 | std::map> expire_events_; 101 | 102 | std::vector finished_fibers_; 103 | }; 104 | 105 | 106 | class Fiber 107 | { 108 | public: 109 | Fiber(std::function run, XFiber *xfiber, size_t stack_size, std::string fiber_name); 110 | 111 | ~Fiber(); 112 | 113 | XFiberCtx *Ctx(); 114 | 115 | std::string Name(); 116 | 117 | bool IsFinished(); 118 | 119 | uint64_t Seq(); 120 | 121 | static void Start(Fiber *fiber); 122 | 123 | struct FdEvent { 124 | int fd_; 125 | int64_t expired_at_; 126 | 127 | FdEvent(int fd =-1, int64_t expired_at=-1) { 128 | if (expired_at <= 0) { 129 | expired_at = -1; 130 | } 131 | fd_ = fd; 132 | expired_at_ = expired_at; 133 | } 134 | }; 135 | 136 | WaitingEvents &GetWaitingEvents() { 137 | return waiting_events_; 138 | } 139 | 140 | void SetWaitingEvent(const WaitingEvents &events); 141 | 142 | private: 143 | uint64_t seq_; 144 | 145 | XFiber *xfiber_; 146 | 147 | std::string fiber_name_; 148 | 149 | FiberStatus status_; 150 | 151 | ucontext_t ctx_; 152 | 153 | uint8_t *stack_ptr_; 154 | 155 | size_t stack_size_; 156 | 157 | std::function run_; 158 | 159 | WaitingEvents waiting_events_; 160 | 161 | }; 162 | 163 | -------------------------------------------------------------------------------- /xsocket.cpp: -------------------------------------------------------------------------------- 1 | #include "xsocket.h" 2 | #include "xfiber.h" 3 | 4 | 5 | uint32_t Fd::next_seq_ = 0; 6 | 7 | 8 | Fd::Fd() { 9 | fd_ = -1; 10 | seq_ = Fd::next_seq_++; 11 | } 12 | 13 | Fd::~Fd() { 14 | 15 | } 16 | 17 | bool Fd::Available() { 18 | return fd_ > 0; 19 | } 20 | 21 | int Fd::RawFd() { 22 | return fd_; 23 | } 24 | 25 | void Fd::RegisterFdToSched() { 26 | XFiber *xfiber = XFiber::xfiber(); 27 | xfiber->TakeOver(fd_); 28 | } 29 | 30 | Listener::Listener() { 31 | 32 | } 33 | 34 | Listener::~Listener() { 35 | close(fd_); 36 | } 37 | 38 | Listener Listener::ListenTCP(uint16_t port) { 39 | int fd = socket(AF_INET, SOCK_STREAM, 0); 40 | if (fd < 0) { 41 | exit(-1); 42 | } 43 | 44 | struct sockaddr_in addr; 45 | addr.sin_family = AF_INET; 46 | addr.sin_port = htons(port); 47 | addr.sin_addr.s_addr = htonl(INADDR_ANY); 48 | 49 | int flag = 1; 50 | if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag)) < 0) { 51 | LOG_ERROR("try set SO_REUSEADDR failed, msg=%s", strerror(errno)); 52 | exit(-1); 53 | } 54 | 55 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { 56 | LOG_ERROR("set set listen fd O_NONBLOCK failed, msg=%s", strerror(errno)); 57 | exit(-1); 58 | } 59 | 60 | //bind 61 | if (bind(fd, (sockaddr *)&addr, sizeof(sockaddr_in)) < 0) { 62 | LOG_ERROR("try bind port [%d] failed, msg=%s", port, strerror(errno)); 63 | exit(-1); 64 | } 65 | 66 | //listen 67 | if (listen(fd, 10) < 0) { 68 | LOG_ERROR("try listen port[%d] failed, msg=%s", port, strerror(errno)); 69 | exit(-1); 70 | } 71 | 72 | Listener listener; 73 | listener.FromRawFd(fd); 74 | 75 | LOG_INFO("listen %d success...", port); 76 | XFiber::xfiber()->TakeOver(fd); 77 | 78 | return listener; 79 | } 80 | 81 | void Listener::FromRawFd(int fd) { 82 | fd_ = fd; 83 | } 84 | 85 | std::shared_ptr Listener::Accept() { 86 | XFiber *xfiber = XFiber::xfiber(); 87 | 88 | while (true) { 89 | int client_fd = accept(fd_, nullptr, nullptr); 90 | if (client_fd > 0) { 91 | if (fcntl(client_fd, F_SETFL, O_NONBLOCK) != 0) { 92 | perror("fcntl"); 93 | exit(-1); 94 | } 95 | int nodelay = 1; 96 | if (setsockopt(client_fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) { 97 | LOG_ERROR("try set TCP_NODELAY failed, msg=%s", strerror(errno)); 98 | close(client_fd); 99 | client_fd = -1; 100 | } 101 | xfiber->TakeOver(client_fd); 102 | return std::shared_ptr(new Connection(client_fd)); 103 | } 104 | else { 105 | if (errno == EAGAIN) { 106 | // accept失败,协程切出 107 | WaitingEvents events; 108 | events.waiting_fds_r_.push_back(fd_); 109 | xfiber->RegisterWaitingEvents(events); 110 | xfiber->SwitchToSched(); 111 | } 112 | else if (errno == EINTR) { 113 | LOG_INFO("accept client connect return interrupt error, ignore and conitnue..."); 114 | } 115 | else { 116 | perror("accept"); 117 | } 118 | } 119 | } 120 | return std::shared_ptr(new Connection(-1)); 121 | } 122 | 123 | 124 | Connection::Connection() { 125 | } 126 | 127 | Connection::Connection(int fd) { 128 | fd_ = fd; 129 | } 130 | 131 | Connection::~Connection() { 132 | XFiber::xfiber()->UnregisterFd(fd_); 133 | LOG_INFO("close fd[%d]", fd_); 134 | close(fd_); 135 | fd_ = -1; 136 | } 137 | 138 | 139 | std::shared_ptr Connection::ConnectTCP(const char *ipv4, uint16_t port) { 140 | int fd = socket(AF_INET,SOCK_STREAM, 0); 141 | 142 | struct sockaddr_in svr_addr; 143 | memset(&svr_addr, 0, sizeof(svr_addr)); 144 | svr_addr.sin_family = AF_INET; 145 | svr_addr.sin_port = htons(port); 146 | svr_addr.sin_addr.s_addr = inet_addr(ipv4); 147 | 148 | //连接服务器,成功返回0,错误返回-1 149 | if (connect(fd, (struct sockaddr *)&svr_addr, sizeof(svr_addr)) < 0) 150 | { 151 | LOG_ERROR("try connect %s:%d failed, msg=%s", ipv4, port, strerror(errno)); 152 | return std::shared_ptr(new Connection(-1)); 153 | } 154 | 155 | int nodelay = 1; 156 | if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &nodelay, sizeof(nodelay)) < 0) { 157 | LOG_ERROR("try set TCP_NODELAY failed, msg=%s", strerror(errno)); 158 | close(fd); 159 | return std::shared_ptr(new Connection(-1)); 160 | } 161 | 162 | if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { 163 | LOG_ERROR("set set fd[%d] O_NONBLOCK failed, msg=%s", fd, strerror(errno)); 164 | close(fd); 165 | return std::shared_ptr(new Connection(-1)); 166 | } 167 | LOG_DEBUG("connect %s:%d success with fd[%d]", ipv4, port, fd); 168 | XFiber::xfiber()->TakeOver(fd); 169 | 170 | return std::shared_ptr(new Connection(fd)); 171 | } 172 | 173 | ssize_t Connection::Write(const char *buf, size_t sz, int timeout_ms) const { 174 | size_t write_bytes = 0; 175 | XFiber *xfiber = XFiber::xfiber(); 176 | int64_t expire_at = timeout_ms > 0 ? util::NowMs() + timeout_ms : -1; 177 | 178 | while (write_bytes < sz) { 179 | int n = write(fd_, buf + write_bytes, sz - write_bytes); 180 | if (n > 0) { 181 | write_bytes += n; 182 | LOG_DEBUG("write to fd[%d] return %d, total send %ld bytes", fd_, n, write_bytes); 183 | } 184 | else if (n == 0) { 185 | LOG_INFO("write to fd[%d] return 0 byte, peer has closed", fd_); 186 | return 0; 187 | } 188 | else { 189 | if (expire_at > 0 && util::NowMs() >= expire_at) { 190 | LOG_WARNING("write to fd[%d] timeout after wait %dms", fd_, timeout_ms); 191 | return 0; 192 | } 193 | if (errno != EAGAIN && errno != EINTR) { 194 | LOG_DEBUG("write to fd[%d] failed, msg=%s", fd_, strerror(errno)); 195 | return -1; 196 | } 197 | else if (errno == EAGAIN) { 198 | LOG_DEBUG("write to fd[%d] return EAGIN, add fd into IO waiting events and switch to sched", fd_); 199 | WaitingEvents events; 200 | events.expire_at_ = expire_at; 201 | events.waiting_fds_w_.push_back(fd_); 202 | xfiber->RegisterWaitingEvents(events); 203 | xfiber->SwitchToSched(); 204 | } 205 | else { 206 | //pass 207 | } 208 | } 209 | } 210 | LOG_DEBUG("write to fd[%d] for %ld byte(s) success", fd_, sz); 211 | return sz; 212 | } 213 | 214 | ssize_t Connection::Read(char *buf, size_t sz, int timeout_ms) const { 215 | XFiber *xfiber = XFiber::xfiber(); 216 | int64_t expire_at = timeout_ms > 0 ? util::NowMs() + timeout_ms : -1; 217 | 218 | while (true) { 219 | int n = read(fd_, buf, sz); 220 | LOG_DEBUG("read from fd[%d] reutrn %d bytes", fd_, n); 221 | if (n > 0) { 222 | return n; 223 | } 224 | else if (n == 0) { 225 | LOG_DEBUG("read from fd[%d] return 0 byte, peer has closed", fd_); 226 | return 0; 227 | } 228 | else { 229 | if (expire_at > 0 && util::NowMs() >= expire_at) { 230 | LOG_WARNING("read from fd[%d] timeout after wait %dms", fd_, timeout_ms); 231 | return 0; 232 | } 233 | if (errno != EAGAIN && errno != EINTR) { 234 | LOG_DEBUG("read from fd[%d] failed, msg=%s", fd_, strerror(errno)) 235 | return -1; 236 | } 237 | else if (errno == EAGAIN) { 238 | LOG_DEBUG("read from fd[%d] return EAGIN, add into waiting/expire events with expire at %ld and switch to sched", fd_, expire_at); 239 | WaitingEvents events; 240 | events.expire_at_ = expire_at; 241 | events.waiting_fds_r_.push_back(fd_); 242 | xfiber->RegisterWaitingEvents(events); 243 | xfiber->SwitchToSched(); 244 | } 245 | else if (errno == EINTR) { 246 | //pass 247 | } 248 | } 249 | } 250 | return -1; 251 | } 252 | -------------------------------------------------------------------------------- /xsocket.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include "util.h" 14 | 15 | 16 | class Fd { 17 | public: 18 | Fd(); 19 | 20 | ~Fd(); 21 | 22 | static uint32_t next_seq_; 23 | 24 | int RawFd(); 25 | 26 | void RegisterFdToSched(); 27 | 28 | bool Available(); 29 | 30 | protected: 31 | int fd_; 32 | 33 | int seq_; 34 | }; 35 | 36 | 37 | class Connection; 38 | 39 | class Listener : public Fd { 40 | public: 41 | 42 | Listener(); 43 | 44 | ~Listener(); 45 | 46 | std::shared_ptr Accept(); 47 | 48 | void FromRawFd(int fd); 49 | 50 | static Listener ListenTCP(uint16_t port); 51 | 52 | private: 53 | uint16_t port_; 54 | }; 55 | 56 | 57 | class Connection : public Fd { 58 | public: 59 | Connection(); 60 | 61 | Connection(int fd); 62 | 63 | ~Connection(); 64 | 65 | static std::shared_ptr ConnectTCP(const char *ipv4, uint16_t port); 66 | 67 | ssize_t Write(const char *buf, size_t sz, int timeout_ms=-1) const; 68 | 69 | ssize_t Read(char *buf, size_t sz, int timeout_ms=-1) const; 70 | }; 71 | --------------------------------------------------------------------------------