root/juggler/tags/1.0.5/Sync/vjGuard.h

Revision 7539, 2.6 kB (checked in by anonymous, 7 years ago)

This commit was manufactured by cvs2svn to create tag
'RELENG_1_0_5_RELEASE'.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 /*************** <auto-copyright.pl BEGIN do not edit this line> **************
2  *
3  * VR Juggler is (C) Copyright 1998, 1999, 2000 by Iowa State University
4  *
5  * Original Authors:
6  *   Allen Bierbaum, Christopher Just,
7  *   Patrick Hartling, Kevin Meinert,
8  *   Carolina Cruz-Neira, Albert Baker
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the
22  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23  * Boston, MA 02111-1307, USA.
24  *
25  * -----------------------------------------------------------------
26  * File:          $RCSfile$
27  * Date modified: $Date$
28  * Version:       $Revision$
29  * -----------------------------------------------------------------
30  *
31  *************** <auto-copyright.pl END do not edit this line> ***************/
32
33
34 #ifndef _VJGuard_h_
35 #define _VJGuard_h_
36 //----------------------------------------------
37 // vjGuard
38 //
39 // Purpose:
40 //    Guard wrapper
41 //
42 //
43 // Author:
44 //      Allen Bierbaum
45 //
46 // Date: 1-31-97
47 //-----------------------------------------------
48
49 #include <vjConfig.h>
50 //#include <Sync/vjSemaphore.h>
51 //#include <Sync/vjMutex.h>
52
53 //: Guard wrapper.
54 //!PUBLIC_API:
55 template <class LOCK_TYPE>
56 class vjGuard
57 {
58 public:
59    //: Acquire the lock implicitly.
60    // If block = 1 then use a blocking acquire
61    vjGuard(LOCK_TYPE &lock, int block = 1)
62    : theLock(&lock)
63    {
64       lockStatus = block ? acquire() : tryAcquire();
65    }
66
67    //: Release the lock
68    ~vjGuard()
69    {
70       if (lockStatus >= 0)
71       {   // != -1
72          theLock->release();
73       }
74    }
75
76    //! RETURNS: 1 - Locked
77    //! RETURNS: 0 - Not locked
78    int locked() {
79       return (lockStatus >= 0);  // != -1
80    }
81
82    //: Acquire the lock
83    int acquire()
84    {
85       return theLock->acquire();
86    }
87
88    //: Try to acquire lock
89    int tryAcquire()
90    {
91       return theLock->tryAcquire();
92    }
93
94    //: Explicity release the lock
95    int release()
96    {
97       return theLock->release();
98    }
99
100
101 private:
102    LOCK_TYPE* theLock;  //: The lock that we are using
103    int        lockStatus;       //: Are we locked or not
104 };
105
106 #endif
Note: See TracBrowser for help on using the browser.