From 3e8c1915a289088aa801427defc18a165ba43cd1 Mon Sep 17 00:00:00 2001 From: Michael Dickens Date: Mon, 19 Apr 2010 09:29:43 -0600 Subject: [PATCH] Initial changes to remove mld_thread and instead use gruel:: namespace classes --- gr-audio-osx/src/Makefile.am | 8 +- gr-audio-osx/src/audio_osx_sink.cc | 35 ++-- gr-audio-osx/src/audio_osx_sink.h | 6 +- gr-audio-osx/src/audio_osx_source.cc | 49 ++--- gr-audio-osx/src/audio_osx_source.h | 6 +- gr-audio-osx/src/circular_buffer.h | 69 ++++--- gr-audio-osx/src/mld_threads.h | 272 --------------------------- 7 files changed, 77 insertions(+), 368 deletions(-) delete mode 100644 gr-audio-osx/src/mld_threads.h diff --git a/gr-audio-osx/src/Makefile.am b/gr-audio-osx/src/Makefile.am index 6099dc20..5bf28b8e 100644 --- a/gr-audio-osx/src/Makefile.am +++ b/gr-audio-osx/src/Makefile.am @@ -1,5 +1,5 @@ # -# Copyright 2006,2008,2009 Free Software Foundation, Inc. +# Copyright 2006,2008,2009,2010 Free Software Foundation, Inc. # # This file is part of GNU Radio. # @@ -32,8 +32,7 @@ grinclude_HEADERS = \ noinst_HEADERS = \ audio_osx.h \ - circular_buffer.h \ - mld_threads.h + circular_buffer.h noinst_PYTHON = \ qa_osx.py \ @@ -46,8 +45,7 @@ libgnuradio_audio_osx_la_SOURCES = \ audio_osx_source.cc libgnuradio_audio_osx_la_LIBADD = \ - $(GNURADIO_CORE_LA) \ - $(OMNITHREAD_LA) + $(GNURADIO_CORE_LA) libgnuradio_audio_osx_la_LDFLAGS = \ -framework AudioUnit \ diff --git a/gr-audio-osx/src/audio_osx_sink.cc b/gr-audio-osx/src/audio_osx_sink.cc index e91716c0..20fd895b 100644 --- a/gr-audio-osx/src/audio_osx_sink.cc +++ b/gr-audio-osx/src/audio_osx_sink.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2006 Free Software Foundation, Inc. + * Copyright 2006,2010 Free Software Foundation, Inc. * * This file is part of GNU Radio. * @@ -24,8 +24,6 @@ #include "config.h" #endif -#define _USE_OMNI_THREADS_ - #include #include #include @@ -172,11 +170,15 @@ audio_osx_sink::audio_osx_sink (int sample_rate, // create the stuff to regulate I/O - d_cond_data = new mld_condition (); + d_cond_data = new gruel::condition_variable (); if (d_cond_data == NULL) - CheckErrorAndThrow (errno, "new mld_condition (data)", - "audio_osx_source::audio_osx_source"); - d_internal = d_cond_data->mutex (); + CheckErrorAndThrow (errno, "new condition (data)", + "audio_osx_sink::audio_osx_sink"); + + d_internal = new gruel::mutex (); + if (d_internal == NULL) + CheckErrorAndThrow (errno, "new mutex (internal)", + "audio_osx_sink::audio_osx_sink"); // initialize the AU for output @@ -253,6 +255,9 @@ audio_osx_sink::~audio_osx_sink () // close and delete control stuff delete d_cond_data; + d_cond_data = 0; + delete d_internal; + d_internal = 0; } audio_osx_sink_sptr @@ -274,7 +279,7 @@ audio_osx_sink::work (int noutput_items, gr_vector_const_void_star &input_items, gr_vector_void_star &output_items) { - d_internal->lock (); + gruel::scoped_lock l (*d_internal); /* take the input data, copy it, and push it to the bottom of the queue mono input are pushed onto queue[0]; @@ -307,8 +312,8 @@ audio_osx_sink::work (int noutput_items, while (d_queueSampleCount > l_max_count) { // release control so-as to allow data to be retrieved; // block until there is data to return - d_cond_data->wait (); -// the condition's signal() was called; acquire control + d_cond_data->wait (l); +// the condition's 'notify' was called; acquire control // to keep thread safe } } @@ -353,9 +358,6 @@ audio_osx_sink::work (int noutput_items, << d_queueSampleCount << ", mSC = " << d_max_sample_count << std::endl; #endif -// release control to allow for other processing parts to run - d_internal->unlock (); - return (noutput_items); } @@ -370,7 +372,7 @@ OSStatus audio_osx_sink::AUOutputCallback audio_osx_sink* This = (audio_osx_sink*) inRefCon; OSStatus err = noErr; - This->d_internal->lock (); + gruel::scoped_lock l (*This->d_internal); #if _OSX_AU_DEBUG_ std::cerr << "cb_in: SC = " << This->d_queueSampleCount @@ -403,10 +405,7 @@ OSStatus audio_osx_sink::AUOutputCallback #endif // signal that data is available - This->d_cond_data->signal (); - -// release control to allow for other processing parts to run - This->d_internal->unlock (); + This->d_cond_data->notify_one (); return (err); } diff --git a/gr-audio-osx/src/audio_osx_sink.h b/gr-audio-osx/src/audio_osx_sink.h index ceb291d0..a1a56502 100644 --- a/gr-audio-osx/src/audio_osx_sink.h +++ b/gr-audio-osx/src/audio_osx_sink.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2006 Free Software Foundation, Inc. + * Copyright 2006,2010 Free Software Foundation, Inc. * * This file is part of GNU Radio. * @@ -59,8 +59,8 @@ class audio_osx_sink : public gr_sync_block { UInt32 d_n_channels; UInt32 d_queueSampleCount, d_max_sample_count; bool d_do_block; - mld_mutex_ptr d_internal; - mld_condition_ptr d_cond_data; + gruel::mutex* d_internal; + gruel::condition_variable* d_cond_data; circular_buffer** d_buffers; // AudioUnits and Such diff --git a/gr-audio-osx/src/audio_osx_source.cc b/gr-audio-osx/src/audio_osx_source.cc index 61838745..538cfd8f 100644 --- a/gr-audio-osx/src/audio_osx_source.cc +++ b/gr-audio-osx/src/audio_osx_source.cc @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2006 Free Software Foundation, Inc. + * Copyright 2006,2010 Free Software Foundation, Inc. * * This file is part of GNU Radio. * @@ -24,8 +24,6 @@ #include "config.h" #endif -#define _USE_OMNI_THREADS_ - #include #include #include @@ -446,11 +444,15 @@ audio_osx_source::audio_osx_source (int sample_rate, // create the stuff to regulate I/O - d_cond_data = new mld_condition (); + d_cond_data = new gruel::condition_variable (); if (d_cond_data == NULL) - CheckErrorAndThrow (errno, "new mld_condition (data)", + CheckErrorAndThrow (errno, "new condition (data)", + "audio_osx_source::audio_osx_source"); + + d_internal = new gruel::mutex (); + if (d_internal == NULL) + CheckErrorAndThrow (errno, "new mutex (internal)", "audio_osx_source::audio_osx_source"); - d_internal = d_cond_data->mutex (); // initialize the AU for input @@ -600,6 +602,9 @@ audio_osx_source::~audio_osx_source () // close and delete the control stuff delete d_cond_data; + d_cond_data = 0; + delete d_internal; + d_internal = 0; } audio_osx_source_sptr @@ -654,7 +659,7 @@ audio_osx_source::work gr_vector_void_star &output_items) { // acquire control to do processing here only - d_internal->lock (); + gruel::scoped_lock l (*d_internal); #if _OSX_AU_DEBUG_ std::cerr << "work1: SC = " << d_queueSampleCount @@ -677,14 +682,12 @@ audio_osx_source::work while (d_queueSampleCount == 0) { // release control so-as to allow data to be retrieved; // block until there is data to return - d_cond_data->wait (); - // the condition's signal() was called; acquire control to + d_cond_data->wait (l); + // the condition's 'notify' was called; acquire control to // keep thread safe } } else { // no data & not blocking; return nothing - // release control so-as to allow data to be retrieved - d_internal->unlock (); return (0); } } @@ -718,15 +721,8 @@ audio_osx_source::work #if _OSX_AU_DEBUG_ std::cerr << "work2: SC = " << d_queueSampleCount - << ", act#OI = " << actual_noutput_items << std::endl; -#endif - - // release control to allow for other processing parts to run - - d_internal->unlock (); - -#if _OSX_AU_DEBUG_ - std::cerr << "work3: Returning." << std::endl; + << ", act#OI = " << actual_noutput_items << std::endl + << "Returning." << std::endl; #endif return (actual_noutput_items); @@ -782,7 +778,7 @@ audio_osx_source::AUInputCallback (void* inRefCon, OSStatus err = noErr; audio_osx_source* This = static_cast(inRefCon); - This->d_internal->lock (); + gruel::scoped_lock l (*This->d_internal); #if _OSX_AU_DEBUG_ std::cerr << "cb0: in#F = " << inNumberFrames @@ -911,17 +907,10 @@ audio_osx_source::AUInputCallback (void* inRefCon, #endif // signal that data is available, if appropraite - This->d_cond_data->signal (); - -#if _OSX_AU_DEBUG_ - std::cerr << "cb5: releasing internal mutex." << std::endl; -#endif - -// release control to allow for other processing parts to run - This->d_internal->unlock (); + This->d_cond_data->notify_one (); #if _OSX_AU_DEBUG_ - std::cerr << "cb6: returning." << std::endl; + std::cerr << "cb5: returning." << std::endl; #endif return (err); diff --git a/gr-audio-osx/src/audio_osx_source.h b/gr-audio-osx/src/audio_osx_source.h index 780f7ec6..e8df47b1 100644 --- a/gr-audio-osx/src/audio_osx_source.h +++ b/gr-audio-osx/src/audio_osx_source.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2006 Free Software Foundation, Inc. + * Copyright 2006,2010 Free Software Foundation, Inc. * * This file is part of GNU Radio. * @@ -66,8 +66,8 @@ class audio_osx_source : public gr_sync_block { UInt32 d_n_AvailableInputFrames, d_n_ActualInputFrames; UInt32 d_n_user_channels, d_n_max_channels, d_n_deviceChannels; bool d_do_block, d_passThrough, d_waiting_for_data; - mld_mutex_ptr d_internal; - mld_condition_ptr d_cond_data; + gruel::mutex* d_internal; + gruel::condition_variable* d_cond_data; circular_buffer** d_buffers; // AudioUnits and Such diff --git a/gr-audio-osx/src/circular_buffer.h b/gr-audio-osx/src/circular_buffer.h index 6d491fb6..48758bf8 100644 --- a/gr-audio-osx/src/circular_buffer.h +++ b/gr-audio-osx/src/circular_buffer.h @@ -1,6 +1,6 @@ /* -*- c++ -*- */ /* - * Copyright 2006,2009 Free Software Foundation, Inc. + * Copyright 2006,2009,2010 Free Software Foundation, Inc. * * This file is part of GNU Radio. * @@ -23,7 +23,7 @@ #ifndef _CIRCULAR_BUFFER_H_ #define _CIRCULAR_BUFFER_H_ -#include "mld_threads.h" +#include #include #include @@ -37,7 +37,8 @@ #define DEBUG(X) do{} while(0); #endif -template class circular_buffer +template +class circular_buffer { private: // the buffer to use @@ -48,8 +49,9 @@ private: size_t d_n_avail_write_I, d_n_avail_read_I; // stuff to control access to class internals - mld_mutex_ptr d_internal; - mld_condition_ptr d_readBlock, d_writeBlock; + gruel::mutex* d_internal; + gruel::condition_variable* d_readBlock; + gruel::condition_variable* d_writeBlock; // booleans to decide how to control reading, writing, and aborting bool d_doWriteBlock, d_doFullRead, d_doAbort; @@ -94,16 +96,14 @@ public: }; inline size_t n_avail_write_items () { - d_internal->lock (); + gruel::scoped_lock l (*d_internal); size_t retVal = d_n_avail_write_I; - d_internal->unlock (); return (retVal); }; inline size_t n_avail_read_items () { - d_internal->lock (); + gruel::scoped_lock l (*d_internal); size_t retVal = d_n_avail_read_I; - d_internal->unlock (); return (retVal); }; @@ -120,13 +120,13 @@ public: // create a mutex to handle contention of shared resources; // any routine needed access to shared resources uses lock() // before doing anything, then unlock() when finished. - d_internal = new mld_mutex (); + d_internal = new gruel::mutex (); // link the internal mutex to the read and write conditions; // when wait() is called, the internal mutex will automatically - // be unlock()'ed. Upon return (from a signal() to the condition), + // be unlock()'ed. Upon return (from a notify_one() to the condition), // the internal mutex will be lock()'ed. - d_readBlock = new mld_condition (d_internal); - d_writeBlock = new mld_condition (d_internal); + d_readBlock = new gruel::condition_variable (); + d_writeBlock = new gruel::condition_variable (); }; /* @@ -167,9 +167,8 @@ public: if (!buf) throw std::runtime_error ("circular_buffer::enqueue(): " "input buffer is NULL.\n"); - d_internal->lock (); + gruel::scoped_lock l (*d_internal); if (d_doAbort) { - d_internal->unlock (); return (2); } // set the return value to 1: success; change if needed @@ -178,11 +177,11 @@ public: if (d_doWriteBlock) { while (bufLen_I > d_n_avail_write_I) { DEBUG (std::cerr << "enqueue: #len > #a, waiting." << std::endl); - // wait will automatically unlock() the internal mutex - d_writeBlock->wait (); - // and lock() it here. + // wait; will automatically unlock() the internal mutex via + // the scoped lock + d_writeBlock->wait (l); + // and auto re-lock() it here. if (d_doAbort) { - d_internal->unlock (); DEBUG (std::cerr << "enqueue: #len > #a, aborting." << std::endl); return (2); } @@ -208,8 +207,7 @@ public: d_writeNdx_I += n_now_I; d_n_avail_read_I += bufLen_I; d_n_avail_write_I -= bufLen_I; - d_readBlock->signal (); - d_internal->unlock (); + d_readBlock->notify_one (); return (retval); }; @@ -255,19 +253,18 @@ public: throw std::runtime_error ("circular_buffer::dequeue()"); } - d_internal->lock (); + gruel::scoped_lock l (*d_internal); if (d_doAbort) { - d_internal->unlock (); return (2); } if (d_doFullRead) { while (d_n_avail_read_I < l_bufLen_I) { DEBUG (std::cerr << "dequeue: #a < #len, waiting." << std::endl); - // wait will automatically unlock() the internal mutex - d_readBlock->wait (); - // and lock() it here. + // wait; will automatically unlock() the internal mutex via + // the scoped lock + d_readBlock->wait (l); + // and re-lock() it here. if (d_doAbort) { - d_internal->unlock (); DEBUG (std::cerr << "dequeue: #a < #len, aborting." << std::endl); return (2); } @@ -276,11 +273,11 @@ public: } else { while (d_n_avail_read_I == 0) { DEBUG (std::cerr << "dequeue: #a == 0, waiting." << std::endl); - // wait will automatically unlock() the internal mutex - d_readBlock->wait (); - // and lock() it here. + // wait; will automatically unlock() the internal mutex via + // the scoped lock + d_readBlock->wait (l); + // and re-lock() it here. if (d_doAbort) { - d_internal->unlock (); DEBUG (std::cerr << "dequeue: #a == 0, aborting." << std::endl); return (2); } @@ -303,17 +300,15 @@ public: *bufLen_I = l_bufLen_I; d_n_avail_read_I -= l_bufLen_I; d_n_avail_write_I += l_bufLen_I; - d_writeBlock->signal (); - d_internal->unlock (); + d_writeBlock->notify_one (); return (1); }; void abort () { - d_internal->lock (); + gruel::scoped_lock l (*d_internal); d_doAbort = true; - d_writeBlock->signal (); - d_readBlock->signal (); - d_internal->unlock (); + d_writeBlock->notify_one (); + d_readBlock->notify_one (); }; }; diff --git a/gr-audio-osx/src/mld_threads.h b/gr-audio-osx/src/mld_threads.h deleted file mode 100644 index d2fb4ea7..00000000 --- a/gr-audio-osx/src/mld_threads.h +++ /dev/null @@ -1,272 +0,0 @@ -/* -*- c++ -*- */ -/* - * Copyright 2006 Free Software Foundation, Inc. - * - * This file is part of GNU Radio. - * - * Primary Author: Michael Dickens, NCIP Lab, University of Notre Dame - * - * GNU Radio is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3, or (at your option) - * any later version. - * - * GNU Radio is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with GNU Radio; see the file COPYING. If not, write to - * the Free Software Foundation, Inc., 51 Franklin Street, - * Boston, MA 02110-1301, USA. - */ - -#ifndef _INCLUDED_MLD_THREADS_H_ -#define _INCLUDED_MLD_THREADS_H_ - -/* classes which allow for either pthreads or omni_threads */ - -#define __macos__ -#ifdef _USE_OMNI_THREADS_ -#include -#else -#include -#endif - -#include - -#define __INLINE__ inline -#define DO_DEBUG 0 - -#if DO_DEBUG -#define DEBUG(X) do{X} while(0); -#else -#define DEBUG(X) do{} while(0); -#endif - -class mld_condition_t; - -class mld_mutex_t { -#ifdef _USE_OMNI_THREADS_ - typedef omni_mutex l_mutex, *l_mutex_ptr; -#else - typedef pthread_mutex_t l_mutex, *l_mutex_ptr; -#endif - - friend class mld_condition_t; - -private: - l_mutex_ptr d_mutex; - -protected: - inline l_mutex_ptr mutex () { return (d_mutex); }; - -public: - __INLINE__ mld_mutex_t () { -#ifdef _USE_OMNI_THREADS_ - d_mutex = new omni_mutex (); -#else - d_mutex = (l_mutex_ptr) new l_mutex; - int l_ret = pthread_mutex_init (d_mutex, NULL); - if (l_ret != 0) { - fprintf (stderr, "Error %d creating mutex.\n", l_ret); - throw std::runtime_error ("mld_mutex_t::mld_mutex_t()\n"); - } -#endif - }; - - __INLINE__ ~mld_mutex_t () { - unlock (); -#ifndef _USE_OMNI_THREADS_ - int l_ret = pthread_mutex_destroy (d_mutex); - if (l_ret != 0) { - fprintf (stderr, "mld_mutex_t::~mld_mutex_t(): " - "Error %d destroying mutex.\n", l_ret); - } -#endif - delete d_mutex; - d_mutex = NULL; - }; - - __INLINE__ void lock () { -#ifdef _USE_OMNI_THREADS_ - d_mutex->lock (); -#else - int l_ret = pthread_mutex_lock (d_mutex); - if (l_ret != 0) { - fprintf (stderr, "mld_mutex_t::lock(): " - "Error %d locking mutex.\n", l_ret); - } -#endif - }; - - __INLINE__ void unlock () { -#ifdef _USE_OMNI_THREADS_ - d_mutex->unlock (); -#else - int l_ret = pthread_mutex_unlock (d_mutex); - if (l_ret != 0) { - fprintf (stderr, "mld_mutex_t::unlock(): " - "Error %d locking mutex.\n", l_ret); - } -#endif - }; - - __INLINE__ bool trylock () { -#ifdef _USE_OMNI_THREADS_ - int l_ret = d_mutex->trylock (); -#else - int l_ret = pthread_mutex_unlock (d_mutex); -#endif - return (l_ret == 0 ? true : false); - }; - - inline void acquire () { lock(); }; - inline void release () { unlock(); }; - inline void wait () { lock(); }; - inline void post () { unlock(); }; -}; - -typedef mld_mutex_t mld_mutex, *mld_mutex_ptr; - -class mld_condition_t { -#ifdef _USE_OMNI_THREADS_ - typedef omni_condition l_condition, *l_condition_ptr; -#else - typedef pthread_cond_t l_condition, *l_condition_ptr; -#endif - -private: - l_condition_ptr d_condition; - mld_mutex_ptr d_mutex; - bool d_i_own_mutex; - -public: - __INLINE__ mld_condition_t (mld_mutex_ptr mutex = NULL) { - if (mutex) { - d_i_own_mutex = false; - d_mutex = mutex; - } else { - d_i_own_mutex = true; - d_mutex = new mld_mutex (); - } -#ifdef _USE_OMNI_THREADS_ - d_condition = new omni_condition (d_mutex->mutex ()); -#else - d_condition = (l_condition_ptr) new l_condition; - int l_ret = pthread_cond_init (d_condition, NULL); - if (l_ret != 0) { - fprintf (stderr, "Error %d creating condition.\n", l_ret); - throw std::runtime_error ("mld_condition_t::mld_condition_t()\n"); - } -#endif - }; - - __INLINE__ ~mld_condition_t () { - signal (); -#ifndef _USE_OMNI_THREADS_ - int l_ret = pthread_cond_destroy (d_condition); - if (l_ret != 0) { - fprintf (stderr, "mld_condition_t::mld_condition_t(): " - "Error %d destroying condition.\n", l_ret); - } -#endif - delete d_condition; - d_condition = NULL; - if (d_i_own_mutex) - delete d_mutex; - d_mutex = NULL; - }; - - __INLINE__ mld_mutex_ptr mutex () {return (d_mutex);}; - - __INLINE__ void signal () { - DEBUG (fprintf (stderr, "a ")); - -#ifdef _USE_OMNI_THREADS_ - d_condition->signal (); -#else - int l_ret = pthread_cond_signal (d_condition); - if (l_ret != 0) { - fprintf (stderr, "mld_condition_t::signal(): " - "Error %d.\n", l_ret); - } -#endif - DEBUG (fprintf (stderr, "b ")); - }; - - __INLINE__ void wait () { - DEBUG (fprintf (stderr, "c ")); -#ifdef _USE_OMNI_THREADS_ - d_condition->wait (); -#else - int l_ret = pthread_cond_wait (d_condition, d_mutex->mutex ()); - if (l_ret != 0) { - fprintf (stderr, "mld_condition_t::wait(): " - "Error %d.\n", l_ret); - } -#endif - DEBUG (printf (stderr, "d ")); - }; -}; - -typedef mld_condition_t mld_condition, *mld_condition_ptr; - -class mld_thread_t { -#ifdef _USE_OMNI_THREADS_ - typedef omni_thread l_thread, *l_thread_ptr; -#else - typedef pthread_t l_thread, *l_thread_ptr; -#endif - -private: -#ifndef _USE_OMNI_THREADS_ - l_thread d_thread; - void (*d_start_routine)(void*); - void *d_arg; -#else - l_thread_ptr d_thread; -#endif - -#ifndef _USE_OMNI_THREADS_ - static void* local_start_routine (void *arg) { - mld_thread_t* This = (mld_thread_t*) arg; - (*(This->d_start_routine))(This->d_arg); - return (NULL); - }; -#endif - -public: - __INLINE__ mld_thread_t (void (*start_routine)(void *), void *arg) { -#ifdef _USE_OMNI_THREADS_ - d_thread = new omni_thread (start_routine, arg); - d_thread->start (); -#else - d_start_routine = start_routine; - d_arg = arg; - int l_ret = pthread_create (&d_thread, NULL, local_start_routine, this); - if (l_ret != 0) { - fprintf (stderr, "Error %d creating thread.\n", l_ret); - throw std::runtime_error ("mld_thread_t::mld_thread_t()\n"); - } -#endif - }; - - __INLINE__ ~mld_thread_t () { -#ifdef _USE_OMNI_THREADS_ -// delete d_thread; - d_thread = NULL; -#else - int l_ret = pthread_detach (d_thread); - if (l_ret != 0) { - fprintf (stderr, "Error %d detaching thread.\n", l_ret); - throw std::runtime_error ("mld_thread_t::~mld_thread_t()\n"); - } -#endif - }; -}; - -typedef mld_thread_t mld_thread, *mld_thread_ptr; - -#endif /* _INCLUDED_MLD_THREADS_H_ */ -- 2.30.2