Imported Upstream version 3.0
[debian/gnuradio] / gr-audio-osx / src / mld_threads.h
1 /* -*- c++ -*- */
2 /*
3  * Copyright 2006 Free Software Foundation, Inc.
4  * 
5  * This file is part of GNU Radio.
6  *
7  * GNU Radio 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, or (at your option)
10  * any later version.
11  * 
12  * GNU Radio is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with GNU Radio; see the file COPYING.  If not, write to
19  * the Free Software Foundation, Inc., 51 Franklin Street,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 #ifndef _INCLUDED_MLD_THREADS_H_
24 #define _INCLUDED_MLD_THREADS_H_
25
26 /* classes which allow for either pthreads or omni_threads */
27
28 #ifdef _USE_OMNI_THREADS_
29 #include <omnithread.h>
30 #else
31 #include <pthread.h>
32 #endif
33
34 #include <stdexcept>
35
36 #define __INLINE__ inline
37
38 class mld_condition_t;
39
40 class mld_mutex_t {
41 #ifdef _USE_OMNI_THREADS_
42   typedef omni_mutex l_mutex, *l_mutex_ptr;
43 #else
44   typedef pthread_mutex_t l_mutex, *l_mutex_ptr;
45 #endif
46
47   friend class mld_condition_t;
48
49 private:
50   l_mutex_ptr d_mutex;
51
52 protected:
53   inline l_mutex_ptr mutex () { return (d_mutex); };
54
55 public:
56   __INLINE__ mld_mutex_t () {
57 #ifdef _USE_OMNI_THREADS_
58     d_mutex = new omni_mutex ();
59 #else
60     d_mutex = (l_mutex_ptr) new l_mutex;
61     int l_ret = pthread_mutex_init (d_mutex, NULL);
62     if (l_ret != 0) {
63       fprintf (stderr, "Error %d creating mutex.\n", l_ret);
64       throw std::runtime_error ("mld_mutex_t::mld_mutex_t()\n");
65     }
66 #endif
67   };
68
69   __INLINE__ ~mld_mutex_t () {
70     unlock ();
71 #ifndef _USE_OMNI_THREADS_
72     int l_ret = pthread_mutex_destroy (d_mutex);
73     if (l_ret != 0) {
74       fprintf (stderr, "mld_mutex_t::~mld_mutex_t(): "
75                "Error %d destroying mutex.\n", l_ret);
76     }
77 #endif
78     delete d_mutex;
79     d_mutex = NULL;
80   };
81
82   __INLINE__ void lock () {
83 #ifdef _USE_OMNI_THREADS_
84     d_mutex->lock ();
85 #else
86     int l_ret = pthread_mutex_lock (d_mutex);
87     if (l_ret != 0) {
88       fprintf (stderr, "mld_mutex_t::lock(): "
89                "Error %d locking mutex.\n", l_ret);
90     }
91 #endif
92   };
93
94   __INLINE__ void unlock () {
95 #ifdef _USE_OMNI_THREADS_
96     d_mutex->unlock ();
97 #else
98     int l_ret = pthread_mutex_unlock (d_mutex);
99     if (l_ret != 0) {
100       fprintf (stderr, "mld_mutex_t::unlock(): "
101                "Error %d locking mutex.\n", l_ret);
102     }
103 #endif
104   };
105
106   __INLINE__ bool trylock () {
107 #ifdef _USE_OMNI_THREADS_
108     int l_ret = d_mutex->trylock ();
109 #else
110     int l_ret = pthread_mutex_unlock (d_mutex);
111 #endif
112     return (l_ret == 0 ? true : false);
113   };
114
115   inline void acquire () { lock(); };
116   inline void release () { unlock(); };
117   inline void wait () { lock(); };
118   inline void post () { unlock(); };
119 };
120
121 typedef mld_mutex_t mld_mutex, *mld_mutex_ptr;
122
123 class mld_condition_t {
124 #ifdef _USE_OMNI_THREADS_
125   typedef omni_condition l_condition, *l_condition_ptr;
126 #else
127   typedef pthread_cond_t l_condition, *l_condition_ptr;
128 #endif
129
130 private:
131   l_condition_ptr d_condition;
132   mld_mutex_ptr d_mutex;
133   bool d_waiting;
134
135 public:
136   __INLINE__ mld_condition_t () {
137     d_waiting = false;
138     d_mutex = new mld_mutex ();
139 #ifdef _USE_OMNI_THREADS_
140     d_condition = new omni_condition (d_mutex->mutex ());
141 #else
142     d_condition = (l_condition_ptr) new l_condition;
143     int l_ret = pthread_cond_init (d_condition, NULL);
144     if (l_ret != 0) {
145       fprintf (stderr, "Error %d creating condition.\n", l_ret);
146       throw std::runtime_error ("mld_condition_t::mld_condition_t()\n");
147     }
148 #endif
149   };
150
151   __INLINE__ ~mld_condition_t () {
152     signal ();
153 #ifndef _USE_OMNI_THREADS_
154     int l_ret = pthread_cond_destroy (d_condition);
155     if (l_ret != 0) {
156       fprintf (stderr, "mld_condition_t::mld_condition_t(): "
157                "Error %d destroying condition.\n", l_ret);
158     }
159 #endif
160     delete d_condition;
161     d_condition = NULL;
162     delete d_mutex;
163     d_mutex = NULL;
164   };
165
166   __INLINE__ void signal () {
167     if (d_waiting == true) {
168 #ifdef _USE_OMNI_THREADS_
169       d_condition->signal ();
170 #else
171       int l_ret = pthread_cond_signal (d_condition);
172       if (l_ret != 0) {
173         fprintf (stderr, "mld_condition_t::signal(): "
174                  "Error %d.\n", l_ret);
175       }
176 #endif
177       d_waiting = false;
178     }
179   };
180
181   __INLINE__ void wait () {
182     if (d_waiting == false) {
183       d_waiting = true;
184 #ifdef _USE_OMNI_THREADS_
185       d_condition->wait ();
186 #else
187       int l_ret = pthread_cond_wait (d_condition, d_mutex->mutex ());
188       if (l_ret != 0) {
189         fprintf (stderr, "mld_condition_t::wait(): "
190                  "Error %d.\n", l_ret);
191       }
192 #endif
193     }
194   };
195 };
196
197 typedef mld_condition_t mld_condition, *mld_condition_ptr;
198
199 class mld_thread_t {
200 #ifdef _USE_OMNI_THREADS_
201   typedef omni_thread l_thread, *l_thread_ptr;
202 #else
203   typedef pthread_t l_thread, *l_thread_ptr;
204 #endif
205
206 private:
207 #ifndef _USE_OMNI_THREADS_
208   l_thread d_thread;
209   void (*d_start_routine)(void*);
210   void *d_arg;
211 #else
212   l_thread_ptr d_thread;
213 #endif
214
215 #ifndef _USE_OMNI_THREADS_
216   static void* local_start_routine (void *arg) {
217     mld_thread_t* This = (mld_thread_t*) arg;
218     (*(This->d_start_routine))(This->d_arg);
219     return (NULL);
220   };
221 #endif
222
223 public:
224   __INLINE__ mld_thread_t (void (*start_routine)(void *), void *arg) {
225 #ifdef _USE_OMNI_THREADS_
226     d_thread = new omni_thread (start_routine, arg);
227     d_thread->start ();
228 #else
229     d_start_routine = start_routine;
230     d_arg = arg;
231     int l_ret = pthread_create (&d_thread, NULL, local_start_routine, this);
232     if (l_ret != 0) {
233       fprintf (stderr, "Error %d creating thread.\n", l_ret);
234       throw std::runtime_error ("mld_thread_t::mld_thread_t()\n");
235     }
236 #endif
237   };
238
239   __INLINE__ ~mld_thread_t () {
240 #ifdef _USE_OMNI_THREADS_
241 //  delete d_thread;
242     d_thread = NULL;
243 #else
244     int l_ret = pthread_detach (d_thread);
245     if (l_ret != 0) {
246       fprintf (stderr, "Error %d detaching thread.\n", l_ret);
247       throw std::runtime_error ("mld_thread_t::~mld_thread_t()\n");
248     }
249 #endif
250   };
251 };
252
253 typedef mld_thread_t mld_thread, *mld_thread_ptr;
254
255 #endif /* _INCLUDED_MLD_THREADS_H_ */