File: spl_mutex.cpp

package info (click to toggle)
libterralib 4.3.0+dfsg.2-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 61,560 kB
  • ctags: 37,110
  • sloc: cpp: 225,052; ansic: 31,562; makefile: 807; sh: 80; xml: 37
file content (143 lines) | stat: -rw-r--r-- 4,131 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

/*!

\section terms_of_use Terms of Use

libspl - the simple plugin layer library.
Copyright (C) 2004 Andreas Loeffler and Ren� Stuhr (www.unitedbytes.de).

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

\section author Author
Ren� Stuhr (www.unitedbytes.de)

*/

//---------------------------------------------------------------------------------------------------------------------------
/// @file		spl_mutex.cpp
/// @version	1.0
/// @author		Ren� Stuhr
//---------------------------------------------------------------------------------------------------------------------------

#ifndef		__SPL_MUTEX_H__
#include	"spl_mutex.h"
#endif

#ifndef		__SPL_TYPEDEFS_H__
#include	"spl_typedefs.h"
#endif

#include <stdio.h>

using namespace SPL;

slcMutex::slcMutex( void )
{
#if	(SPL_PLATFORM == SPL_PLATFORM_LINUX) || (SPL_PLATFORM == SPL_PLATFORM_HURD) || (SPL_PLATFORM == SPL_PLATFORM_KFREEBSD)

	pthread_mutexattr_t access_attr;
	pthread_mutexattr_init( &access_attr );
	pthread_mutexattr_settype( &access_attr, PTHREAD_MUTEX_RECURSIVE );
	pthread_mutex_init( &this->m_access, &access_attr );

#elif	SPL_PLATFORM == SPL_PLATFORM_WIN32	

	this->m_sa.nLength = sizeof( m_sa );
	this->m_sa.lpSecurityDescriptor = NULL;
	this->m_sa.bInheritHandle = TRUE;

	//
	// CREATE A RANDOM MUTEX UUID
	//
	char szUUID[ 41 ] = { 0 };
	SYSTEMTIME st;
	::GetSystemTime( &st );
	sprintf( szUUID, "UUID:%02d:%02d:%02d:%03d:%02d:%02d:%02d:%04d", 
		st.wDay, st.wDayOfWeek, st.wHour, st.wMilliseconds, st.wMinute,
		st.wMonth, st.wSecond, st.wYear );

	this->m_access = ::CreateMutex( &this->m_sa, FALSE, szUUID );

#endif
}

slcMutex::~slcMutex( void )
{
#if	(SPL_PLATFORM == SPL_PLATFORM_LINUX) || (SPL_PLATFORM == SPL_PLATFORM_HURD) || (SPL_PLATFORM == SPL_PLATFORM_KFREEBSD)

	pthread_mutex_destroy( &this->m_access );

#elif	SPL_PLATFORM == SPL_PLATFORM_WIN32	

	::CloseHandle( this->m_access );

#endif
}

void slcMutex::Lock( void )
{
#if	(SPL_PLATFORM == SPL_PLATFORM_LINUX)  || (SPL_PLATFORM == SPL_PLATFORM_HURD) || (SPL_PLATFORM == SPL_PLATFORM_KFREEBSD)

	pthread_mutex_lock( &this->m_access );

#elif	SPL_PLATFORM == SPL_PLATFORM_WIN32	

	::WaitForSingleObject( this->m_access, INFINITE );

#endif
}

long slcMutex::TryLock( void )
{
	// 
	// Set default to signaled.
	//
	long lResult = 0;

#if	(SPL_PLATFORM == SPL_PLATFORM_LINUX) || (SPL_PLATFORM == SPL_PLATFORM_HURD) || (SPL_PLATFORM == SPL_PLATFORM_KFREEBSD)

	//
	// Check if object signaled and lock if success.
	// If object is non signaled return busy otherwise object is now locked.
	//
	if( ( lResult = pthread_mutex_trylock( &this->m_access ) ) == EBUSY )
		 return EBUSY;		///< Return busy.

#elif	SPL_PLATFORM == SPL_PLATFORM_WIN32	

	//
	// Wait 10 ms of signaled state and lock if success.
	// If object is non signaled return busy otherwise object is now locked.
	//
	if( ( lResult = ::WaitForSingleObject( this->m_access, 10 ) ) == WAIT_TIMEOUT )
		return EBUSY;			///< Return busy.

#endif

	return lResult;		///< If 0 object is successfully locked otherwise error, return result.
}

void slcMutex::Unlock( void )
{
#if (SPL_PLATFORM == SPL_PLATFORM_LINUX) || (SPL_PLATFORM == SPL_PLATFORM_HURD) || (SPL_PLATFORM == SPL_PLATFORM_KFREEBSD)

	pthread_mutex_unlock( &this->m_access );

#elif	SPL_PLATFORM == SPL_PLATFORM_WIN32	

	::ReleaseMutex( this->m_access );

#endif
}