Commit 8a3ff4ff authored by Davis King's avatar Davis King

Added an add_task_by_value() function to the thread_pool.

parent cf6f55c2
...@@ -260,7 +260,8 @@ namespace dlib ...@@ -260,7 +260,8 @@ namespace dlib
uint64 thread_pool_implementation:: uint64 thread_pool_implementation::
add_task_internal ( add_task_internal (
const bfp_type& bfp const bfp_type& bfp,
shared_ptr<function_object_copy>& item
) )
{ {
auto_mutex M(m); auto_mutex M(m);
...@@ -293,6 +294,7 @@ namespace dlib ...@@ -293,6 +294,7 @@ namespace dlib
tasks[idx].thread_id = my_thread_id; tasks[idx].thread_id = my_thread_id;
tasks[idx].task_id = make_next_task_id(idx); tasks[idx].task_id = make_next_task_id(idx);
tasks[idx].bfp = bfp; tasks[idx].bfp = bfp;
tasks[idx].function_copy.swap(item);
task_ready_signaler.signal(); task_ready_signaler.signal();
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include "../uintn.h" #include "../uintn.h"
#include "../array.h" #include "../array.h"
#include "../smart_pointers_thread_safe.h" #include "../smart_pointers_thread_safe.h"
#include "../smart_pointers.h"
namespace dlib namespace dlib
{ {
...@@ -296,9 +297,34 @@ namespace dlib ...@@ -296,9 +297,34 @@ namespace dlib
return tasks[idx].task_id; return tasks[idx].task_id;
} }
struct function_object_copy
{
virtual ~function_object_copy(){}
};
template <typename T>
struct function_object_copy_instance : function_object_copy
{
function_object_copy_instance(const T& item_) : item(item_) {}
T item;
virtual ~function_object_copy_instance(){}
};
uint64 add_task_internal ( uint64 add_task_internal (
const bfp_type& bfp const bfp_type& bfp,
shared_ptr<function_object_copy>& item
); );
/*!
ensures
- adds a task to call the given bfp object.
- swaps item into the internal task object which will have a lifetime
at least as long as the running task.
- returns the task id for this new task
!*/
uint64 add_task_internal (
const bfp_type& bfp
) { shared_ptr<function_object_copy> temp; return add_task_internal(bfp, temp); }
/*! /*!
ensures ensures
- adds a task to call the given bfp object. - adds a task to call the given bfp object.
...@@ -424,10 +450,12 @@ namespace dlib ...@@ -424,10 +450,12 @@ namespace dlib
member_function_pointer<long,long>::kernel_1a mfp2; member_function_pointer<long,long>::kernel_1a mfp2;
bfp_type bfp; bfp_type bfp;
shared_ptr<function_object_copy> function_copy;
}; };
array<task_state_type>::expand_1d_c tasks; array<task_state_type>::expand_1d tasks;
array<thread_id_type>::expand_1d_c worker_thread_ids; array<thread_id_type>::expand_1d worker_thread_ids;
mutex m; mutex m;
signaler task_done_signaler; signaler task_done_signaler;
...@@ -528,6 +556,23 @@ namespace dlib ...@@ -528,6 +556,23 @@ namespace dlib
return id; return id;
} }
template <typename F>
uint64 add_task_by_value (
const F& function_object
)
{
thread_pool_implementation::function_object_copy_instance<F>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<F>(function_object);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item);
uint64 id = impl->add_task_internal(temp, function_copy);
return id;
}
template <typename T> template <typename T>
uint64 add_task ( uint64 add_task (
const T& obj, const T& obj,
...@@ -573,6 +618,26 @@ namespace dlib ...@@ -573,6 +618,26 @@ namespace dlib
return id; return id;
} }
template <typename F, typename A1>
uint64 add_task_by_value (
const F& function_object,
future<A1>& arg1
)
{
thread_pool_implementation::function_object_copy_instance<F>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<F>(function_object);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, arg1.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the future to this task
arg1.task_id = id;
arg1.tp = impl;
return id;
}
template <typename T, typename T1, typename A1> template <typename T, typename T1, typename A1>
uint64 add_task ( uint64 add_task (
T& obj, T& obj,
...@@ -647,6 +712,29 @@ namespace dlib ...@@ -647,6 +712,29 @@ namespace dlib
return id; return id;
} }
template <typename F, typename A1, typename A2>
uint64 add_task_by_value (
const F& function_object,
future<A1>& arg1,
future<A2>& arg2
)
{
thread_pool_implementation::function_object_copy_instance<F>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<F>(function_object);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, arg1.get(), arg2.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the future to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
return id;
}
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2> typename T2, typename A2>
uint64 add_task ( uint64 add_task (
...@@ -736,6 +824,32 @@ namespace dlib ...@@ -736,6 +824,32 @@ namespace dlib
return id; return id;
} }
template <typename F, typename A1, typename A2, typename A3>
uint64 add_task_by_value (
const F& function_object,
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3
)
{
thread_pool_implementation::function_object_copy_instance<F>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<F>(function_object);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, arg1.get(), arg2.get(), arg3.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the future to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
arg3.task_id = id;
arg3.tp = impl;
return id;
}
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3> typename T3, typename A3>
...@@ -840,6 +954,35 @@ namespace dlib ...@@ -840,6 +954,35 @@ namespace dlib
return id; return id;
} }
template <typename F, typename A1, typename A2, typename A3, typename A4>
uint64 add_task_by_value (
const F& function_object,
future<A1>& arg1,
future<A2>& arg2,
future<A3>& arg3,
future<A4>& arg4
)
{
thread_pool_implementation::function_object_copy_instance<F>* ptr = 0;
ptr = new thread_pool_implementation::function_object_copy_instance<F>(function_object);
shared_ptr<thread_pool_implementation::function_object_copy> function_copy(ptr);
bfp_type temp;
temp.set(ptr->item, arg1.get(), arg2.get(), arg3.get(), arg4.get());
uint64 id = impl->add_task_internal(temp, function_copy);
// tie the future to this task
arg1.task_id = id;
arg1.tp = impl;
arg2.task_id = id;
arg2.tp = impl;
arg3.task_id = id;
arg3.tp = impl;
arg4.task_id = id;
arg4.tp = impl;
return id;
}
template <typename T, typename T1, typename A1, template <typename T, typename T1, typename A1,
typename T2, typename A2, typename T2, typename A2,
typename T3, typename A3, typename T3, typename A3,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment