1
/* Copyright (C) 2009 Sun Microsystems
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
16
#include "function_map.h"
17
#include <libgearman/client.h>
23
/* Constructor and destructor happen during module dlopen/dlclose. */
24
static GearmanFunctionMap _functionMap;
26
GearmanFunctionMap& GetFunctionMap(void)
31
GearmanFunctionMap::GearmanFunctionMap()
33
(void) pthread_mutex_init(&lock, NULL);
36
GearmanFunctionMap::~GearmanFunctionMap()
38
map<string, gearman_client_st>::iterator x;
40
for (x= functionMap.begin(); x != functionMap.end(); x++)
41
gearman_client_free(&((*x).second));
43
(void) pthread_mutex_destroy(&lock);
46
bool GearmanFunctionMap::add(string function, string servers)
48
map<string, gearman_client_st>::iterator x;
51
pthread_mutex_lock(&lock);
53
x= functionMap.find(function);
54
if (x == functionMap.end())
56
if (gearman_client_create(&(functionMap[function])) == NULL)
58
pthread_mutex_unlock(&lock);
63
gearman_client_remove_servers(&(functionMap[function]));
64
ret= gearman_client_add_servers(&(functionMap[function]), servers.c_str());
65
pthread_mutex_unlock(&lock);
66
if (ret != GEARMAN_SUCCESS)
72
bool GearmanFunctionMap::get(string function, gearman_client_st *client)
74
map<string, gearman_client_st>::iterator x;
76
pthread_mutex_lock(&lock);
78
x= functionMap.find(function);
79
if (x == functionMap.end())
81
x= functionMap.find(string(""));
82
if (x == functionMap.end())
84
pthread_mutex_unlock(&lock);
89
/* Clone the object, the list of host:port pairs get cloned with it. */
90
if (gearman_client_clone(client, &((*x).second)) == NULL)
92
pthread_mutex_unlock(&lock);
96
pthread_mutex_unlock(&lock);