2 * Copyright (C) 2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: connects to a host, and makes sure it is alive.
12 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
13 #include "libmemcached/common.h"
14 #include "libmemcached/memcached_util.h"
19 struct memcached_pool_st
21 pthread_mutex_t mutex
;
27 uint32_t current_size
;
31 static memcached_return_t
mutex_enter(pthread_mutex_t
*mutex
)
35 ret
= pthread_mutex_lock(mutex
);
36 while (ret
== -1 && errno
== EINTR
);
38 return (ret
== -1) ? MEMCACHED_ERRNO
: MEMCACHED_SUCCESS
;
41 static memcached_return_t
mutex_exit(pthread_mutex_t
*mutex
)
45 ret
= pthread_mutex_unlock(mutex
);
46 while (ret
== -1 && errno
== EINTR
);
48 return (ret
== -1) ? MEMCACHED_ERRNO
: MEMCACHED_SUCCESS
;
52 * Grow the connection pool by creating a connection structure and clone the
53 * original memcached handle.
55 static int grow_pool(memcached_pool_st
* pool
)
57 memcached_st
*obj
= calloc(1, sizeof(*obj
));
62 if (memcached_clone(obj
, pool
->master
) == NULL
)
68 pool
->mmc
[++pool
->firstfree
] = obj
;
74 memcached_pool_st
*memcached_pool_create(memcached_st
* mmc
,
75 uint32_t initial
, uint32_t max
)
77 memcached_pool_st
* ret
= NULL
;
78 memcached_pool_st object
= { .mutex
= PTHREAD_MUTEX_INITIALIZER
,
79 .cond
= PTHREAD_COND_INITIALIZER
,
81 .mmc
= calloc(max
, sizeof(memcached_st
*)),
86 if (object
.mmc
!= NULL
)
88 ret
= calloc(1, sizeof(*ret
));
98 Try to create the initial size of the pool. An allocation failure at
99 this time is not fatal..
101 for (unsigned int ii
= 0; ii
< initial
; ++ii
)
103 if (grow_pool(ret
) == -1)
111 memcached_st
* memcached_pool_destroy(memcached_pool_st
* pool
)
113 memcached_st
*ret
= pool
->master
;
115 for (int xx
= 0; xx
<= pool
->firstfree
; ++xx
)
117 memcached_free(pool
->mmc
[xx
]);
119 pool
->mmc
[xx
] = NULL
;
122 pthread_mutex_destroy(&pool
->mutex
);
123 pthread_cond_destroy(&pool
->cond
);
130 memcached_st
* memcached_pool_pop(memcached_pool_st
* pool
,
132 memcached_return_t
*rc
)
134 memcached_st
*ret
= NULL
;
135 if ((*rc
= mutex_enter(&pool
->mutex
)) != MEMCACHED_SUCCESS
)
140 if (pool
->firstfree
> -1)
141 ret
= pool
->mmc
[pool
->firstfree
--];
142 else if (pool
->current_size
== pool
->size
)
146 *rc
= mutex_exit(&pool
->mutex
);
150 if (pthread_cond_wait(&pool
->cond
, &pool
->mutex
) == -1)
153 mutex_exit(&pool
->mutex
);
155 *rc
= MEMCACHED_ERRNO
;
159 else if (grow_pool(pool
) == -1)
161 *rc
= mutex_exit(&pool
->mutex
);
167 *rc
= mutex_exit(&pool
->mutex
);
172 memcached_return_t
memcached_pool_push(memcached_pool_st
* pool
,
175 memcached_return_t rc
= mutex_enter(&pool
->mutex
);
177 if (rc
!= MEMCACHED_SUCCESS
)
180 char* version
= memcached_get_user_data(mmc
);
181 /* Someone updated the behavior on the object.. */
182 if (version
!= pool
->version
)
185 memset(mmc
, 0, sizeof(*mmc
));
186 if (memcached_clone(mmc
, pool
->master
) == NULL
)
188 rc
= MEMCACHED_SOME_ERRORS
;
192 pool
->mmc
[++pool
->firstfree
]= mmc
;
194 if (pool
->firstfree
== 0 && pool
->current_size
== pool
->size
)
196 /* we might have people waiting for a connection.. wake them up :-) */
197 pthread_cond_broadcast(&pool
->cond
);
200 memcached_return_t rval
= mutex_exit(&pool
->mutex
);
201 if (rc
== MEMCACHED_SOME_ERRORS
)
208 memcached_return_t
memcached_pool_behavior_set(memcached_pool_st
*pool
,
209 memcached_behavior_t flag
,
213 memcached_return_t rc
= mutex_enter(&pool
->mutex
);
214 if (rc
!= MEMCACHED_SUCCESS
)
217 /* update the master */
218 rc
= memcached_behavior_set(pool
->master
, flag
, data
);
219 if (rc
!= MEMCACHED_SUCCESS
)
221 mutex_exit(&pool
->mutex
);
226 memcached_set_user_data(pool
->master
, pool
->version
);
227 /* update the clones */
228 for (int xx
= 0; xx
<= pool
->firstfree
; ++xx
)
230 rc
= memcached_behavior_set(pool
->mmc
[xx
], flag
, data
);
231 if (rc
== MEMCACHED_SUCCESS
)
232 memcached_set_user_data(pool
->mmc
[xx
], pool
->version
);
235 memcached_free(pool
->mmc
[xx
]);
236 memset(pool
->mmc
[xx
], 0, sizeof(*pool
->mmc
[xx
]));
237 if (memcached_clone(pool
->mmc
[xx
], pool
->master
) == NULL
)
239 /* I'm not sure what to do in this case.. this would happen
240 if we fail to push the server list inside the client..
241 I should add a testcase for this, but I believe the following
242 would work, except that you would add a hole in the pool list..
243 in theory you could end up with an empty pool....
251 return mutex_exit(&pool
->mutex
);
254 memcached_return_t
memcached_pool_behavior_get(memcached_pool_st
*pool
,
255 memcached_behavior_t flag
,
258 memcached_return_t rc
= mutex_enter(&pool
->mutex
);
260 if (rc
!= MEMCACHED_SUCCESS
)
265 *value
= memcached_behavior_get(pool
->master
, flag
);
267 return mutex_exit(&pool
->mutex
);