]> www.vanbest.org Git - sasc-ng.git/commitdiff
testing: double locking test tool
authorleslie <unknown>
Tue, 22 Nov 2011 19:32:47 +0000 (20:32 +0100)
committerleslie <unknown>
Tue, 22 Nov 2011 19:32:47 +0000 (20:32 +0100)
testing/Makefile
testing/testMutex.c [new file with mode: 0644]

index 0140ba0f0916d08a34db298e38ac8931c598d82e..26a1bc13f9b77d43503e865a9ae4cb708e527f35 100644 (file)
@@ -56,7 +56,7 @@ $(VDRDIR)/%.o: $(VDRDIR)/%.c
 
 ### Targets:
 
-all: testECM testEMM testN1Emu testN2Emu testN2RunEmu testTPS testExtAU
+all: testECM testEMM testN1Emu testN2Emu testN2RunEmu testTPS testExtAU testMutex
 
 testECM.o: testECM.c compat.h
 testECM: testECM.o $(SHAREDOBJS) $(NOBJS)
@@ -94,8 +94,12 @@ testINIT: testINIT.o $(SHAREDOBJS) $(NOBJS)
 
 filterhelper: filterhelper.o
        $(CXX) $(CXXFLAGS) $^ -o $@
+
+testMutex: testMutex.o
+       $(CXX) $(CXXFLAGS) $^ -lpthread -o $@
+
 clean:
        @-rm -f *.o core* *~
        @-rm -f testECM testEMM testN1Emu testN2Emu testN2RunEmu testTPS testExtAU testINIT
-       @-rm -f filterhelper
+       @-rm -f filterhelper testMutex
        @-rm -f dump.txt
diff --git a/testing/testMutex.c b/testing/testMutex.c
new file mode 100644 (file)
index 0000000..1ab5019
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * pthread double locking testing tool.
+ * Locks a mutex 2 times.
+ * This a valid operation for an ERRORCHECK mutex!
+ * If this test locks up, your pthread implementation is faulty.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+
+#include <pthread.h>
+#include <sys/types.h>
+
+class cMutex {
+  friend class cCondVar;
+private:
+  pthread_mutex_t mutex;
+  int locked;
+public:
+  cMutex(void);
+  ~cMutex();
+  void Lock(void);
+  void Unlock(void);
+  };
+
+cMutex::cMutex(void)
+{
+  locked = 0;
+  pthread_mutexattr_t attr;
+  pthread_mutexattr_init(&attr);
+  printf("mutex attr init returned %d:%s\n",errno,strerror(errno));
+  pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);
+  printf("mutex attr settype returned %d:%s\n",errno,strerror(errno));
+  pthread_mutex_init(&mutex, &attr);
+  printf("mutex init returned %d:%s\n",errno,strerror(errno));
+}
+
+cMutex::~cMutex()
+{
+  pthread_mutex_destroy(&mutex);
+  printf("mutex destroy returned %d:%s\n",errno,strerror(errno));
+}
+
+void cMutex::Lock(void)
+{
+  pthread_mutex_lock(&mutex);
+  printf("mutex lock returned %d:%s\n",errno,strerror(errno));
+  locked++;
+}
+
+void cMutex::Unlock(void)
+{
+ if (!--locked) {
+    pthread_mutex_unlock(&mutex);
+    printf("mutex unlock returned %d:%s\n",errno,strerror(errno));
+    }
+}
+
+int main(int argc, char *argv[])
+{
+  cMutex mutex;
+  printf("doing first lock\n");
+  mutex.Lock();
+  printf("first lock successful\n");
+  printf("doing second lock\n");
+  mutex.Lock();
+  printf("second lock successful\n");
+
+  mutex.Unlock();
+  mutex.Unlock();
+}