Browse Source

Test threaded version

test1
Michael Uleysky 9 years ago
parent
commit
c4fe370b1c
  1. 85
      test/gmttest.cpp

85
test/gmttest.cpp

@ -9,15 +9,59 @@ int print_func(FILE* fd, const char* str)
std::cout<<"PRINT: "<<str<<std::endl;
return 0;
}
/*
int callgmtmodule(void *API, const char *module, int mode, void *args, std::string& res)
struct gmtworkthreadpars
{
void* api;
const char* module;
void* opts;
int fd;
int ret;
};
void* gmtworkthread(void* x)
{
struct gmtworkthreadpars* p=reinterpret_cast<struct gmtworkthreadpars*>(x);
char c='\n';
unshare(CLONE_FILES);
dup2(p->fd,1);
write(1,&c,1); // Use this instead of mutex
p->ret=GMT_Call_Module(p->api,p->module,GMT_MODULE_OPT,p->opts);
close(1);
close(p->fd);
return 0;
}
int callgmtmodule(void *api, const char *module, void *opts, std::string& res)
{
int pipefd[2];
int savefd;
ssize_t br;
char buffer[4096];
pthread_t wthr;
struct gmtworkthreadpars p;
pipe(pipefd);
p.api=api;
p.module=module;
p.opts=opts;
p.fd=pipefd[1];
pthread_create(&wthr,0,&gmtworkthread,&p);
res.erase();
read(pipefd[0],buffer,1);
close(pipefd[1]);
do
{
br=read(pipefd[0],buffer,4096);
std::cout<<res.length()<<" "<<br<<std::endl;
res.append(buffer,br);
} while(0!=br);
res.shrink_to_fit();
pthread_join(wthr,0);
return p.ret;
}
*/
int main()
{
@ -28,48 +72,17 @@ int main()
struct GMT_OPTION* opts;
int io,ret;
char fname[16];
int pipefd[2];
std::string out;
char buffer[4096];
pid_t pid;
gmtapi=GMT_Create_Session(text,2,0,print_func);
//io=GMT_Register_IO(gmtapi,GMT_IS_TEXTSET,GMT_IS_DUPLICATE,GMT_IS_NONE,GMT_OUT,0,0);
//GMT_Encode_ID(gmtapi,fname,io);
pipe(pipefd);
args+=" ->/dev/fd/"+std::to_string(pipefd[1]);
opts=GMT_Create_Options(gmtapi,0,(void*)args.c_str());
//ret=GMT_Init_IO(gmtapi,GMT_IS_TEXTSET,GMT_IS_NONE,GMT_OUT,5,0,opts);
std::cout<<io<<" "<<GMT_NOTSET<<" "<<fname<<" "<<ret<<std::endl;
callgmtmodule(gmtapi,"psbasemap",opts,out);
pid=fork();
if(0==pid)
{
//dup2(pipefd[1],1);
GMT_Call_Module(gmtapi,"psbasemap",GMT_MODULE_OPT,opts);
close(pipefd[1]);
exit(0);
}
else
{
ssize_t br;
// std::string s="CALL\n";
// write(1,s.c_str(),s.length());
// close(pipefd[1]);
do
{
br=read(pipefd[0],buffer,4096);
std::cout<<out.max_size()<<" "<<out.capacity()<<" "<<out.size()<<std::endl;
out.append(buffer,br);
} while(0!=br);
wait(0);
// close(pipefd[0]);
}
out.shrink_to_fit();
std::cout<<out.max_size()<<" "<<out.capacity()<<" "<<out.size()<<std::endl;
GMT_Destroy_Options(gmtapi,&opts);

Loading…
Cancel
Save