DS3 C SDK  4.1.0
Provides access to the Spectra S3 API with C
ds3_connection.c
Go to the documentation of this file.
1 
2 /*
3  * ******************************************************************************
4  * Copyright 2014-2017 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 /* This Code is Auto-Generated; DO NOT MODIFY! */
18 
19 
20 #include <curl/curl.h>
21 #include <glib.h>
22 #include <inttypes.h>
23 #include "ds3_connection.h"
24 
25 //-- Opaque struct
26 struct _ds3_connection_pool{
27  ds3_connection** connections;
28  uint16_t num_connections; // the number of connections created
29  ds3_connection** connection_queue;
30  uint16_t max_connections; // max number of possible connections, which the connections and queue arrays will be initialized to
31  int queue_head;
32  int queue_tail;
33  ds3_mutex mutex;
34  ds3_condition available_connection_notifier;
35  uint16_t ref_count;
36 };
37 
40 }
41 
43  ds3_connection_pool* pool = g_new0(ds3_connection_pool, 1);
44 
45  pool->connections = g_new0(ds3_connection*, pool_size);
46  pool->connection_queue = g_new0(ds3_connection*, pool_size);
47 
48  pool->max_connections = pool_size;
49 
50  g_mutex_init(&pool->mutex);
51  g_cond_init(&pool->available_connection_notifier);
52  pool->ref_count = 1;
53  return pool;
54 }
55 
57  int index;
58 
59  if (pool == NULL) {
60  return;
61  }
62 
63  if (already_locked == False) {
64  g_mutex_lock(&pool->mutex);
65  }
66 
67  for (index = 0; index < pool->num_connections; index++) {
68  if (pool->connections[index] != NULL) {
69  curl_easy_cleanup(pool->connections[index]);
70  }
71  }
72 
73  g_free(pool->connections);
74  g_free(pool->connection_queue);
75  g_mutex_unlock(&pool->mutex);
76  g_mutex_clear(&pool->mutex); // an attempt to clear a locked mutex is undefined
77  g_cond_clear(&pool->available_connection_notifier);
78 }
79 
80 static int _queue_inc(int index, uint16_t size) {
81  return (index+1) % size;
82 }
83 
85  int queue_head = pool->queue_head;
86  return pool->queue_tail == queue_head && pool->connection_queue[queue_head] == NULL;
87 }
88 
90  ds3_connection* connection = NULL;
91 
92  g_mutex_lock(&pool->mutex);
93  while (_queue_is_empty(pool) && pool->num_connections >= pool->max_connections) {
94  g_cond_wait(&pool->available_connection_notifier, &pool->mutex);
95  }
96 
97  if (_queue_is_empty(pool)) {
98  connection = curl_easy_init();
99 
100  pool->connections[pool->num_connections] = connection;
101  pool->num_connections++;
102  } else {
103  connection = pool->connection_queue[pool->queue_tail];
104  pool->connection_queue[pool->queue_tail] = NULL;
105  pool->queue_tail = _queue_inc(pool->queue_tail, pool->max_connections);
106  }
107 
108  g_mutex_unlock(&pool->mutex);
109 
110  return connection;
111 }
112 
114  g_mutex_lock(&pool->mutex);
115 
116  curl_easy_reset(connection);
117 
118  pool->connection_queue[pool->queue_head] = connection;
119 
120  pool->queue_head = _queue_inc(pool->queue_head, pool->max_connections);
121 
122  g_cond_signal(&pool->available_connection_notifier);
123  g_mutex_unlock(&pool->mutex);
124 }
125 
127  g_mutex_lock(&pool->mutex);
128  pool->ref_count++;
129  g_mutex_unlock(&pool->mutex);
130 }
131 
133  g_mutex_lock(&pool->mutex);
134  pool->ref_count--;
135 
136  if (pool->ref_count == 0) {
138  g_free(pool);
139  } else {
140  g_mutex_unlock(&pool->mutex);
141  }
142 }
ds3_connection_pool * ds3_connection_pool_init_with_size(uint16_t pool_size)
CURL ds3_connection
ds3_bool
Definition: ds3.h:72
GMutex ds3_mutex
void ds3_connection_pool_dec_ref(ds3_connection_pool *pool)
Definition: ds3.h:73
ds3_connection_pool * ds3_connection_pool_init(void)
void ds3_connection_pool_inc_ref(ds3_connection_pool *pool)
static int _queue_inc(int index, uint16_t size)
void ds3_connection_pool_clear(ds3_connection_pool *pool, ds3_bool already_locked)
GCond ds3_condition
struct _ds3_connection_pool ds3_connection_pool
Definition: ds3.h:57
void ds3_connection_release(ds3_connection_pool *pool, ds3_connection *connection)
static int _queue_is_empty(ds3_connection_pool *pool)
#define DEFAULT_CONNECTION_POOL_SIZE
ds3_connection * ds3_connection_acquire(ds3_connection_pool *pool)
Definition: ds3.h:73