]> ruderich.org/simon Gitweb - tlsproxy/tlsproxy.git/blobdiff - src/sem.c
Update copyright year.
[tlsproxy/tlsproxy.git] / src / sem.c
index 86fe39882c54a315e04a2f0b773f0b08db4aa801..b4c4a89b5135ddeacbb76a22f735c1f62bddb847 100644 (file)
--- a/src/sem.c
+++ b/src/sem.c
@@ -1,7 +1,7 @@
 /*
  * Simple semaphore implementation, P() and V().
  *
- * Copyright (C) 2011  Simon Ruderich
+ * Copyright (C) 2011-2014  Simon Ruderich
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdlib.h>
-#include <pthread.h>
-
 #include "sem.h"
 
+#include <pthread.h>
+#include <stdlib.h>
+
 
 struct SEM {
     pthread_mutex_t mutex;
@@ -30,16 +30,16 @@ struct SEM {
 };
 
 SEM *sem_init(int init_value) {
-    SEM *sem = (SEM *)malloc(sizeof(SEM));
-    if (NULL == sem) {
+    SEM *sem = malloc(sizeof(*sem));
+    if (sem == NULL) {
         return NULL;
     }
 
-    if (0 != pthread_mutex_init(&sem->mutex, NULL)) {
+    if (pthread_mutex_init(&sem->mutex, NULL) != 0) {
         free(sem);
         return NULL;
     }
-    if (0 != pthread_cond_init(&sem->condition, NULL)) {
+    if (pthread_cond_init(&sem->condition, NULL) != 0) {
         pthread_mutex_destroy(&sem->mutex);
         free(sem);
         return NULL;
@@ -50,15 +50,15 @@ SEM *sem_init(int init_value) {
 }
 
 int sem_del(SEM *sem) {
-    if (NULL == sem) {
+    if (sem == NULL) {
         return 0;
     }
 
-    if (0 != pthread_mutex_destroy(&sem->mutex)) {
+    if (pthread_mutex_destroy(&sem->mutex) != 0) {
         free(sem);
         return -1;
     }
-    if (0 != pthread_cond_destroy(&sem->condition)) {
+    if (pthread_cond_destroy(&sem->condition) != 0) {
         free(sem);
         return -1;
     }
@@ -69,7 +69,7 @@ int sem_del(SEM *sem) {
 
 void P(SEM *sem) {
     pthread_mutex_lock(&sem->mutex);
-    while (0 == sem->value) {
+    while (sem->value <= 0) {
         pthread_cond_wait(&sem->condition, &sem->mutex);
     }
     sem->value--;