Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

编译plugin_boost出错 #38

Open
ezxfv opened this issue May 22, 2018 · 2 comments
Open

编译plugin_boost出错 #38

ezxfv opened this issue May 22, 2018 · 2 comments

Comments

@ezxfv
Copy link

ezxfv commented May 22, 2018

OS: Ubuntu18.04 64bit
GCC: 8.0
Boost: 1.66

由于使用到的boost::context下的类和函数已经转移到boost::context::detail下,并且有的函数参数发生变化,需要进行更改plugin_boost/network下的uthread_context_boost.h和uthread_context_boost.cpp,之后可以成功编译,但是未进行测试.
代码中中文注释部分为修改的地方.
其中uthread_context_boost.h改为:

#pragma once

#include <unistd.h>
#include <functional>
#include <assert.h>
#include <boost/context/all.hpp>
#include "phxrpc/network.h"
// 添加
using boost::context::detail::fcontext_t;
using boost::context::detail::transfer_t;

namespace phxrpc {

class UThreadContextBoostInit {
public:
    UThreadContextBoostInit();
};

class UThreadContextBoost : public UThreadContext {
public:
    UThreadContextBoost(size_t stack_size, UThreadFunc_t func, void * args, 
            UThreadDoneCallback_t callback, const bool need_stack_protect);
    ~UThreadContextBoost();

    static UThreadContext * DoCreate(size_t stack_size, 
            UThreadFunc_t func, void * args, UThreadDoneCallback_t callback,
            const bool need_stack_protect);

    void Make(UThreadFunc_t func, void * args) override;
    bool Resume() override;
    bool Yield() override;

    fcontext_t & GetMainContext();

private:
    // 参数从intptr_t变为transfer_t
    static void UThreadFuncWrapper(transfer_t ptr);

    fcontext_t context_;
    UThreadFunc_t func_;
    void * args_;
    UThreadStackMemory stack_;
    UThreadDoneCallback_t callback_;
};

} //namespace phxrpc

uthread_context_boost.cpp改为:

#include <unistd.h>
#include <functional>
#include <assert.h>
#include <boost/context/all.hpp>

#include "uthread_context_boost.h"
#include "phxrpc/network.h"
using boost::context::detail::transfer_t;
using boost::context::detail::fcontext_t;
using boost::context::detail::jump_fcontext;
using boost::context::detail::make_fcontext;
namespace phxrpc {

UThreadContextBoostInit g_uthread_context_boost_init_;

UThreadContextBoostInit :: UThreadContextBoostInit() {
    UThreadContext::SetContextCreateFunc(UThreadContextBoost::DoCreate);
}

UThreadContextBoost :: UThreadContextBoost(size_t stack_size, UThreadFunc_t func, 
        void * args, UThreadDoneCallback_t callback, const bool need_stack_protect)
    : func_(func), args_(args), stack_(stack_size, need_stack_protect), callback_(callback) {
    Make(func, args);
}

UThreadContextBoost :: ~UThreadContextBoost() {
}

UThreadContext * UThreadContextBoost:: DoCreate(size_t stack_size, 
        UThreadFunc_t func, void * args, UThreadDoneCallback_t callback,
        const bool need_stack_protect) {
    return new UThreadContextBoost(stack_size, func, args, callback, need_stack_protect);
}

// 参数从intptr_t变为transfer_t
void UThreadContextBoost :: UThreadFuncWrapper(transfer_t ptr) {
    // 类型转换前将transfer_t取地址转为intptr_t
    UThreadContextBoost * uc = reinterpret_cast<UThreadContextBoost *>(intptr_t(&ptr));
    uc->func_(uc->args_);
    if (uc->callback_ != nullptr) {
        uc->callback_();
    }
    uc->Yield();
}
void UThreadContextBoost :: Make(UThreadFunc_t func, void * args) {
    func_ = func;
    args_ = args;
    void * stack_p = (void *)((char *)stack_.top() + stack_.size());
    context_ = make_fcontext(stack_p, stack_.size(), &UThreadFuncWrapper);
}

bool UThreadContextBoost :: Resume() {
    //boost::context::detail::jump_fcontext(&GetMainContext(), context_, reinterpret_cast<intptr_t>(this));
    //删除最后一个参数
    jump_fcontext(&GetMainContext(), context_);
    return true;
}

bool UThreadContextBoost :: Yield() {
    //boost::context::detail::jump_fcontext(&context_, GetMainContext(), 0);
   // 删除最后一个参数
    jump_fcontext(&context_, GetMainContext());

    return true;
}

fcontext_t & UThreadContextBoost :: GetMainContext() {
    static thread_local fcontext_t main_context;
    return main_context;
}

} //namespace phxrpc

@taohexxx
Copy link
Collaborator

taohexxx commented Jun 4, 2018

谢谢,建议提交PullRequest

@zhenxiaoyuan
Copy link

OS: Fedora28 64bit
GCC: 8.1.1
Boost: 1.68

编译报错:

fatal error: boost/context/all.hpp: No such file or directory

解决方法:

// #include <boost/context/all.hpp>   
// 修改为  
#include <boost/context/detail/fcontext.hpp>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants