1 /*
2     KHOMP generic endpoint/channel library.
3     Copyright (C) 2007-2009 Khomp Ind. & Com.
4 
5   The contents of this file are subject to the Mozilla Public License Version 1.1
6   (the "License"); you may not use this file except in compliance with the
7   License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
8 
9   Software distributed under the License is distributed on an "AS IS" basis,
10   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
11   the specific language governing rights and limitations under the License.
12 
13   Alternatively, the contents of this file may be used under the terms of the
14   "GNU Lesser General Public License 2.1" license (the “LGPL" License), in which
15   case the provisions of "LGPL License" are applicable instead of those above.
16 
17   If you wish to allow use of your version of this file only under the terms of
18   the LGPL License and not to allow others to use your version of this file under
19   the MPL, indicate your decision by deleting the provisions above and replace them
20   with the notice and other provisions required by the LGPL License. If you do not
21   delete the provisions above, a recipient may use your version of this file under
22   either the MPL or the LGPL License.
23 
24   The LGPL header follows below:
25 
26     This library is free software; you can redistribute it and/or
27     modify it under the terms of the GNU Lesser General Public
28     License as published by the Free Software Foundation; either
29     version 2.1 of the License, or (at your option) any later version.
30 
31     This library is distributed in the hope that it will be useful,
32     but WITHOUT ANY WARRANTY; without even the implied warranty of
33     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
34     Lesser General Public License for more details.
35 
36     You should have received a copy of the GNU Lesser General Public License
37     along with this library; if not, write to the Free Software Foundation, Inc.,
38     51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
39 
40 */
41 
42 #ifndef _SIMPLE_LOCK_COMMON_HPP_
43 #define _SIMPLE_LOCK_COMMON_HPP_
44 
45 #include <config_commons.hpp>
46 
47 #include <unistd.h>
48 #include <noncopyable.hpp>
49 
50 #include <refcounter.hpp>
51 
52 /* This struct uses static polymorphism, and derived classes should implement  *
53  * the "lock/trylock/unlock()" methods for correct code compilation.           *
54  * The base class also features reference counting, so derived classes should  *
55  * implement the "unreference_data()" method for releasing resources.               */
56 
57 template < typename Implementor >
58 struct SimpleLockCommon: COUNTER_SUPER( SimpleLockCommon < Implementor > )
59 {
60     friend class ReferenceCounter < SimpleLockCommon < Implementor > >;
61 
62     typedef enum
63     {
64         ISINUSE = 0, // operation not succeded (no error)
65         SUCCESS = 1, // operation succeded (no error)
66         FAILURE = 2, // mutex or state is somehow invalid (error! run for your life!)
67     }
68     Result;
69 
SimpleLockCommonSimpleLockCommon70     SimpleLockCommon()
71     {};
72 
SimpleLockCommonSimpleLockCommon73     SimpleLockCommon(const SimpleLockCommon & o)
74     : COUNTER_REFER(o, SimpleLockCommon)
75     {};
76 
~SimpleLockCommonSimpleLockCommon77     virtual ~SimpleLockCommon()
78     {};
79 
lockSimpleLockCommon80     inline Result lock()
81     {
82         return static_cast<Implementor*>(this)->lock();
83     }
84 
trylockSimpleLockCommon85     inline Result trylock()
86     {
87         return static_cast<Implementor*>(this)->trylock();
88     }
89 
unlockSimpleLockCommon90     inline void unlock()
91     {
92         static_cast<Implementor*>(this)->unlock();
93     }
94 
95   protected:
unreferenceSimpleLockCommon96     void unreference(void)
97     {
98         static_cast<Implementor*>(this)->unreference_data();
99     }
100 };
101 
102 #include COMMONS_INCLUDE(simple_lock.hpp)
103 
104 #endif /* _SIMPLE_LOCK_COMMON_HPP_ */
105