3 * Copyright 2006 Free Software Foundation, Inc.
5 * This file is part of GNU Radio.
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)
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.
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.
23 #ifndef _INCLUDED_MLD_THREADS_H_
24 #define _INCLUDED_MLD_THREADS_H_
26 /* classes which allow for either pthreads or omni_threads */
28 #ifdef _USE_OMNI_THREADS_
29 #include <omnithread.h>
36 #define __INLINE__ inline
38 class mld_condition_t;
41 #ifdef _USE_OMNI_THREADS_
42 typedef omni_mutex l_mutex, *l_mutex_ptr;
44 typedef pthread_mutex_t l_mutex, *l_mutex_ptr;
47 friend class mld_condition_t;
53 inline l_mutex_ptr mutex () { return (d_mutex); };
56 __INLINE__ mld_mutex_t () {
57 #ifdef _USE_OMNI_THREADS_
58 d_mutex = new omni_mutex ();
60 d_mutex = (l_mutex_ptr) new l_mutex;
61 int l_ret = pthread_mutex_init (d_mutex, NULL);
63 fprintf (stderr, "Error %d creating mutex.\n", l_ret);
64 throw std::runtime_error ("mld_mutex_t::mld_mutex_t()\n");
69 __INLINE__ ~mld_mutex_t () {
71 #ifndef _USE_OMNI_THREADS_
72 int l_ret = pthread_mutex_destroy (d_mutex);
74 fprintf (stderr, "mld_mutex_t::~mld_mutex_t(): "
75 "Error %d destroying mutex.\n", l_ret);
82 __INLINE__ void lock () {
83 #ifdef _USE_OMNI_THREADS_
86 int l_ret = pthread_mutex_lock (d_mutex);
88 fprintf (stderr, "mld_mutex_t::lock(): "
89 "Error %d locking mutex.\n", l_ret);
94 __INLINE__ void unlock () {
95 #ifdef _USE_OMNI_THREADS_
98 int l_ret = pthread_mutex_unlock (d_mutex);
100 fprintf (stderr, "mld_mutex_t::unlock(): "
101 "Error %d locking mutex.\n", l_ret);
106 __INLINE__ bool trylock () {
107 #ifdef _USE_OMNI_THREADS_
108 int l_ret = d_mutex->trylock ();
110 int l_ret = pthread_mutex_unlock (d_mutex);
112 return (l_ret == 0 ? true : false);
115 inline void acquire () { lock(); };
116 inline void release () { unlock(); };
117 inline void wait () { lock(); };
118 inline void post () { unlock(); };
121 typedef mld_mutex_t mld_mutex, *mld_mutex_ptr;
123 class mld_condition_t {
124 #ifdef _USE_OMNI_THREADS_
125 typedef omni_condition l_condition, *l_condition_ptr;
127 typedef pthread_cond_t l_condition, *l_condition_ptr;
131 l_condition_ptr d_condition;
132 mld_mutex_ptr d_mutex;
136 __INLINE__ mld_condition_t () {
138 d_mutex = new mld_mutex ();
139 #ifdef _USE_OMNI_THREADS_
140 d_condition = new omni_condition (d_mutex->mutex ());
142 d_condition = (l_condition_ptr) new l_condition;
143 int l_ret = pthread_cond_init (d_condition, NULL);
145 fprintf (stderr, "Error %d creating condition.\n", l_ret);
146 throw std::runtime_error ("mld_condition_t::mld_condition_t()\n");
151 __INLINE__ ~mld_condition_t () {
153 #ifndef _USE_OMNI_THREADS_
154 int l_ret = pthread_cond_destroy (d_condition);
156 fprintf (stderr, "mld_condition_t::mld_condition_t(): "
157 "Error %d destroying condition.\n", l_ret);
166 __INLINE__ void signal () {
167 if (d_waiting == true) {
168 #ifdef _USE_OMNI_THREADS_
169 d_condition->signal ();
171 int l_ret = pthread_cond_signal (d_condition);
173 fprintf (stderr, "mld_condition_t::signal(): "
174 "Error %d.\n", l_ret);
181 __INLINE__ void wait () {
182 if (d_waiting == false) {
184 #ifdef _USE_OMNI_THREADS_
185 d_condition->wait ();
187 int l_ret = pthread_cond_wait (d_condition, d_mutex->mutex ());
189 fprintf (stderr, "mld_condition_t::wait(): "
190 "Error %d.\n", l_ret);
197 typedef mld_condition_t mld_condition, *mld_condition_ptr;
200 #ifdef _USE_OMNI_THREADS_
201 typedef omni_thread l_thread, *l_thread_ptr;
203 typedef pthread_t l_thread, *l_thread_ptr;
207 #ifndef _USE_OMNI_THREADS_
209 void (*d_start_routine)(void*);
212 l_thread_ptr d_thread;
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);
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);
229 d_start_routine = start_routine;
231 int l_ret = pthread_create (&d_thread, NULL, local_start_routine, this);
233 fprintf (stderr, "Error %d creating thread.\n", l_ret);
234 throw std::runtime_error ("mld_thread_t::mld_thread_t()\n");
239 __INLINE__ ~mld_thread_t () {
240 #ifdef _USE_OMNI_THREADS_
244 int l_ret = pthread_detach (d_thread);
246 fprintf (stderr, "Error %d detaching thread.\n", l_ret);
247 throw std::runtime_error ("mld_thread_t::~mld_thread_t()\n");
253 typedef mld_thread_t mld_thread, *mld_thread_ptr;
255 #endif /* _INCLUDED_MLD_THREADS_H_ */