All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
POSIX_Mutex.cpp
Go to the documentation of this file.
1 /*
2  *POSIX_Mutex.cpp
3  *
4  * This file is part of the HausmiSEP project
5  *
6  * Copyright (C) 2012, 2013 Marco Alvarado (malvcr@gmail.com)
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <HSEP/Mutex.h>
23 
24 namespace HSEP {
25 
26  Mutex::Mutex(bool pShared) {
27  pthread_mutexattr_t vMutexAttr;
28  pthread_mutexattr_init(&vMutexAttr);
29  if (pShared) {
30  pthread_mutexattr_setpshared(&vMutexAttr,PTHREAD_PROCESS_SHARED);
31  }
32  else {
33  pthread_mutexattr_setpshared(&vMutexAttr,PTHREAD_PROCESS_PRIVATE);
34  }
35  pthread_mutex_init(&aMutexHandler,&vMutexAttr);
36  pthread_mutexattr_destroy(&vMutexAttr);
37  }
38 
40  pthread_mutex_destroy (&aMutexHandler);
41  }
42 
43  void Mutex::acquire() {
44  pthread_mutex_lock(&aMutexHandler);
45  }
46 
47  void Mutex::release() {
48  pthread_mutex_unlock(&aMutexHandler);
49  }
50 
51  PosixExclusiveScope::PosixExclusiveScope(pthread_mutex_t* pMutex) {
52  aMutex = pMutex;
53  pthread_mutex_lock(aMutex);
54  }
56  pthread_mutex_unlock(aMutex);
57  }
58 
59 } // HSEP namespace
60