3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio.
7 * Primary Author: Michael Dickens, NCIP Lab, University of Notre Dame
9 * GNU Radio is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3, or (at your option)
14 * GNU Radio is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with GNU Radio; see the file COPYING. If not, write to
21 * the Free Software Foundation, Inc., 51 Franklin Street,
22 * Boston, MA 02110-1301, USA.
25 #ifndef _INCLUDED_MLD_THREADS_H_
26 #define _INCLUDED_MLD_THREADS_H_
28 /* classes which allow for either pthreads or omni_threads */
31 #ifdef _USE_OMNI_THREADS_
32 #include <gnuradio/omnithread.h>
39 #define __INLINE__ inline
43 #define DEBUG(X) do{X} while(0);
45 #define DEBUG(X) do{} while(0);
48 class mld_condition_t;
51 #ifdef _USE_OMNI_THREADS_
52 typedef omni_mutex l_mutex, *l_mutex_ptr;
54 typedef pthread_mutex_t l_mutex, *l_mutex_ptr;
57 friend class mld_condition_t;
63 inline l_mutex_ptr mutex () { return (d_mutex); };
66 __INLINE__ mld_mutex_t () {
67 #ifdef _USE_OMNI_THREADS_
68 d_mutex = new omni_mutex ();
70 d_mutex = (l_mutex_ptr) new l_mutex;
71 int l_ret = pthread_mutex_init (d_mutex, NULL);
73 fprintf (stderr, "Error %d creating mutex.\n", l_ret);
74 throw std::runtime_error ("mld_mutex_t::mld_mutex_t()\n");
79 __INLINE__ ~mld_mutex_t () {
81 #ifndef _USE_OMNI_THREADS_
82 int l_ret = pthread_mutex_destroy (d_mutex);
84 fprintf (stderr, "mld_mutex_t::~mld_mutex_t(): "
85 "Error %d destroying mutex.\n", l_ret);
92 __INLINE__ void lock () {
93 #ifdef _USE_OMNI_THREADS_
96 int l_ret = pthread_mutex_lock (d_mutex);
98 fprintf (stderr, "mld_mutex_t::lock(): "
99 "Error %d locking mutex.\n", l_ret);
104 __INLINE__ void unlock () {
105 #ifdef _USE_OMNI_THREADS_
108 int l_ret = pthread_mutex_unlock (d_mutex);
110 fprintf (stderr, "mld_mutex_t::unlock(): "
111 "Error %d locking mutex.\n", l_ret);
116 __INLINE__ bool trylock () {
117 #ifdef _USE_OMNI_THREADS_
118 int l_ret = d_mutex->trylock ();
120 int l_ret = pthread_mutex_unlock (d_mutex);
122 return (l_ret == 0 ? true : false);
125 inline void acquire () { lock(); };
126 inline void release () { unlock(); };
127 inline void wait () { lock(); };
128 inline void post () { unlock(); };
131 typedef mld_mutex_t mld_mutex, *mld_mutex_ptr;
133 class mld_condition_t {
134 #ifdef _USE_OMNI_THREADS_
135 typedef omni_condition l_condition, *l_condition_ptr;
137 typedef pthread_cond_t l_condition, *l_condition_ptr;
141 l_condition_ptr d_condition;
142 mld_mutex_ptr d_mutex;
146 __INLINE__ mld_condition_t (mld_mutex_ptr mutex = NULL) {
148 d_i_own_mutex = false;
151 d_i_own_mutex = true;
152 d_mutex = new mld_mutex ();
154 #ifdef _USE_OMNI_THREADS_
155 d_condition = new omni_condition (d_mutex->mutex ());
157 d_condition = (l_condition_ptr) new l_condition;
158 int l_ret = pthread_cond_init (d_condition, NULL);
160 fprintf (stderr, "Error %d creating condition.\n", l_ret);
161 throw std::runtime_error ("mld_condition_t::mld_condition_t()\n");
166 __INLINE__ ~mld_condition_t () {
168 #ifndef _USE_OMNI_THREADS_
169 int l_ret = pthread_cond_destroy (d_condition);
171 fprintf (stderr, "mld_condition_t::mld_condition_t(): "
172 "Error %d destroying condition.\n", l_ret);
182 __INLINE__ mld_mutex_ptr mutex () {return (d_mutex);};
184 __INLINE__ void signal () {
185 DEBUG (fprintf (stderr, "a "));
187 #ifdef _USE_OMNI_THREADS_
188 d_condition->signal ();
190 int l_ret = pthread_cond_signal (d_condition);
192 fprintf (stderr, "mld_condition_t::signal(): "
193 "Error %d.\n", l_ret);
196 DEBUG (fprintf (stderr, "b "));
199 __INLINE__ void wait () {
200 DEBUG (fprintf (stderr, "c "));
201 #ifdef _USE_OMNI_THREADS_
202 d_condition->wait ();
204 int l_ret = pthread_cond_wait (d_condition, d_mutex->mutex ());
206 fprintf (stderr, "mld_condition_t::wait(): "
207 "Error %d.\n", l_ret);
210 DEBUG (printf (stderr, "d "));
214 typedef mld_condition_t mld_condition, *mld_condition_ptr;
217 #ifdef _USE_OMNI_THREADS_
218 typedef omni_thread l_thread, *l_thread_ptr;
220 typedef pthread_t l_thread, *l_thread_ptr;
224 #ifndef _USE_OMNI_THREADS_
226 void (*d_start_routine)(void*);
229 l_thread_ptr d_thread;
232 #ifndef _USE_OMNI_THREADS_
233 static void* local_start_routine (void *arg) {
234 mld_thread_t* This = (mld_thread_t*) arg;
235 (*(This->d_start_routine))(This->d_arg);
241 __INLINE__ mld_thread_t (void (*start_routine)(void *), void *arg) {
242 #ifdef _USE_OMNI_THREADS_
243 d_thread = new omni_thread (start_routine, arg);
246 d_start_routine = start_routine;
248 int l_ret = pthread_create (&d_thread, NULL, local_start_routine, this);
250 fprintf (stderr, "Error %d creating thread.\n", l_ret);
251 throw std::runtime_error ("mld_thread_t::mld_thread_t()\n");
256 __INLINE__ ~mld_thread_t () {
257 #ifdef _USE_OMNI_THREADS_
261 int l_ret = pthread_detach (d_thread);
263 fprintf (stderr, "Error %d detaching thread.\n", l_ret);
264 throw std::runtime_error ("mld_thread_t::~mld_thread_t()\n");
270 typedef mld_thread_t mld_thread, *mld_thread_ptr;
272 #endif /* _INCLUDED_MLD_THREADS_H_ */