Exemplo n.º 1
0
bool gearman_server_thread_init(gearman_server_st *server,
                                gearman_server_thread_st *thread,
                                gearman_log_server_fn *log_function,
                                gearmand_thread_st *context,
                                gearmand_event_watch_fn *event_watch)
{
  assert(server);
  assert(thread);
  if (server->thread_count == 1)
  {
    /* The server is going to be multi-threaded, start processing thread. */
    if (_proc_thread_start(server) != GEARMAN_SUCCESS)
    {
      return false;
    }
  }

  thread->con_count= 0;
  thread->io_count= 0;
  thread->proc_count= 0;
  thread->to_be_freed_count= 0;
  thread->free_con_count= 0;
  thread->free_packet_count= 0;
  thread->log_fn= log_function;
  thread->log_context= context;
  thread->run_fn= NULL;
  thread->run_fn_arg= NULL;
  thread->con_list= NULL;
  thread->io_list= NULL;
  thread->proc_list= NULL;
  thread->free_con_list= NULL;
  thread->free_packet_list= NULL;
  thread->to_be_freed_list= NULL;

  int error;
  if ((error= pthread_mutex_init(&(thread->lock), NULL)))
  {
    errno= error;
    gearmand_perror("pthread_mutex_init");
    return false;
  }

  GEARMAN_LIST_ADD(server->thread, thread,);

  thread->gearman= &(thread->gearmand_connection_list_static);
  gearmand_connection_list_init(thread->gearman, event_watch, NULL);

  return true;
}
Exemplo n.º 2
0
gearman_server_thread_st *
gearman_server_thread_create(gearman_server_st *server,
                             gearman_server_thread_st *thread)
{
  if (server->thread_count == 1)
  {
    /* The server is going to be multi-threaded, start processing thread. */
    if (_proc_thread_start(server) != GEARMAN_SUCCESS)
      return NULL;
  }

  if (thread == NULL)
  {
    thread= (gearman_server_thread_st *)malloc(sizeof(gearman_server_thread_st));
    if (thread == NULL)
    {
      _proc_thread_kill(server);
      return NULL;
    }

    thread->options.allocated= true;
  }
  else
  {
    thread->options.allocated= false;
  }

  thread->con_count= 0;
  thread->io_count= 0;
  thread->proc_count= 0;
  thread->free_con_count= 0;
  thread->free_packet_count= 0;
  thread->server= server;
  thread->log_fn= NULL;
  thread->log_context= NULL;
  thread->run_fn= NULL;
  thread->run_fn_arg= NULL;
  thread->con_list= NULL;
  thread->io_list= NULL;
  thread->proc_list= NULL;
  thread->free_con_list= NULL;
  thread->free_packet_list= NULL;

  if (pthread_mutex_init(&(thread->lock), NULL) != 0)
  {
    if (thread->options.allocated)
      free(thread);

    return NULL;
  }

  GEARMAN_LIST_ADD(server->thread, thread,);

  gearman_universal_options_t options[]= { GEARMAN_NON_BLOCKING, GEARMAN_DONT_TRACK_PACKETS, GEARMAN_MAX};
  thread->gearman= gearman_universal_create(&(thread->gearman_universal_static), options);
  if (thread->gearman == NULL)
  {
    gearman_server_thread_free(thread);
    return NULL;
  }

  return thread;
}