[RFC] Reference counting on Audio objects for /dev/dsp

Igor Pechtchanski pechtcha@cs.nyu.edu
Thu Jul 15 13:44:00 GMT 2004


Gerd,

I'd really like your comments on this patch.  As I reported before, it
didn't quite work for me, but with the recent problems in testing another
(presumably working) patch, I suspect my test procedure isn't quite
correct anyway.  The patch basically adds a (very problem-specific)
reference count to the Audio object(s), and doesn't delete the shared ones
until all pointers are gone.  It doesn't seem to fix the bash redirection
problem, but does allow the "dsp_dup_close" testcase to run (again, I'd
like your opinion on whether it runs correctly).

The ChangeLog below is just for the record -- as I said, I don't expect
this to be checked in yet.
	Igor
==============================================================================
ChangeLog:
2004-07-06  Igor Pechtchanski  <pechtcha@cs.nyu.edu>

	* fhandler_dsp.cc (fhandler_dev_dsp::Audio::reference_count_):
	New instance variable.
	(fhandler_dev_dsp::Audio::inc): New function.  Increment the
	reference_count_.
	(fhandler_dev_dsp::Audio::dec): New function.  Decrement the
	reference_count_ and delete if zero.
	(fhandler_dev_dsp::close): Replace delete with a call to dec().
	(fhandler_dev_dsp::dup): Copy audio_in_ and audio_out_ and call
	inc() on each.

-- 
				http://cs.nyu.edu/~pechtcha/
      |\      _,,,---,,_		pechtcha@cs.nyu.edu
ZZZzz /,`.-'`'    -.  ;-;;,_		igor@watson.ibm.com
     |,4-  ) )-,_. ,\ (  `'-'		Igor Pechtchanski, Ph.D.
    '---''(_/--'  `-'\_) fL	a.k.a JaguaR-R-R-r-r-r-.-.-.  Meow!

"I have since come to realize that being between your mentor and his route
to the bathroom is a major career booster."  -- Patrick Naughton
-------------- next part --------------
Index: fhandler_dsp.cc
===================================================================
RCS file: /cvs/src/src/winsup/cygwin/fhandler_dsp.cc,v
retrieving revision 1.37
diff -u -p -r1.37 fhandler_dsp.cc
--- fhandler_dsp.cc	28 May 2004 19:50:05 -0000	1.37
+++ fhandler_dsp.cc	15 Jul 2004 13:40:36 -0000
@@ -80,7 +80,13 @@ class fhandler_dev_dsp::Audio
     (unsigned char *buffer, int size_bytes);
   inline void lock () { EnterCriticalSection (&lock_); }
   inline void unlock () { LeaveCriticalSection (&lock_); }
+
+//  inline void inc (void) { reference_count_++; }
+//  inline void dec (void) { if (--reference_count_ == 0) delete this; }
+  void inc (void);
+  void dec (void);
  private:
+  DWORD reference_count_; /* HACK!!!! A quick-and-dirty reference count */
   DWORD owner_; /* Process ID when wave operation started, else 0 */
   CRITICAL_SECTION lock_;
 };
@@ -231,6 +237,20 @@ fhandler_dev_dsp::Audio::Audio ()
   InitializeCriticalSection (&lock_);
   convert_ = &fhandler_dev_dsp::Audio::convert_none;
   owner_ = 0L;
+  reference_count_ = 1; // Going to assign right after allocation
+}
+
+void fhandler_dev_dsp::Audio::inc (void) {
+  reference_count_++;
+  debug_printf("%08p; ref_count=%ld\n", this, reference_count_);
+}
+
+void fhandler_dev_dsp::Audio::dec (void) {
+  debug_printf("%08p; ref_count=%ld\n", this, reference_count_-1);
+  if (--reference_count_ == 0) {
+    debug_printf("-- deleting\n");
+    delete this;
+  }
 }
 
 fhandler_dev_dsp::Audio::~Audio ()
@@ -1202,7 +1222,7 @@ fhandler_dev_dsp::close (void)
 		(int)audio_in_, (int)audio_out_);
   if (audio_in_)
     {
-      delete audio_in_;
+      audio_in_->dec ();
       audio_in_ = NULL;
     }
   if (audio_out_)
@@ -1212,7 +1232,7 @@ fhandler_dev_dsp::close (void)
 	 // do not wait for all pending audio to be played
 	 audio_out_->stop (true);
        }
-      delete audio_out_;
+      audio_out_ -> dec ();
       audio_out_ = NULL;
     }
   if (open_count > 0)
@@ -1231,6 +1251,9 @@ fhandler_dev_dsp::dup (fhandler_base * c
   fhc->audiobits_ = audiobits_;
   fhc->audiofreq_ = audiofreq_;
   fhc->audioformat_ = audioformat_;
+
+  fhc->audio_in_ = audio_in_; if (audio_in_) audio_in_->inc ();
+  fhc->audio_out_ = audio_out_; if (audio_out_) audio_out_->inc ();
   return 0;
 }
 


More information about the Cygwin-patches mailing list