From edf1d9f805eb756afb42bfd0b4d610a64b8c0870 Mon Sep 17 00:00:00 2001 From: zhengshuxin Date: Sun, 8 Sep 2024 18:23:49 +0800 Subject: [PATCH 1/2] test fiber --- lib_fiber/samples-c/test/Makefile_cpp.in | 137 ++++++++++++++++++++ lib_fiber/samples-c/test/fiber1/main.c | 2 +- lib_fiber/samples-c/test/fiber1/makefile | 9 +- lib_fiber/samples-c/test/fiber2/makefile | 13 +- lib_fiber/samples-c/test/fiber3/main.cpp | 1 + lib_fiber/samples-c/test/fiber3/makefile | 14 +- lib_fiber/samples-c/test/fiber4/makefile | 13 +- lib_fiber/samples-c/test/fiber5/Makefile | 2 + lib_fiber/samples-c/test/fiber5/main.c | 23 ++++ lib_fiber/samples-c/test/fiber5/valgrind.sh | 4 + 10 files changed, 177 insertions(+), 41 deletions(-) create mode 100644 lib_fiber/samples-c/test/Makefile_cpp.in create mode 100644 lib_fiber/samples-c/test/fiber5/Makefile create mode 100644 lib_fiber/samples-c/test/fiber5/main.c create mode 100644 lib_fiber/samples-c/test/fiber5/valgrind.sh diff --git a/lib_fiber/samples-c/test/Makefile_cpp.in b/lib_fiber/samples-c/test/Makefile_cpp.in new file mode 100644 index 000000000..97ec08157 --- /dev/null +++ b/lib_fiber/samples-c/test/Makefile_cpp.in @@ -0,0 +1,137 @@ +CC = $(ENV_CC) + +CFLAGS = -c -g -W \ +-Wall \ +-Wcast-qual \ +-Wcast-align \ +-Wno-long-long \ +-Wpointer-arith \ +-Werror \ +-Wshadow \ +-D_REENTRANT \ +-D_POSIX_PTHREAD_SEMANTICS + +########################################################### +#Check system: +# Linux, SunOS, Solaris, BSD variants, AIX, HP-UX +SYSLIB = -lpthread -ldl +CHECKSYSRES = @echo "Unknow system type!";exit 1 +UNIXNAME = $(shell uname -sm) + +ifeq ($(CC),) + CC = g++ +endif + +ifeq ($(findstring clang++, $(CC)), clang++) + CFLAGS += -Wno-invalid-source-encoding \ + -Wno-invalid-offsetof +endif + +# For FreeBSD +ifeq ($(findstring FreeBSD, $(UNIXNAME)), FreeBSD) + CFLAGS += -DFREEBSD + SYSLIB += -lcrypt -rdynamic +endif + +# For Darwin +ifeq ($(findstring Darwin, $(UNIXNAME)), Darwin) +# CC += -arch x86_64 -arch arm64 + CFLAGS += -DMACOSX -Wno-invalid-source-encoding \ + -Wno-invalid-offsetof + SYSLIB += -rdynamic + UNIXTYPE = MACOSX +endif + +#Path for Linux +ifeq ($(findstring Linux, $(UNIXNAME)), Linux) + CFLAGS += -DLINUX2 + SYSLIB += -lcrypt -rdynamic +endif + +# For MINGW +ifeq ($(findstring MINGW, $(UNIXNAME)), MINGW) + SYSLIB = -lpthread-2 -rdynamic + CFLAGS += -DLINUX2 -DMINGW + UNIXTYPE = LINUX +endif + +#Path for SunOS +ifeq ($(findstring SunOS, $(UNIXNAME)), SunOS) + ifeq ($(findstring 86, $(UNIXNAME)), 86) + SYSLIB += -lsocket -lnsl -lrt + endif + ifeq ($(findstring sun4u, $(UNIXNAME)), sun4u) + SYSLIB += -lsocket -lnsl -lrt + endif + CFLAGS += -DSUNOS5 + SYSLIB += -lcrypt -rdynamic +endif + +#Path for HP-UX +ifeq ($(findstring HP-UX, $(UNIXNAME)), HP-UX) + PLAT_NAME=hp-ux + CFLAGS += -DHP_UX -DHPUX11 + SYSLIB += -lcrypt -rdynamic +endif + +#Find system type. +ifneq ($(SYSPATH),) + CHECKSYSRES = @echo "System is $(shell uname -sm)" +endif +########################################################### + +ACL_PATH = ../../../../lib_acl +ACL_INC = $(ACL_PATH)/include +ACL_LIB = $(ACL_PATH)/lib +ACL_CPP_PATH = ../../../../lib_acl_cpp +ACL_CPP_INC = $(ACL_CPP_PATH)/include +ACL_CPP_LIB = $(ACL_CPP_PATH)/lib + +PRO_PATH = ../../../../lib_protocol +PRO_INC = $(PRO_PATH)/include +PRO_LIB = $(PRO_PATH)/lib + +FIBER_PATH = ../../../../lib_fiber +FIBER_INC = $(FIBER_PATH)/c/include +FIBER_LIB = $(FIBER_PATH)/lib + +EXTLIBS = +CFLAGS += -I.. -I$(PRO_INC) -I$(ACL_INC) -I$(FIBER_INC) -I$(FIBER_PATH)/cpp/include -I$(ACL_CPP_INC) +LDFLAGS = -L$(FIBER_LIB) -lfiber_cpp \ + -L$(ACL_CPP_LIB) -lacl_cpp \ + -L$(PRO_LIB) -l_protocol \ + -L$(ACL_LIB) -l_acl \ + -L$(FIBER_LIB) -lfiber \ + $(EXTLIBS) $(SYSLIB) + +########################################################### + +OUT_PATH = . +OBJ_PATH = $(OUT_PATH) + +#Project's objs +SRC = $(wildcard *.cpp) +OBJ = $(patsubst %.cpp, $(OBJ_PATH)/%.o, $(notdir $(SRC))) +########################################################### + +.PHONY = all clean +PROG = + +COMPILE = $(CC) $(CFLAGS) + +#-Wl,-rpath,$(ACL_LIB) -Wl,-rpath,$(PROTO_LIB) -o $(OBJ_PATH)/$(PROG) +all: RM $(OBJ) + $(CC) $(OBJ) $(LDFLAGS) -o $(OBJ_PATH)/$(PROG) + @echo "" + @echo "All ok! Output:$(PROG)" + @echo "" +$(OBJ_PATH)/%.o: %.cpp + $(COMPILE) $< -o $@ +RM: + rm -f $(PROG) +clean cl: + rm -f $(PROG) + rm -f $(OBJ) + +rebuild rb: clean all +########################################################### diff --git a/lib_fiber/samples-c/test/fiber1/main.c b/lib_fiber/samples-c/test/fiber1/main.c index 03f35998f..21f46dfa0 100644 --- a/lib_fiber/samples-c/test/fiber1/main.c +++ b/lib_fiber/samples-c/test/fiber1/main.c @@ -24,7 +24,7 @@ static void fiber_main(ACL_FIBER *fiber, void *ctx acl_unused) int main(void) { - int ch, i; + int i; /* 创建协程 */ for (i = 0; i < __max_fiber; i++) diff --git a/lib_fiber/samples-c/test/fiber1/makefile b/lib_fiber/samples-c/test/fiber1/makefile index 0729407e4..abef54044 100644 --- a/lib_fiber/samples-c/test/fiber1/makefile +++ b/lib_fiber/samples-c/test/fiber1/makefile @@ -1,7 +1,2 @@ -fiber: main.o - gcc -o fiber main.o -L../../../lib -l_fiber -L../../../../lib_acl/lib -l_acl -lpthread -ldl -main.o: main.c - gcc -O3 -c main.c -DLINUX2 -I.. -I../../../c/include -I../../../../lib_acl/include - -clean: - rm -f fiber main.o +include ../Makefile.in +PROG = fiber diff --git a/lib_fiber/samples-c/test/fiber2/makefile b/lib_fiber/samples-c/test/fiber2/makefile index d9d85f1ca..c5831c38b 100644 --- a/lib_fiber/samples-c/test/fiber2/makefile +++ b/lib_fiber/samples-c/test/fiber2/makefile @@ -1,11 +1,2 @@ -fiber: main.o - g++ -o fiber main.o -L../../../lib -l_fiber_cpp -l_fiber \ - -L../../../../lib_acl_cpp/lib -l_acl_cpp \ - -L../../../../lib_acl/lib -l_acl \ - -lpthread -ldl -main.o: main.cpp - g++ -O3 -Wall -c main.cpp -DLINUX2 -I.. -I../../../cpp/include \ - -I../../../../lib_acl_cpp/include - -clean: - rm -f fiber main.o +include ../Makefile_cpp.in +PROG = fiber diff --git a/lib_fiber/samples-c/test/fiber3/main.cpp b/lib_fiber/samples-c/test/fiber3/main.cpp index 701a9d256..93ca6af7b 100644 --- a/lib_fiber/samples-c/test/fiber3/main.cpp +++ b/lib_fiber/samples-c/test/fiber3/main.cpp @@ -2,6 +2,7 @@ #include #include "acl_cpp/lib_acl.hpp" #include "fiber/lib_fiber.hpp" +#include "fiber/go_fiber.hpp" static void fiber_main(int max_loop) { diff --git a/lib_fiber/samples-c/test/fiber3/makefile b/lib_fiber/samples-c/test/fiber3/makefile index 248d6c421..d1fe16358 100644 --- a/lib_fiber/samples-c/test/fiber3/makefile +++ b/lib_fiber/samples-c/test/fiber3/makefile @@ -1,11 +1,3 @@ -fiber: main.o - g++ -o fiber main.o -L../../../lib -l_fiber_cpp -l_fiber \ - -L../../../../lib_acl_cpp/lib -l_acl_cpp \ - -L../../../../lib_acl/lib -l_acl \ - -lpthread -ldl -main.o: main.cpp - g++ -std=c++11 -O3 -Wall -c main.cpp -DLINUX2 -I.. -I../../../cpp/include \ - -I../../../../lib_acl_cpp/include - -clean: - rm -f fiber main.o +include ../Makefile_cpp.in +CFLAGS += -std=c++11 +PROG = fiber diff --git a/lib_fiber/samples-c/test/fiber4/makefile b/lib_fiber/samples-c/test/fiber4/makefile index d9d85f1ca..c5831c38b 100644 --- a/lib_fiber/samples-c/test/fiber4/makefile +++ b/lib_fiber/samples-c/test/fiber4/makefile @@ -1,11 +1,2 @@ -fiber: main.o - g++ -o fiber main.o -L../../../lib -l_fiber_cpp -l_fiber \ - -L../../../../lib_acl_cpp/lib -l_acl_cpp \ - -L../../../../lib_acl/lib -l_acl \ - -lpthread -ldl -main.o: main.cpp - g++ -O3 -Wall -c main.cpp -DLINUX2 -I.. -I../../../cpp/include \ - -I../../../../lib_acl_cpp/include - -clean: - rm -f fiber main.o +include ../Makefile_cpp.in +PROG = fiber diff --git a/lib_fiber/samples-c/test/fiber5/Makefile b/lib_fiber/samples-c/test/fiber5/Makefile new file mode 100644 index 000000000..abef54044 --- /dev/null +++ b/lib_fiber/samples-c/test/fiber5/Makefile @@ -0,0 +1,2 @@ +include ../Makefile.in +PROG = fiber diff --git a/lib_fiber/samples-c/test/fiber5/main.c b/lib_fiber/samples-c/test/fiber5/main.c new file mode 100644 index 000000000..9e562fd0e --- /dev/null +++ b/lib_fiber/samples-c/test/fiber5/main.c @@ -0,0 +1,23 @@ +#include "lib_acl.h" +#include +#include +#include "fiber/lib_fiber.h" + +/* 协程处理入口函数 */ +static void fiber_main(ACL_FIBER *fiber acl_unused, void *ctx acl_unused) +{ + printf("---fiber is running---\r\n"); + acl_fiber_delay(2000); + printf("---fiber is exiting---\r\n"); +} + +int main(void) +{ + acl_fiber_schedule_init(1); + + printf("---begin create one fiber----\r\n"); + acl_fiber_create(fiber_main, NULL, 64000); + + printf("---- all fibers exit ----\r\n"); + return 0; +} diff --git a/lib_fiber/samples-c/test/fiber5/valgrind.sh b/lib_fiber/samples-c/test/fiber5/valgrind.sh new file mode 100644 index 000000000..696368204 --- /dev/null +++ b/lib_fiber/samples-c/test/fiber5/valgrind.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +#valgrind --tool=memcheck --leak-check=yes --leak-check=full --show-reachable=yes --max-stackframe=3426305034400000 -v ./fiber -n 10 -m 20 +valgrind --tool=memcheck --leak-check=yes --leak-check=full --show-reachable=yes -v ./fiber -n 1 -m 2 From 80602947cf02412789cca07a81c31a4c7a0a6c8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=91=E6=A0=91=E6=96=B0?= Date: Mon, 9 Sep 2024 09:45:38 +0800 Subject: [PATCH 2/2] Optimize and test fiber for QT UI. --- lib_fiber/cpp/include/fiber/go_fiber.hpp | 2 +- lib_fiber/samples-gui/QtFiber/inputdialog.cpp | 24 ++++++ lib_fiber/samples-gui/QtFiber/inputdialog.h | 27 +++++++ lib_fiber/samples-gui/QtFiber/main.cpp | 13 ++-- lib_fiber/samples-gui/QtFiber/mainwindow.cpp | 77 +++++++++++++++++-- lib_fiber/samples-gui/QtFiber/mainwindow.h | 16 ++++ lib_fiber/samples-gui/QtFiber/stdafx.h | 4 +- 7 files changed, 150 insertions(+), 13 deletions(-) create mode 100644 lib_fiber/samples-gui/QtFiber/inputdialog.cpp create mode 100644 lib_fiber/samples-gui/QtFiber/inputdialog.h diff --git a/lib_fiber/cpp/include/fiber/go_fiber.hpp b/lib_fiber/cpp/include/fiber/go_fiber.hpp index 3704e445b..822101d69 100644 --- a/lib_fiber/cpp/include/fiber/go_fiber.hpp +++ b/lib_fiber/cpp/include/fiber/go_fiber.hpp @@ -12,7 +12,7 @@ // 201703L (C++17) // 202002L (C++20) -#if __cplusplus >= 201103L // Support c++11 ? +#if defined(USE_CPP11) || __cplusplus >= 201103L // Support c++11 ? struct ACL_FIBER; diff --git a/lib_fiber/samples-gui/QtFiber/inputdialog.cpp b/lib_fiber/samples-gui/QtFiber/inputdialog.cpp new file mode 100644 index 000000000..5dc057129 --- /dev/null +++ b/lib_fiber/samples-gui/QtFiber/inputdialog.cpp @@ -0,0 +1,24 @@ +#include "inputdialog.h" + +InputDialog::InputDialog(QWidget *parent) : QDialog(parent) +{ + setWindowTitle("input: "); + setGeometry(200, 200, 300, 200); + + QVBoxLayout *layout = new QVBoxLayout(this); + + lineEdit = new QLineEdit(this); + lineEdit->setPlaceholderText("输入内容..."); + layout->addWidget(lineEdit); + + button = new QPushButton("确定", this); + layout->addWidget(button); + + connect(button, &QPushButton::clicked, this, &InputDialog::onAccept); +} + +void InputDialog::onAccept() +{ + emit dialogAccepted(lineEdit->text()); + accept(); +} diff --git a/lib_fiber/samples-gui/QtFiber/inputdialog.h b/lib_fiber/samples-gui/QtFiber/inputdialog.h new file mode 100644 index 000000000..37582911c --- /dev/null +++ b/lib_fiber/samples-gui/QtFiber/inputdialog.h @@ -0,0 +1,27 @@ +#ifndef INPUTDIALOG_H +#define INPUTDIALOG_H + +#include +#include +#include +#include + +class InputDialog : public QDialog +{ + Q_OBJECT + +public: + explicit InputDialog(QWidget *parent = nullptr); + +signals: + void dialogAccepted(const QString &text); + +private slots: + void onAccept(); + +private: + QLineEdit *lineEdit; + QPushButton *button; +}; + +#endif // INPUTDIALOG_H diff --git a/lib_fiber/samples-gui/QtFiber/main.cpp b/lib_fiber/samples-gui/QtFiber/main.cpp index 7a22c4c7d..93eee36a1 100644 --- a/lib_fiber/samples-gui/QtFiber/main.cpp +++ b/lib_fiber/samples-gui/QtFiber/main.cpp @@ -4,23 +4,26 @@ #include #include -class fiber_dummy : public acl::fiber { +class fiber_backend : public acl::fiber { public: - fiber_dummy() {} + fiber_backend() {} protected: void run() override { - qDebug() << "fiber_dummy started!"; + qDebug() << "fiber_backend started!"; + while (true) { + acl::fiber::delay(1000); + } delete this; } - ~fiber_dummy() = default; + ~fiber_backend() = default; }; static void startupCallback() { acl::fiber::init(acl::FIBER_EVENT_T_WMSG, true); - acl::fiber* fb = new fiber_dummy; + acl::fiber* fb = new fiber_backend; fb->start(); } diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.cpp b/lib_fiber/samples-gui/QtFiber/mainwindow.cpp index 5a8affd99..905fe55e6 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.cpp +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.cpp @@ -6,6 +6,7 @@ #include "mainwindow.h" #include "./ui_mainwindow.h" #include "childwindows.h" +#include "inputdialog.h" #include "fiber_server.h" #include "fiber_client.h" @@ -21,20 +22,31 @@ MainWindow::MainWindow(QWidget *parent) connect(button_, &QPushButton::clicked, this, &MainWindow::onButtonClicked); start_server_ = new QPushButton("Start fiber server", this); - start_server_->setGeometry(QRect(QPoint(100, 200), QSize(300, 50))); + start_server_->setGeometry(QRect(QPoint(100, 150), QSize(300, 50))); connect(start_server_, &QPushButton::clicked, this, &MainWindow::onStartServer); start_client_ = new QPushButton("Start fiber client", this); - start_client_->setGeometry(QRect(QPoint(100, 300), QSize(300, 50))); + start_client_->setGeometry(QRect(QPoint(100, 200), QSize(300, 50))); connect(start_client_, &QPushButton::clicked, this, &MainWindow::onStartClient); + url_get_ = new QPushButton("Http download", this); + url_get_->setGeometry(QRect(QPoint(100, 250), QSize(300, 50))); + connect(url_get_, &QPushButton::clicked, this, &MainWindow::onUrlGet); + stop_fiber_ = new QPushButton("Stop fiber schedule", this); - stop_fiber_->setGeometry(QRect(QPoint(100, 400), QSize(300, 50))); + stop_fiber_->setGeometry(QRect(QPoint(100, 300), QSize(300, 50))); connect(stop_fiber_, &QPushButton::clicked, this, &MainWindow::onStopSchedule); open_child_ = new QPushButton("Open Child Window", this); - open_child_->setGeometry(QRect(QPoint(100, 500), QSize(300, 50))); + open_child_->setGeometry(QRect(QPoint(100, 350), QSize(300, 50))); connect(open_child_, &QPushButton::clicked, this, &MainWindow::onOpenChildWindow); + + input_button_= new QPushButton("Open dialog", this); + input_button_->setGeometry(100, 400, 300,50); + connect(input_button_, &QPushButton::clicked, this, &MainWindow::onInputClicked); + + input_display_ = new QLabel("输入内容: ", this); + input_display_->setGeometry(100, 450, 200, 50); } MainWindow::~MainWindow() @@ -60,9 +72,9 @@ void MainWindow::onStartServer() } server_ = new fiber_server("127.0.0.1", 9001, this); - qDebug() << "Start one fiber"; + qDebug() << "Start fiber server"; server_->start(); - qDebug() << "Fiber started\r\n"; + qDebug() << "Fiber server started"; } void MainWindow::onStartClient() @@ -73,6 +85,42 @@ void MainWindow::onStartClient() qDebug() << "Fiber client started!"; } +void MainWindow::onUrlGet() +{ + go[this] { + const char *addr = "www.baidu.com:80"; + const char *host = "www.baidu.com"; + acl::http_request req(addr); + req.request_header() + .set_url("/") + .set_host(host); + if (!req.request(nullptr, 0)) { + qDebug() << "Send http request to " << addr << " error: " << acl::last_serror(); + return; + } + + acl::string buf; + if (req.get_body(buf)) { + this->onDownloadFinish(true, req); + } else { + this->onDownloadFinish(false, req); + } + }; +} + +void MainWindow::onDownloadFinish(bool ok, const acl::http_request& req) +{ + if (ok) { + acl::http_client *client = req.get_client(); + acl::string buf; + client->sprint_header(buf); + qDebug() << "Got response body ok!"; + qDebug() << buf.c_str(); + } else { + qDebug() << "Got response body error!"; + } +} + void MainWindow::onStopSchedule() { if (server_) { @@ -109,6 +157,23 @@ void MainWindow::onOpenChildWindow() qDebug() << "Second window isActiveWindow:" << child_window_->isActiveWindow(); } +void MainWindow::onInputClicked() +{ + InputDialog dialog(this); + QRect mainWindowGeometry = this->frameGeometry(); + QPoint mainWindowPos = this->pos(); + int x = mainWindowPos.x() + (mainWindowGeometry.width() - dialog.width()) / 2; + int y = mainWindowPos.y() + (mainWindowGeometry.height() - dialog.height()) / 2; + dialog.move(x, y); + connect(&dialog, &InputDialog::dialogAccepted, this, &MainWindow::onDialogAccepted); + dialog.exec(); +} + +void MainWindow::onDialogAccepted(const QString &text) +{ + input_display_->setText("输入内容: " + text); +} + void MainWindow::keyPressEvent(QKeyEvent *event) { if (event->modifiers() == Qt::ControlModifier) { diff --git a/lib_fiber/samples-gui/QtFiber/mainwindow.h b/lib_fiber/samples-gui/QtFiber/mainwindow.h index 568790a83..af9f4b500 100644 --- a/lib_fiber/samples-gui/QtFiber/mainwindow.h +++ b/lib_fiber/samples-gui/QtFiber/mainwindow.h @@ -5,6 +5,11 @@ #include #include #include +#include + +namespace acl { + class http_request; +} class fiber_server; @@ -30,10 +35,17 @@ protected: void onStartClient(); void onStopSchedule(); void onOpenChildWindow(); + void onInputClicked(); + void onUrlGet(); + + void onDialogAccepted(const QString &text); public: void onAboutToQuit(); +private: + void onDownloadFinish(bool ok, const acl::http_request& req); + private: Ui::MainWindow *ui_; QPushButton *button_; @@ -41,6 +53,10 @@ private: QPushButton *start_client_; QPushButton *stop_fiber_; QPushButton *open_child_; + QPushButton *url_get_; + std::string url_; + QPushButton *input_button_; + QLabel *input_display_; fiber_server *server_ = nullptr; QProcess *process_; diff --git a/lib_fiber/samples-gui/QtFiber/stdafx.h b/lib_fiber/samples-gui/QtFiber/stdafx.h index e5b94d3e1..ee639c3cf 100644 --- a/lib_fiber/samples-gui/QtFiber/stdafx.h +++ b/lib_fiber/samples-gui/QtFiber/stdafx.h @@ -5,7 +5,9 @@ #include "acl_cpp/lib_acl.hpp" #include "fiber/libfiber.h" #include "fiber/libfiber.hpp" -#include "fiber/fiber_tbox.hpp" + +#define USE_CPP11 +#include "fiber/go_fiber.hpp" #include "patch.h" #include