lzms-common.c, lzms-compress.c: Use pthread_once()
authorEric Biggers <ebiggers3@gmail.com>
Thu, 5 Jun 2014 13:10:18 +0000 (08:10 -0500)
committerEric Biggers <ebiggers3@gmail.com>
Thu, 5 Jun 2014 13:10:18 +0000 (08:10 -0500)
src/lzms-common.c
src/lzms-compress.c

index a34fb24..c657ef4 100644 (file)
@@ -165,17 +165,9 @@ lzms_compute_slots(void)
 void
 lzms_init_slots(void)
 {
-       static bool done = false;
-       static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-
-       if (unlikely(!done)) {
-               pthread_mutex_lock(&mutex);
-               if (!done) {
-                       lzms_compute_slots();
-                       done = true;
-               }
-               pthread_mutex_unlock(&mutex);
-       }
+       static pthread_once_t once = PTHREAD_ONCE_INIT;
+
+       pthread_once(&once, lzms_compute_slots);
 }
 
 static s32
index 763be00..2c9356d 100644 (file)
@@ -666,17 +666,9 @@ lzms_do_init_rc_costs(void)
 static void
 lzms_init_rc_costs(void)
 {
-       static bool done = false;
-       static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
-
-       if (unlikely(!done)) {
-               pthread_mutex_lock(&mutex);
-               if (!done) {
-                       lzms_do_init_rc_costs();
-                       done = true;
-               }
-               pthread_mutex_unlock(&mutex);
-       }
+       static pthread_once_t once = PTHREAD_ONCE_INIT;
+
+       pthread_once(&once, lzms_do_init_rc_costs);
 }
 
 /*