Main Page | Class Hierarchy | Class List | File List | Class Members

MTasker< EventKey, EventVal > Class Template Reference

The main MTasker class. More...

#include <mtasker.hh>

Collaboration diagram for MTasker< EventKey, EventVal >:

Collaboration graph
[legend]
List of all members.

Public Types

typedef void tfunc_t (void *)
 type of the pointer that starts a thread


Public Member Functions

 MTasker (size_t stacksize=8192)
 Constructor.

int waitEvent (const EventKey &key, EventVal *val=0, unsigned int timeout=0)
 puts a thread to sleep waiting until a specified event arrives

void yield ()
 yields control to the kernel or other threads

int sendEvent (const EventKey &key, const EventVal *val=0)
 reports that an event took place for which threads may be waiting

void getEvents (std::vector< EventKey > &events)
 gives access to the list of Events threads are waiting for

void makeThread (tfunc_t *start, void *val)
 launches a new thread

bool schedule ()
 needs to be called periodically so threads can run and housekeeping can be performed

bool noProcesses ()
 returns true if there are no processes

unsigned int numProcesses ()
 returns the number of processes running

int getTid ()
 Returns the current Thread ID (tid).


Detailed Description

template<class EventKey = int, class EventVal = int>
class MTasker< EventKey, EventVal >

The main MTasker class. See the main page for more information.
Parameters:
EventKey Type of the key with which events are to be identified. Defaults to int.
EventVal Type of the content or value of an event. Defaults to int. Cannot be set to void.
Note:
The EventKey needs to have an operator< defined because it is used as the key of an associative array

Definition at line 16 of file mtasker.hh.


Constructor & Destructor Documentation

template<class EventKey = int, class EventVal = int>
MTasker< EventKey, EventVal >::MTasker size_t  stacksize = 8192  )  [inline]
 

Constructor with a small default stacksize. If any of your threads exceeds this stack, your application will crash. This limit applies solely to the stack, the heap is not limited in any way. If threads need to allocate a lot of data, the use of new/delete is suggested.

Definition at line 46 of file mtasker.hh.


Member Function Documentation

template<class Key, class Val>
void MTasker< Key, Val >::getEvents std::vector< Key > &  events  ) 
 

The kernel can call this to get a list of Events threads are waiting for. This is very useful to setup 'select' or 'poll' or 'aio' events needed to satisfy these requests. getEvents clears the events parameter before filling it.

Parameters:
events Vector which is to be filled with keys threads are waiting for

Definition at line 287 of file mtasker.cc.

template<class Key, class Val>
int MTasker< Key, Val >::getTid  ) 
 

Processes can call this to get a numerical representation of their current thread ID. This can be useful for logging purposes.

Definition at line 308 of file mtasker.cc.

template<class Key, class Val>
void MTasker< Key, Val >::makeThread tfunc_t start,
void *  val
 

The kernel can call this to make a new thread, which starts at the function start and gets passed the val void pointer.

Parameters:
start Pointer to the function which will form the start of the thread
val A void pointer that can be used to pass data to the thread

Definition at line 202 of file mtasker.cc.

References MTasker< EventKey, EventVal >::tfunc_t.

template<class Key, class Val>
bool MTasker< Key, Val >::noProcesses  ) 
 

Call this to check if no processes are running anymore

Returns:
true if no processes are left

Definition at line 265 of file mtasker.cc.

template<class Key, class Val>
unsigned int MTasker< Key, Val >::numProcesses  ) 
 

Call this to perhaps limit activities if too many threads are running

Returns:
number of processes running

Definition at line 274 of file mtasker.cc.

template<class Key, class Val>
bool MTasker< Key, Val >::schedule  ) 
 

The kernel should call this function every once in a while. It makes sense to call this function if you:

  • reported an event
  • called makeThread
  • want to have threads running waitEvent() to get a timeout if enough time passed

Returns:
Returns if there is more work scheduled and recalling schedule now would be useful

Definition at line 231 of file mtasker.cc.

template<class EventKey, class EventVal>
int MTasker< EventKey, EventVal >::sendEvent const EventKey &  key,
const EventVal *  val = 0
 

From the kernel loop, sendEvent can be called to report that something occured for which there may be waiters.

Parameters:
key Key of the event for which threads may be waiting
val If non-zero, pointer to the content of the event
Returns:
Returns -1 in case of error, 0 if there were no waiters, 1 if a thread was woken up.

Definition at line 177 of file mtasker.cc.

template<class EventKey, class EventVal>
int MTasker< EventKey, EventVal >::waitEvent const EventKey &  key,
EventVal *  val = 0,
unsigned int  timeout = 0
 

Threads can call waitEvent to register that they are waiting on an event with a certain key. If so desidered, the event can carry data which is returned in val in case that is non-zero.

Furthermore, a timeout can be specified in seconds.

Only one thread can be waiting on a key, results of trying to have more threads waiting on the same key are undefined.

Parameters:
key Event key to wait for. Needs to match up to a key reported to sendEvent
val If non-zero, the value of the event will be stored in *val
timeout If non-zero, number of seconds to wait for an event.
Returns:
returns -1 in case of error, 0 in case of timeout, 1 in case of an answer

Definition at line 146 of file mtasker.cc.

template<class Key, class Val>
void MTasker< Key, Val >::yield  ) 
 

Hands over control to the kernel, allowing other processes to run, or events to arrive

Definition at line 165 of file mtasker.cc.


The documentation for this class was generated from the following files:
Generated on Sun Feb 8 12:07:52 2004 for MTasker by doxygen 1.3.5