SemaphoreCommon.hpp

00001 #if   !defined(__SEMAPHORECOMMON_HPP)
00002 #define __SEMAPHORECOMMON_HPP
00003 
00004 /*
00005   CoreLinux++ 
00006   Copyright (C) 1999,2000 CoreLinux Consortium
00007   
00008    The CoreLinux++ Library is free software; you can redistribute it and/or
00009    modify it under the terms of the GNU Library General Public License as
00010    published by the Free Software Foundation; either version 2 of the
00011    License, or (at your option) any later version.
00012 
00013    The CoreLinux++ Library Library is distributed in the hope that it will 
00014    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016    Library General Public License for more details.
00017 
00018    You should have received a copy of the GNU Library General Public
00019    License along with the GNU C Library; see the file COPYING.LIB.  If not,
00020    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00021    Boston, MA 02111-1307, USA.  
00022 */
00023 
00024 #if !defined(__COMMON_HPP)
00025 #include <Common.hpp>
00026 #endif
00027 
00028 namespace corelinux
00029 {
00034    struct   _CSAHeader
00035    {
00036       Int      creatorId;        // 0 if not opened before
00037       Int      currentUsed;      // The current used up in the pool
00038       Int      currentGrps;      // The current groups present
00039       Int      reserved;         // 
00040    };
00041 
00042    DECLARE_TYPE( struct _CSAHeader, CSAHeader );
00043 
00048    struct   _CSAGroupHeader
00049    {
00050       Int   groupInfo;           // Either an Id, 0 if tail, or -1 if available
00051       Int   groupShares;         // How many processes are using this
00052       Int   groupType;           // -1 not used, 0 Mutex, 1 Gateway, 2-10 res
00053       Int   groupSemCount;       // Semaphore count if info is -1 
00054       // and not reclaimed | !0 
00055    };
00056 
00057    DECLARE_TYPE( struct _CSAGroupHeader, CSAGrpHeader );
00058 
00063    struct   _CSASemaphoreHeader
00064    {
00065       Int   semOwner;            // Current semaphore owner
00066       Int   maxSemValue;         // 1 for mutexes, n for others, -1 if control
00067       Word  isRecursive;         // ? for instance, 0 for control
00068       Word  isBalking;           // ? for instance, 0 for control
00069       Int   semShares;           // Shares for this semaphore
00070    };
00071 
00072    DECLARE_TYPE( struct _CSASemaphoreHeader, CSASemHeader );
00073 
00074    DECLARE_CLASS( CoreLinuxGuardPool );
00075 
00076    DECLARE_CLASS( SemaphoreGroup );
00077 
00078    DECLARE_CLASS( MemoryStorage );
00079  
00080    DECLARE_CLASS( SemaphoreCommon );
00081 
00089    class SemaphoreCommon : public Synchronized
00090    {
00091    public:
00092 
00093       //
00094       // Accessors
00095       //
00096 
00104       static   Int   getSemaphoreMaxValue( SemaphoreGroupPtr, Int );
00105 
00106       //
00107       // Mutators
00108       //
00117       static   void  groupDefined( SemaphoreGroupPtr );
00118 
00127       static   Int   groupUnDefined( SemaphoreGroupPtr );
00128 
00139       static   Int   setLock( SemaphoreGroupPtr, Int, Int, Int );
00140 
00151       static   Int   setUnLock( SemaphoreGroupPtr, Int, Int, Int );
00152 
00163       static   Int   waitZero( SemaphoreGroupPtr, Int, Int, Int );
00164 
00176       static   Int   obtainSemaphore
00177          (
00178             SemaphoreGroupPtr,
00179             Int,
00180             IntRef,
00181             IntRef,
00182             IntRef,
00183             Int
00184          );
00185                
00195       static   Int   relinquishSemaphore(SemaphoreGroupPtr,Int);
00196 
00205       static   Int   setMaxValue( SemaphoreGroupPtr, Int, Int );
00206 
00207 
00208 
00209    protected:
00210 
00212 
00213     SemaphoreCommon( void );
00214 
00216 
00217     virtual           ~SemaphoreCommon( void );
00218 
00219     //
00220     // Instance mutators
00221     //
00222 
00229     void  registerGroup( SemaphoreGroupPtr );
00230 
00237     Int   deregisterGroup( SemaphoreGroupPtr );
00238 
00250     Int   claimSemaphore
00251     (
00252      SemaphoreGroupPtr,
00253      Int,
00254      IntRef,
00255      IntRef,
00256      IntRef,
00257      Int
00258      );
00259 
00268     Int   reclaimSemaphore(SemaphoreGroupPtr,Int);
00269 
00270 
00272 
00273     CSAGrpHeaderPtr   findGroup
00274     ( 
00275      IntCref , 
00276      IntCref , 
00277      CSAGrpHeaderPtr 
00278      );
00279 
00281 
00282     CSAGrpHeaderPtr   findAvailableGroup
00283     ( 
00284      IntCref , 
00285      IntCref , 
00286      CSAGrpHeaderPtr 
00287      );
00288 
00290 
00291     CSAGrpHeaderPtr   subsetGroup( Int, CSAGrpHeaderPtr ) ;
00292 
00293     CSAGrpHeaderPtr   combineGroup( Int, CSAGrpHeaderPtr ) ;
00294 
00295     bool  isOriginator( void ) const;
00296 
00297     Int   getOriginatorId( void ) const;
00298 
00299     Int   canonicalUndefined( void );
00300 
00301 
00303 
00304     static   void  createAttachment( void );
00305 
00306     friend   class CoreLinuxGuardPool;
00307 
00309 
00310     static   void  exitAttachment( void );
00311 
00312 
00313   private:
00314 
00315     SemaphoreCommon( SemaphoreCommonCref )
00316       throw ( Assertion )
00317       :
00318       Synchronized()
00319     {
00320       NEVER_GET_HERE;
00321     }
00322 
00323   private:
00324 
00325     MemoryStoragePtr     theCSA;
00326     CSAHeaderPtr         theBase;
00327     bool                 theOriginator;
00328     static   SemaphoreCommonPtr   theInstance;
00329     static   SemaphoreGroupPtr    theControlGroup;
00330     static   bool                 theInitializeFlag;
00331   };
00332 
00333 }
00334 
00335 #endif // if !defined(__SEMAPHORECOMMON_HPP)
00336 
00337 /*
00338    Common rcs information do not modify
00339    $Author: dulimart $
00340    $Revision: 1.9 $
00341    $Date: 2000/09/09 07:06:17 $
00342    $Locker:  $
00343 */
00344 

This is the CoreLinux++ reference manual
Provided by The CoreLinux Consortium