Scroom 0.14-48-ga0fee447
Loading...
Searching...
No Matches
threadpool-tests.cc File Reference
#include <scroom/threadpool.hh>
#include <memory>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include <gtest/gtest.h>
#include <scroom/function-additor.hh>
#include <scroom/semaphore.hh>
#include "helpers.hh"
Include dependency graph for threadpool-tests.cc:

Classes

class  A
 
class  B< R >
 

Functions

const millisec short_timeout (250)
 
const millisec long_timeout (2000)
 
template<typename R >
no_op (Semaphore *s, R result)
 
bool has_at_least_n_threads (ThreadPool *pool, int count_)
 
bool has_exactly_n_threads (ThreadPool *pool, int count)
 
pool schedule (clear(&s))
 
 EXPECT_FALSE (s.P(long_timeout))
 
pool add ()
 
 EXPECT_TRUE (s.P(long_timeout))
 
pool schedule (clear(&low), PRIO_NORMAL)
 
pool schedule (pass(&low)+clear(&high), PRIO_HIGH)
 
 EXPECT_FALSE (high.P(short_timeout))
 
 EXPECT_TRUE (high.P(long_timeout))
 
 EXPECT_TRUE (has_exactly_n_threads(&pool, 0))
 
pool schedule (A::create(&a))
 
 EXPECT_TRUE (a.P(long_timeout))
 
boost::unique_future< int > result (pool.schedule< int >([pa=&a] { return no_op(pa, 42);}))
 
 EXPECT_FALSE (a.P(short_timeout))
 
 EXPECT_FALSE (result.is_ready())
 
 EXPECT_EQ (42, result.get())
 
boost::unique_future< bool > result (pool.schedule< bool, B< bool > >(B< bool >::create(&a, false)))
 
 EXPECT_EQ (false, result.get())
 
 EXPECT_TRUE (has_exactly_n_threads(t.get(), expected))
 
 EXPECT_TRUE (has_exactly_n_threads(t.get(), 1))
 

Variables

ThreadPool pool (0)
 
Semaphore low (0)
 
const int expected = 2
 
Semaphore a (0)
 

Function Documentation

◆ add()

pool add ( )

◆ EXPECT_EQ() [1/2]

EXPECT_EQ ( 42  ,
result.  get() 
)

◆ EXPECT_EQ() [2/2]

EXPECT_EQ ( false  ,
result.  get() 
)

◆ EXPECT_FALSE() [1/4]

EXPECT_FALSE ( a.  Pshort_timeout)

◆ EXPECT_FALSE() [2/4]

EXPECT_FALSE ( high.  Pshort_timeout)

◆ EXPECT_FALSE() [3/4]

EXPECT_FALSE ( result.  is_ready())

◆ EXPECT_FALSE() [4/4]

EXPECT_FALSE ( s.  Plong_timeout)

◆ EXPECT_TRUE() [1/6]

EXPECT_TRUE ( a.  Plong_timeout)

◆ EXPECT_TRUE() [2/6]

EXPECT_TRUE ( has_exactly_n_threads &,  0pool)
Initial value:
{
Definition threadpool.hh:43
ThreadPool pool(0)

◆ EXPECT_TRUE() [3/6]

EXPECT_TRUE ( has_exactly_n_threads(t.get(), 1)  )

◆ EXPECT_TRUE() [4/6]

EXPECT_TRUE ( has_exactly_n_threads(t.get(), expected )

◆ EXPECT_TRUE() [5/6]

EXPECT_TRUE ( high.  Plong_timeout)

◆ EXPECT_TRUE() [6/6]

EXPECT_TRUE ( s.  Plong_timeout)

◆ has_at_least_n_threads()

bool has_at_least_n_threads ( ThreadPool pool,
int  count_ 
)
88{
89 if(count_ <= 0)
90 {
91 return true;
92 }
93
94 std::vector<Semaphore*> semaphores(count_);
95 for(int i = 0; i < count_; i++)
96 {
97 semaphores[i] = new Semaphore(0);
98 }
99
100 for(int i = 0; i < count_ - 1; i++)
101 {
102 pool->schedule(pass(semaphores[i + 1]) + clear(semaphores[i]));
103 }
104
105 // All tasks are blocked on semaphores[count-1]
106
107 pool->schedule(clear(semaphores[count_ - 1]));
108 // If jobs of the same priority are scheduled in order, and if
109 // there are at least count_ threads, then this final job will get
110 // scheduled on the last available thread, thus freeing all
111 // others.
112 bool const result = semaphores[0]->P(long_timeout);
113
114 if(!result)
115 {
116 // If there are too few threads, then all threads are still
117 // blocked. This will ultimately block the ThreadPool destructor,
118 // so we have to unblock them manually here.
119 for(int i = 1; i < count_; i++)
120 {
121 semaphores[i]->V();
122 }
123 }
124 return result;
125}
Definition semaphore.hh:16
void schedule(boost::function< void()> const &fn, int priority=defaultPriority, const Queue::Ptr &queue=defaultQueue())
Definition threadpoolimpl.cc:345
boost::function< void()> pass(Semaphore *s)
Definition helpers.cc:23
observers clear()
SampleIterator< const uint8_t > result
Definition sampleiterator-tests.cc:94
const millisec long_timeout(2000)

Referenced by has_exactly_n_threads().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ has_exactly_n_threads()

bool has_exactly_n_threads ( ThreadPool pool,
int  count 
)
128{
130}
const size_t count
Definition pageprovider-tests.cc:21
bool has_at_least_n_threads(ThreadPool *pool, int count_)
Definition threadpool-tests.cc:87
Here is the call graph for this function:

◆ long_timeout()

const millisec long_timeout ( 2000  )

Referenced by has_at_least_n_threads().

Here is the caller graph for this function:

◆ no_op()

template<typename R >
R no_op ( Semaphore s,
result 
)
80{
81 s->V();
82 return result;
83}
BitmapSurface::Ptr const s
Definition transformpresentation_test.cc:70

◆ result() [1/2]

boost::unique_future< bool > result ( pool.schedule< bool, B< bool > >  B< bool >::create(&a, false))

◆ result() [2/2]

boost::unique_future< int > result ( pool.schedule< int >  [pa=&a] { return no_op(pa, 42);})

◆ schedule() [1/4]

pool schedule ( A::create a)

◆ schedule() [2/4]

pool schedule ( clear low,
PRIO_NORMAL   
)

◆ schedule() [3/4]

pool schedule ( clear s)

◆ schedule() [4/4]

pool schedule ( pass &low+clear high,
PRIO_HIGH   
)

◆ short_timeout()

const millisec short_timeout ( 250  )

Variable Documentation

◆ a

Semaphore a(0) ( )
Initial value:

◆ expected

expected = 2

◆ low

Semaphore low(0) ( )
Initial value:
{
Semaphore high(0)

◆ pool

ThreadPool pool(0) ( )
Initial value:
{

Referenced by has_at_least_n_threads(), and has_exactly_n_threads().