Gearman Developer Documentation

libgearman-server/function.c
Go to the documentation of this file.
00001 /* Gearman server and library
00002  * Copyright (C) 2008 Brian Aker, Eric Day
00003  * All rights reserved.
00004  *
00005  * Use and distribution licensed under the BSD license.  See
00006  * the COPYING file in the parent directory for full text.
00007  */
00008 
00014 #include "common.h"
00015 
00016 /*
00017  * Public definitions
00018  */
00019 
00020 gearman_server_function_st *
00021 gearman_server_function_get(gearman_server_st *server,
00022                             const char *function_name,
00023                             size_t function_name_size)
00024 {
00025   gearman_server_function_st *function;
00026 
00027   for (function= server->function_list; function != NULL;
00028        function= function->next)
00029   {
00030     if (function->function_name_size == function_name_size &&
00031         !memcmp(function->function_name, function_name, function_name_size))
00032     {
00033       return function;
00034     }
00035   }
00036 
00037   function= gearman_server_function_create(server, NULL);
00038   if (function == NULL)
00039     return NULL;
00040 
00041   function->function_name= (char *)malloc(function_name_size + 1);
00042   if (function->function_name == NULL)
00043   {
00044     gearman_server_function_free(function);
00045     return NULL;
00046   }
00047 
00048   memcpy(function->function_name, function_name, function_name_size);
00049   function->function_name[function_name_size]= 0;
00050   function->function_name_size= function_name_size;
00051 
00052   return function;
00053 }
00054 
00055 gearman_server_function_st *
00056 gearman_server_function_create(gearman_server_st *server,
00057                                gearman_server_function_st *function)
00058 {
00059   if (function == NULL)
00060   {
00061     function= (gearman_server_function_st *)malloc(sizeof(gearman_server_function_st));
00062     if (function == NULL)
00063       return NULL;
00064 
00065     function->options.allocated= true;
00066   }
00067   else
00068   {
00069     function->options.allocated= false;
00070   }
00071 
00072   function->worker_count= 0;
00073   function->job_count= 0;
00074   function->job_total= 0;
00075   function->job_running= 0;
00076   function->max_queue_size= GEARMAN_DEFAULT_MAX_QUEUE_SIZE;
00077   function->function_name_size= 0;
00078   function->server= server;
00079   GEARMAN_LIST_ADD(server->function, function,)
00080   function->function_name= NULL;
00081   function->worker_list= NULL;
00082   memset(function->job_list, 0,
00083          sizeof(gearman_server_job_st *) * GEARMAN_JOB_PRIORITY_MAX);
00084   memset(function->job_end, 0,
00085          sizeof(gearman_server_job_st *) * GEARMAN_JOB_PRIORITY_MAX);
00086 
00087   return function;
00088 }
00089 
00090 void gearman_server_function_free(gearman_server_function_st *function)
00091 {
00092   if (function->function_name != NULL)
00093     free(function->function_name);
00094 
00095   GEARMAN_LIST_DEL(function->server->function, function,)
00096 
00097   if (function->options.allocated)
00098     free(function);
00099 }