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

Last change on this file since 1093 was 1093, checked in by sam, 9 years ago

core: add a mediocre threading system as the base for the real thing.

  • Property svn:keywords set to Id
File size: 2.7 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#else
22#   error No threading support yet :(
23#endif
24
25namespace lol
26{
27
28class MutexBase
29{
30public:
31    MutexBase()
32    {
33#if defined __linux__ || defined __native_client__
34        pthread_mutex_init(&m_mutex, NULL);
35#endif
36    }
37
38    ~MutexBase()
39    {
40#if defined __linux__ || defined __native_client__
41        pthread_mutex_destroy(&m_mutex);
42#endif
43    }
44
45    void Lock()
46    {
47#if defined __linux__ || defined __native_client__
48        pthread_mutex_lock(&m_mutex);
49#endif
50    }
51
52    void Unlock()
53    {
54#if defined __linux__ || defined __native_client__
55        pthread_mutex_unlock(&m_mutex);
56#endif
57    }
58
59private:
60#if defined __linux__ || defined __native_client__
61    pthread_mutex_t m_mutex;
62#endif
63};
64
65class ConditionBase
66{
67public:
68    ConditionBase()
69    {
70#if defined __linux__ || defined __native_client__
71        pthread_mutex_init(&m_mutex, NULL);
72        pthread_cond_init(&m_cond, NULL);
73#endif
74    }
75
76    ~ConditionBase()
77    {
78#if defined __linux__ || defined __native_client__
79        pthread_cond_destroy(&m_cond);
80        pthread_mutex_destroy(&m_mutex);
81#endif
82    }
83
84    void Acquire()
85    {
86#if defined __linux__ || defined __native_client__
87        pthread_mutex_lock(&m_mutex);
88#endif
89    }
90
91    void Release()
92    {
93#if defined __linux__ || defined __native_client__
94        pthread_mutex_unlock(&m_mutex);
95#endif
96    }
97
98    void Wait()
99    {
100#if defined __linux__ || defined __native_client__
101        pthread_cond_wait(&m_cond, &m_mutex);
102#endif
103    }
104
105    void Notify()
106    {
107#if defined __linux__ || defined __native_client__
108        pthread_cond_signal(&m_cond);
109#endif
110    }
111
112private:
113#if defined __linux__ || defined __native_client__
114    pthread_mutex_t m_mutex;
115    pthread_cond_t m_cond;
116#endif
117};
118
119class ThreadBase
120{
121public:
122    ThreadBase(void *(*fn)(void *), void *data)
123    {
124#if defined __linux__ || defined __native_client__
125        pthread_create(&m_thread, NULL, fn, data);
126#endif
127    }
128
129    virtual ~ThreadBase()
130    {
131#if defined __linux__ || defined __native_client__
132        pthread_join(m_thread, NULL);
133#endif
134    }
135
136private:
137#if defined __linux__ || defined __native_client__
138    pthread_t m_thread;
139#endif
140};
141
142} /* namespace lol */
143
144#endif // __LOL_THREADBASE_H__
145
Note: See TracBrowser for help on using the repository browser.