Skip to content

Commit 25b2907

Browse files
committed
added AlgorithmWrapper.h Buffer.h BufferDispatcher.h and removed authro and date from templates
1 parent 7164891 commit 25b2907

8 files changed

Lines changed: 212 additions & 8 deletions

File tree

UserTools/template/MyTool.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*
1313
* This is a balnk template for a Tool used by the script to generate a new custom tool. Please fill out the descripton and author information.
1414
*
15-
* $Author: B.Richards $
16-
* $Date: 2019/05/28 10:44:00 $
15+
* $Author: $
16+
* $Date: $
1717
*/
1818

1919
class MyTool: public Tool {

UserTools/template/MyToolDynamicMultiThread.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
* This is a struct to place data you want your thread to acess or exchange with it. The idea is the datainside is only used by the threa\
1414
d and so will be thread safe
1515
*
16-
* $Author: B.Richards $
17-
* $Date: 2019/05/28 10:44:00 $
16+
* $Author: $
17+
* $Date: $
1818
*/
1919

2020
struct MyToolDynamicMultiThread_args:Thread_args{

UserTools/template/MyToolMultiThread.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*
1313
* This is a struct to place data you want your thread to acess or exchange with it. The idea is the datainside is only used by the thread and so will be thread safe
1414
*
15-
* $Author: B.Richards $
16-
* $Date: 2019/05/28 10:44:00 $
15+
* $Author: $
16+
* $Date: $
1717
*/
1818

1919
struct MyToolMultiThread_args:Thread_args{

UserTools/template/MyToolThread.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*
1313
* This is a struct to place data you want your thread to access or exchange with it. The idea is the datainside is only used by the threa\d and so will be thread safe
1414
*
15-
* $Author: B.Richards $
16-
* $Date: 2019/05/28 10:44:00 $
15+
* $Author: $
16+
* $Date: $
1717
*/
1818

1919
struct MyToolThread_args:Thread_args{
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#ifndef ALGORITHM_WRAPPER_H
2+
#define ALGORITHM_WRAPPER_H
3+
4+
#include <string>
5+
#include <functional>
6+
7+
namespace ToolFramework{
8+
/**
9+
* \struct Trigger_struct
10+
*
11+
* This tricts acts as a generator for trigger jobs with fucntionpointers
12+
*/
13+
14+
template<class T> struct AlgorithmWrapper{
15+
16+
AlgorithmWrapper(std::string in_name, bool (*in_algo)(void*&), std::function<void*(T)> in_setup_func, void (*in_fail_func)(void*&)){
17+
name = in_name;
18+
algo = in_algo;
19+
setup_func = in_setup_func;
20+
fail_func = in_fail_func;
21+
22+
}
23+
std::string name; ///< name of algorihtm
24+
bool (*algo)(void*&); ///< algorithm to run on data
25+
void (*fail_func)(void*&); ///< fail funciton if algroithm fails
26+
std::function<void*(T)> setup_func; ///< setup function to create arguments
27+
28+
};
29+
}
30+
#endif

src/DataModelBase/Buffer.h

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#ifndef BUFFER_H
2+
#define BUFFER_H
3+
4+
#include <vector>
5+
#include <mutex>
6+
#include <SerialisableObject.h>
7+
#include <BinaryStream.h>
8+
9+
namespace ToolFramework{
10+
11+
template<class T> class Buffer: SerialisableObject{
12+
13+
public:
14+
15+
Buffer(){;}
16+
void Add(T &in){
17+
std::lock_guard<std::mutex> lock(mtx);
18+
data.push_back(in);
19+
};
20+
void Swap(std::vector<T> &in){
21+
std::lock_guard<std::mutex> lock(mtx);
22+
if(data.size()) std::swap (data, in);
23+
24+
}
25+
size_t Size(){
26+
27+
std::lock_guard<std::mutex> lock(mtx);
28+
return data.size();
29+
30+
}
31+
void Clear(){
32+
std::lock_guard<std::mutex> lock(mtx);
33+
data.clear();
34+
}
35+
36+
37+
std::string GetVersion(){ return "1";}
38+
39+
bool Print(){
40+
41+
return true;
42+
}
43+
44+
bool Serialise(BinaryStream& bs){
45+
46+
bs & data;
47+
return true;
48+
}
49+
50+
51+
private:
52+
std::vector<T> data;
53+
std::mutex mtx;
54+
55+
};
56+
57+
}
58+
59+
#endif
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#ifndef BUFFER_DISPATCHER_H
2+
#define BUFFER_DISPATCHER_H
3+
4+
#include <atomic>
5+
#include <vector>
6+
#include <Utilities.h>
7+
#include <JobQueue.h>
8+
#include <Pool.h>
9+
#include <Buffer.h>
10+
#include <AlgorithmWrapper.h>
11+
12+
namespace ToolFramework{
13+
14+
template<class T> struct BufferDispatcher_args:Thread_args{
15+
BufferDispatcher_args(){;}
16+
~BufferDispatcher_args(){;}
17+
18+
Buffer<T>* buffer = 0;
19+
std::vector<T> local_buffer;
20+
std::vector<AlgorithmWrapper<T> >* algorithms = 0;
21+
Job* job = 0;
22+
23+
JobQueue* job_queue = 0;
24+
Pool<Job>* job_pool = 0;
25+
26+
std::atomic<uint64_t>* counter = 0;
27+
28+
};
29+
30+
template<class T> class BufferDispatcher{
31+
32+
public:
33+
34+
BufferDispatcher(){;}
35+
~BufferDispatcher(){Close();}
36+
bool Init(Buffer<T>* buffer, std::vector<AlgorithmWrapper<T> >* algorithms, JobQueue* job_queue, Pool<Job>* job_pool){
37+
38+
args.buffer = buffer;
39+
args.algorithms = algorithms;
40+
args.job_queue = job_queue;
41+
args.job_pool = job_pool;
42+
counter = 0;
43+
args.counter = &counter;
44+
45+
if(buffer == 0 || algorithms == 0 || job_queue == 0 || job_pool == 0) return false;
46+
47+
m_util.CreateThread("BufferDispatcher", &Thread, &args);
48+
49+
}
50+
51+
void Close(){
52+
if(args.buffer == 0 || args.algorithms == 0 || args.job_queue == 0 || args.job_pool == 0) return;
53+
m_util.KillThread(&args);
54+
55+
args.buffer = 0;
56+
args.algorithms = 0;
57+
args.job_queue = 0;
58+
args.job_pool = 0;
59+
60+
}
61+
62+
std::atomic<uint64_t> counter;
63+
64+
65+
private:
66+
67+
static void Thread(Thread_args* arg){
68+
BufferDispatcher_args<T>* args=reinterpret_cast<BufferDispatcher_args<T>*>(arg);
69+
if(args->algorithms->size()==0){
70+
usleep(100);
71+
return;
72+
}
73+
74+
args->buffer->Swap(args->local_buffer);
75+
76+
if(args->local_buffer.size() == 0){
77+
usleep(100);
78+
return;
79+
}
80+
81+
for(size_t i = 0; i < args->local_buffer.size(); i++){
82+
83+
for(size_t j = 0; j < args->algorithms->size(); j++){
84+
85+
args->job = args->job_pool->GetNew(args->algorithms->at(j).name);
86+
args->job->m_id = args->algorithms->at(j).name;
87+
args->job->func = args->algorithms->at(j).algo;
88+
args->job->fail_func = args->algorithms->at(j).fail_func;
89+
args->job->data = args->algorithms->at(j).setup_func(args->local_buffer.at(i));
90+
args->job->out_pool = args->job_pool;
91+
92+
args->job_queue->AddJob(args->job);
93+
args->job = 0;
94+
(*args->counter)++;
95+
}
96+
97+
args->local_buffer.at(i) = 0;
98+
99+
}
100+
101+
args->local_buffer.clear();
102+
103+
return;
104+
}
105+
106+
BufferDispatcher_args<T> args;
107+
Utilities m_util;
108+
109+
110+
};
111+
112+
}
113+
114+
#endif

src/DataModelBase/DataModelBase.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "Utilities.h"
88
#include "WorkerPoolManager.h"
99
#include "Pool.h"
10+
#include "BufferDispatcher.h"
1011

1112
#include "Store.h"
1213
#include "BStore.h"

0 commit comments

Comments
 (0)