|
| 1 | + |
| 2 | +/* |
| 3 | + * ****************************************************************************** |
| 4 | + * Copyright 2014-2016 Spectra Logic Corporation. All Rights Reserved. |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"). You may not use |
| 6 | + * this file except in compliance with the License. A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. |
| 11 | + * This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 12 | + * CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 13 | + * specific language governing permissions and limitations under the License. |
| 14 | + * **************************************************************************** |
| 15 | + */ |
| 16 | + |
| 17 | +#include <string.h> |
| 18 | +#include <curl/curl.h> |
| 19 | +#include <glib.h> |
| 20 | +#include "ds3_net.h" |
| 21 | + |
| 22 | +ds3_connection_pool* ds3_connection_pool_init(void) { |
| 23 | + ds3_connection_pool* pool = g_new0(ds3_connection_pool, 1); |
| 24 | + g_mutex_init(&pool->mutex); |
| 25 | + g_cond_init(&pool->available_connections); |
| 26 | + return pool; |
| 27 | +} |
| 28 | + |
| 29 | +void ds3_connection_pool_clear(ds3_connection_pool* pool) { |
| 30 | + int index; |
| 31 | + |
| 32 | + if (pool == NULL) { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + g_mutex_lock(&pool->mutex); |
| 37 | + |
| 38 | + for (index = 0; index < CONNECTION_POOL_SIZE; index++) { |
| 39 | + if (pool->connections[index] != NULL) { |
| 40 | + curl_easy_cleanup(pool->connections[index]); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + g_mutex_unlock(&pool->mutex); |
| 45 | + g_mutex_clear(&pool->mutex); |
| 46 | + g_cond_clear(&pool->available_connections); |
| 47 | +} |
| 48 | + |
| 49 | +static int _pool_inc(ds3_connection_pool* pool, int index) { |
| 50 | + return (index+1) % CONNECTION_POOL_SIZE; |
| 51 | +} |
| 52 | + |
| 53 | +static int _pool_full(ds3_connection_pool* pool) { |
| 54 | + return (_pool_inc(pool, pool->tail) == pool->head); |
| 55 | +} |
| 56 | + |
| 57 | + |
| 58 | +ds3_connection* ds3_connection_acquire(ds3_connection_pool* pool) { |
| 59 | + ds3_connection* connection = NULL; |
| 60 | + |
| 61 | + g_mutex_lock(&pool->mutex); |
| 62 | + while (_pool_full(pool)) { |
| 63 | + g_cond_wait(&pool->available_connections, &pool->mutex); |
| 64 | + } |
| 65 | + |
| 66 | + if (pool->connections[pool->head] == NULL) { |
| 67 | + connection = curl_easy_init(); |
| 68 | + |
| 69 | + pool->connections[pool->head] = connection; |
| 70 | + } else { |
| 71 | + connection = pool->connections[pool->head]; |
| 72 | + } |
| 73 | + pool->head = _pool_inc(pool, pool->head); |
| 74 | + |
| 75 | + g_mutex_unlock(&pool->mutex); |
| 76 | + |
| 77 | + return connection; |
| 78 | +} |
| 79 | + |
| 80 | +void ds3_connection_release(ds3_connection_pool* pool, ds3_connection* connection) { |
| 81 | + g_mutex_lock(&pool->mutex); |
| 82 | + |
| 83 | + curl_easy_reset(connection); |
| 84 | + pool->tail = _pool_inc(pool, pool->tail); |
| 85 | + |
| 86 | + g_mutex_unlock(&pool->mutex); |
| 87 | + g_cond_signal(&pool->available_connections); |
| 88 | +} |
| 89 | + |
0 commit comments