浏览代码

Seperated all Mutexes

Philipp Hinz 7 年之前
父节点
当前提交
21076f3a92
共有 7 个文件被更改,包括 22 次插入18 次删除
  1. 1 1
      CoffeeCode/buildno
  2. 2 2
      CoffeeCode/database.cpp
  3. 1 1
      CoffeeCode/global.h
  4. 4 4
      CoffeeCode/lcd.cpp
  5. 6 6
      CoffeeCode/logger.cpp
  6. 6 2
      CoffeeCode/main.cpp
  7. 2 2
      CoffeeCode/stripe.cpp

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-110
+112

+ 2 - 2
CoffeeCode/database.cpp

@@ -105,7 +105,7 @@ int sqlExecute(string query) {
 	int rc;
 	struct timeval tv1, tv2;
 
-	pthread_mutex_lock(&mutex);
+	//pthread_mutex_lock(&mutex);
 
 	logger(V_SQL, "Executing SQL Query: %s\n", query.c_str());
 
@@ -118,7 +118,7 @@ int sqlExecute(string query) {
 				(double) (tv2.tv_usec - tv1.tv_usec) / 1000000
 						+ (double) (tv2.tv_sec - tv1.tv_sec));
 
-	pthread_mutex_unlock(&mutex);
+	//pthread_mutex_unlock(&mutex);
 	if (rc != SQLITE_OK) {
 		logger_error("SQL error (%d): %s\n", rc, zErrMsg);
 		sqlite3_free(zErrMsg);

+ 1 - 1
CoffeeCode/global.h

@@ -23,7 +23,7 @@
 
 extern int verbose;
 extern bool optDate, optPower;
-extern pthread_mutex_t mutex;
+extern pthread_mutex_t mutex_spi, mutex_i2c, mutex_logger;
 extern pthread_t thread[];
 
 

+ 4 - 4
CoffeeCode/lcd.cpp

@@ -238,9 +238,9 @@ void lcdPosition(const int fd, int x, int y) {
 	if ((y > lcd->rows) || (y < 0))
 		return;
 
-	pthread_mutex_lock(&mutex);
+	pthread_mutex_lock(&mutex_spi);
 	putCommand(lcd, x + (LCD_DGRAM | rowOff[y]));
-	pthread_mutex_unlock(&mutex);
+	pthread_mutex_unlock(&mutex_spi);
 
 	lcd->cx = x;
 	lcd->cy = y;
@@ -272,7 +272,7 @@ void lcdCharDef(const int fd, int index, unsigned char data[8]) {
 
 void lcdPutchar(const int fd, unsigned char data) {
 	struct lcdDataStruct *lcd = lcds[fd];
-	pthread_mutex_lock(&mutex);
+	pthread_mutex_lock(&mutex_spi);
 
 	digitalWrite(lcd->rsPin, 1);
 	sendDataCmd(lcd, data);
@@ -284,7 +284,7 @@ void lcdPutchar(const int fd, unsigned char data) {
 
 		putCommand(lcd, lcd->cx + (LCD_DGRAM | rowOff[lcd->cy]));
 	}
-	pthread_mutex_unlock(&mutex);
+	pthread_mutex_unlock(&mutex_spi);
 }
 
 /*

+ 6 - 6
CoffeeCode/logger.cpp

@@ -36,7 +36,7 @@ void logger_print_time(_IO_FILE* stream) {
  */
 
 void logger_error(const char* format, ...) {
-	pthread_mutex_lock(&mutex);
+	pthread_mutex_lock(&mutex_logger);
 	va_list args;
 	fprintf( stderr, KRED);
 	if (optDate)
@@ -47,7 +47,7 @@ void logger_error(const char* format, ...) {
 	va_end(args);
 	fprintf( stderr, KNRM);
 	lastlog = LOG_ERROR;
-	pthread_mutex_unlock(&mutex);
+	pthread_mutex_unlock(&mutex_logger);
 }
 
 /**
@@ -70,7 +70,7 @@ void logger(logger_verbose_t verboselevel, logger_type_t logtype,
 		const char* format, ...) {
 	if (verbose < verboselevel)
 		return;
-	pthread_mutex_lock(&mutex);
+	pthread_mutex_lock(&mutex_logger);
 	switch (logtype) {
 	case LOG_CAN:
 		printf(KBOLD);
@@ -101,7 +101,7 @@ void logger(logger_verbose_t verboselevel, logger_type_t logtype,
 	if (logtype > 0)
 		printf(KNRM);
 	lastlog = logtype;
-	pthread_mutex_unlock(&mutex);
+	pthread_mutex_unlock(&mutex_logger);
 }
 
 /**
@@ -113,7 +113,7 @@ void logger(logger_verbose_t verboselevel, logger_type_t logtype,
 void logger(logger_verbose_t verboselevel, const char* format, ...) {
 	if (verbose < verboselevel)
 		return;
-	pthread_mutex_lock(&mutex);
+	pthread_mutex_lock(&mutex_logger);
 	if (optDate)
 		logger_print_time(stdout);
 	va_list args;
@@ -121,5 +121,5 @@ void logger(logger_verbose_t verboselevel, const char* format, ...) {
 	vprintf(format, args);
 	va_end(args);
 	lastlog = 0;
-	pthread_mutex_unlock(&mutex);
+	pthread_mutex_unlock(&mutex_logger);
 }

+ 6 - 2
CoffeeCode/main.cpp

@@ -38,7 +38,9 @@ void terminationHandler(int signum);
 void usr1Handler(int signum);
 void hupHandler(int signum);
 pthread_t thread[4];
-pthread_mutex_t mutex;
+pthread_mutex_t mutex_spi;
+pthread_mutex_t mutex_i2c;
+pthread_mutex_t mutex_logger;
 
 int main(int argc, char *argv[]) {
 	pthread_attr_t attr;
@@ -125,7 +127,9 @@ int main(int argc, char *argv[]) {
 	rc = pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE_NP);
 	if (rc != 0)
 		throw(L"pthread_mutexattr_settype returns " + rc);
-	pthread_mutex_init(&mutex, &mutexattr);
+	pthread_mutex_init(&mutex_spi, &mutexattr);
+	pthread_mutex_init(&mutex_i2c, &mutexattr);
+	pthread_mutex_init(&mutex_logger, &mutexattr);
 	rc = pthread_mutexattr_destroy(&mutexattr);
 	if (rc != 0)
 		throw(L"pthread_mutexattr_destroy returns " + rc);

+ 2 - 2
CoffeeCode/stripe.cpp

@@ -140,7 +140,7 @@ void stripeInit(void) {
 int stripeCommand(int len, char* data) {
 	int ret = 0;
 	int i = 0;
-	pthread_mutex_lock(&mutex);
+	pthread_mutex_lock(&mutex_i2c);
 	for (i = 0; i < len; i++) {
 		ret -= wiringPiI2CWriteReg8(i2cfd, i, data[i]);
 	}
@@ -148,7 +148,7 @@ int stripeCommand(int len, char* data) {
 	if (ret)
 		logger(V_NONE, LOG_WARN,
 				"Failed to write %d of %d bytes on i2c device\n", ret, len);
-	pthread_mutex_unlock(&mutex);
+	pthread_mutex_unlock(&mutex_i2c);
 	return ret;
 }