cgma
CubitQtConcurrent Class Reference

Api for concurrency based on Qt. More...

#include <CubitQtConcurrentApi.h>

Inheritance diagram for CubitQtConcurrent:
CubitConcurrent

List of all members.

Public Member Functions

 CubitQtConcurrent ()
virtual ~CubitQtConcurrent ()
const std::string & get_name () const
const char * get_type () const
ThreadLocalStorageInterfacecreate_local_storage (void(*cleanup_function)(void *))
void destroy_local_storage (ThreadLocalStorageInterface *i)
virtual void wait (Task *task)
virtual void idle_wait (Task *task)
virtual void wait (const std::vector< Task * > &task)
void wait_for_any (const std::vector< Task * > &tasks, std::vector< Task * > &finished_tasks)
virtual bool is_completed (Task *task)
virtual bool is_running (Task *task)
virtual void wait (TaskGroup *task_group)
virtual bool is_completed (TaskGroup *task_group)
virtual bool is_running (TaskGroup *task_group)
virtual void cancel (TaskGroup *task_group)

Protected Member Functions

virtual void schedule (Task *task)
virtual void schedule (TaskGroup *task_group)

Static Protected Member Functions

static void execute (Task *t)

Protected Attributes

QMap< TaskGroup *, QFuture
< void > > 
taskgroupmap
QMutex m2
QMap< Task *, QFuture< void > > taskmap
QMutex m
std::string _name

Detailed Description

Api for concurrency based on Qt.

Definition at line 16 of file CubitQtConcurrentApi.h.


Constructor & Destructor Documentation

Definition at line 66 of file CubitQtConcurrentApi.cpp.

{
    _name = "CubitQtConcurrent";

    // If there is no global instance, set this object as the instance.
  if(!CubitConcurrent::mInstance)
    CubitConcurrent::mInstance = this;
}

Definition at line 75 of file CubitQtConcurrentApi.cpp.

{
  // If this is the global instance, clear the pointer.
  if(this == CubitConcurrent::mInstance)
    CubitConcurrent::mInstance = 0;


}

Member Function Documentation

Implements CubitConcurrent.

Definition at line 287 of file CubitQtConcurrentApi.cpp.

{
    m2.lock();
    QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
    m2.unlock();

    iter->cancel();
}

Implements CubitConcurrent.

Definition at line 106 of file CubitQtConcurrentApi.cpp.

{
  return new QtTLS(cleanup_function);
}

Implements CubitConcurrent.

Definition at line 111 of file CubitQtConcurrentApi.cpp.

{
  delete static_cast<QtTLS*>(i);
}
void CubitQtConcurrent::execute ( CubitConcurrent::Task t) [static, protected]

Definition at line 245 of file CubitQtConcurrentApi.cpp.

{
    t->execute();
}
const std::string & CubitQtConcurrent::get_name ( ) const

Definition at line 84 of file CubitQtConcurrentApi.cpp.

{
    return _name;
}
const char * CubitQtConcurrent::get_type ( ) const

Definition at line 89 of file CubitQtConcurrentApi.cpp.

{
    return _name.c_str();
}

Reimplemented from CubitConcurrent.

Definition at line 135 of file CubitQtConcurrentApi.cpp.

{
  m.lock();
  QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
  m.unlock();
  if(!iter->isFinished())
  {
    QEventLoop loop;
    QFutureWatcher<void> watcher;
    watcher.setFuture(*iter);
    QObject::connect(&watcher, SIGNAL(finished()), &loop, SLOT(quit()));
    loop.exec();
  }
  m.lock();
  taskmap.erase(iter);
  m.unlock();
}

Implements CubitConcurrent.

Definition at line 226 of file CubitQtConcurrentApi.cpp.

{
    m.lock();
    QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
    m.unlock();

    return iter->isFinished();
}

Implements CubitConcurrent.

Definition at line 269 of file CubitQtConcurrentApi.cpp.

{
    m2.lock();
    QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
    m2.unlock();

    return iter->isFinished();
}

Implements CubitConcurrent.

Definition at line 235 of file CubitQtConcurrentApi.cpp.

{
    m.lock();
    QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
    m.unlock();

    return iter->isRunning();
}

Implements CubitConcurrent.

Definition at line 278 of file CubitQtConcurrentApi.cpp.

{
    m2.lock();
    QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
    m2.unlock();

    return iter->isRunning();
}
void CubitQtConcurrent::schedule ( CubitConcurrent::Task task) [protected, virtual]

Implements CubitConcurrent.

Definition at line 116 of file CubitQtConcurrentApi.cpp.

{
    QFuture<void> f = ::QtConcurrent::run(task, &Task::execute);    
    m.lock();
    taskmap[task] = f;
    m.unlock();
}
void CubitQtConcurrent::schedule ( CubitConcurrent::TaskGroup tg) [protected, virtual]

Implements CubitConcurrent.

Definition at line 250 of file CubitQtConcurrentApi.cpp.

{
    QFuture<void> f = ::QtConcurrent::map(tg->tasks, CubitQtConcurrent::execute);
    m2.lock();
    taskgroupmap[tg] = f;
    m2.unlock();
}
void CubitQtConcurrent::wait ( CubitConcurrent::Task task) [virtual]

Implements CubitConcurrent.

Definition at line 124 of file CubitQtConcurrentApi.cpp.

{
    m.lock();
    QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(task);
    m.unlock();
    iter->waitForFinished();
    m.lock();
    taskmap.erase(iter);
    m.unlock();
}
virtual void CubitQtConcurrent::wait ( const std::vector< Task * > &  task) [virtual]

Implements CubitConcurrent.

Implements CubitConcurrent.

Definition at line 258 of file CubitQtConcurrentApi.cpp.

{
    m2.lock();
    QMap<CubitConcurrent::TaskGroup*, QFuture<void> >::iterator iter = taskgroupmap.find(tg);
    m2.unlock();
    iter->waitForFinished();
    m2.lock();
    taskgroupmap.erase(iter);
    m2.unlock();
}
void CubitQtConcurrent::wait_for_any ( const std::vector< Task * > &  tasks,
std::vector< Task * > &  finished_tasks 
) [virtual]

Implements CubitConcurrent.

Definition at line 152 of file CubitQtConcurrentApi.cpp.

{
    m.lock();
    for(size_t i=0; i<tasks.size(); i++)
    {
      QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(tasks[i]);
      if(iter->isFinished())
      {
        finished_tasks.push_back(tasks[i]);
        taskmap.erase(iter);
      }
    }
    m.unlock();

    if(!finished_tasks.empty())
      return;


    if(!QCoreApplication::instance())
    {
      int arg=0;
      new QCoreApplication(arg,NULL);
    }

    QEventLoop evLoop;

    m.lock();
    for(size_t i=0; i<tasks.size(); i++)
    {
      QFutureWatcher<void> *f= new QFutureWatcher<void>(&evLoop);
      QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(tasks[i]);
      f->setFuture(*iter);
      QObject::connect(f,SIGNAL(finished()),&evLoop,SLOT(quit()));
    }
    m.unlock();

    evLoop.exec();

    m.lock();
    for(size_t i=0; i<tasks.size(); i++)
    {
      QMap<CubitConcurrent::Task*, QFuture<void> >::iterator iter = taskmap.find(tasks[i]);
      if(iter->isFinished())
      {
        finished_tasks.push_back(tasks[i]);
        taskmap.erase(iter);
      }
    }
    m.unlock();

}

Member Data Documentation

std::string CubitQtConcurrent::_name [protected]

Definition at line 79 of file CubitQtConcurrentApi.h.

QMutex CubitQtConcurrent::m [protected]

Definition at line 77 of file CubitQtConcurrentApi.h.

QMutex CubitQtConcurrent::m2 [protected]

Definition at line 74 of file CubitQtConcurrentApi.h.

QMap<TaskGroup*, QFuture<void> > CubitQtConcurrent::taskgroupmap [protected]

Definition at line 73 of file CubitQtConcurrentApi.h.

QMap<Task*, QFuture<void> > CubitQtConcurrent::taskmap [protected]

Definition at line 76 of file CubitQtConcurrentApi.h.


The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines