diff options
author | michaelld | 2007-08-18 02:56:04 +0000 |
---|---|---|
committer | michaelld | 2007-08-18 02:56:04 +0000 |
commit | d33a2b199ec6ddaf648ec6ae0081807a70d278f9 (patch) | |
tree | 0df21932b46ecf8cb4bdb51b311cb5b9c7a7b80e /gr-audio-osx/src/mld_threads.h | |
parent | 0693ac18e31a9014dd83b4a4c7ac43071279353a (diff) | |
download | gnuradio-d33a2b199ec6ddaf648ec6ae0081807a70d278f9.tar.gz gnuradio-d33a2b199ec6ddaf648ec6ae0081807a70d278f9.tar.bz2 gnuradio-d33a2b199ec6ddaf648ec6ae0081807a70d278f9.zip |
Fixed use of mutex and condition, which corrects a long-standing rare
bug over contention of shared variables.
git-svn-id: http://gnuradio.org/svn/gnuradio/trunk@6153 221aa14e-8319-0410-a670-987f0aec2ac5
Diffstat (limited to 'gr-audio-osx/src/mld_threads.h')
-rw-r--r-- | gr-audio-osx/src/mld_threads.h | 63 |
1 files changed, 40 insertions, 23 deletions
diff --git a/gr-audio-osx/src/mld_threads.h b/gr-audio-osx/src/mld_threads.h index cd62edffe..a59a92863 100644 --- a/gr-audio-osx/src/mld_threads.h +++ b/gr-audio-osx/src/mld_threads.h @@ -4,6 +4,8 @@ * * 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) @@ -25,6 +27,7 @@ /* classes which allow for either pthreads or omni_threads */ +#define __macos__ #ifdef _USE_OMNI_THREADS_ #include <omnithread.h> #else @@ -34,6 +37,13 @@ #include <stdexcept> #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; @@ -130,12 +140,17 @@ class mld_condition_t { private: l_condition_ptr d_condition; mld_mutex_ptr d_mutex; - bool d_waiting; + bool d_i_own_mutex; public: - __INLINE__ mld_condition_t () { - d_waiting = false; - d_mutex = new mld_mutex (); + __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 @@ -159,38 +174,40 @@ public: #endif delete d_condition; d_condition = NULL; - delete d_mutex; + if (d_i_own_mutex) + delete d_mutex; d_mutex = NULL; }; + __INLINE__ mld_mutex_ptr mutex () {return (d_mutex);}; + __INLINE__ void signal () { - if (d_waiting == true) { + DEBUG (fprintf (stderr, "a ")); + #ifdef _USE_OMNI_THREADS_ - d_condition->signal (); + 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 - d_waiting = false; + 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 () { - if (d_waiting == false) { - d_waiting = true; + DEBUG (fprintf (stderr, "c ")); #ifdef _USE_OMNI_THREADS_ - d_condition->wait (); + 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 + 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 ")); }; }; |