项目结构及几个文件初步编写
This commit is contained in:
parent
7af8ba3bc6
commit
222b364df0
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
@ -0,0 +1,4 @@
|
||||
bin/
|
||||
build/
|
||||
lib/
|
||||
core.*
|
31
CMakeLists.txt
Normal file
31
CMakeLists.txt
Normal file
@ -0,0 +1,31 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
project(tinyrpc)
|
||||
|
||||
enable_language(CXX ASM)
|
||||
|
||||
add_compile_options(-g -Wall -std=c++11)
|
||||
|
||||
include_directories(includes/coroutine)
|
||||
include_directories(includes/log)
|
||||
|
||||
aux_source_directory(${CMAKE_SOURCE_DIR}/src/coroutine SRC_LIST)
|
||||
|
||||
# set(ASM_FILES ${CMAKE_SOURCE_DIR}/src/swap.S)
|
||||
|
||||
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_SOURCE_DIR}/bin)
|
||||
set(LIBRARY_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/lib)
|
||||
|
||||
|
||||
add_library(tinyrpc
|
||||
${SRC_LIST}
|
||||
# ${ASM_FILES}
|
||||
|
||||
)
|
||||
aux_source_directory(${CMAKE_SOURCE_DIR}/test/logtest TEST_SRC_LIST)
|
||||
|
||||
add_executable(test_tinyrpc
|
||||
${TEST_SRC_LIST}
|
||||
|
||||
)
|
||||
|
33
includes/coroutine/coctx.h
Normal file
33
includes/coroutine/coctx.h
Normal file
@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
namespace tinyrpc {
|
||||
enum class reg : unsigned int { // https://wiki.osdev.org/System_V_ABI
|
||||
kRBP = 6, // 栈底指针
|
||||
kRDI = 7, // rdi,调用函数时的第一个参数
|
||||
kRSI = 8, // rsi, 调用函数时的第二个参数 这两个是 根据调用约定确定的
|
||||
kRETAddr = 9, // 下一个要执行的命令地址,它将被分配给 rip
|
||||
kRSP = 13, // 堆栈顶部指针
|
||||
/*
|
||||
|
||||
High memory
|
||||
-----------------
|
||||
| 调用者的 rbp | <- 被调用函数栈帧的起点
|
||||
-----------------
|
||||
| 返回地址 (rip) | <- 存储调用函数后的下一条指令地址
|
||||
-----------------
|
||||
| 局部变量 |
|
||||
-----------------
|
||||
Low memory
|
||||
*/
|
||||
};
|
||||
|
||||
struct coctx { // Coroutine Context
|
||||
void* regs[14]{}; // 初始化为 0
|
||||
};
|
||||
|
||||
// 将当前寄存器的状态保存到第一个上下文中,然后从第二个上下文中取出寄存器的状态并将其分配给寄存器。
|
||||
extern "C" void coctx_swap(coctx *, coctx *) asm("coctx_swap");
|
||||
|
||||
|
||||
|
||||
}
|
28
includes/coroutine/coroutine.hpp
Normal file
28
includes/coroutine/coroutine.hpp
Normal file
@ -0,0 +1,28 @@
|
||||
#pragma once
|
||||
#include "coctx.h"
|
||||
#include <functional>
|
||||
namespace tinyrpc {
|
||||
|
||||
|
||||
class Coroutine {
|
||||
private:
|
||||
Coroutine();
|
||||
public:
|
||||
Coroutine(std::size_t stack_size, void* stack_sp);
|
||||
Coroutine(std::size_t stack_size, void* stack_sp, std::function<void()> cb);
|
||||
|
||||
int getCorID() const {return m_cor_id;}
|
||||
|
||||
private:
|
||||
coctx m_ctx {}; // 这个协程的上下文信息
|
||||
int m_cor_id {0}; // 这个协程的 id
|
||||
char* m_stack_sp {nullptr}; // 这个协程的栈空间指针
|
||||
std::size_t m_stack_size {0};
|
||||
bool m_is_in_cofunc {true}; // 调用 CoFunction 时为真,CoFunction 完成时为假。
|
||||
std::function<void()> m_callback {};
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
18
includes/log/logger.h
Normal file
18
includes/log/logger.h
Normal file
@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
#include <iostream>
|
||||
#include <ostream>
|
||||
|
||||
// #define LOGGER (std::cout << __FILE__ << ":" << __LINE__)
|
||||
|
||||
struct logger {
|
||||
logger() = default;
|
||||
|
||||
template<typename T>
|
||||
std::ostream& operator <<(T msg) {
|
||||
return std::cout << __FILE__ << ":" << __LINE__ << " " << msg;
|
||||
}
|
||||
|
||||
~logger() {
|
||||
std::cout << std::endl;
|
||||
}
|
||||
};
|
40
src/coroutine/coctx_swap.S
Normal file
40
src/coroutine/coctx_swap.S
Normal file
@ -0,0 +1,40 @@
|
||||
.globl coctx_swap
|
||||
coctx_swap:
|
||||
leaq (%rsp),%rax
|
||||
movq %rax, 104(%rdi)
|
||||
movq %rbx, 96(%rdi)
|
||||
movq %rcx, 88(%rdi)
|
||||
movq %rdx, 80(%rdi)
|
||||
movq 0(%rax), %rax
|
||||
movq %rax, 72(%rdi)
|
||||
movq %rsi, 64(%rdi)
|
||||
movq %rdi, 56(%rdi)
|
||||
movq %rbp, 48(%rdi)
|
||||
movq %r8, 40(%rdi)
|
||||
movq %r9, 32(%rdi)
|
||||
movq %r12, 24(%rdi)
|
||||
movq %r13, 16(%rdi)
|
||||
movq %r14, 8(%rdi)
|
||||
movq %r15, (%rdi)
|
||||
xorq %rax, %rax
|
||||
|
||||
movq 48(%rsi), %rbp
|
||||
movq 104(%rsi), %rsp
|
||||
movq (%rsi), %r15
|
||||
movq 8(%rsi), %r14
|
||||
movq 16(%rsi), %r13
|
||||
movq 24(%rsi), %r12
|
||||
movq 32(%rsi), %r9
|
||||
movq 40(%rsi), %r8
|
||||
movq 56(%rsi), %rdi
|
||||
movq 80(%rsi), %rdx
|
||||
movq 88(%rsi), %rcx
|
||||
movq 96(%rsi), %rbx
|
||||
leaq 8(%rsp), %rsp # 把调用这个函数时主调函数压栈的返回地址弹出
|
||||
pushq 72(%rsi) # 把我们要恢复的返回地址压栈
|
||||
movq 64(%rsi), %rsi
|
||||
ret
|
||||
|
||||
|
||||
# https://zhuanlan.zhihu.com/p/27409164
|
||||
# https://blog.csdn.net/m0_47696151/article/details/121324729
|
15
test/asmtest/main.cc
Normal file
15
test/asmtest/main.cc
Normal file
@ -0,0 +1,15 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
|
||||
extern "C" void asm_swap(void *a, void *b) asm("asm_swap");
|
||||
|
||||
|
||||
int main() {
|
||||
long a = 1, b = 2;
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
asm_swap(&a, &b);
|
||||
cout << "a = " << a << ", b = " << b << endl;
|
||||
return 0;
|
||||
|
||||
}
|
7
test/asmtest/swap.S
Normal file
7
test/asmtest/swap.S
Normal file
@ -0,0 +1,7 @@
|
||||
.global asm_swap # 声明 swap 为全局符号,使得该符号可以被其他文件或模块引用。
|
||||
asm_swap: # 表明后续的指令是该函数的实现。
|
||||
movq (%rdi), %rax # 实现 swap 逻辑
|
||||
movq (%rsi), %rbx
|
||||
movq %rax, (%rsi)
|
||||
movq %rbx, (%rdi)
|
||||
ret # 它将控制权返回给调用者
|
16
test/logtest/main.cc
Normal file
16
test/logtest/main.cc
Normal file
@ -0,0 +1,16 @@
|
||||
#include <iostream>
|
||||
#include "logger.h"
|
||||
using namespace std;
|
||||
|
||||
|
||||
// extern "C" void asm_swap(void *a, void *b) asm("asm_swap");
|
||||
|
||||
|
||||
int main() {
|
||||
// long a = 1, b = 2;
|
||||
// cout << "a = " << a << ", b = " << b << endl;
|
||||
// asm_swap(&a, &b);
|
||||
// cout << "a = " << a << ", b = " << b << endl;
|
||||
// return 0;
|
||||
logger() << "test";
|
||||
}
|
Loading…
Reference in New Issue
Block a user