00001 /*!@file Channels/InputHandlerThreaded.C InputHandler subclass than can be plugged into SingleChannel to get multi-threaded computations */ 00002 00003 // //////////////////////////////////////////////////////////////////// // 00004 // The iLab Neuromorphic Vision C++ Toolkit - Copyright (C) 2000-2005 // 00005 // by the University of Southern California (USC) and the iLab at USC. // 00006 // See http://iLab.usc.edu for information about this project. // 00007 // //////////////////////////////////////////////////////////////////// // 00008 // Major portions of the iLab Neuromorphic Vision Toolkit are protected // 00009 // under the U.S. patent ``Computation of Intrinsic Perceptual Saliency // 00010 // in Visual Environments, and Applications'' by Christof Koch and // 00011 // Laurent Itti, California Institute of Technology, 2001 (patent // 00012 // pending; application number 09/912,225 filed July 23, 2001; see // 00013 // http://pair.uspto.gov/cgi-bin/final/home.pl for current status). // 00014 // //////////////////////////////////////////////////////////////////// // 00015 // This file is part of the iLab Neuromorphic Vision C++ Toolkit. // 00016 // // 00017 // The iLab Neuromorphic Vision C++ Toolkit is free software; you can // 00018 // redistribute it and/or modify it under the terms of the GNU General // 00019 // Public License as published by the Free Software Foundation; either // 00020 // version 2 of the License, or (at your option) any later version. // 00021 // // 00022 // The iLab Neuromorphic Vision C++ Toolkit is distributed in the hope // 00023 // that it will be useful, but WITHOUT ANY WARRANTY; without even the // 00024 // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR // 00025 // PURPOSE. See the GNU General Public License for more details. // 00026 // // 00027 // You should have received a copy of the GNU General Public License // 00028 // along with the iLab Neuromorphic Vision C++ Toolkit; if not, write // 00029 // to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, // 00030 // Boston, MA 02111-1307 USA. // 00031 // //////////////////////////////////////////////////////////////////// // 00032 // 00033 // Primary maintainer for this file: Rob Peters <rjpeters at usc dot edu> 00034 // $HeadURL: svn://isvn.usc.edu/software/invt/trunk/saliency/src/Channels/InputHandlerThreaded.C $ 00035 // $Id: InputHandlerThreaded.C 8590 2007-07-18 22:18:37Z rjpeters $ 00036 // 00037 00038 #ifndef CHANNELS_INPUTHANDLERTHREADED_C_DEFINED 00039 #define CHANNELS_INPUTHANDLERTHREADED_C_DEFINED 00040 00041 #include "Channels/InputHandlerThreaded.H" 00042 00043 #include "Channels/SingleChannel.H" 00044 #include "Util/JobServer.H" 00045 #include "Util/JobWithSemaphore.H" 00046 #include "Util/MainJobServer.H" 00047 00048 #include <unistd.h> 00049 00050 /// Represents a pending SingleChannel input job 00051 struct InputHandlerThreaded::Job : public JobWithSemaphore 00052 { 00053 Job(SingleChannel& chan_, 00054 const Image<float>& bwimg_, 00055 const SimTime& t_, 00056 const Image<byte>& clipMask_, 00057 const rutz::shared_ptr<PyramidCache<float> >& cache_); 00058 00059 virtual ~Job(); 00060 00061 virtual void run(); 00062 00063 virtual const char* jobType() const; 00064 00065 SingleChannel* const chan; 00066 Image<float> const bwimg; 00067 SimTime const t; 00068 Image<byte> const clipMask; 00069 rutz::shared_ptr<PyramidCache<float> > const cache; 00070 00071 private: 00072 Job(const Job&); // not implemented 00073 Job& operator=(const Job&); // not implemented 00074 }; 00075 00076 // ###################################################################### 00077 InputHandlerThreaded::Job::Job(SingleChannel& chan_, 00078 const Image<float>& bwimg_, 00079 const SimTime& t_, 00080 const Image<byte>& clipMask_, 00081 const rutz::shared_ptr<PyramidCache<float> >& cache_) 00082 : 00083 chan(&chan_), bwimg(bwimg_), t(t_), clipMask(clipMask_), cache(cache_) 00084 {} 00085 00086 // ###################################################################### 00087 InputHandlerThreaded::Job::~Job() 00088 {} 00089 00090 // ###################################################################### 00091 void InputHandlerThreaded::Job::run() 00092 { 00093 // now do the job 00094 this->chan->setClipPyramid(this->clipMask); 00095 this->chan->storePyramid(this->chan->computePyramid(this->bwimg, this->cache), 00096 this->t); 00097 // force the output to be computed and cached 00098 this->chan->storeOutputCache(this->chan->combineSubMaps()); 00099 this->markFinished(); 00100 } 00101 00102 // ###################################################################### 00103 const char* InputHandlerThreaded::Job::jobType() const 00104 { 00105 return "SingleChannelInputJob"; 00106 } 00107 00108 // ###################################################################### 00109 InputHandlerThreaded:: 00110 InputHandlerThreaded() 00111 : 00112 itsJob() 00113 {} 00114 00115 // ###################################################################### 00116 InputHandlerThreaded::~InputHandlerThreaded() 00117 {} 00118 00119 // ###################################################################### 00120 void InputHandlerThreaded::handleInput(SingleChannel& chan, 00121 const Image<float>& bwimg, 00122 const SimTime& t, 00123 const Image<byte>& clipMask, 00124 const rutz::shared_ptr<PyramidCache<float> >& cache) 00125 { 00126 itsJob = rutz::make_shared(new Job(chan, bwimg, t, clipMask, cache)); 00127 ASSERT(itsJob.is_valid()); 00128 getMainJobServer().enqueueJob(itsJob); 00129 } 00130 00131 // ###################################################################### 00132 void InputHandlerThreaded::waitForOutput(SingleChannel& chan) 00133 { 00134 if (!itsJob.is_valid()) 00135 return; 00136 00137 LDEBUG("waiting for thread job to complete for channel %s", 00138 chan.descriptiveName().c_str()); 00139 00140 itsJob->wait(); 00141 00142 itsJob.reset(NULL); 00143 } 00144 00145 // ###################################################################### 00146 rutz::shared_ptr<InputHandler> InputHandlerThreaded::makeClone() const 00147 { 00148 return rutz::make_shared(new InputHandlerThreaded); 00149 } 00150 00151 // ###################################################################### 00152 /* So things look consistent in everyone's emacs... */ 00153 /* Local Variables: */ 00154 /* mode: c++ */ 00155 /* indent-tabs-mode: nil */ 00156 /* End: */ 00157 00158 #endif // CHANNELS_INPUTHANDLERTHREADED_C_DEFINED