Pyrogenesis  trunk
ThreadUtil.h
Go to the documentation of this file.
1 /* Copyright (C) 2010 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_THREADUTIL
19 #define INCLUDED_THREADUTIL
20 
22 
23 #ifdef DEBUG_LOCKS
24 
25 #define LOCK_MUTEX(_mutex) STMT( \
26  printf("pthread_mutex_lock: 1 %p [pid:%d]\n", _mutex, pthread_self()); \
27  pthread_mutex_lock(_mutex); \
28  printf("pthread_mutex_lock: 2 %p [pid:%d]\n", _mutex, pthread_self()) \
29 )
30 #define UNLOCK_MUTEX(_mutex) STMT( \
31  pthread_mutex_unlock(_mutex); \
32  printf("pthread_mutex_unlock: %p [pid:%d]\n", _mutex, pthread_self()) \
33 )
34 
35 #else
36 
37 #define LOCK_MUTEX(_mutex) pthread_mutex_lock(_mutex)
38 #define UNLOCK_MUTEX(_mutex) pthread_mutex_unlock(_mutex)
39 
40 #endif
41 
42 /**
43  * A non-recursive mutual exclusion lock.
44  */
45 class CMutex
46 {
48 
49  friend class CScopeLock;
50 
51 public:
53  {
54  int ret = pthread_mutex_init(&m_Mutex, NULL);
55  ENSURE(ret == 0);
56  }
57 
59  {
60  int ret = pthread_mutex_destroy(&m_Mutex);
61  ENSURE(ret == 0);
62  }
63 
64 private:
66 };
67 
68 /**
69  * Locks a CMutex over this object's lifetime.
70  * The mutexes are non-recursive - a single thread locking a mutex more than once
71  * results in undefined behaviour.
72  */
74 {
76 
77 public:
79  m_Mutex(mutex)
80  {
82  }
83 
84  CScopeLock(CMutex& mutex) :
85  m_Mutex(&mutex.m_Mutex)
86  {
88  }
89 
91  {
93  }
94 
95 private:
97 };
98 
99 
100 namespace ThreadUtil
101 {
102 
103 /**
104  * Returns whether the current thread is the 'main' thread
105  * (i.e. matches an earlier call to SetMainThread).
106  */
107 bool IsMainThread();
108 
109 /**
110  * Set the current thread as the 'main' thread.
111  * (This is called during engine initialisation.)
112  */
113 void SetMainThread();
114 
115 }
116 
117 #endif // INCLUDED_THREADUTIL
NONCOPYABLE(CMutex)
void * pthread_mutex_t
Definition: wpthread.h:82
~CScopeLock()
Definition: ThreadUtil.h:90
Locks a CMutex over this object&#39;s lifetime.
Definition: ThreadUtil.h:73
A non-recursive mutual exclusion lock.
Definition: ThreadUtil.h:45
CMutex()
Definition: ThreadUtil.h:52
~CMutex()
Definition: ThreadUtil.h:58
pthread_mutex_t m_Mutex
Definition: ThreadUtil.h:65
#define ENSURE(expr)
ensure the expression <expr> evaluates to non-zero.
Definition: debug.h:287
#define LOCK_MUTEX(_mutex)
Definition: ThreadUtil.h:37
void SetMainThread()
Set the current thread as the &#39;main&#39; thread.
Definition: ThreadUtil.cpp:35
Definition: ThreadUtil.h:100
#define UNLOCK_MUTEX(_mutex)
Definition: ThreadUtil.h:38
int pthread_mutex_init(pthread_mutex_t *m, const pthread_mutexattr_t *)
Definition: wpthread.cpp:323
CScopeLock(CMutex &mutex)
Definition: ThreadUtil.h:84
CScopeLock(pthread_mutex_t *mutex)
Definition: ThreadUtil.h:78
bool IsMainThread()
Returns whether the current thread is the &#39;main&#39; thread (i.e.
Definition: ThreadUtil.cpp:25
int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: wpthread.cpp:312
pthread_mutex_t * m_Mutex
Definition: ThreadUtil.h:96