ONPOSIX  2.0
 All Classes Namespaces Files Functions Variables Enumerator Friends Macros Pages
PosixDescriptor::Worker Class Reference

Worker thread to perform asynchronous operations. More...

Inheritance diagram for PosixDescriptor::Worker:
Inheritance graph

Public Member Functions

 Worker (shared_queue *q, PosixDescriptor *des)
 Constructor.
 ~Worker ()
void startAsyncOperation (bool read_operation, void(*handler)(Buffer *b, size_t size), Buffer *buff, size_t size)
 Function to start an asynchronous operation.
void startAsyncOperation (bool read_operation, void(*handler)(void *b, size_t size), void *buff, size_t size)
 Function so start an asynchronous operation.
- Public Member Functions inherited from AbstractThread
 AbstractThread ()
 Constructor. Initialize the class attributes.
virtual ~AbstractThread ()
 Destructor.
bool start ()
 Starts execution of the thread by calling run().
bool stop ()
 Stops the running thread.
bool waitForTermination ()
 Blocks the calling thread until the thread is finished.
bool sendSignal (int sig)
 Sends a signal to the thread.
bool setSchedParam (int policy, int priority)
 Set scheduling policy and priority.
bool getSchedParam (int *policy, int *priority)
 Get current scheduling policy and priority.

Private Member Functions

 Worker ()
 Disable the default constructor.
void run ()
 Method automatically called by start()

Private Attributes

PosixDescriptordes_
 File descriptor.
shared_queuequeue_
 Pointer to the shared queue.

Additional Inherited Members

- Static Public Member Functions inherited from AbstractThread
static bool blockSignal (int sig)
 Masks a specific signal on this thread.
static bool unblockSignal (int sig)
 Unmasks a signal previously masked.
static bool setSignalHandler (int sig, void(*handler)(int))
 Set a handler for a specific signal.
- Protected Member Functions inherited from AbstractThread
virtual void run ()=0
 This function must be reimplemented in a derived class to define the specific function run by the thread.
- Static Protected Member Functions inherited from AbstractThread
static void checkTermination ()
 Function to know if the thread has finished computation.
- Protected Attributes inherited from AbstractThread
pthread_t handle_

Detailed Description

Worker thread to perform asynchronous operations.

This class is used to run asynchronous operations (i.e., read and write). These operations are run on a different thread.

Definition at line 262 of file PosixDescriptor.hpp.

Constructor & Destructor Documentation

Worker ( )
private

Disable the default constructor.

Worker ( shared_queue q,
PosixDescriptor des 
)
inline

Constructor.

It just initializes the variables.

Parameters
qPointer to the shared_queue for synchronization and pending jobs
desPointer to the PosixDescriptor that "owns" this worker

Definition at line 305 of file PosixDescriptor.hpp.

:
des_(des), queue_(q) {}
~Worker ( )
inline

Definition at line 308 of file PosixDescriptor.hpp.

{
}

Member Function Documentation

void run ( )
private

Method automatically called by start()

Function run on the separate thread.

This method is automatically called by start() which, in turn, is called by startAsyncOperation()

This is the function automatically called by start() which, in turn, is called by startAsyncOperation(). The function is run by the worker thread. It performs the read/write operation and invokes the handler.

Exceptions
Itthrows runtime_error in case no operation has been scheduled

Definition at line 96 of file PosixDescriptor.cpp.

{
DEBUG("Worker running");
for (;;) {
DEBUG("===================");
DEBUG("New cycle");
bool close;
job* j = queue_->pop(&close);
if (j != 0){
int n;
DEBUG("Found one item in queue");
DEBUG("Need to read " << j->size_ << " bytes");
DEBUG("File descriptor = " << des_->getDescriptorNumber());
if (j->job_type_ == job::READ_BUFFER)
n = des_->do_read(j->buff_buffer_->getBuffer(), j->size_);
else if (j->job_type_ == job::READ_VOID)
n = des_->do_read(j->void_buffer_, j->size_);
else if (j->job_type_ == job::WRITE_BUFFER)
n = des_->do_write(j->buff_buffer_->getBuffer(), j->size_);
else if (j->job_type_ == job::WRITE_VOID)
n = des_->do_write(j->void_buffer_, j->size_);
else {
ERROR("Handler called without operation!");
throw std::runtime_error ("Async error");
}
DEBUG("Read " << n << " bytes");
DEBUG("Calling handler");
if ((j->job_type_ == job::READ_BUFFER) || (j->job_type_ == job::WRITE_BUFFER))
j->buff_handler_(j->buff_buffer_, n);
else
j->void_handler_(j->void_buffer_, n);
delete j;
} else {
if (!close) {
DEBUG("No data in queue");
} else {
DEBUG("Exiting!!");
pthread_exit(0);
}
}
}
}

Here is the call graph for this function:

void startAsyncOperation ( bool  read_operation,
void(*)(Buffer *b, size_t size)  handler,
Buffer buff,
size_t  size 
)

Function to start an asynchronous operation.

This method allows to start an asynchronous operation, either read or write.

Parameters
read_operationSpecifies if it is a read (true) or write (false) operation
handlerFunction to be run at the end of the operation. This function will have as arguments a Buffer* where data is stored and the number of bytes actually transferred.
buffBuffer where data must be copied to (for a read operation) or where data is contained (for a write operation)
sizeAmount of bytes to be transferred

Definition at line 37 of file PosixDescriptor.cpp.

{
DEBUG("Async operation started with buffer*");
struct job* j = new job;
j->size_ = size;
j->buff_handler_ = handler;
j->buff_buffer_ = buff;
if (read_operation)
j->job_type_ = job::READ_BUFFER;
else
j->job_type_ = job::WRITE_BUFFER;
queue_->push(j);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void startAsyncOperation ( bool  read_operation,
void(*)(void *b, size_t size)  handler,
void *  buff,
size_t  size 
)

Function so start an asynchronous operation.

This method allows to start an asynchronous operation, either read or write.

Parameters
read_operationSpecifies if it is a read (true) or write (false) operation
handlerFunction to be run at the end of the operation. This function will have as arguments a void* where data is stored and the number of bytes actually transferred.
buffvoid* where data must be copied to (for a read operation) or where data is contained (for a write operation)
sizeAmount of bytes to be transferred

Definition at line 69 of file PosixDescriptor.cpp.

{
DEBUG("Async operation started with void*");
struct job* j = new job;
j->size_ = size;
j->void_handler_ = handler;
j->void_buffer_ = buff;
if (read_operation)
j->job_type_ = job::READ_VOID;
else
j->job_type_ = job::WRITE_VOID;
queue_->push(j);
}

Here is the call graph for this function:

Member Data Documentation

PosixDescriptor* des_
private

File descriptor.

This is a pointer to the same PosixDescriptor that "owns" this instance of Worker. The pointer is needed to perform the operation (i.e., read or write).

Definition at line 283 of file PosixDescriptor.hpp.

shared_queue* queue_
private

Pointer to the shared queue.

This variable points the shared_queue used for asynchronous operations and synchronization between the main thread and the worker thread.

Definition at line 292 of file PosixDescriptor.hpp.


The documentation for this class was generated from the following files: