Added and fixed.
This commit is contained in:
parent
929d5b9369
commit
d94f2e54e6
@ -14,6 +14,7 @@
|
|||||||
92A1F29821F0C19500340EFA /* socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29721F0C19500340EFA /* socket.cpp */; };
|
92A1F29821F0C19500340EFA /* socket.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29721F0C19500340EFA /* socket.cpp */; };
|
||||||
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29921F0C5CC00340EFA /* clock.cpp */; };
|
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29921F0C5CC00340EFA /* clock.cpp */; };
|
||||||
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29D21F0C72C00340EFA /* addr.cpp */; };
|
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29D21F0C72C00340EFA /* addr.cpp */; };
|
||||||
|
92A1F2A121F1663300340EFA /* memory.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92A1F29F21F1663300340EFA /* memory.cpp */; };
|
||||||
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92D6CE6721EE4920005AEF3B /* server.cpp */; };
|
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 92D6CE6721EE4920005AEF3B /* server.cpp */; };
|
||||||
/* End PBXBuildFile section */
|
/* End PBXBuildFile section */
|
||||||
|
|
||||||
@ -45,6 +46,8 @@
|
|||||||
92A1F29A21F0C5CC00340EFA /* clock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = clock.h; sourceTree = "<group>"; };
|
92A1F29A21F0C5CC00340EFA /* clock.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = clock.h; sourceTree = "<group>"; };
|
||||||
92A1F29C21F0C67600340EFA /* type.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = type.h; sourceTree = "<group>"; };
|
92A1F29C21F0C67600340EFA /* type.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = type.h; sourceTree = "<group>"; };
|
||||||
92A1F29D21F0C72C00340EFA /* addr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = addr.cpp; sourceTree = "<group>"; };
|
92A1F29D21F0C72C00340EFA /* addr.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = addr.cpp; sourceTree = "<group>"; };
|
||||||
|
92A1F29F21F1663300340EFA /* memory.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = memory.cpp; sourceTree = "<group>"; };
|
||||||
|
92A1F2A021F1663300340EFA /* memory.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = memory.h; sourceTree = "<group>"; };
|
||||||
92D6CE6721EE4920005AEF3B /* server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = "<group>"; };
|
92D6CE6721EE4920005AEF3B /* server.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = server.cpp; sourceTree = "<group>"; };
|
||||||
92D6CE6821EE4920005AEF3B /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = "<group>"; };
|
92D6CE6821EE4920005AEF3B /* server.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = server.h; sourceTree = "<group>"; };
|
||||||
/* End PBXFileReference section */
|
/* End PBXFileReference section */
|
||||||
@ -89,6 +92,7 @@
|
|||||||
92D6CE6721EE4920005AEF3B /* server.cpp */,
|
92D6CE6721EE4920005AEF3B /* server.cpp */,
|
||||||
92A1F29921F0C5CC00340EFA /* clock.cpp */,
|
92A1F29921F0C5CC00340EFA /* clock.cpp */,
|
||||||
92A1F29D21F0C72C00340EFA /* addr.cpp */,
|
92A1F29D21F0C72C00340EFA /* addr.cpp */,
|
||||||
|
92A1F29F21F1663300340EFA /* memory.cpp */,
|
||||||
);
|
);
|
||||||
name = Net;
|
name = Net;
|
||||||
sourceTree = "<group>";
|
sourceTree = "<group>";
|
||||||
@ -96,6 +100,7 @@
|
|||||||
925A13AA21EC989500CBD427 /* include */ = {
|
925A13AA21EC989500CBD427 /* include */ = {
|
||||||
isa = PBXGroup;
|
isa = PBXGroup;
|
||||||
children = (
|
children = (
|
||||||
|
92A1F2A021F1663300340EFA /* memory.h */,
|
||||||
92A1F29A21F0C5CC00340EFA /* clock.h */,
|
92A1F29A21F0C5CC00340EFA /* clock.h */,
|
||||||
925A13A821EC973000CBD427 /* cmap.h */,
|
925A13A821EC973000CBD427 /* cmap.h */,
|
||||||
9221DA1421EB62F6007310A7 /* cpart.h */,
|
9221DA1421EB62F6007310A7 /* cpart.h */,
|
||||||
@ -168,6 +173,7 @@
|
|||||||
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */,
|
92A1F29B21F0C5CC00340EFA /* clock.cpp in Sources */,
|
||||||
925A13A921EC973000CBD427 /* cmap.cpp in Sources */,
|
925A13A921EC973000CBD427 /* cmap.cpp in Sources */,
|
||||||
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */,
|
92A1F29E21F0C72C00340EFA /* addr.cpp in Sources */,
|
||||||
|
92A1F2A121F1663300340EFA /* memory.cpp in Sources */,
|
||||||
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */,
|
92D6CE6921EE4920005AEF3B /* server.cpp in Sources */,
|
||||||
92A1F29821F0C19500340EFA /* socket.cpp in Sources */,
|
92A1F29821F0C19500340EFA /* socket.cpp in Sources */,
|
||||||
925A13AD21EC9DB900CBD427 /* cthread.cpp in Sources */,
|
925A13AD21EC9DB900CBD427 /* cthread.cpp in Sources */,
|
||||||
|
1
cmap.cpp
1
cmap.cpp
@ -6,6 +6,7 @@
|
|||||||
// Copyright © 2019年 Bakantu. All rights reserved.
|
// Copyright © 2019年 Bakantu. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
#include "cmap.h"
|
#include "cmap.h"
|
||||||
|
|
||||||
|
|
||||||
|
48
cpart.cpp
48
cpart.cpp
@ -6,6 +6,7 @@
|
|||||||
// Copyright © 2019年 Bakantu. All rights reserved.
|
// Copyright © 2019年 Bakantu. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
#include "cpart.h"
|
#include "cpart.h"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -74,15 +75,9 @@ int CPart::GetSo(void){
|
|||||||
|
|
||||||
CPart::~CPart(){
|
CPart::~CPart(){
|
||||||
// 释放储存接口输入参数所占用的内存
|
// 释放储存接口输入参数所占用的内存
|
||||||
for(auto k = 0; k < args_in.size(); k++){
|
for(auto arg : args_in) main_pool.b_free(arg);
|
||||||
if(fargs_in[k] == 0) delete (int *)(args_in[k]);
|
|
||||||
else delete (double *)(args_in[k]);
|
|
||||||
}
|
|
||||||
// 释放储存接口输出参数所占用的内存
|
// 释放储存接口输出参数所占用的内存
|
||||||
for(auto k = 0; k < args_out.size(); k++){
|
for(auto arg : args_out) main_pool.b_free(arg);
|
||||||
if(fargs_in[k] == 0) delete (int *)(args_out[k]);
|
|
||||||
else delete (double *)(args_out[k]);
|
|
||||||
}
|
|
||||||
// 停止对lib文件的操作
|
// 停止对lib文件的操作
|
||||||
if(handle != nullptr)
|
if(handle != nullptr)
|
||||||
dlclose(handle);
|
dlclose(handle);
|
||||||
@ -108,27 +103,16 @@ void CPart::setArgsType(vector<int> fargs_in, vector<int> fargs_out){
|
|||||||
int CPart::Run(void){
|
int CPart::Run(void){
|
||||||
if(func == nullptr) throw "func is nullptr";
|
if(func == nullptr) throw "func is nullptr";
|
||||||
// 对计算模块传入参数
|
// 对计算模块传入参数
|
||||||
unsigned long count = fargs_in.size()-1;
|
for(auto arg : args_in) libargs_in->push_back(arg);
|
||||||
for(auto k = args_in.rbegin(); k != args_in.rend();k++,count--){
|
|
||||||
if(fargs_in[count] == INT){
|
|
||||||
CPart::addArg(libargs_in, *((int *)(*k)));
|
|
||||||
}
|
|
||||||
else if(fargs_in[count] == DOUBLE){
|
|
||||||
CPart::addArg(libargs_in, *((double *)(*k)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 执行计算模块
|
// 执行计算模块
|
||||||
if(func() == SUCCESS){
|
if(func() == SUCCESS){
|
||||||
int count = 0;
|
|
||||||
//储存计算结果
|
//储存计算结果
|
||||||
for(auto k = libargs_out->begin(); k != libargs_out->end();k++,count++){
|
for(auto arg : *libargs_out){
|
||||||
if(fargs_out[count] == INT){
|
// 获得内存块的访问量
|
||||||
CPart::addArg(&args_out, *((int *)(*k)));
|
main_pool.b_get(arg);
|
||||||
}
|
args_out.push_back(arg);
|
||||||
else if(fargs_out[count] == DOUBLE){
|
|
||||||
CPart::addArg(&args_out, *((double *)(*k)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
libargs_out->clear();
|
||||||
return SUCCESS;
|
return SUCCESS;
|
||||||
}
|
}
|
||||||
else return -1;
|
else return -1;
|
||||||
@ -140,15 +124,9 @@ int CPart::Run(void){
|
|||||||
*/
|
*/
|
||||||
void CPart::Clear(void){
|
void CPart::Clear(void){
|
||||||
// 释放传入参数所占的空间
|
// 释放传入参数所占的空间
|
||||||
for(auto k = args_in.size() - 1; ~k; k--){
|
for(auto arg : args_in) main_pool.b_free(arg);
|
||||||
if(fargs_in[k] == INT) delete (int *)(args_in[k]);
|
args_in.clear();
|
||||||
else delete (double *)(args_in[k]);
|
|
||||||
args_in.pop_back();
|
|
||||||
}
|
|
||||||
// 释放传出参数所占用的内存空间
|
// 释放传出参数所占用的内存空间
|
||||||
for(auto k = args_out.size() - 1; ~k; k--){
|
for(auto arg : args_out) main_pool.b_free(arg);
|
||||||
if(fargs_in[k] == INT) delete (int *)(args_out[k]);
|
args_out.clear();
|
||||||
else delete (double *)(args_out[k]);
|
|
||||||
args_out.pop_back();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
11
cpart.h
11
cpart.h
@ -10,6 +10,7 @@
|
|||||||
#define cpart_h
|
#define cpart_h
|
||||||
|
|
||||||
#include "type.h"
|
#include "type.h"
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
//声明计算模块的传入与传出参数列表
|
//声明计算模块的传入与传出参数列表
|
||||||
#define ARGS_DECLAER(name) vector<void *> __##name##_args_in, __##name##_args_out
|
#define ARGS_DECLAER(name) vector<void *> __##name##_args_in, __##name##_args_out
|
||||||
@ -98,7 +99,8 @@ public:
|
|||||||
// 一般由lib文件中的计算模块调用的向vector中添加参数并分配内存空间而后初始化
|
// 一般由lib文件中的计算模块调用的向vector中添加参数并分配内存空间而后初始化
|
||||||
template<class T>
|
template<class T>
|
||||||
static void addArg(vector<void *> *args,T value){
|
static void addArg(vector<void *> *args,T value){
|
||||||
T *p_value = new T(value);
|
T *p_value = (T *) main_pool.b_malloc(sizeof(T));
|
||||||
|
*p_value = value;
|
||||||
if(p_value == nullptr) throw "fail to malloc";
|
if(p_value == nullptr) throw "fail to malloc";
|
||||||
args->push_back(p_value);
|
args->push_back(p_value);
|
||||||
}
|
}
|
||||||
@ -106,9 +108,16 @@ public:
|
|||||||
template<class T>
|
template<class T>
|
||||||
static T popArg(vector<void *> *args){
|
static T popArg(vector<void *> *args){
|
||||||
if(args == nullptr) throw "the pointer to vector is null";
|
if(args == nullptr) throw "the pointer to vector is null";
|
||||||
|
|
||||||
T *p_value = (T *)args->back();
|
T *p_value = (T *)args->back();
|
||||||
|
|
||||||
|
p_value = main_pool.b_get(p_value);
|
||||||
|
if(p_value == nullptr) throw "infomation lost";
|
||||||
|
|
||||||
|
p_value = main_pool.b_free(p_value);
|
||||||
T value = *p_value;
|
T value = *p_value;
|
||||||
args->pop_back();
|
args->pop_back();
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
150
cthread.cpp
150
cthread.cpp
@ -6,53 +6,32 @@
|
|||||||
// Copyright © 2019年 Bakantu. All rights reserved.
|
// Copyright © 2019年 Bakantu. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
#include "cthread.h"
|
#include "cthread.h"
|
||||||
|
|
||||||
list<CThread *> daemon_list = {};
|
list<CThread *> daemon_list = {};
|
||||||
|
|
||||||
CThread::CThread(CMap *tp_map,int thdnum):p_map(tp_map),idxtid(0),thdnum(thdnum){
|
CThread::CThread(CMap *tp_map,int thdnum):p_map(tp_map),idxtid(0),thdnum(thdnum){
|
||||||
lpcs.if_als = false;
|
lpcs.if_als = false;
|
||||||
// 构造空的传入与传出参数列表
|
|
||||||
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
|
for(auto cp : p_map->cparts){
|
||||||
|
// 构造空的传入与传出参数列表
|
||||||
vector<void *> args,args_out;
|
vector<void *> args,args_out;
|
||||||
rargs.insert(pair<string,vector<void *>>((*k).first,args));
|
rargs.insert(pair<string,vector<void *>>(cp.first,args));
|
||||||
rargs_out.insert(pair<string,vector<void *>>((*k).first,args_out));
|
rargs_out.insert(pair<string,vector<void *>>(cp.first,args_out));
|
||||||
}
|
// 构造任务进度列表
|
||||||
// 构造任务进度列表
|
ifsolved.insert(pair<string,bool>(cp.first,false));
|
||||||
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
|
if_rargs.insert(pair<string,bool>(cp.first,false));
|
||||||
ifsolved.insert(pair<string,bool>((*k).first,false));
|
|
||||||
if_rargs.insert(pair<string,bool>((*k).first,false));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CThread::~CThread(){
|
CThread::~CThread(){
|
||||||
for(auto item = rargs.begin(); item != rargs.end(); item++){
|
for(auto item : rargs){
|
||||||
int count = 0;
|
for(auto litem : item.second) main_pool.b_free(litem);
|
||||||
vector<int> fargs = p_map->cparts.find(item->first)->second->fargs_in;
|
|
||||||
for(auto litem = item->second.begin(); litem != item->second.end(); litem++,count++){
|
|
||||||
if(fargs[count] == INT){
|
|
||||||
delete (int *)(*litem);
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (fargs[count] == DOUBLE){
|
|
||||||
delete (double *)(*litem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for(auto item = rargs_out.begin(); item != rargs_out.end(); item++){
|
for(auto item : rargs){
|
||||||
int count = 0;
|
for(auto litem : item.second)
|
||||||
vector<int> fargs = p_map->cparts.find(item->first)->second->fargs_out;
|
if(litem != nullptr) main_pool.b_free(litem);
|
||||||
for(auto litem = item->second.begin(); litem != item->second.end(); litem++,count++){
|
|
||||||
if((*litem) != nullptr){
|
|
||||||
if(fargs[count] == INT){
|
|
||||||
delete (int *)(*litem);
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (fargs[count] == DOUBLE){
|
|
||||||
delete (double *)(*litem);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,15 +40,15 @@ void CThread::Analyse(void){
|
|||||||
if(lpcs.if_als == true){
|
if(lpcs.if_als == true){
|
||||||
//还没想好怎么做
|
//还没想好怎么做
|
||||||
}
|
}
|
||||||
for(auto k = p_map->cparts.begin(); k != p_map->cparts.end(); k++){
|
for(auto cp : p_map->cparts){
|
||||||
auto cpart_depends = (*k).second->depends;
|
auto cpart_depends = cp.second->depends;
|
||||||
// 如果计算模块已经执行则跳过
|
// 如果计算模块已经执行则跳过
|
||||||
if(ifsolved.find(k->second->name)->second) continue;
|
if(ifsolved.find(cp.second->name)->second) continue;
|
||||||
// 如果该计算模块含有依赖模块
|
// 如果该计算模块含有依赖模块
|
||||||
if(cpart_depends.size()){
|
if(cpart_depends.size()){
|
||||||
bool if_ok = true;
|
bool if_ok = true;
|
||||||
for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){
|
for(auto cpd : cpart_depends){
|
||||||
string name = ditem->t_cpart->name;
|
string name = cpd.t_cpart->name;
|
||||||
// 如果依赖模块还没有被调用过
|
// 如果依赖模块还没有被调用过
|
||||||
if(!(ifsolved.find(name)->second)){
|
if(!(ifsolved.find(name)->second)){
|
||||||
if_ok = false;
|
if_ok = false;
|
||||||
@ -78,41 +57,36 @@ void CThread::Analyse(void){
|
|||||||
}
|
}
|
||||||
if(if_ok){
|
if(if_ok){
|
||||||
int count = 0;
|
int count = 0;
|
||||||
vector<int> s_fargs_in = k->second->fargs_in;
|
vector<int> s_fargs_in = cp.second->fargs_in;
|
||||||
for(auto ditem = cpart_depends.begin(); ditem != cpart_depends.end(); ditem++){
|
for(auto cpd : cpart_depends){
|
||||||
vector<int> args = ditem->args;
|
vector<int> args = cpd.args;
|
||||||
// 输入参数列表
|
// 输入参数列表
|
||||||
vector<void *> &args_in = rargs.find(k->second->name)->second;
|
vector<void *> &args_in = rargs.find(cp.second->name)->second;
|
||||||
// 输出形式参数列表
|
// 输出形式参数列表
|
||||||
vector<int> f_fargs_out = ditem->t_cpart->fargs_out;
|
vector<int> f_fargs_out = cpd.t_cpart->fargs_out;
|
||||||
// 输出参数列表
|
// 输出参数列表
|
||||||
vector<void *> args_out = rargs_out.find(ditem->t_cpart->name)->second;
|
vector<void *> args_out = rargs_out.find(cpd.t_cpart->name)->second;
|
||||||
// 检查传入传出参数的类型是否匹配
|
// 检查传入传出参数的类型是否匹配
|
||||||
for(auto itm = args.begin(); itm != args.end();itm++){
|
for(auto itm : args){
|
||||||
// 参数不匹配则报异常
|
// 参数不匹配则报异常
|
||||||
if(s_fargs_in[count++] != f_fargs_out[*itm]) throw "type conflict";
|
if(s_fargs_in[count++] != f_fargs_out[itm]) throw "type conflict";
|
||||||
// 重新分配内存
|
// 重新分配内存
|
||||||
if(f_fargs_out[*itm] == INT){
|
args_in.push_back(args_out[itm]);
|
||||||
CPart::addArg<int>(&args_in, *((int *)(args_out[*itm])));
|
|
||||||
}
|
|
||||||
else if(f_fargs_out[*itm] == DOUBLE){
|
|
||||||
CPart::addArg<double>(&args_in, *((double *)(args_out[*itm])));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
lpcs.line.push_back((*k).second);
|
lpcs.line.push_back(cp.second);
|
||||||
// 大于线程最高并行数则跳出
|
// 大于线程最高并行数则跳出
|
||||||
if(count == thdnum) break;
|
if(count == thdnum) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 如果该计算模块没有依赖模块
|
// 如果该计算模块没有依赖模块
|
||||||
else{
|
else{
|
||||||
string name = (*k).second->name;
|
string name = cp.second->name;
|
||||||
if(rargs.find(k->second->name)->second.size() == k->second->fargs_in.size()){
|
if(rargs.find(cp.second->name)->second.size() == cp.second->fargs_in.size()){
|
||||||
// 如果该模块还没有被调用
|
// 如果该模块还没有被调用
|
||||||
if(ifsolved.find(name)->second == false){
|
if(ifsolved.find(name)->second == false){
|
||||||
lpcs.line.push_back(k->second);
|
lpcs.line.push_back(cp.second);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,22 +95,22 @@ void CThread::Analyse(void){
|
|||||||
}
|
}
|
||||||
|
|
||||||
void CThread::DoLine(void){
|
void CThread::DoLine(void){
|
||||||
for(auto pcp = lpcs.line.begin(); pcp != lpcs.line.end(); pcp++){
|
for(auto pcp : lpcs.line){
|
||||||
string name = (*pcp)->name;
|
string name = pcp->name;
|
||||||
|
|
||||||
vector<void *> args = rargs.find(name)->second;
|
vector<void *> args = rargs.find(name)->second;
|
||||||
vector<int> fargs = (*pcp)->fargs_in;
|
vector<int> fargs = pcp->fargs_in;
|
||||||
vector<int> fargs_out = (*pcp)->fargs_out;
|
vector<int> fargs_out = pcp->fargs_out;
|
||||||
|
|
||||||
unsigned long ntid = idxtid++;
|
unsigned long ntid = idxtid++;
|
||||||
pthread_t npdt = 0;
|
pthread_t npdt = 0;
|
||||||
// 创建新线程
|
// 创建新线程
|
||||||
struct thread_args *pt_ta = new struct thread_args({ntid,this,(*pcp),-1});
|
struct thread_args *pt_ta = new struct thread_args({ntid,this,pcp,-1});
|
||||||
if(pthread_create(&npdt,NULL,&CThread::NewThread,(void *)(pt_ta))){
|
if(pthread_create(&npdt,NULL,&CThread::NewThread,(void *)(pt_ta))){
|
||||||
throw "fail to create thread";
|
throw "fail to create thread";
|
||||||
}
|
}
|
||||||
lpcs.threads.insert({ntid,npdt});
|
lpcs.threads.insert({ntid,npdt});
|
||||||
lpcs.cpttid.insert({(*pcp),ntid});
|
lpcs.cpttid.insert({pcp,ntid});
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -147,8 +121,8 @@ void CThread::SetDaemon(void){
|
|||||||
|
|
||||||
void CThread::Daemon(void){
|
void CThread::Daemon(void){
|
||||||
// 等待线程返回
|
// 等待线程返回
|
||||||
for(auto i = lpcs.child_finished.begin(); i != lpcs.child_finished.end(); i++){
|
for(auto cfh : lpcs.child_finished){
|
||||||
unsigned long tid = (*i)->tid;
|
unsigned long tid = cfh->tid;
|
||||||
pthread_t cpdt = lpcs.threads.find(tid)->second;
|
pthread_t cpdt = lpcs.threads.find(tid)->second;
|
||||||
struct thread_args *rpv = nullptr;
|
struct thread_args *rpv = nullptr;
|
||||||
pthread_join(cpdt, (void **)&rpv);
|
pthread_join(cpdt, (void **)&rpv);
|
||||||
@ -168,6 +142,7 @@ void CThread::Daemon(void){
|
|||||||
lpcs.threads.erase(tid);
|
lpcs.threads.erase(tid);
|
||||||
lpcs.cpttid.erase(rpv->pcp);
|
lpcs.cpttid.erase(rpv->pcp);
|
||||||
printf("TID: %lu Deleted.\n",tid);
|
printf("TID: %lu Deleted.\n",tid);
|
||||||
|
// 删除线程传入参数
|
||||||
delete rpv;
|
delete rpv;
|
||||||
}
|
}
|
||||||
lpcs.child_finished.clear();
|
lpcs.child_finished.clear();
|
||||||
@ -221,14 +196,9 @@ void CThread::PrepareArgsIn(CThread *pct,CPart *pcp){
|
|||||||
// 清空历史数据
|
// 清空历史数据
|
||||||
pcp->Clear();
|
pcp->Clear();
|
||||||
// 传入输入参数
|
// 传入输入参数
|
||||||
int cout = 0;
|
for(auto arg : args){
|
||||||
for(auto arg = args.begin(); arg != args.end(); arg++,cout++){
|
if(main_pool.b_get(arg) == nullptr) throw "information lost";
|
||||||
if(fargs[cout] == INT){
|
pcp->args_in.push_back(arg);
|
||||||
pcp->addArgsIn<int>(*((int *)(*arg)));
|
|
||||||
}
|
|
||||||
else if(fargs[cout] == DOUBLE){
|
|
||||||
pcp->addArgsIn<double>(*((double *)(*arg)));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -242,17 +212,7 @@ void CThread::GetArgsOut(CThread *pct,CPart *pcp){
|
|||||||
vector<void *> &args_out = pct->rargs_out.find(pcp->name)->second;
|
vector<void *> &args_out = pct->rargs_out.find(pcp->name)->second;
|
||||||
|
|
||||||
// 处理输出
|
// 处理输出
|
||||||
int cout = 0;
|
for(auto argo : argso) args_out.push_back(argo);
|
||||||
for(auto argo = argso.begin(); argo != argso.end(); argo++,cout++){
|
|
||||||
if(fargs_out[cout] == INT){
|
|
||||||
int *p_value = new int(*((int *)(*argo)));
|
|
||||||
args_out.push_back((void *)p_value);
|
|
||||||
}
|
|
||||||
else if(fargs_out[cout] == DOUBLE){
|
|
||||||
double *p_value = new double(*((double *)(*argo)));
|
|
||||||
args_out.push_back((double *)p_value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -268,29 +228,13 @@ int CThread::CancelChildPCS(unsigned long tid){
|
|||||||
int CThread::GetCPUResult(struct compute_result *pcrt){
|
int CThread::GetCPUResult(struct compute_result *pcrt){
|
||||||
ifsolved.find(pcrt->name)->second = true;
|
ifsolved.find(pcrt->name)->second = true;
|
||||||
// 处理输出参数
|
// 处理输出参数
|
||||||
int count = 0;
|
|
||||||
CPart *pcp = p_map->cparts.find(pcrt->name)->second;
|
CPart *pcp = p_map->cparts.find(pcrt->name)->second;
|
||||||
vector<int> farg_out = pcp->fargs_out;
|
vector<int> farg_out = pcp->fargs_out;
|
||||||
|
for(auto argo : *pcrt->args_out) AddArgsOut(pcrt->name, argo);
|
||||||
|
|
||||||
for(auto i = pcrt->args_out->begin(); i != pcrt->args_out->end(); i++,count++){
|
|
||||||
if(farg_out[count] == INT){
|
|
||||||
AddArgsOut<int>(pcrt->name, *((int *)(*i)));
|
|
||||||
}
|
|
||||||
else if(farg_out[count] == DOUBLE){
|
|
||||||
AddArgsOut<double>(pcrt->name, *((double *)(*i)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 处理输入参数
|
// 处理输入参数
|
||||||
vector<int> farg_in = pcp->fargs_in;
|
vector<int> farg_in = pcp->fargs_in;
|
||||||
count = 0;
|
for(auto argi : *pcrt->args_in) AddArgs(pcrt->name, argi);
|
||||||
for(auto i = pcrt->args_in->begin(); i != pcrt->args_in->end(); i++,count++){
|
|
||||||
if(farg_in[count] == INT){
|
|
||||||
AddArgs<int>(pcrt->name, *((int *)(*i)));
|
|
||||||
}
|
|
||||||
else if(farg_in[count] == DOUBLE){
|
|
||||||
AddArgs<double>(pcrt->name, *((double *)(*i)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ifsolved.find(pcrt->name)->second = true;
|
ifsolved.find(pcrt->name)->second = true;
|
||||||
if_rargs.find(pcrt->name)->second = true;
|
if_rargs.find(pcrt->name)->second = true;
|
||||||
|
|
||||||
|
20
cthread.h
20
cthread.h
@ -70,22 +70,18 @@ public:
|
|||||||
CThread(CMap *tp_map, int thdnum = 4);
|
CThread(CMap *tp_map, int thdnum = 4);
|
||||||
~CThread();
|
~CThread();
|
||||||
|
|
||||||
template<class T>
|
|
||||||
// 添加相关计算模块的传入参数
|
// 添加相关计算模块的传入参数
|
||||||
void AddArgs(string name, T value){
|
void AddArgs(string name, void *pvle){
|
||||||
auto k = rargs.find(name);
|
if(main_pool.b_get(pvle) == nullptr) throw "information lost";
|
||||||
T *p_value = new T();
|
auto argil = rargs.find(name);
|
||||||
*p_value = value;
|
argil->second.push_back(pvle);
|
||||||
(*k).second.push_back((void *)p_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class T>
|
|
||||||
// 添加相关计算模块的传出参数
|
// 添加相关计算模块的传出参数
|
||||||
void AddArgsOut(string name, T value){
|
void AddArgsOut(string name, void *pvle){
|
||||||
auto k = rargs_out.find(name);
|
if(main_pool.b_get(pvle) == nullptr) throw "information lost";
|
||||||
T *p_value = new T();
|
auto argol = rargs_out.find(name);
|
||||||
*p_value = value;
|
argol->second.push_back(pvle);
|
||||||
(*k).second.push_back((void *)p_value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置守护进程
|
// 设置守护进程
|
||||||
|
5
main.cpp
5
main.cpp
@ -7,6 +7,7 @@
|
|||||||
//
|
//
|
||||||
|
|
||||||
#include "type.h"
|
#include "type.h"
|
||||||
|
#include "memory.h"
|
||||||
#include "clock.h"
|
#include "clock.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
#include "cpart.h"
|
#include "cpart.h"
|
||||||
@ -42,10 +43,6 @@ int main(void){
|
|||||||
void CPMT(void){
|
void CPMT(void){
|
||||||
CMap map("./PCS");
|
CMap map("./PCS");
|
||||||
CThread thread(&map);
|
CThread thread(&map);
|
||||||
thread.AddArgs<int>("B", 4);
|
|
||||||
thread.AddArgs<double>("B", 9.0);
|
|
||||||
thread.AddArgs<int>("C", 1.0);
|
|
||||||
thread.AddArgs<double>("C", 3.0);
|
|
||||||
thread.Analyse();
|
thread.Analyse();
|
||||||
thread.DoLine();
|
thread.DoLine();
|
||||||
thread.SetDaemon();
|
thread.SetDaemon();
|
||||||
|
11
memory.cpp
Normal file
11
memory.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
//
|
||||||
|
// memory.cpp
|
||||||
|
// Net
|
||||||
|
//
|
||||||
|
// Created by 胡一兵 on 2019/1/18.
|
||||||
|
// Copyright © 2019年 Bakantu. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
|
|
||||||
|
BlocksPool main_pool;
|
67
memory.h
Normal file
67
memory.h
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
//
|
||||||
|
// memory.h
|
||||||
|
// Net
|
||||||
|
//
|
||||||
|
// Created by 胡一兵 on 2019/1/18.
|
||||||
|
// Copyright © 2019年 Bakantu. All rights reserved.
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef memory_h
|
||||||
|
#define memory_h
|
||||||
|
|
||||||
|
#include "type.h"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
struct block_info{
|
||||||
|
uint32_t size = 0;
|
||||||
|
int lock = 0;
|
||||||
|
bool pted = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
class BlocksPool{
|
||||||
|
map<void *, block_info> blocks_list;
|
||||||
|
public:
|
||||||
|
void *b_malloc(uint32_t size){
|
||||||
|
void *ptr = malloc(size);
|
||||||
|
if(ptr == nullptr) return nullptr;
|
||||||
|
blocks_list.insert({ptr,{size,1}});
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
void *b_get(void *ptr){
|
||||||
|
auto blk = blocks_list.find(ptr);
|
||||||
|
if(blk != blocks_list.end()){
|
||||||
|
blk->second.lock++;
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
else return nullptr;
|
||||||
|
}
|
||||||
|
void b_protect(void *ptr){
|
||||||
|
auto blk = blocks_list.find(ptr);
|
||||||
|
if(blk != blocks_list.end()){
|
||||||
|
blk->second.pted = true;
|
||||||
|
}
|
||||||
|
else throw "protect nil value";
|
||||||
|
}
|
||||||
|
void b_noprotect(void *ptr){
|
||||||
|
auto blk = blocks_list.find(ptr);
|
||||||
|
if(blk != blocks_list.end()){
|
||||||
|
blk->second.pted = false;
|
||||||
|
}
|
||||||
|
else throw "noprotect nil value";
|
||||||
|
}
|
||||||
|
void b_free(void *ptr){
|
||||||
|
auto blk = blocks_list.find(ptr);
|
||||||
|
if(blk != blocks_list.end() && blk->second.pted == false){
|
||||||
|
if(blk->second.lock - 1 == 0){
|
||||||
|
free(blk->first);
|
||||||
|
blocks_list.erase(blk);
|
||||||
|
}
|
||||||
|
else blk->second.lock--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
extern BlocksPool main_pool;
|
||||||
|
|
||||||
|
#endif /* memory_h */
|
@ -6,11 +6,9 @@
|
|||||||
// Copyright © 2019年 Bakantu. All rights reserved.
|
// Copyright © 2019年 Bakantu. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
#include "server.h"
|
#include "server.h"
|
||||||
|
|
||||||
list<server_clock> server_list;
|
|
||||||
list<server_clock> process_list;
|
|
||||||
|
|
||||||
extern list<clock_register> clocks_list;
|
extern list<clock_register> clocks_list;
|
||||||
|
|
||||||
void setServerClock(Server *psvr, int clicks){
|
void setServerClock(Server *psvr, int clicks){
|
||||||
@ -34,7 +32,7 @@ void Server::SetSendIP(string ip_addr){
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 将计算结果包转化为结构数据包
|
// 将计算结果包转化为结构数据包
|
||||||
packet Server::CPURS2Packet(compute_result tcpur){
|
packet CNodeServer::CPURS2Packet(compute_result tcpur){
|
||||||
packet rawpkt;
|
packet rawpkt;
|
||||||
rawpkt.type = 0;
|
rawpkt.type = 0;
|
||||||
int count = 0;
|
int count = 0;
|
||||||
@ -122,7 +120,7 @@ packet Server::Rawdata2Packet(raw_data trdta){
|
|||||||
return pkt;
|
return pkt;
|
||||||
}
|
}
|
||||||
|
|
||||||
compute_result Server::Packet2CPUR(packet *tpkt){
|
compute_result CNodeServer::Packet2CPUR(packet *tpkt){
|
||||||
compute_result tcpur;
|
compute_result tcpur;
|
||||||
tcpur.args_in = new vector<void *>();
|
tcpur.args_in = new vector<void *>();
|
||||||
tcpur.args_out = new vector<void *>();
|
tcpur.args_out = new vector<void *>();
|
||||||
|
10
server.h
10
server.h
@ -47,17 +47,18 @@ public:
|
|||||||
unsigned long msg_size = 0;
|
unsigned long msg_size = 0;
|
||||||
// 用简单字符串直接出适合
|
// 用简单字符串直接出适合
|
||||||
void setData(string str){
|
void setData(string str){
|
||||||
data = (char *)malloc(str.size()+1);
|
data = (char *)malloc(str.size());
|
||||||
size = str.size()+1;
|
size = str.size();
|
||||||
memcpy(data, str.data(),str.size());
|
memcpy(data, str.data(),str.size());
|
||||||
data[str.size()+1] = '\0';
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
//通用服务器类
|
//通用服务器类
|
||||||
class Server{
|
class Server{
|
||||||
protected:
|
protected:
|
||||||
|
// 缓存通用数据包
|
||||||
vector<packet> packets_in;
|
vector<packet> packets_in;
|
||||||
|
// 缓存带标签的二进制串管理结构
|
||||||
vector<raw_data> rawdata_in;
|
vector<raw_data> rawdata_in;
|
||||||
public:
|
public:
|
||||||
// 服务器类的接收套接字对象与发送套接字对象
|
// 服务器类的接收套接字对象与发送套接字对象
|
||||||
@ -88,7 +89,8 @@ public:
|
|||||||
static bool CheckRawMsg(char *p_rdt, ssize_t size);
|
static bool CheckRawMsg(char *p_rdt, ssize_t size);
|
||||||
// 处理一个已贴上标签的原始二进制串,获得其包含的信息
|
// 处理一个已贴上标签的原始二进制串,获得其包含的信息
|
||||||
static raw_data ProcessSignedRawMsg(char *p_rdt, ssize_t size);
|
static raw_data ProcessSignedRawMsg(char *p_rdt, ssize_t size);
|
||||||
|
// 服务器守护线程
|
||||||
|
friend void *serverDeamon(void *psvr);
|
||||||
|
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
// Copyright © 2019年 Bakantu. All rights reserved.
|
// Copyright © 2019年 Bakantu. All rights reserved.
|
||||||
//
|
//
|
||||||
|
|
||||||
|
#include "memory.h"
|
||||||
#include "net.h"
|
#include "net.h"
|
||||||
|
|
||||||
void SocketClient::SetSendPort(int port){
|
void SocketClient::SetSendPort(int port){
|
||||||
|
Loading…
Reference in New Issue
Block a user