18 #include "plugin/gearman_udf/function_map.h"
20 #include <libgearman/gearman.h>
34 GearmanFunctionMap::GearmanFunctionMap()
36 (void) pthread_mutex_init(&lock, NULL);
39 GearmanFunctionMap::~GearmanFunctionMap()
41 map<string, gearman_client_st>::iterator x;
43 for (x= functionMap.begin(); x != functionMap.end(); x++)
44 gearman_client_free(&((*x).second));
46 (void) pthread_mutex_destroy(&lock);
49 bool GearmanFunctionMap::add(
string function,
string servers)
51 map<string, gearman_client_st>::iterator x;
54 pthread_mutex_lock(&lock);
56 x= functionMap.find(
function);
57 if (x == functionMap.end())
59 if (gearman_client_create(&(functionMap[
function])) == NULL)
61 pthread_mutex_unlock(&lock);
66 gearman_client_remove_servers(&(functionMap[
function]));
67 ret= gearman_client_add_servers(&(functionMap[
function]), servers.c_str());
68 pthread_mutex_unlock(&lock);
69 if (ret != GEARMAN_SUCCESS)
77 bool GearmanFunctionMap::get(
string function, gearman_client_st *
client)
79 map<string, gearman_client_st>::iterator x;
81 if (pthread_mutex_lock(&lock) != 0)
86 x= functionMap.find(
function);
87 if (x == functionMap.end())
89 x= functionMap.find(
string(
""));
90 if (x == functionMap.end())
92 pthread_mutex_unlock(&lock);
98 if (gearman_client_clone(client, &((*x).second)) == NULL)
100 pthread_mutex_unlock(&lock);
104 pthread_mutex_unlock(&lock);