LTI-Lib latest version v1.9 - last update 10 Apr 2010

ltiSemaphore.h

00001 /*
00002  * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
00003  * Lehrstuhl fuer Technische Informatik, RWTH-Aachen, Germany
00004  *
00005  * This file is part of the LTI-Computer Vision Library (LTI-Lib)
00006  *
00007  * The LTI-Lib is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public License (LGPL)
00009  * as published by the Free Software Foundation; either version 2.1 of
00010  * the License, or (at your option) any later version.
00011  *
00012  * The LTI-Lib is distributed in the hope that it will be
00013  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
00014  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with the LTI-Lib; see the file LICENSE.  If
00019  * not, write to the Free Software Foundation, Inc., 59 Temple Place -
00020  * Suite 330, Boston, MA 02111-1307, USA.
00021  */
00022 
00023 
00024 /*----------------------------------------------------------------
00025  * project ....: LTI Digital Image/Signal Processing Library
00026  * file .......: ltiSemaphore.h
00027  * authors ....: Thomas Rusert
00028  * organization: LTI, RWTH Aachen
00029  * creation ...: 04.11.99
00030  * revisions ..: $Id: ltiSemaphore.h,v 1.6 2006/02/08 12:54:38 ltilib Exp $
00031  */
00032 
00033 #ifndef _LTI_SEMAPHORE_H_
00034 #define _LTI_SEMAPHORE_H_
00035 
00036 #include "ltiObject.h"
00037 
00038 #ifndef _LTI_WIN32
00039 #  include <semaphore.h>
00040 #else
00041 #  include <windows.h>
00042 #  include <process.h>
00043 #  define SEM_VALUE_MAX 16777215 // (2^32-1)
00044 #endif
00045 
00046 namespace lti {
00047   /** lti semaphore class for inter-thread (not inter-process!) synchronisation
00048       @see lti::mutex, lti::thread
00049    */
00050   class semaphore : public object {
00051   public:
00052     /// default constructor
00053     semaphore(const int initialValue = 1);
00054 
00055     /// destructor
00056     virtual ~semaphore();
00057 
00058     /**
00059      * wait on semaphore, i.e. decrease the value or wait if counter <= 0
00060      *
00061      * @return true if successful, false otherwise
00062      */
00063     bool wait();
00064 
00065     /**
00066      * Try to wait on semaphore, but do not block.
00067      *
00068      * @return true if value was decreased.
00069      */
00070     bool tryWait();
00071 
00072     /**
00073      * post semaphore, i.e. increase the value
00074      *
00075      * @return true if successful, false otherwise
00076      */
00077     bool post();
00078 
00079     /// get current value
00080     int getValue();
00081 
00082     /// reset value to initialValue
00083     void reset();
00084 
00085     /// returns the name of this type
00086     virtual const char* getTypeName() const;
00087 
00088   protected:
00089     void destroy();
00090 
00091     int initValue;
00092 
00093 #if defined(_LTI_MACOSX)
00094     sem_t *osxSemaphore;
00095     int osxSemNumber;
00096     char myName[40];
00097 #elif defined(_LTI_WIN32)
00098     /// the WIN32 semaphore object
00099     HANDLE theSemaphore;
00100     int counter;
00101 #else 
00102     /// the posix semaphore
00103     sem_t theSemaphore;
00104 # endif
00105   };
00106 }
00107 
00108 #endif

Generated on Sat Apr 10 15:26:08 2010 for LTI-Lib by Doxygen 1.6.1