? birnettests-my.h
? x
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/beast/birnet/ChangeLog,v
retrieving revision 1.32
diff -u -p -r1.32 ChangeLog
--- ChangeLog	26 May 2006 15:38:27 -0000	1.32
+++ ChangeLog	29 May 2006 15:20:37 -0000
@@ -1,3 +1,11 @@
+Mon May 29 17:15:19 2006  Stefan Westerfeld  <stefan@space.twc.de>
+
+	* birnetthreadxx.hh: Provide Birnet::AutoLocker<MutexType> template,
+	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 -r1.1 birnetthreadxx.hh
--- birnetthreadxx.hh	20 Mar 2006 23:22:51 -0000	1.1
+++ birnetthreadxx.hh	29 May 2006 15:20:37 -0000
@@ -60,6 +60,27 @@ 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.
+ */
+template<class MutexType>
+class AutoLocker {
+  MutexType&  mutex;
+  BIRNET_PRIVATE_CLASS_COPY (AutoLocker);
+public:
+  explicit
+  AutoLocker (MutexType& m) : mutex (m)
+  {
+    mutex.lock();
+  }
+  void          unlock	    ()			{ mutex.unlock(); }
+  void          relock	    ()			{ mutex.lock(); }
+  /*Des*/	~AutoLocker ()			{ mutex.unlock(); }
+};
+
 namespace Atomic {
 /* atomic integers */
 inline void     int_set      (volatile int *iptr, int value)    { birnet_atomic_int_set (iptr, value); }
