项目结构及几个文件初步编写

This commit is contained in:
yhy 2024-12-15 21:49:26 +08:00
parent 7af8ba3bc6
commit 222b364df0
9 changed files with 192 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
bin/
build/
lib/
core.*

31
CMakeLists.txt Normal file
View 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}
)

View 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");
}

View 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
View 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;
}
};

View 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
View 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
View 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
View 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";
}