Browse Source

Add gearmand_st to thread.

Brian Aker 11 years ago
parent
commit
4e0f1c3b5d

+ 1 - 1
libgearman-server/gearmand.cc

@@ -379,7 +379,7 @@ gearmand_error_t gearmand_run(gearmand_st *gearmand)
     uint32_t x= 0;
     do
     {
-      gearmand->ret= gearmand_thread_create(gearmand);
+      gearmand->ret= gearmand_thread_create(*gearmand);
       if (gearmand->ret != GEARMAND_SUCCESS)
         return gearmand->ret;
       x++;

+ 24 - 7
libgearman-server/gearmand_thread.cc

@@ -159,21 +159,38 @@ namespace {
 
 }
 
+gearmand_thread_st::gearmand_thread_st(gearmand_st& gearmand_):
+  is_thread_lock(false),
+  is_wakeup_event(false),
+  count(0),
+  dcon_count(0),
+  dcon_add_count(0),
+  free_dcon_count(0),
+  _gearmand(gearmand_),
+  next(NULL),
+  prev(NULL),
+  base(NULL),
+  dcon_list(NULL),
+  dcon_add_list(NULL),
+  free_dcon_list(0)
+{
+}
+
 /** @} */
 
 /*
  * Public definitions
  */
 
-gearmand_error_t gearmand_thread_create(gearmand_st *gearmand)
+gearmand_error_t gearmand_thread_create(gearmand_st& gearmand)
 {
-  gearmand_thread_st* thread= new (std::nothrow) gearmand_thread_st;
+  gearmand_thread_st* thread= new (std::nothrow) gearmand_thread_st(gearmand);
   if (thread == NULL)
   {
     return gearmand_merror("new", gearmand_thread_st, 1);
   }
 
-  if (! gearman_server_thread_init(gearmand_server(gearmand), &(thread->server_thread),
+  if (! gearman_server_thread_init(gearmand_server(&gearmand), &(thread->server_thread),
                                    _log, thread, gearmand_connection_watch))
   {
     delete thread;
@@ -198,9 +215,9 @@ gearmand_error_t gearmand_thread_create(gearmand_st *gearmand)
 
   /* If we have no threads, we still create a fake thread that uses the main
      libevent instance. Otherwise create a libevent instance for each thread. */
-  if (gearmand->threads == 0)
+  if (gearmand.threads == 0)
   {
-    thread->base= gearmand->base;
+    thread->base= gearmand.base;
   }
   else
   {
@@ -224,12 +241,12 @@ gearmand_error_t gearmand_thread_create(gearmand_st *gearmand)
   }
 
   /* If we are not running multi-threaded, just return the thread context. */
-  if (gearmand->threads == 0)
+  if (gearmand.threads == 0)
   {
     return GEARMAND_SUCCESS;
   }
 
-  thread->count= gearmand->thread_count;
+  thread->count= gearmand.thread_count;
 
   int pthread_ret= pthread_mutex_init(&(thread->lock), NULL);
   if (pthread_ret != 0)

+ 1 - 15
libgearman-server/gearmand_thread.h

@@ -47,10 +47,6 @@
 
 #include <libgearman-server/struct/gearmand_thread.h>
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 /**
  * @addtogroup gearmand_thread Thread Declarations
  * @ingroup gearmand
@@ -66,14 +62,12 @@ extern "C" {
  *        gearmand_create.
  * @return Standard gearman return value.
  */
-GEARMAN_API
-gearmand_error_t gearmand_thread_create(struct gearmand_st *gearmand);
+gearmand_error_t gearmand_thread_create(struct gearmand_st& gearmand);
 
 /**
  * Free resources used by a thread.
  * @param thread Thread previously initialized with gearmand_thread_create.
  */
-GEARMAN_API
 void gearmand_thread_free(gearmand_thread_st *thread);
 
 /**
@@ -82,7 +76,6 @@ void gearmand_thread_free(gearmand_thread_st *thread);
  * gearmand_thread_create.
  * @param wakeup Wakeup event to send to running thread.
  */
-GEARMAN_API
 void gearmand_thread_wakeup(gearmand_thread_st *thread,
                             gearmand_wakeup_t wakeup);
 
@@ -91,11 +84,4 @@ void gearmand_thread_wakeup(gearmand_thread_st *thread,
  * @param thread Thread structure previously initialized with
  * gearmand_thread_create.
  */
-GEARMAN_API
 void gearmand_thread_run(gearmand_thread_st *thread);
-
-/** @} */
-
-#ifdef __cplusplus
-}
-#endif

+ 4 - 15
libgearman-server/struct/gearmand_thread.h

@@ -37,6 +37,8 @@
 
 #pragma once
 
+class gearmand_st;
+
 struct gearmand_thread_st
 {
   bool is_thread_lock;
@@ -46,6 +48,7 @@ struct gearmand_thread_st
   uint32_t dcon_add_count;
   uint32_t free_dcon_count;
   int wakeup_fd[2];
+  gearmand_st& _gearmand;
   gearmand_thread_st *next;
   gearmand_thread_st *prev;
   struct event_base *base;
@@ -57,19 +60,5 @@ struct gearmand_thread_st
   pthread_t id;
   pthread_mutex_t lock;
 
-  gearmand_thread_st():
-    is_thread_lock(false),
-    is_wakeup_event(false),
-    count(0),
-    dcon_count(0),
-    dcon_add_count(0),
-    free_dcon_count(0),
-    next(NULL),
-    prev(NULL),
-    base(NULL),
-    dcon_list(NULL),
-    dcon_add_list(NULL),
-    free_dcon_list(0)
-  {
-  }
+  gearmand_thread_st(gearmand_st&);
 };