← Back to team overview

linuxdcpp-team team mailing list archive

[Branch ~dcplusplus-team/dcplusplus/trunk] Rev 2471: Replace non-win32 parts of Semaphore.h with something that compiles

 

------------------------------------------------------------
revno: 2471
committer: Razzloss <razzloss@xxxxxxxxx>
branch nick: dcplusplus
timestamp: Sun 2011-03-27 05:13:40 +0300
message:
  Replace non-win32 parts of Semaphore.h with something that compiles
modified:
  dcpp/Semaphore.h


--
lp:dcplusplus
https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk

Your team Dcplusplus-team is subscribed to branch lp:dcplusplus.
To unsubscribe from this branch go to https://code.launchpad.net/~dcplusplus-team/dcplusplus/trunk/+edit-subscription
=== modified file 'dcpp/Semaphore.h'
--- dcpp/Semaphore.h	2011-01-02 17:12:02 +0000
+++ dcpp/Semaphore.h	2011-03-27 02:13:40 +0000
@@ -20,7 +20,8 @@
 #define SEMAPHORE_H
 
 #ifndef _WIN32
-#include "CriticalSection.h"
+#include <errno.h>
+#include <semaphore.h>
 #include <sys/time.h>
 #endif
 
@@ -49,47 +50,48 @@
 	HANDLE h;
 #else
 public:
-	Semaphore() throw() : count(0) { pthread_cond_init(&cond, NULL); }
-	~Semaphore() throw() { pthread_cond_destroy(&cond); }
-	void signal() throw() {
-		Lock l(cs);
-		count++;
-		pthread_cond_signal(&cond);
+	Semaphore() throw() { 
+		sem_init(&semaphore, 0, 0); 
+	}
+	
+	~Semaphore() throw() { 
+		sem_destroy(&semaphore); 
+	}
+
+	void signal() throw() { 
+		sem_post(&semaphore); 
 	}
 
 	bool wait() throw() {
-		Lock l(cs);
-		while (count == 0) {
-			pthread_cond_wait(&cond, &cs.getMutex());
-		}
-		count--;
+		int retval = 0;
+		do {
+			retval = sem_wait(&semaphore);
+		} while (retval != 0);
+
 		return true;
 	}
+
 	bool wait(uint32_t millis) throw() {
-		Lock l(cs);
-		if(count == 0) {
-			timeval timev;
-			timespec t;
-			gettimeofday(&timev, NULL);
-			millis+=timev.tv_usec/1000;
-			t.tv_sec = timev.tv_sec + (millis/1000);
-			t.tv_nsec = (millis%1000)*1000*1000;
-			int ret;
-			do {
-				ret = pthread_cond_timedwait(&cond, &cs.getMutex(), &t);
-			} while (ret==0 && count==0);
-			if(ret != 0) {
-				return false;
-			}
+		timeval timev;
+		timespec t;
+		gettimeofday(&timev, NULL);
+		millis+=timev.tv_usec/1000;
+		t.tv_sec = timev.tv_sec + (millis/1000);
+		t.tv_nsec = (millis%1000)*1000*1000;
+		int ret;
+		do {
+			ret = sem_timedwait(&semaphore, &t);
+		} while (ret != 0 && errno == EINTR);
+
+		if (ret != 0) {
+			return false;
 		}
-		count--;
+
 		return true;
 	}
 
 private:
-	pthread_cond_t cond;
-	CriticalSection cs;
-	int count;
+	sem_t semaphore;
 #endif
 	Semaphore(const Semaphore&);
 	Semaphore& operator=(const Semaphore&);