scripting::ThreadQueue Class Reference

Keeps a list of SquirrelThreads that wait for a wakeup event. More...

#include <thread_queue.hpp>

List of all members.

Public Member Functions

 ThreadQueue ()
virtual ~ThreadQueue ()
void add (HSQUIRRELVM vm)
 adds a thread (actually a weakref to the thread)
void wakeup ()
 wakes up threads in the list

Private Types

typedef std::vector< HSQOBJECT > ThreadList

Private Attributes

ThreadList threads


Detailed Description

Keeps a list of SquirrelThreads that wait for a wakeup event.

Definition at line 28 of file thread_queue.hpp.


Member Typedef Documentation

typedef std::vector<HSQOBJECT> scripting::ThreadQueue::ThreadList [private]

Definition at line 40 of file thread_queue.hpp.


Constructor & Destructor Documentation

scripting::ThreadQueue::ThreadQueue (  ) 

Definition at line 25 of file thread_queue.cpp.

00025                          :
00026   threads()
00027 {
00028 }

scripting::ThreadQueue::~ThreadQueue (  )  [virtual]

Definition at line 30 of file thread_queue.cpp.

00031 {
00032 }


Member Function Documentation

void scripting::ThreadQueue::add ( HSQUIRRELVM  vm  ) 

adds a thread (actually a weakref to the thread)

Definition at line 35 of file thread_queue.cpp.

References scripting::global_vm, threads, and scripting::vm_to_object().

Referenced by scripting::wait_for_screenswitch().

00036 {
00037   // create a weakref to the VM
00038   HSQOBJECT vm_obj = vm_to_object(vm);
00039   sq_pushobject(global_vm, vm_obj);
00040   sq_weakref(global_vm, -1);
00041 
00042   HSQOBJECT object;
00043   if(SQ_FAILED(sq_getstackobj(global_vm, -1, &object))) {
00044     sq_pop(global_vm, 2);
00045     throw SquirrelError(global_vm, "Couldn't get thread weakref from vm");
00046   }
00047   sq_addref(global_vm, &object);
00048   threads.push_back(object);
00049 
00050   sq_pop(global_vm, 2);
00051 }

void scripting::ThreadQueue::wakeup (  ) 

wakes up threads in the list

Definition at line 54 of file thread_queue.cpp.

References scripting::global_vm, log_warning, and threads.

Referenced by ScreenManager::handle_screen_switch().

00055 {
00056   // we traverse the list in reverse orders and use indices. This should be
00057   // robust for scripts that add new entries to the list while we're traversing
00058   // it
00059   size_t i = threads.size() - 1;
00060   size_t end = (size_t) 0 - 1;
00061   size_t size_begin = threads.size();
00062   while(i != end) {
00063     HSQOBJECT object = threads[i];
00064 
00065     sq_pushobject(global_vm, object);
00066     sq_getweakrefval(global_vm, -1);
00067 
00068     HSQUIRRELVM scheduled_vm;
00069     if(sq_gettype(global_vm, -1) == OT_THREAD &&
00070        SQ_SUCCEEDED(sq_getthread(global_vm, -1, &scheduled_vm))) {
00071       if(SQ_FAILED(sq_wakeupvm(scheduled_vm, SQFalse, SQFalse, SQTrue, SQFalse))) {
00072         log_warning << "Couldn't wakeup scheduled squirrel VM" << std::endl;
00073       }
00074     }
00075 
00076     sq_release(global_vm, &object);
00077     sq_pop(global_vm, 1);
00078     i--;
00079   }
00080 
00081   threads.erase(threads.begin(), threads.begin() + size_begin);
00082 }


Member Data Documentation

ThreadList scripting::ThreadQueue::threads [private]

Definition at line 41 of file thread_queue.hpp.

Referenced by add(), and wakeup().


The documentation for this class was generated from the following files:
Generated on Mon Jun 9 03:38:40 2014 for SuperTux by  doxygen 1.5.1