source: trunk/src/thread/threadbase.h @ 1095

Last change on this file since 1095 was 1095, checked in by gary, 9 years ago

win32: implement threads and mutexes.

  • Property svn:keywords set to Id
File size: 3.3 KB
Line 
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2011 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
7//   Public License, Version 2, as published by Sam Hocevar. See
8//   http://sam.zoy.org/projects/COPYING.WTFPL for more details.
9//
10
11//
12// The ThreadBase class
13// --------------------
14//
15
16#if !defined __LOL_THREADBASE_H__
17#define __LOL_THREADBASE_H__
18
19#if defined __linux__ || defined __native_client__
20#   include <pthread.h>
21#elif defined _WIN32
22#   include <windows.h>
23#else
24#   error No threading support yet :(
25#endif
26
27namespace lol
28{
29
30class MutexBase
31{
32public:
33    MutexBase()
34    {
35#if defined __linux__ || defined __native_client__
36        pthread_mutex_init(&m_mutex, NULL);
37#elif defined _WIN32
38        InitializeCriticalSection(&m_mutex);
39#endif
40    }
41
42    ~MutexBase()
43    {
44#if defined __linux__ || defined __native_client__
45        pthread_mutex_destroy(&m_mutex);
46#elif defined _WIN32
47        DeleteCriticalSection(&m_mutex);
48#endif
49    }
50
51    void Lock()
52    {
53#if defined __linux__ || defined __native_client__
54        pthread_mutex_lock(&m_mutex);
55#elif defined _WIN32
56        EnterCriticalSection(&m_mutex);
57#endif
58    }
59
60    void Unlock()
61    {
62#if defined __linux__ || defined __native_client__
63        pthread_mutex_unlock(&m_mutex);
64#elif defined _WIN32
65        LeaveCriticalSection(&m_mutex);
66#endif
67    }
68
69private:
70#if defined __linux__ || defined __native_client__
71    pthread_mutex_t m_mutex;
72#elif defined _WIN32
73    CRITICAL_SECTION m_mutex;
74#endif
75};
76
77class ConditionBase
78{
79public:
80    ConditionBase()
81    {
82#if defined __linux__ || defined __native_client__
83        pthread_mutex_init(&m_mutex, NULL);
84        pthread_cond_init(&m_cond, NULL);
85#endif
86    }
87
88    ~ConditionBase()
89    {
90#if defined __linux__ || defined __native_client__
91        pthread_cond_destroy(&m_cond);
92        pthread_mutex_destroy(&m_mutex);
93#endif
94    }
95
96    void Acquire()
97    {
98#if defined __linux__ || defined __native_client__
99        pthread_mutex_lock(&m_mutex);
100#endif
101    }
102
103    void Release()
104    {
105#if defined __linux__ || defined __native_client__
106        pthread_mutex_unlock(&m_mutex);
107#endif
108    }
109
110    void Wait()
111    {
112#if defined __linux__ || defined __native_client__
113        pthread_cond_wait(&m_cond, &m_mutex);
114#endif
115    }
116
117    void Notify()
118    {
119#if defined __linux__ || defined __native_client__
120        pthread_cond_signal(&m_cond);
121#endif
122    }
123
124private:
125#if defined __linux__ || defined __native_client__
126    pthread_mutex_t m_mutex;
127    pthread_cond_t m_cond;
128#endif
129};
130
131class ThreadBase
132{
133public:
134    ThreadBase(void *(*fn)(void *), void *data)
135    {
136#if defined __linux__ || defined __native_client__
137        pthread_create(&m_thread, NULL, fn, data);
138#elif defined _WIN32
139        m_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)fn, data, 0, &m_tid);
140#endif
141    }
142
143    virtual ~ThreadBase()
144    {
145#if defined __linux__ || defined __native_client__
146        pthread_join(m_thread, NULL);
147#elif defined _WIN32
148        WaitForSingleObject(m_thread, INFINITE);
149#endif
150    }
151
152private:
153#if defined __linux__ || defined __native_client__
154    pthread_t m_thread;
155#elif defined _WIN32
156    HANDLE m_thread;
157    DWORD m_tid;
158#endif
159};
160
161} /* namespace lol */
162
163#endif // __LOL_THREADBASE_H__
164
Note: See TracBrowser for help on using the repository browser.