1 /*
2 * root_pools.c : Implement svn_root_pools__* API
3 *
4 * ====================================================================
5 * Licensed to the Apache Software Foundation (ASF) under one
6 * or more contributor license agreements. See the NOTICE file
7 * distributed with this work for additional information
8 * regarding copyright ownership. The ASF licenses this file
9 * to you under the Apache License, Version 2.0 (the
10 * "License"); you may not use this file except in compliance
11 * with the License. You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing,
16 * software distributed under the License is distributed on an
17 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
18 * KIND, either express or implied. See the License for the
19 * specific language governing permissions and limitations
20 * under the License.
21 * ====================================================================
22 */
23
24 #include "svn_pools.h"
25
26 #include "private/svn_subr_private.h"
27 #include "private/svn_mutex.h"
28
29 struct svn_root_pools__t
30 {
31 /* unused pools.
32 * Use MUTEX to serialize access to this collection.
33 */
34 apr_array_header_t *unused_pools;
35
36 /* Mutex to serialize access to UNUSED_POOLS */
37 svn_mutex__t *mutex;
38
39 };
40
41 svn_error_t *
svn_root_pools__create(svn_root_pools__t ** pools)42 svn_root_pools__create(svn_root_pools__t **pools)
43 {
44 /* the collection of root pools must be managed independently from
45 any other pool */
46 apr_pool_t *pool
47 = apr_allocator_owner_get(svn_pool_create_allocator(FALSE));
48
49 /* construct result object */
50 svn_root_pools__t *result = apr_pcalloc(pool, sizeof(*result));
51 SVN_ERR(svn_mutex__init(&result->mutex, TRUE, pool));
52 result->unused_pools = apr_array_make(pool, 16, sizeof(apr_pool_t *));
53
54 /* done */
55 *pools = result;
56
57 return SVN_NO_ERROR;
58 }
59
60 /* Return a currently unused connection pool in *POOL. If no such pool
61 * exists, create a new root pool and return that in *POOL.
62 */
63 static svn_error_t *
acquire_pool_internal(apr_pool_t ** pool,svn_root_pools__t * pools)64 acquire_pool_internal(apr_pool_t **pool,
65 svn_root_pools__t *pools)
66 {
67 SVN_ERR(svn_mutex__lock(pools->mutex));
68 *pool = pools->unused_pools->nelts
69 ? *(apr_pool_t **)apr_array_pop(pools->unused_pools)
70 : apr_allocator_owner_get(svn_pool_create_allocator(FALSE));
71 SVN_ERR(svn_mutex__unlock(pools->mutex, SVN_NO_ERROR));
72
73 return SVN_NO_ERROR;
74 }
75
76 apr_pool_t *
svn_root_pools__acquire_pool(svn_root_pools__t * pools)77 svn_root_pools__acquire_pool(svn_root_pools__t *pools)
78 {
79 apr_pool_t *pool;
80 svn_error_t *err = acquire_pool_internal(&pool, pools);
81 if (err)
82 {
83 /* Mutex failure?! Well, try to continue with unrecycled data. */
84 svn_error_clear(err);
85 pool = apr_allocator_owner_get(svn_pool_create_allocator(FALSE));
86 }
87
88 return pool;
89 }
90
91 void
svn_root_pools__release_pool(apr_pool_t * pool,svn_root_pools__t * pools)92 svn_root_pools__release_pool(apr_pool_t *pool,
93 svn_root_pools__t *pools)
94 {
95 svn_error_t *err;
96
97 svn_pool_clear(pool);
98
99 err = svn_mutex__lock(pools->mutex);
100 if (err)
101 {
102 svn_error_clear(err);
103 svn_pool_destroy(pool);
104 }
105 else
106 {
107 APR_ARRAY_PUSH(pools->unused_pools, apr_pool_t *) = pool;
108 svn_error_clear(svn_mutex__unlock(pools->mutex, SVN_NO_ERROR));
109 }
110 }
111