- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Executives/DeductiveController/Rtapi/MultiThreadedObject.cpp

    00001
    00002 //
    00003 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00004 // All Rights Reserved          Massachusetts Institute of Technology
    00005 //
    00006 // This software is provided as is. Model-based Embedded and Robotic Systems
    00007 // (MERS) group does not assume any responsibility.
    00008 //
    00009 // Organization:        Model-based Embedded and Robotic Systems Group
    00010 //                                      Massachusetts Institute of Technology
    00011 //
    00013
    00014 #include "MultiThreadedObject.h"
    00015 #include <stdio.h>
    00016 // #include <sys/errno.h>
    00017
    00019
    00020 using namespace Mers::Executives::DeductiveController::Rtapi;
    00021
    00023 // Construction/Destruction
    00025
    00026 MultiThreadedObject::MultiThreadedObject() : m_ThreadIsRunning(false)
    00027 {
    00028 }
    00029
    00031
    00032 MultiThreadedObject::~MultiThreadedObject()
    00033 {
    00034         if(m_ThreadIsRunning)
    00035                 stop();
    00036 }
    00037
    00039
    00040 int MultiThreadedObject::start()
    00041 {
    00042         int ret;
    00043         if(m_ThreadIsRunning)
    00044                 return -1;
    00045
    00046         ret = pthread_attr_init(&m_ThreadAttributes);
    00047         if(ret != 0)
    00048                 return ret;
    00049
    00050         /*
    00051         ret = pthread_attr_setscope(&m_ThreadAttributes, PTHREAD_SCOPE_SYSTEM);
    00052         if(ret != 0){
    00053                 if(ret == ENOSYS){
    00054                         printf("Warning, pthread scheduling not supported\n");
    00055                 }else if(ret == ENOTSUP){
    00056                         printf("Warning, pthread scope not supported\n");
    00057                 }else{
    00058                         return ret;
    00059                 }
    00060         }
    00061         */
    00062
    00063         m_ThreadIsRunning = true;
    00064         ret = pthread_create(&m_Thread, &m_ThreadAttributes, runCallback, this);
    00065
    00066         if( ret != 0 )
    00067                 m_ThreadIsRunning = false;
    00068
    00069         return ret;
    00070 }
    00071
    00073
    00074 int MultiThreadedObject::stop()
    00075 {
    00076         if(!m_ThreadIsRunning)
    00077                 return -1;
    00078         //pthread_cancel(m_Thread); // sets a flag and waits until test_cancel
    00079         //int ret = pthread_join(m_Thread, NULL);
    00080         m_ThreadIsRunning = false;
    00081         //return ret;
    00082         return 0;
    00083 }
    00084
    00086
    00087 void * MultiThreadedObject::runCallback(void * thisCallback)
    00088 {
    00089         MultiThreadedObject * mtObj = (MultiThreadedObject *)thisCallback;
    00090         void * ret = mtObj->run();
    00091         mtObj->m_ThreadIsRunning = false;
    00092         return ret;
    00093 }
    00094