]> git.stg.codes - stg.git/blob - stglibs/locker.lib/include/stg/locker.h
51ceb738e53253cfec1a2e9dd54605a20cf997a9
[stg.git] / stglibs / locker.lib / 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 #ifdef DEBUG_LOCKER
34
35 #include <iostream>
36 #include <string>
37 #include <pthread.h>
38
39 #endif
40
41 //-----------------------------------------------------------------------------
42 class STG_LOCKER
43 {
44 public:
45     #ifdef DEBUG_LOCKER
46     STG_LOCKER(pthread_mutex_t * m, const char * __file__, int __line__)
47         : mutex(m),
48           file(__file__),
49           line(__line__),
50           lockerMutex(),
51           lockID(0)
52     #else
53     STG_LOCKER(pthread_mutex_t * m, const char *, int)
54         : mutex(m)
55     #endif
56         {
57         mutex = m;
58         #ifdef DEBUG_LOCKER
59         pthread_mutex_lock(&lockerMutex);
60         file = __file__;
61         line = __line__;
62         if (id == 0)
63             pthread_mutex_init(&lockerMutex, NULL);
64
65         lockID = ++id;
66         std::cout << "Lock: " << lockID << " " << file << ":" << line << " " << mutex << " " << pthread_self() << std::endl;
67         pthread_mutex_unlock(&lockerMutex);
68         #endif
69         pthread_mutex_lock(mutex);
70         }
71
72     ~STG_LOCKER()
73         {
74         pthread_mutex_unlock(mutex);
75         #ifdef DEBUG_LOCKER
76         pthread_mutex_lock(&lockerMutex);
77         std::cout << "Unlock: " << lockID << " " << file << ":" << line << " " << mutex << " " << pthread_self() << std::endl;
78         pthread_mutex_unlock(&lockerMutex);
79         #endif
80         }
81 private:
82     STG_LOCKER(const STG_LOCKER & rvalue);
83     STG_LOCKER & operator=(const STG_LOCKER & rvalue);
84
85     pthread_mutex_t * mutex;
86     #ifdef DEBUG_LOCKER
87     std::string file;
88     int line;
89     static pthread_mutex_t lockerMutex;
90     static long long id;
91     long long lockID;
92     #endif
93 };
94 //-----------------------------------------------------------------------------
95
96 #endif //STG_LOCKER_H