]> git.stg.codes - stg.git/blob - include/stg/locker.h
16b0323f3876c78941fe41c2b7faa310741eb5bb
[stg.git] / include / stg / locker.h
1 /*
2  *    This program is free software; you can redistribute it and/or modify
3  *    it under the terms of the GNU General Public License as published by
4  *    the Free Software Foundation; either version 2 of the License, or
5  *    (at your option) any later version.
6  *
7  *    This program is distributed in the hope that it will be useful,
8  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  *    GNU General Public License for more details.
11  *
12  *    You should have received a copy of the GNU General Public License
13  *    along with this program; if not, write to the Free Software
14  *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
15  */
16
17 /*
18  *    Author : Boris Mikhailenko <stg34@stargazer.dp.ua>
19  */
20
21 /*
22  $Revision: 1.5 $
23  $Date: 2010/03/04 11:57:11 $
24  $Author: faust $
25 */
26
27
28 #ifndef STG_LOCKER_H
29 #define STG_LOCKER_H
30
31 #include <pthread.h>
32
33 //-----------------------------------------------------------------------------
34 class STG_LOCKER
35 {
36 public:
37     explicit STG_LOCKER(pthread_mutex_t& m)
38         : mutex(&m)
39         {
40         pthread_mutex_lock(mutex);
41         }
42     explicit STG_LOCKER(pthread_mutex_t * m)
43         : mutex(m)
44         {
45         pthread_mutex_lock(mutex);
46         }
47
48     ~STG_LOCKER()
49         {
50         pthread_mutex_unlock(mutex);
51         }
52 private:
53     STG_LOCKER(const STG_LOCKER & rvalue);
54     STG_LOCKER & operator=(const STG_LOCKER & rvalue);
55
56     pthread_mutex_t * mutex;
57 };
58 //-----------------------------------------------------------------------------
59
60 #endif //STG_LOCKER_H