libmoldeo (Moldeo 1.0 Core)  1.0
libmoldeo is the group of objects and functions that executes the basic operations of Moldeo 1.0 Platform.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
moLock.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2 
3  moLock.cpp
4 
5  ****************************************************************************
6  * *
7  * This source is free software; you can redistribute it and/or modify *
8  * it under the terms of the GNU General Public License as published by *
9  * the Free Software Foundation; either version 2 of the License, or *
10  * (at your option) any later version. *
11  * *
12  * This code is distributed in the hope that it will be useful, but *
13  * WITHOUT ANY WARRANTY; without even the implied warranty of *
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
15  * General Public License for more details. *
16  * *
17  * A copy of the GNU General Public License is available on the World *
18  * Wide Web at <http://www.gnu.org/copyleft/gpl.html>. You can also *
19  * obtain it by writing to the Free Software Foundation, *
20  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
21  * *
22  ****************************************************************************
23 
24  Copyright(C) 2006 Fabricio Costa
25 
26  Authors:
27  Fabricio Costa
28 
29 
30 *******************************************************************************/
31 
32 #include "moLock.h"
33 
34 #define SDLTHREADS 1
35 
36 #ifdef MO_WIN32
37  //we use plain WINAPI code: CreateMutex, ReleaseMutex, WaitForSingleObject, ...
38 #endif
39 
40 #ifdef MO_LINUX
41  #ifdef SDLTHREADS
42  #include "SDL/SDL_thread.h"
43  #endif
44 
45  #include "pthread.h"
46 #endif
47 
48 #ifdef MO_MACOSX
49  //#include "SDL/SDL_thread.h"
50 #endif
51 
52 
54 
55  m_lock = NULL;
56 
57 #ifndef MO_WIN32
58  #ifdef SDLTHREADS
59  m_lock = (void*) SDL_CreateMutex();
60  #else
61  pthread_mutexattr_t attr;
62 
63  pthread_mutex_init( (pthread_mutex_t*)&m_lock, &attr);
64 
65  pthread_mutexattr_init(&attr);
66  pthread_mutexattr_setrobust(&attr, PTHREAD_MUTEX_ROBUST);
67  pthread_mutexattr_settype( &attr, PTHREAD_MUTEX_RECURSIVE );
68  if ( pthread_mutex_init((pthread_mutex_t*)&m_lock, &attr) != 0 ) {
69  cout << "couldn create mutex and init with PTHREAD_MUTEX_RECURSIVE" << endl;
70  exit(1);
71  }
72  #endif
73 #else
74  #ifdef UNICODE
75  m_lock = (void*)CreateMutex( NULL, FALSE, L"lockMutex" );
76  #else
77  m_lock = (void*)CreateMutex( NULL, FALSE, "lockMutex" );
78  #endif
79 #endif
80 
81 }
82 
84 
85 #ifndef WIN32
86  #ifdef SDLTHREADS
87  SDL_DestroyMutex((SDL_mutex*)m_lock);
88  #else
89  if (m_lock!=NULL) {
90  pthread_mutex_destroy( (pthread_mutex_t*)&m_lock );
91  }
92  #endif
93 #else
94  CloseHandle(m_lock);
95 #endif
96 
97  m_lock = NULL;
98 }
99 
100 bool
102 
103  if (m_lock==NULL) {
104  return false;
105  }
106 
107 #ifndef WIN32
108  #ifdef SDLTHREADS
109  return(SDL_mutexP( (SDL_mutex*)m_lock )!=-1);
110  #else
111  if (pthread_mutex_lock( (pthread_mutex_t*)&m_lock ) < 0 ) {
112  return false;
113  }
114  #endif
115 #else
116  WaitForSingleObject( m_lock, INFINITE);
117 #endif
118 
119 /*
120  #ifdef MO_WIN32
121  timed_mutex *pMutex = (timed_mutex*) m_mutex;
122  timed_mutex::scoped_timed_lock *pLock = (timed_mutex::scoped_timed_lock *) m_lock;
123 
124  try {
125  //pLock->timed_lock();
126  pLock->lock();
127  }
128  catch(...) {
129  return false;
130  }
131  #endif
132 */
133  return true;
134 }
135 
136 bool
138 
139  if (m_lock==NULL) {
140  return false;
141  }
142 
143 #ifndef WIN32
144  #ifdef SDLTHREADS
145  return(SDL_mutexV( (SDL_mutex*)m_lock )!=-1);
146  #else
147  if (pthread_mutex_unlock( (pthread_mutex_t*)&m_lock )<0) {
148  return false;
149  }
150  #endif
151 #else
152  return ReleaseMutex( m_lock );
153 #endif
154 
155  return true;
156 }
157 
bool Unlock()
Definition: moLock.cpp:137
moLock()
Definition: moLock.cpp:53
bool Lock()
Definition: moLock.cpp:101
~moLock()
Definition: moLock.cpp:83