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
46 #define DEBUG(X) do{X} while(0);
48 #define DEBUG(X) do{} while(0);
51 class mld_condition_t;
54 #ifdef _USE_OMNI_THREADS_
55 typedef omni_mutex l_mutex, *l_mutex_ptr;
57 typedef pthread_mutex_t l_mutex, *l_mutex_ptr;
60 friend class mld_condition_t;
66 inline l_mutex_ptr mutex () { return (d_mutex); };
69 __INLINE__ mld_mutex_t () {
70 #ifdef _USE_OMNI_THREADS_
71 d_mutex = new omni_mutex ();
73 d_mutex = (l_mutex_ptr) new l_mutex;
74 int l_ret = pthread_mutex_init (d_mutex, NULL);
76 fprintf (stderr, "Error %d creating mutex.\n", l_ret);
77 throw std::runtime_error ("mld_mutex_t::mld_mutex_t()\n");
82 __INLINE__ ~mld_mutex_t () {
84 #ifndef _USE_OMNI_THREADS_
85 int l_ret = pthread_mutex_destroy (d_mutex);
87 fprintf (stderr, "mld_mutex_t::~mld_mutex_t(): "
88 "Error %d destroying mutex.\n", l_ret);
95 __INLINE__ void lock () {
96 #ifdef _USE_OMNI_THREADS_
99 int l_ret = pthread_mutex_lock (d_mutex);
101 fprintf (stderr, "mld_mutex_t::lock(): "
102 "Error %d locking mutex.\n", l_ret);
107 __INLINE__ void unlock () {
108 #ifdef _USE_OMNI_THREADS_
111 int l_ret = pthread_mutex_unlock (d_mutex);
113 fprintf (stderr, "mld_mutex_t::unlock(): "
114 "Error %d locking mutex.\n", l_ret);
119 __INLINE__ bool trylock () {
120 #ifdef _USE_OMNI_THREADS_
121 int l_ret = d_mutex->trylock ();
123 int l_ret = pthread_mutex_unlock (d_mutex);
125 return (l_ret == 0 ? true : false);
128 inline void acquire () { lock(); };
129 inline void release () { unlock(); };
130 inline void wait () { lock(); };
131 inline void post () { unlock(); };
134 typedef mld_mutex_t mld_mutex, *mld_mutex_ptr;
136 class mld_condition_t {
137 #ifdef _USE_OMNI_THREADS_
138 typedef omni_condition l_condition, *l_condition_ptr;
140 typedef pthread_cond_t l_condition, *l_condition_ptr;
144 l_condition_ptr d_condition;
145 mld_mutex_ptr d_mutex;
149 __INLINE__ mld_condition_t (mld_mutex_ptr mutex = NULL) {
151 d_i_own_mutex = false;
154 d_i_own_mutex = true;
155 d_mutex = new mld_mutex ();
157 #ifdef _USE_OMNI_THREADS_
158 d_condition = new omni_condition (d_mutex->mutex ());
160 d_condition = (l_condition_ptr) new l_condition;
161 int l_ret = pthread_cond_init (d_condition, NULL);
163 fprintf (stderr, "Error %d creating condition.\n", l_ret);
164 throw std::runtime_error ("mld_condition_t::mld_condition_t()\n");
169 __INLINE__ ~mld_condition_t () {
171 #ifndef _USE_OMNI_THREADS_
172 int l_ret = pthread_cond_destroy (d_condition);
174 fprintf (stderr, "mld_condition_t::mld_condition_t(): "
175 "Error %d destroying condition.\n", l_ret);
185 __INLINE__ mld_mutex_ptr mutex () {return (d_mutex);};
187 __INLINE__ void signal () {
188 DEBUG (fprintf (stderr, "a "););
190 #ifdef _USE_OMNI_THREADS_
191 d_condition->signal ();
193 int l_ret = pthread_cond_signal (d_condition);
195 fprintf (stderr, "mld_condition_t::signal(): "
196 "Error %d.\n", l_ret);
199 DEBUG (fprintf (stderr, "b "););
202 __INLINE__ void wait () {
203 DEBUG (fprintf (stderr, "c "););
204 #ifdef _USE_OMNI_THREADS_
205 d_condition->wait ();
207 int l_ret = pthread_cond_wait (d_condition, d_mutex->mutex ());
209 fprintf (stderr, "mld_condition_t::wait(): "
210 "Error %d.\n", l_ret);
213 DEBUG (fprintf (stderr, "d "););
217 typedef mld_condition_t mld_condition, *mld_condition_ptr;
220 #ifdef _USE_OMNI_THREADS_
221 typedef omni_thread l_thread, *l_thread_ptr;
223 typedef pthread_t l_thread, *l_thread_ptr;
227 #ifndef _USE_OMNI_THREADS_
229 void (*d_start_routine)(void*);
232 l_thread_ptr d_thread;
235 #ifndef _USE_OMNI_THREADS_
236 static void* local_start_routine (void *arg) {
237 mld_thread_t* This = (mld_thread_t*) arg;
238 (*(This->d_start_routine))(This->d_arg);
244 __INLINE__ mld_thread_t (void (*start_routine)(void *), void *arg) {
245 #ifdef _USE_OMNI_THREADS_
246 d_thread = new omni_thread (start_routine, arg);
249 d_start_routine = start_routine;
251 int l_ret = pthread_create (&d_thread, NULL, local_start_routine, this);
253 fprintf (stderr, "Error %d creating thread.\n", l_ret);
254 throw std::runtime_error ("mld_thread_t::mld_thread_t()\n");
259 __INLINE__ ~mld_thread_t () {
260 #ifdef _USE_OMNI_THREADS_
264 int l_ret = pthread_detach (d_thread);
266 fprintf (stderr, "Error %d detaching thread.\n", l_ret);
267 throw std::runtime_error ("mld_thread_t::~mld_thread_t()\n");
273 typedef mld_thread_t mld_thread, *mld_thread_ptr;
275 #endif /* _INCLUDED_MLD_THREADS_H_ */