1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 #include "libmemcached/common.h"
3 #include "libmemcached/memcached_pool.h"
8 struct memcached_pool_st
10 pthread_mutex_t mutex
;
16 uint32_t current_size
;
20 static memcached_return
mutex_enter(pthread_mutex_t
*mutex
)
24 ret
= pthread_mutex_lock(mutex
);
25 while (ret
== -1 && errno
== EINTR
);
27 return (ret
== -1) ? MEMCACHED_ERRNO
: MEMCACHED_SUCCESS
;
30 static memcached_return
mutex_exit(pthread_mutex_t
*mutex
) {
33 ret
= pthread_mutex_unlock(mutex
);
34 while (ret
== -1 && errno
== EINTR
);
36 return (ret
== -1) ? MEMCACHED_ERRNO
: MEMCACHED_SUCCESS
;
40 * Grow the connection pool by creating a connection structure and clone the
41 * original memcached handle.
43 static int grow_pool(memcached_pool_st
* pool
) {
44 memcached_st
*obj
= calloc(1, sizeof(*obj
));
48 if (memcached_clone(obj
, pool
->master
) == NULL
)
54 pool
->mmc
[++pool
->firstfree
] = obj
;
60 memcached_pool_st
*memcached_pool_create(memcached_st
* mmc
,
61 uint32_t initial
, uint32_t max
)
63 memcached_pool_st
* ret
= NULL
;
64 memcached_pool_st object
= { .mutex
= PTHREAD_MUTEX_INITIALIZER
,
65 .cond
= PTHREAD_COND_INITIALIZER
,
67 .mmc
= calloc(max
, sizeof(memcached_st
*)),
72 if (object
.mmc
!= NULL
)
74 ret
= calloc(1, sizeof(*ret
));
83 /* Try to create the initial size of the pool. An allocation failure at
84 * this time is not fatal..
86 for (unsigned int ii
=0; ii
< initial
; ++ii
)
87 if (grow_pool(ret
) == -1)
94 memcached_st
* memcached_pool_destroy(memcached_pool_st
* pool
)
96 memcached_st
*ret
= pool
->master
;
98 for (int xx
= 0; xx
<= pool
->firstfree
; ++xx
)
100 memcached_free(pool
->mmc
[xx
]);
102 pool
->mmc
[xx
] = NULL
;
105 pthread_mutex_destroy(&pool
->mutex
);
106 pthread_cond_destroy(&pool
->cond
);
113 memcached_st
* memcached_pool_pop(memcached_pool_st
* pool
,
115 memcached_return
*rc
)
117 memcached_st
*ret
= NULL
;
118 if ((*rc
= mutex_enter(&pool
->mutex
)) != MEMCACHED_SUCCESS
)
123 if (pool
->firstfree
> -1)
124 ret
= pool
->mmc
[pool
->firstfree
--];
125 else if (pool
->current_size
== pool
->size
)
129 *rc
= mutex_exit(&pool
->mutex
);
133 if (pthread_cond_wait(&pool
->cond
, &pool
->mutex
) == -1)
136 mutex_exit(&pool
->mutex
);
138 *rc
= MEMCACHED_ERRNO
;
142 else if (grow_pool(pool
) == -1)
144 *rc
= mutex_exit(&pool
->mutex
);
150 *rc
= mutex_exit(&pool
->mutex
);
155 memcached_return
memcached_pool_push(memcached_pool_st
* pool
,
158 memcached_return rc
= mutex_enter(&pool
->mutex
);
160 if (rc
!= MEMCACHED_SUCCESS
)
163 char* version
= memcached_get_user_data(mmc
);
164 /* Someone updated the behavior on the object.. */
165 if (version
!= pool
->version
)
168 memset(mmc
, 0, sizeof(*mmc
));
169 if (memcached_clone(mmc
, pool
->master
) == NULL
)
171 rc
= MEMCACHED_SOME_ERRORS
;
175 pool
->mmc
[++pool
->firstfree
]= mmc
;
177 if (pool
->firstfree
== 0 && pool
->current_size
== pool
->size
)
179 /* we might have people waiting for a connection.. wake them up :-) */
180 pthread_cond_broadcast(&pool
->cond
);
183 memcached_return rval
= mutex_exit(&pool
->mutex
);
184 if (rc
== MEMCACHED_SOME_ERRORS
)
191 memcached_return
memcached_pool_behavior_set(memcached_pool_st
*pool
,
192 memcached_behavior flag
,
196 memcached_return rc
= mutex_enter(&pool
->mutex
);
197 if (rc
!= MEMCACHED_SUCCESS
)
200 /* update the master */
201 rc
= memcached_behavior_set(pool
->master
, flag
, data
);
202 if (rc
!= MEMCACHED_SUCCESS
)
204 mutex_exit(&pool
->mutex
);
209 memcached_set_user_data(pool
->master
, pool
->version
);
210 /* update the clones */
211 for (int xx
= 0; xx
<= pool
->firstfree
; ++xx
)
213 rc
= memcached_behavior_set(pool
->mmc
[xx
], flag
, data
);
214 if (rc
== MEMCACHED_SUCCESS
)
215 memcached_set_user_data(pool
->mmc
[xx
], pool
->version
);
218 memcached_free(pool
->mmc
[xx
]);
219 memset(pool
->mmc
[xx
], 0, sizeof(*pool
->mmc
[xx
]));
220 if (memcached_clone(pool
->mmc
[xx
], pool
->master
) == NULL
)
222 /* I'm not sure what to do in this case.. this would happen
223 if we fail to push the server list inside the client..
224 I should add a testcase for this, but I believe the following
225 would work, except that you would add a hole in the pool list..
226 in theory you could end up with an empty pool....
234 return mutex_exit(&pool
->mutex
);
237 memcached_return
memcached_pool_behavior_get(memcached_pool_st
*pool
,
238 memcached_behavior flag
,
241 memcached_return rc
= mutex_enter(&pool
->mutex
);
242 if (rc
!= MEMCACHED_SUCCESS
)
244 *value
= memcached_behavior_get(pool
->master
, flag
);
245 return mutex_exit(&pool
->mutex
);