Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/beast/birnet/ChangeLog,v
retrieving revision 1.32
diff -u -p -u -r1.32 ChangeLog
--- ChangeLog	26 May 2006 15:38:27 -0000	1.32
+++ ChangeLog	30 May 2006 12:26:17 -0000
@@ -1,3 +1,11 @@
+Tue May 30 14:24:32 2006  Stefan Westerfeld  <stefan@space.twc.de>
+
+	* birnetthreadxx.hh: Provide Birnet::AutoLocker class which locks a
+	mutex on construction, and automatically unlocks it on destruction, so
+	that putting an AutoLocker object on the stack conveniently ensures
+	that the mutex will be properly unlocked for instance when the
+	function returns or an exception gets thrown.
+
 Fri May 26 17:38:00 2006  Tim Janik  <timj@gtk.org>
 
 	* birnetcore.h: define BIRNET_*LIKELY() macros.
Index: birnetthreadxx.hh
===================================================================
RCS file: /cvs/gnome/beast/birnet/birnetthreadxx.hh,v
retrieving revision 1.1
diff -u -p -u -r1.1 birnetthreadxx.hh
--- birnetthreadxx.hh	20 Mar 2006 23:22:51 -0000	1.1
+++ birnetthreadxx.hh	30 May 2006 12:26:17 -0000
@@ -60,6 +60,30 @@ public:
   /*Des*/       ~Cond         ()                { birnet_thread_table.cond_destroy (&cond); }
 };
 
+/**
+ * Locks a mutex on construction, and automatically unlocks it on
+ * destruction, so that putting an AutoLocker object on the stack
+ * conveniently ensures that the mutex will be properly unlocked for
+ * instance when the function returns or an exception gets thrown.
+ */
+class AutoLocker {
+  union {
+    Mutex    *mutex;
+    RecMutex *rec_mutex;
+  } mtx;
+  const bool recursive;
+
+  BIRNET_PRIVATE_CLASS_COPY (AutoLocker);
+public:
+  explicit	AutoLocker    (Mutex& m)
+		  : recursive (false)		    { mtx.mutex = &m; relock(); }
+  explicit	AutoLocker    (RecMutex& m)
+		  : recursive (true)		    { mtx.rec_mutex = &m; relock(); }
+  void		relock	      () const		    { if (recursive) mtx.rec_mutex->lock(); else mtx.mutex->lock(); }
+  void		unlock	      () const		    { if (recursive) mtx.rec_mutex->unlock(); else mtx.mutex->unlock(); }
+  /*Des */	~AutoLocker   ()		    { unlock(); }
+};
+
 namespace Atomic {
 /* atomic integers */
 inline void     int_set      (volatile int *iptr, int value)    { birnet_atomic_int_set (iptr, value); }
