2 * ====================================================================
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
19 * ====================================================================
22 #include <apr_thread_mutex.h>
27 #include "svn_pools.h"
28 #include "svn_private_config.h"
30 #include "private/svn_mutex.h"
31 #include "private/svn_atomic.h"
32 #include "private/svn_subr_private.h"
34 /* A mutex to protect our global pool and cache. */
35 static svn_mutex__t *dso_mutex = NULL;
37 /* Global pool to allocate DSOs in. */
38 static apr_pool_t *dso_pool;
40 /* Global cache for storing DSO objects. */
41 static apr_hash_t *dso_cache;
43 /* Just an arbitrary location in memory... */
44 static int not_there_sentinel;
46 static volatile svn_atomic_t atomic_init_status = 0;
48 /* A specific value we store in the dso_cache to indicate that the
49 library wasn't found. This keeps us from allocating extra memory
50 from dso_pool when trying to find libraries we already know aren't
52 #define NOT_THERE ((void *) ¬_there_sentinel)
55 atomic_init_func(void *baton,
58 dso_pool = svn_pool_create(NULL);
60 SVN_ERR(svn_mutex__init(&dso_mutex, TRUE, dso_pool));
62 dso_cache = apr_hash_make(dso_pool);
67 svn_dso_initialize2(void)
69 SVN_ERR(svn_atomic__init_once(&atomic_init_status, atomic_init_func,
77 svn_dso_load_internal(apr_dso_handle_t **dso, const char *fname)
79 *dso = svn_hash_gets(dso_cache, fname);
81 /* First check to see if we've been through this before... We do this
82 to avoid calling apr_dso_load multiple times for a given library,
83 which would result in wasting small amounts of memory each time. */
84 if (*dso == NOT_THERE)
90 /* If we got nothing back from the cache, try and load the library. */
93 apr_status_t status = apr_dso_load(dso, fname, dso_pool);
99 "Dynamic loading of '%s' failed with the following error:\n%s\n",
101 apr_dso_error(*dso, buf, 1024));
105 /* It wasn't found, so set the special "we didn't find it" value. */
106 svn_hash_sets(dso_cache, apr_pstrdup(dso_pool, fname), NOT_THERE);
111 /* Stash the dso so we can use it next time. */
112 svn_hash_sets(dso_cache, apr_pstrdup(dso_pool, fname), *dso);
119 svn_dso_load(apr_dso_handle_t **dso, const char *fname)
121 SVN_ERR(svn_dso_initialize2());
123 SVN_MUTEX__WITH_LOCK(dso_mutex, svn_dso_load_internal(dso, fname));
134 #endif /* APR_HAS_DSO */