IVT
Mutex.cpp
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: Mutex.cpp
37 // Author: Kai Welke
38 // Date: 13.01.2006
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "Mutex.h"
49 
50 
51 
52 // ****************************************************************************
53 // Constructor / Destructor
54 // ****************************************************************************
55 
57 {
58 #ifdef WIN32
59  InitializeCriticalSection(&m_CSWindowsMutex);
60 #else
61  pthread_mutexattr_init(&m_MutexAttr);
62  pthread_mutex_init(&m_PosixMutex ,&m_MutexAttr);
63 #endif
64 }
65 
67 {
68 #ifdef WIN32
69  DeleteCriticalSection(&m_CSWindowsMutex);
70 #else
71  pthread_mutex_destroy(&m_PosixMutex);
72  pthread_mutexattr_destroy(&m_MutexAttr);
73 #endif
74 }
75 
76 
77 // ****************************************************************************
78 // Methods
79 // ****************************************************************************
80 
81 // try to lock the mutex. If another thread is owner of mutex, wait till mutex is unlocked
83 {
84 #ifdef WIN32
85  EnterCriticalSection(&m_CSWindowsMutex);
87 #else
88  if (pthread_mutex_lock(&m_PosixMutex) == 0)
90  else
92 #endif
93 }
94 
95 // try to lock the mutex. If another thread is owner of mutex, return EBusy, otherwise lock mutex
97 {
98 #ifdef WIN32
99  if (TryEnterCriticalSection(&m_CSWindowsMutex) == 0)
100  {
101  return Threading::eMutexBusy;
102  }
103  else
104  {
106  }
107 #else
108  const int nResult = pthread_mutex_trylock(&m_PosixMutex);
109 
110  if (nResult == 0)
111  {
113  }
114  else if (nResult == EBUSY)
115  {
116  return Threading::eMutexBusy;
117  }
118  else
119  {
120  return Threading::eMutexError;
121  }
122 #endif
123 }
124 
125 // unlock mutex
127 {
128 #ifdef WIN32
129  LeaveCriticalSection(&m_CSWindowsMutex);
131 #else
132  if (pthread_mutex_unlock(&m_PosixMutex) == 0)
133  {
135  }
136  else
137  {
138  return Threading::eMutexError;
139  }
140 #endif
141 }
Threading::EMutexStatus Lock()
Definition: Mutex.cpp:82
~CMutex()
Definition: Mutex.cpp:66
Threading::EMutexStatus TryLock()
Definition: Mutex.cpp:96
CMutex()
Definition: Mutex.cpp:56
Threading::EMutexStatus UnLock()
Definition: Mutex.cpp:126