Browse Source

Add in more testing for locking.

Brian Aker 12 years ago
parent
commit
160db653de

+ 100 - 58
libgearman-server/connection.c

@@ -155,8 +155,7 @@ static gearman_server_con_st * _server_con_create(gearman_server_thread_st *thre
     GEARMAN_LIST_ADD(thread->con, con,);
     if ((error= pthread_mutex_unlock(&thread->lock)))
     {
-      errno= error;
-      gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", error);
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
       gearman_server_con_free(con);
 
       *ret= GEARMAN_ERRNO;
@@ -166,7 +165,7 @@ static gearman_server_con_st * _server_con_create(gearman_server_thread_st *thre
   else
   {
     assert(error);
-    gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_lock(%d), programming error, please report", error);
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
     gearman_server_con_free(con);
 
     *ret= GEARMAN_ERRNO;
@@ -267,16 +266,16 @@ void gearman_server_con_free(gearman_server_con_st *con)
   if ((lock_error= pthread_mutex_lock(&thread->lock)) == 0)
   {
     GEARMAN_LIST_DEL(con->thread->con, con,);
-    if ((lock_error= pthread_mutex_unlock(&thread->lock)) != 0)
+    if ((lock_error= pthread_mutex_unlock(&thread->lock)))
     {
-      gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
     }
   }
   else
   {
-    gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_lock(%d), programming error, please report", lock_error);
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
   }
-    assert(lock_error == 0);
+  assert(lock_error == 0);
 
   if (thread->free_con_count < GEARMAN_MAX_FREE_SERVER_CON)
   {
@@ -368,9 +367,9 @@ void gearman_server_con_to_be_freed_add(gearman_server_con_st *con)
   {
     if (con->to_be_freed_list)
     {
-      if ((lock_error= pthread_mutex_unlock(&con->thread->lock)) != 0)
+      if ((lock_error= pthread_mutex_unlock(&con->thread->lock)))
       {
-        gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
       }
       assert(lock_error == 0);
       return;
@@ -378,7 +377,7 @@ void gearman_server_con_to_be_freed_add(gearman_server_con_st *con)
   }
   else
   {
-    gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_lock(%d), programming error, please report", lock_error);
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_lock() failed");
   }
   assert(lock_error == 0);
 
@@ -388,18 +387,18 @@ void gearman_server_con_to_be_freed_add(gearman_server_con_st *con)
   /* Looks funny, but need to check to_be_freed_count locked, but call run unlocked. */
   if (con->thread->to_be_freed_count == 1 && con->thread->run_fn)
   {
-    if ((lock_error= pthread_mutex_unlock(&con->thread->lock)) != 0)
+    if ((lock_error= pthread_mutex_unlock(&con->thread->lock)))
     {
-      gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
     }
     assert(lock_error == 0);
     (*con->thread->run_fn)(con->thread, con->thread->run_fn_arg);
   }
   else
   {
-    if ((lock_error= pthread_mutex_unlock(&con->thread->lock)) != 0)
+    if ((lock_error= pthread_mutex_unlock(&con->thread->lock)))
     {
-      gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
     }
     assert(lock_error == 0);
   }
@@ -429,16 +428,16 @@ gearman_server_con_st * gearman_server_con_to_be_freed_next(gearman_server_threa
       con= thread->to_be_freed_list;
     }
 
-    if ((lock_error= pthread_mutex_unlock(&thread->lock)) != 0)
+    if ((lock_error= pthread_mutex_unlock(&thread->lock)))
     {
-      gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
     }
 
     return con;
   }
   else
   {
-    gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_lock(%d), programming error, please report", lock_error);
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_lock() failed");
   }
   assert(lock_error == 0);
 
@@ -455,26 +454,24 @@ void gearman_server_con_io_add(gearman_server_con_st *con)
   int lock_error;
   if ((lock_error= pthread_mutex_lock(&con->thread->lock)) == 0)
   {
-    GEARMAN_LIST_ADD(con->thread->io, con, io_)
-      con->io_list= true;
+    GEARMAN_LIST_ADD(con->thread->io, con, io_);
+    con->io_list= true;
 
     /* Looks funny, but need to check io_count locked, but call run unlocked. */
     if (con->thread->io_count == 1 && con->thread->run_fn)
     {
-      if ((lock_error= pthread_mutex_unlock(&con->thread->lock)) == 0)
-      {
-        (*con->thread->run_fn)(con->thread, con->thread->run_fn_arg);
-      }
-      else
+      if ((lock_error= pthread_mutex_unlock(&con->thread->lock)))
       {
-        gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
       }
+
+      (*con->thread->run_fn)(con->thread, con->thread->run_fn_arg);
     }
     else
     {
-      if ((lock_error= pthread_mutex_unlock(&con->thread->lock)) != 0)
+      if ((lock_error= pthread_mutex_unlock(&con->thread->lock)))
       {
-        gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
       }
     }
   }
@@ -496,14 +493,14 @@ void gearman_server_con_io_remove(gearman_server_con_st *con)
       GEARMAN_LIST_DEL(con->thread->io, con, io_);
       con->io_list= false;
     }
-    if ((lock_error= pthread_mutex_unlock(&con->thread->lock)) != 0)
+    if ((lock_error= pthread_mutex_unlock(&con->thread->lock)))
     {
-      gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_unlock(%d), programming error, please report", lock_error);
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_unlock() failed");
     }
   }
   else
   {
-    gearmand_log_fatal(GEARMAN_DEFAULT_LOG_PARAM, "pthread_mutex_lock(%d), programming error, please report", lock_error);
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, lock_error, "pthread_mutex_lock() failed");
   }
 
   assert(lock_error == 0);
@@ -514,13 +511,11 @@ gearman_server_con_io_next(gearman_server_thread_st *thread)
 {
   gearman_server_con_st *con= thread->io_list;
 
-  if (con == NULL)
+  if (con)
   {
-    return NULL;
+    gearman_server_con_io_remove(con);
   }
 
-  gearman_server_con_io_remove(con);
-
   return con;
 }
 
@@ -531,30 +526,66 @@ void gearman_server_con_proc_add(gearman_server_con_st *con)
     return;
   }
 
-  (void) pthread_mutex_lock(&con->thread->lock);
-  GEARMAN_LIST_ADD(con->thread->proc, con, proc_)
-  con->proc_list= true;
-  (void) pthread_mutex_unlock(&con->thread->lock);
+  int error;
+  if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
+  {
+    GEARMAN_LIST_ADD(con->thread->proc, con, proc_);
+    con->proc_list= true;
+    if ((error= pthread_mutex_unlock(&con->thread->lock)))
+    {
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
+    }
 
-  if (! (Server->proc_shutdown) && !(Server->proc_wakeup))
+    if (! (Server->proc_shutdown) && !(Server->proc_wakeup))
+    {
+      if ((error= pthread_mutex_lock(&(Server->proc_lock))) == 0)
+      {
+        Server->proc_wakeup= true;
+        if ((error= pthread_cond_signal(&(Server->proc_cond))) == 0)
+        {
+          if ((error= pthread_mutex_unlock(&(Server->proc_lock))))
+          {
+            gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
+          }
+        }
+        else
+        {
+          gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_cond_signal() failed");
+        }
+      }
+      else
+      {
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
+      }
+    }
+  }
+  else
   {
-    (void) pthread_mutex_lock(&(Server->proc_lock));
-    Server->proc_wakeup= true;
-    (void) pthread_cond_signal(&(Server->proc_cond));
-    (void) pthread_mutex_unlock(&(Server->proc_lock));
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
   }
 }
 
 void gearman_server_con_proc_remove(gearman_server_con_st *con)
 {
-  (void) pthread_mutex_lock(&con->thread->lock);
+  int error;
 
-  if (con->proc_list)
+  if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
   {
-    GEARMAN_LIST_DEL(con->thread->proc, con, proc_)
-    con->proc_list= false;
+    if (con->proc_list)
+    {
+      GEARMAN_LIST_DEL(con->thread->proc, con, proc_);
+      con->proc_list= false;
+    }
+
+    if ((error= pthread_mutex_unlock(&con->thread->lock)))
+    {
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
+    }
+  }
+  else
+  {
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
   }
-  (void) pthread_mutex_unlock(&con->thread->lock);
 }
 
 gearman_server_con_st *
@@ -565,21 +596,32 @@ gearman_server_con_proc_next(gearman_server_thread_st *thread)
     return NULL;
   }
 
-  (void) pthread_mutex_lock(&thread->lock);
+  gearman_server_con_st *con= NULL;
 
-  gearman_server_con_st *con= thread->proc_list;
-  while (con != NULL)
+  int error;
+  if ((error= pthread_mutex_lock(&thread->lock)) == 0)
   {
-    GEARMAN_LIST_DEL(thread->proc, con, proc_)
-    con->proc_list= false;
-    if (!(con->proc_removed))
+    con= thread->proc_list;
+    while (con != NULL)
     {
-      break;
+      GEARMAN_LIST_DEL(thread->proc, con, proc_);
+      con->proc_list= false;
+      if (!(con->proc_removed))
+      {
+        break;
+      }
+      con= thread->proc_list;
     }
-    con= thread->proc_list;
-  }
 
-  (void) pthread_mutex_unlock(&thread->lock);
+    if ((error= pthread_mutex_unlock(&thread->lock)))
+    {
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
+    }
+  }
+  else
+  {
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
+  }
 
   return con;
 }

+ 26 - 21
libgearman-server/gearmand_con.c

@@ -162,25 +162,29 @@ gearmand_error_t gearmand_con_create(gearmand_st *gearmand, int fd,
   else
   {
     uint32_t free_dcon_count;
-    gearmand_con_st *free_dcon_list;
+    gearmand_con_st *free_dcon_list= NULL;
 
     int error;
-    if ((error= pthread_mutex_lock(&(dcon->thread->lock))) != 0)
+    if ((error= pthread_mutex_lock(&(dcon->thread->lock))) == 0)
     {
-      gearmand_perror(error, "pthread_mutex_lock");
-      gearmand_fatal("Lock could not be taken on dcon->thread->, shutdown to occur");
-      gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
-    }
-
-    GEARMAN_LIST_ADD(dcon->thread->dcon_add, dcon,)
+      GEARMAN_LIST_ADD(dcon->thread->dcon_add, dcon,);
 
-    /* Take the free connection structures back to reuse. */
-    free_dcon_list= dcon->thread->free_dcon_list;
-    free_dcon_count= dcon->thread->free_dcon_count;
-    dcon->thread->free_dcon_list= NULL;
-    dcon->thread->free_dcon_count= 0;
+      /* Take the free connection structures back to reuse. */
+      free_dcon_list= dcon->thread->free_dcon_list;
+      free_dcon_count= dcon->thread->free_dcon_count;
+      dcon->thread->free_dcon_list= NULL;
+      dcon->thread->free_dcon_count= 0;
 
-    (void ) pthread_mutex_unlock(&(dcon->thread->lock));
+      if ((error= pthread_mutex_unlock(&(dcon->thread->lock))))
+      {
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
+      }
+    }
+    else
+    {
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
+      gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
+    }
 
     /* Only wakeup the thread if this is the first in the queue. We don't need
        to lock around the count check, worst case it was already picked up and
@@ -256,11 +260,14 @@ void gearmand_con_free(gearmand_con_st *dcon)
       if ((error=  pthread_mutex_lock(&(dcon->thread->lock))) == 0)
       {
         GEARMAN_LIST_ADD(dcon->thread->free_dcon, dcon,);
-        (void ) pthread_mutex_unlock(&(dcon->thread->lock));
+        if ((error= pthread_mutex_unlock(&(dcon->thread->lock))))
+        {
+          gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
+        }
       }
       else
       {
-        gearmand_perror(error, "pthread_mutex_lock");
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
       }
     }
   }
@@ -288,10 +295,9 @@ void gearmand_con_check_queue(gearmand_thread_st *thread)
       gearmand_con_st *dcon= thread->dcon_add_list;
       GEARMAN_LIST_DEL(thread->dcon_add, dcon,);
 
-      if ((error= pthread_mutex_unlock(&(thread->lock))) != 0)
+      if ((error= pthread_mutex_unlock(&(thread->lock))))
       {
-        gearmand_perror(error, "pthread_mutex_unlock");
-        gearmand_fatal("Error in locking forcing a shutdown");
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed, forcing a shutdown");
         gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
       }
 
@@ -304,8 +310,7 @@ void gearmand_con_check_queue(gearmand_thread_st *thread)
     }
     else
     {
-      gearmand_perror(error, "pthread_mutex_lock");
-      gearmand_fatal("Lock could not be taken on thread->, shutdown to occur");
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed, forcing a shutdown");
       gearmand_wakeup(Gearmand(), GEARMAND_WAKEUP_SHUTDOWN);
     }
   }

+ 9 - 5
libgearman-server/job_plus.cc

@@ -296,10 +296,10 @@ void *_proc(void *data)
     {
       if (server->proc_shutdown)
       {
-        if (pthread_mutex_unlock(&(server->proc_lock)) == -1)
+        int error;
+        if ((error= pthread_mutex_unlock(&(server->proc_lock))))
         {
-          gearmand_fatal("pthread_mutex_unlock()");
-          assert(!"pthread_mutex_lock");
+          gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
         }
         return NULL;
       }
@@ -307,9 +307,13 @@ void *_proc(void *data)
       (void) pthread_cond_wait(&(server->proc_cond), &(server->proc_lock));
     }
     server->proc_wakeup= false;
-    if (pthread_mutex_unlock(&(server->proc_lock)) == -1)
+
     {
-      gearmand_fatal("pthread_mutex_unlock()");
+      int error;
+      if ((error= pthread_mutex_unlock(&(server->proc_lock))))
+      {
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
+      }
     }
 
     for (gearman_server_thread_st *thread= server->thread_list; thread != NULL; thread= thread->next)

+ 20 - 16
libgearman-server/packet.cc

@@ -189,17 +189,18 @@ gearmand_error_t gearman_server_io_packet_add(gearman_server_con_st *con,
     server_packet->packet.options.free_data= true;
   }
 
-  if (pthread_mutex_lock(&con->thread->lock) == 0)
+  int error;
+  if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
   {
     gearmand_server_con_fifo_add(con, server_packet);
-    if (pthread_mutex_unlock(&con->thread->lock) != 0)
+    if ((error= pthread_mutex_unlock(&con->thread->lock)))
     {
-      gearmand_fatal("pthread_mutex_unlock()");
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
     }
   }
   else
   {
-    gearmand_fatal("pthread_mutex_lock()");
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
   }
 
   gearman_server_con_io_add(con);
@@ -213,17 +214,18 @@ void gearman_server_io_packet_remove(gearman_server_con_st *con)
 
   gearmand_packet_free(&(server_packet->packet));
 
-  if (pthread_mutex_lock(&con->thread->lock) == 0)
+  int error;
+  if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
   {
     gearmand_server_con_fifo_free(con, server_packet);
-    if (pthread_mutex_unlock(&con->thread->lock) != 0)
+    if ((error= pthread_mutex_unlock(&con->thread->lock)))
     {
-      gearmand_fatal("pthread_mutex_unlock()");
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
     }
   }
   else
   {
-    gearmand_fatal("pthread_mutex_lock()");
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
   }
 
   gearman_server_packet_free(server_packet, con->thread, true);
@@ -232,17 +234,18 @@ void gearman_server_io_packet_remove(gearman_server_con_st *con)
 void gearman_server_proc_packet_add(gearman_server_con_st *con,
                                     gearman_server_packet_st *packet)
 {
-  if (pthread_mutex_lock(&con->thread->lock) == 0)
+  int error;
+  if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
   {
     gearmand_server_con_fifo_proc_add(con, packet);
-    if (pthread_mutex_unlock(&con->thread->lock) != 0)
+    if ((error= pthread_mutex_unlock(&con->thread->lock)))
     {
-      gearmand_fatal("pthread_mutex_unlock()");
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
     }
   }
   else
   {
-    gearmand_fatal("pthread_mutex_lock()");
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
   }
 
   gearman_server_con_proc_add(con);
@@ -258,17 +261,18 @@ gearman_server_proc_packet_remove(gearman_server_con_st *con)
     return NULL;
   }
 
-  if (pthread_mutex_lock(&con->thread->lock) == 0)
+  int error;
+  if ((error= pthread_mutex_lock(&con->thread->lock)) == 0)
   {
     gearmand_server_con_fifo_proc_free(con, server_packet);
-    if (pthread_mutex_unlock(&con->thread->lock) != 0)
+    if ((error= pthread_mutex_unlock(&con->thread->lock)) != 0)
     {
-      gearmand_fatal("pthread_mutex_unlock()");
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
     }
   }
   else
   {
-    gearmand_fatal("pthread_mutex_lock()");
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
   }
 
   return server_packet;

+ 13 - 13
libgearman-server/plugins/queue/drizzle/queue.cc

@@ -490,7 +490,7 @@ gearmand_error_t gearman_server_queue_libdrizzle_init(plugins::queue::Drizzle *q
     drizzle_result_free(queue->result());
   }
 
-  gearman_server_set_queue(server, queue, _libdrizzle_add, _libdrizzle_flush, _libdrizzle_done, _libdrizzle_replay);
+  gearman_server_set_queue(*server, queue, _libdrizzle_add, _libdrizzle_flush, _libdrizzle_done, _libdrizzle_replay);
 
   return GEARMAN_SUCCESS;
 }
@@ -532,23 +532,23 @@ static gearmand_error_t _libdrizzle_add(gearman_server_st *,
   {
     query_size= (size_t)snprintf(query_buffer, sizeof(query_buffer),
                                  "INSERT INTO %.*s.%.*s SET priority=%u,when_to_run=%lld,unique_key='%.*s',function_name='%.*s',data='",
-                                 queue->schema.size(), queue->schema.c_str(),
-                                 queue->table.size(), queue->table.c_str(),
+                                 int(queue->schema.size()), queue->schema.c_str(),
+                                 int(queue->table.size()), queue->table.c_str(),
                                  uint32_t(priority),
                                  (long long unsigned int)when,
-                                 escaped_unique_name.size(), &escaped_unique_name[0],
-                                 escaped_function_name.size(), &escaped_function_name[0]
+                                 int(escaped_unique_name.size()), &escaped_unique_name[0],
+                                 int(escaped_function_name.size()), &escaped_function_name[0]
                                 );
   }
   else
   {
     query_size= (size_t)snprintf(query_buffer, sizeof(query_buffer),
                                  "INSERT INTO %.*s.%.*s SET priority=%u,unique_key='%.*s',function_name='%.*s',data='",
-                                 queue->schema.size(), queue->schema.c_str(),
-                                 queue->table.size(), queue->table.c_str(),
+                                 int(queue->schema.size()), queue->schema.c_str(),
+                                 int(queue->table.size()), queue->table.c_str(),
                                  uint32_t(priority),
-                                 escaped_unique_name.size(), &escaped_unique_name[0],
-                                 escaped_function_name.size(), &escaped_function_name[0]
+                                 int(escaped_unique_name.size()), &escaped_unique_name[0],
+                                 int(escaped_function_name.size()), &escaped_function_name[0]
                                 );
   }
 
@@ -604,10 +604,10 @@ static gearmand_error_t _libdrizzle_done(gearman_server_st *,
 
   int query_size= snprintf(&query[0], query.size(),
                            "DELETE FROM %.*s.%.*s WHERE unique_key='%.*s' and function_name= '%.*s'",
-                           queue->schema.size(), queue->schema.c_str(),
-                           queue->table.size(), queue->table.c_str(),
-                           escaped_unique_name.size(), &escaped_unique_name[0],
-                           escaped_function_name.size(), &escaped_function_name[0]
+                           int(queue->schema.size()), queue->schema.c_str(),
+                           int(queue->table.size()), queue->table.c_str(),
+                           int(escaped_unique_name.size()), &escaped_unique_name[0],
+                           int(escaped_function_name.size()), &escaped_function_name[0]
                           );
 
   if (query_size < 0 or size_t(query_size) > query.size())

+ 18 - 29
libgearman-server/text.cc

@@ -81,7 +81,7 @@ gearmand_error_t server_run_text(gearman_server_con_st *server_con,
          thread= thread->next)
     {
       int error;
-      if (! (error= pthread_mutex_lock(&thread->lock)))
+      if ((error= pthread_mutex_lock(&thread->lock)) == 0)
       {
         for (gearman_server_con_st *con= thread->con_list; con != NULL; con= con->next)
         {
@@ -101,11 +101,9 @@ gearmand_error_t server_run_text(gearman_server_con_st *server_con,
             char *new_data= (char *)realloc(data, total + GEARMAN_TEXT_RESPONSE_SIZE);
             if (new_data == NULL)
             {
-              int pthread_error;
-              if ((pthread_error= pthread_mutex_unlock(&(thread->lock))) == -1)
+              if ((error= pthread_mutex_unlock(&(thread->lock))))
               {
-                gearmand_perror(pthread_error, "pthread_mutex_unlock()");
-                assert(!"pthread_mutex_lock");
+                gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
               }
               free(data);
               return gearmand_perror(ENOMEM, "realloc");
@@ -120,12 +118,11 @@ gearmand_error_t server_run_text(gearman_server_con_st *server_con,
 
           if ((size_t)sn_checked_length > total - size || sn_checked_length < 0)
           {
-            int pthread_error;
-            if ((pthread_error= pthread_mutex_unlock(&(thread->lock))) == -1)
+            if ((error= pthread_mutex_unlock(&(thread->lock))))
             {
-              gearmand_perror(pthread_error, "pthread_mutex_unlock()");
-              assert(!"pthread_mutex_lock");
+              gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
             }
+
             free(data);
             return gearmand_perror(ENOMEM, "snprintf");
           }
@@ -144,12 +141,11 @@ gearmand_error_t server_run_text(gearman_server_con_st *server_con,
 
             if ((size_t)checked_length > total - size || checked_length < 0)
             {
-              int pthread_error;
-              if ((pthread_error= (pthread_mutex_unlock(&(thread->lock)))) == -1)
+              if ((error= (pthread_mutex_unlock(&(thread->lock)))))
               {
-                gearmand_perror(pthread_error, "pthread_mutex_unlock()");
-                assert(!"pthread_mutex_lock");
+                gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
               }
+
               free(data);
               return gearmand_perror(ENOMEM, "snprintf");
             }
@@ -167,30 +163,25 @@ gearmand_error_t server_run_text(gearman_server_con_st *server_con,
           int checked_length= snprintf(data + size, total - size, "\n");
           if ((size_t)checked_length > total - size || checked_length < 0)
           {
-            int pthread_error;
-            if ((pthread_error= (pthread_mutex_unlock(&(thread->lock)))) == -1)
+            if ((error= (pthread_mutex_unlock(&(thread->lock)))))
             {
-              gearmand_perror(pthread_error, "pthread_mutex_unlock()");
-              assert(!"pthread_mutex_lock");
+              gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
             }
-            gearmand_debug("free");
+
             free(data);
             return gearmand_perror(ENOMEM, "snprintf");
           }
           size+= (size_t)checked_length;
         }
 
-        int pthread_error;
-        if ((pthread_error= (pthread_mutex_unlock(&(thread->lock)))) == -1)
+        if ((error= (pthread_mutex_unlock(&(thread->lock)))) != 0)
         {
-          gearmand_perror(pthread_error, "pthread_mutex_unlock()");
-          assert(!"pthread_mutex_lock");
+          gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
         }
       }
       else
       {
-        gearmand_perror(error, "pthread_mutex_lock");
-        assert(! "pthread_mutex_lock");
+        gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
       }
     }
 
@@ -431,16 +422,14 @@ gearmand_error_t server_run_text(gearman_server_con_st *server_con,
   if ((error= pthread_mutex_lock(&server_con->thread->lock)) == 0)
   {
     GEARMAN_FIFO_ADD(server_con->io_packet, server_packet,);
-    if ((error= pthread_mutex_unlock(&(server_con->thread->lock))) == -1)
+    if ((error= pthread_mutex_unlock(&(server_con->thread->lock))))
     {
-      gearmand_perror(error, "pthread_mutex_unlock()");
-      assert(!"pthread_mutex_lock");
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed");
     }
   }
   else
   {
-    gearmand_perror(error, "pthread_mutex_lock");
-    assert(!"pthread_mutex_lock");
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed");
   }
 
   gearman_server_con_io_add(server_con);

+ 40 - 17
libgearman-server/thread.c

@@ -380,38 +380,36 @@ static gearmand_error_t _proc_thread_start(gearman_server_st *server)
   int error;
   if ((error= pthread_mutex_init(&(server->proc_lock), NULL)))
   {
-    gearmand_perror(error, "pthread_mutex_init");
-    return GEARMAN_ERRNO;
+    return gearmand_perror(error, "pthread_mutex_init");
   }
 
   if ((error= pthread_cond_init(&(server->proc_cond), NULL)))
   {
-    gearmand_perror(error, "pthread_cond_init");
-    return GEARMAN_ERRNO;
+    return gearmand_perror(error, "pthread_cond_init");
   }
 
   pthread_attr_t attr;
   if ((error= pthread_attr_init(&attr)))
   {
-    gearmand_perror(error, "pthread_attr_init");
-    return GEARMAN_ERRNO;
+    return gearmand_perror(error, "pthread_attr_init");
   }
 
   if ((error= pthread_attr_setscope(&attr, PTHREAD_SCOPE_SYSTEM)))
   {
-    gearmand_perror(error, "pthread_attr_setscope");
     (void) pthread_attr_destroy(&attr);
-    return GEARMAN_ERRNO;
+    return gearmand_perror(error, "pthread_attr_setscope");
   }
 
   if ((error= pthread_create(&(server->proc_id), &attr, _proc, server)))
   {
-    gearmand_perror(error, "pthread_create");
     (void) pthread_attr_destroy(&attr);
-    return GEARMAN_ERRNO;
+    return gearmand_perror(error, "pthread_create");
   }
 
-  (void) pthread_attr_destroy(&attr);
+  if ((error= pthread_attr_destroy(&attr)))
+  {
+    gearmand_perror(error, "pthread_create");
+  }
 
   server->flags.threaded= true;
 
@@ -428,12 +426,37 @@ static void _proc_thread_kill(gearman_server_st *server)
   server->proc_shutdown= true;
 
   /* Signal proc thread to shutdown. */
-  (void) pthread_mutex_lock(&(server->proc_lock));
-  (void) pthread_cond_signal(&(server->proc_cond));
-  (void) pthread_mutex_unlock(&(server->proc_lock));
+  int error;
+  if ((error= pthread_mutex_lock(&(server->proc_lock))) == 0)
+  {
+    if ((error= pthread_cond_signal(&(server->proc_cond))))
+    {
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_cond_signal() failed, forcing a shutdown");
+    }
+
+    if ((error= pthread_mutex_unlock(&(server->proc_lock))))
+    {
+      gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_unlock() failed, forcing a shutdown");
+    }
+  }
+  else
+  {
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_lock() failed, forcing a shutdown");
+  }
 
   /* Wait for the proc thread to exit and then cleanup. */
-  (void) pthread_join(server->proc_id, NULL);
-  (void) pthread_cond_destroy(&(server->proc_cond));
-  (void) pthread_mutex_destroy(&(server->proc_lock));
+  if ((error= pthread_join(server->proc_id, NULL)))
+  {
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_join() failed, forcing a shutdown");
+  }
+
+  if ((error= pthread_cond_destroy(&(server->proc_cond))))
+  {
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_cond_destroy() failed, forcing a shutdown");
+  }
+
+  if ((error= pthread_mutex_destroy(&(server->proc_lock))))
+  {
+    gearmand_log_fatal_perror(GEARMAN_DEFAULT_LOG_PARAM, error, "pthread_mutex_destroy() failed, forcing a shutdown");
+  }
 }