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 2, 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 */
30 #ifdef _USE_OMNI_THREADS_
31 #include <gnuradio/omnithread.h>
38 #define __INLINE__ inline
40 class mld_condition_t;
43 #ifdef _USE_OMNI_THREADS_
44 typedef omni_mutex l_mutex, *l_mutex_ptr;
46 typedef pthread_mutex_t l_mutex, *l_mutex_ptr;
49 friend class mld_condition_t;
55 inline l_mutex_ptr mutex () { return (d_mutex); };
58 __INLINE__ mld_mutex_t () {
59 #ifdef _USE_OMNI_THREADS_
60 d_mutex = new omni_mutex ();
62 d_mutex = (l_mutex_ptr) new l_mutex;
63 int l_ret = pthread_mutex_init (d_mutex, NULL);
65 fprintf (stderr, "Error %d creating mutex.\n", l_ret);
66 throw std::runtime_error ("mld_mutex_t::mld_mutex_t()\n");
71 __INLINE__ ~mld_mutex_t () {
73 #ifndef _USE_OMNI_THREADS_
74 int l_ret = pthread_mutex_destroy (d_mutex);
76 fprintf (stderr, "mld_mutex_t::~mld_mutex_t(): "
77 "Error %d destroying mutex.\n", l_ret);
84 __INLINE__ void lock () {
85 #ifdef _USE_OMNI_THREADS_
88 int l_ret = pthread_mutex_lock (d_mutex);
90 fprintf (stderr, "mld_mutex_t::lock(): "
91 "Error %d locking mutex.\n", l_ret);
96 __INLINE__ void unlock () {
97 #ifdef _USE_OMNI_THREADS_
100 int l_ret = pthread_mutex_unlock (d_mutex);
102 fprintf (stderr, "mld_mutex_t::unlock(): "
103 "Error %d locking mutex.\n", l_ret);
108 __INLINE__ bool trylock () {
109 #ifdef _USE_OMNI_THREADS_
110 int l_ret = d_mutex->trylock ();
112 int l_ret = pthread_mutex_unlock (d_mutex);
114 return (l_ret == 0 ? true : false);
117 inline void acquire () { lock(); };
118 inline void release () { unlock(); };
119 inline void wait () { lock(); };
120 inline void post () { unlock(); };
123 typedef mld_mutex_t mld_mutex, *mld_mutex_ptr;
125 class mld_condition_t {
126 #ifdef _USE_OMNI_THREADS_
127 typedef omni_condition l_condition, *l_condition_ptr;
129 typedef pthread_cond_t l_condition, *l_condition_ptr;
133 l_condition_ptr d_condition;
134 mld_mutex_ptr d_mutex;
138 __INLINE__ mld_condition_t () {
140 d_mutex = new mld_mutex ();
141 #ifdef _USE_OMNI_THREADS_
142 d_condition = new omni_condition (d_mutex->mutex ());
144 d_condition = (l_condition_ptr) new l_condition;
145 int l_ret = pthread_cond_init (d_condition, NULL);
147 fprintf (stderr, "Error %d creating condition.\n", l_ret);
148 throw std::runtime_error ("mld_condition_t::mld_condition_t()\n");
153 __INLINE__ ~mld_condition_t () {
155 #ifndef _USE_OMNI_THREADS_
156 int l_ret = pthread_cond_destroy (d_condition);
158 fprintf (stderr, "mld_condition_t::mld_condition_t(): "
159 "Error %d destroying condition.\n", l_ret);
168 __INLINE__ void signal () {
169 if (d_waiting == true) {
170 #ifdef _USE_OMNI_THREADS_
171 d_condition->signal ();
173 int l_ret = pthread_cond_signal (d_condition);
175 fprintf (stderr, "mld_condition_t::signal(): "
176 "Error %d.\n", l_ret);
183 __INLINE__ void wait () {
184 if (d_waiting == false) {
186 #ifdef _USE_OMNI_THREADS_
187 d_condition->wait ();
189 int l_ret = pthread_cond_wait (d_condition, d_mutex->mutex ());
191 fprintf (stderr, "mld_condition_t::wait(): "
192 "Error %d.\n", l_ret);
199 typedef mld_condition_t mld_condition, *mld_condition_ptr;
202 #ifdef _USE_OMNI_THREADS_
203 typedef omni_thread l_thread, *l_thread_ptr;
205 typedef pthread_t l_thread, *l_thread_ptr;
209 #ifndef _USE_OMNI_THREADS_
211 void (*d_start_routine)(void*);
214 l_thread_ptr d_thread;
217 #ifndef _USE_OMNI_THREADS_
218 static void* local_start_routine (void *arg) {
219 mld_thread_t* This = (mld_thread_t*) arg;
220 (*(This->d_start_routine))(This->d_arg);
226 __INLINE__ mld_thread_t (void (*start_routine)(void *), void *arg) {
227 #ifdef _USE_OMNI_THREADS_
228 d_thread = new omni_thread (start_routine, arg);
231 d_start_routine = start_routine;
233 int l_ret = pthread_create (&d_thread, NULL, local_start_routine, this);
235 fprintf (stderr, "Error %d creating thread.\n", l_ret);
236 throw std::runtime_error ("mld_thread_t::mld_thread_t()\n");
241 __INLINE__ ~mld_thread_t () {
242 #ifdef _USE_OMNI_THREADS_
246 int l_ret = pthread_detach (d_thread);
248 fprintf (stderr, "Error %d detaching thread.\n", l_ret);
249 throw std::runtime_error ("mld_thread_t::~mld_thread_t()\n");
255 typedef mld_thread_t mld_thread, *mld_thread_ptr;
257 #endif /* _INCLUDED_MLD_THREADS_H_ */