From: leslie Date: Tue, 22 Nov 2011 19:32:47 +0000 (+0100) Subject: testing: double locking test tool X-Git-Tag: upstream/620~20 X-Git-Url: http://www.vanbest.org/gitweb/?a=commitdiff_plain;h=a06121a79bbf2111ea6ebe40fa2d230b53499052;p=sasc-ng.git testing: double locking test tool --- diff --git a/testing/Makefile b/testing/Makefile index 0140ba0..26a1bc1 100644 --- a/testing/Makefile +++ b/testing/Makefile @@ -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 index 0000000..1ab5019 --- /dev/null +++ b/testing/testMutex.c @@ -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 +#include +#include +#include + +#include +#include + +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(); +}