1251881Speter/*
2251881Speter * ====================================================================
3251881Speter *    Licensed to the Apache Software Foundation (ASF) under one
4251881Speter *    or more contributor license agreements.  See the NOTICE file
5251881Speter *    distributed with this work for additional information
6251881Speter *    regarding copyright ownership.  The ASF licenses this file
7251881Speter *    to you under the Apache License, Version 2.0 (the
8251881Speter *    "License"); you may not use this file except in compliance
9251881Speter *    with the License.  You may obtain a copy of the License at
10251881Speter *
11251881Speter *      http://www.apache.org/licenses/LICENSE-2.0
12251881Speter *
13251881Speter *    Unless required by applicable law or agreed to in writing,
14251881Speter *    software distributed under the License is distributed on an
15251881Speter *    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16251881Speter *    KIND, either express or implied.  See the License for the
17251881Speter *    specific language governing permissions and limitations
18251881Speter *    under the License.
19251881Speter * ====================================================================
20251881Speter */
21251881Speter
22251881Speter#include <apr_hash.h>
23251881Speter
24251881Speter#include "svn_hash.h"
25251881Speter#include "svn_dso.h"
26251881Speter#include "svn_pools.h"
27251881Speter#include "svn_private_config.h"
28251881Speter
29251881Speter#include "private/svn_mutex.h"
30286506Speter#include "private/svn_atomic.h"
31289180Speter#include "private/svn_subr_private.h"
32251881Speter
33251881Speter/* A mutex to protect our global pool and cache. */
34251881Speterstatic svn_mutex__t *dso_mutex = NULL;
35251881Speter
36251881Speter/* Global pool to allocate DSOs in. */
37251881Speterstatic apr_pool_t *dso_pool;
38251881Speter
39251881Speter/* Global cache for storing DSO objects. */
40251881Speterstatic apr_hash_t *dso_cache;
41251881Speter
42251881Speter/* Just an arbitrary location in memory... */
43251881Speterstatic int not_there_sentinel;
44251881Speter
45286506Speterstatic volatile svn_atomic_t atomic_init_status = 0;
46286506Speter
47251881Speter/* A specific value we store in the dso_cache to indicate that the
48251881Speter   library wasn't found.  This keeps us from allocating extra memory
49251881Speter   from dso_pool when trying to find libraries we already know aren't
50251881Speter   there.  */
51251881Speter#define NOT_THERE ((void *) &not_there_sentinel)
52251881Speter
53286506Speterstatic svn_error_t *
54286506Speteratomic_init_func(void *baton,
55286506Speter                 apr_pool_t *pool)
56251881Speter{
57251881Speter  dso_pool = svn_pool_create(NULL);
58251881Speter
59251881Speter  SVN_ERR(svn_mutex__init(&dso_mutex, TRUE, dso_pool));
60251881Speter
61251881Speter  dso_cache = apr_hash_make(dso_pool);
62251881Speter  return SVN_NO_ERROR;
63251881Speter}
64251881Speter
65286506Spetersvn_error_t *
66286506Spetersvn_dso_initialize2(void)
67286506Speter{
68286506Speter  SVN_ERR(svn_atomic__init_once(&atomic_init_status, atomic_init_func,
69286506Speter                                NULL, NULL));
70286506Speter
71286506Speter  return SVN_NO_ERROR;
72286506Speter}
73286506Speter
74251881Speter#if APR_HAS_DSO
75251881Speterstatic svn_error_t *
76251881Spetersvn_dso_load_internal(apr_dso_handle_t **dso, const char *fname)
77251881Speter{
78251881Speter  *dso = svn_hash_gets(dso_cache, fname);
79251881Speter
80251881Speter  /* First check to see if we've been through this before...  We do this
81251881Speter     to avoid calling apr_dso_load multiple times for a given library,
82251881Speter     which would result in wasting small amounts of memory each time. */
83251881Speter  if (*dso == NOT_THERE)
84251881Speter    {
85251881Speter      *dso = NULL;
86251881Speter      return SVN_NO_ERROR;
87251881Speter    }
88251881Speter
89251881Speter  /* If we got nothing back from the cache, try and load the library. */
90251881Speter  if (! *dso)
91251881Speter    {
92251881Speter      apr_status_t status = apr_dso_load(dso, fname, dso_pool);
93251881Speter      if (status)
94251881Speter        {
95251881Speter#ifdef SVN_DEBUG_DSO
96251881Speter          char buf[1024];
97251881Speter          fprintf(stderr,
98251881Speter                  "Dynamic loading of '%s' failed with the following error:\n%s\n",
99251881Speter                  fname,
100251881Speter                  apr_dso_error(*dso, buf, 1024));
101251881Speter#endif
102251881Speter          *dso = NULL;
103251881Speter
104251881Speter          /* It wasn't found, so set the special "we didn't find it" value. */
105251881Speter          svn_hash_sets(dso_cache, apr_pstrdup(dso_pool, fname), NOT_THERE);
106251881Speter
107251881Speter          return SVN_NO_ERROR;
108251881Speter        }
109251881Speter
110251881Speter      /* Stash the dso so we can use it next time. */
111251881Speter      svn_hash_sets(dso_cache, apr_pstrdup(dso_pool, fname), *dso);
112251881Speter    }
113251881Speter
114251881Speter  return SVN_NO_ERROR;
115251881Speter}
116251881Speter
117251881Spetersvn_error_t *
118251881Spetersvn_dso_load(apr_dso_handle_t **dso, const char *fname)
119251881Speter{
120286506Speter  SVN_ERR(svn_dso_initialize2());
121251881Speter
122251881Speter  SVN_MUTEX__WITH_LOCK(dso_mutex, svn_dso_load_internal(dso, fname));
123251881Speter
124251881Speter  return SVN_NO_ERROR;
125251881Speter}
126289180Speter
127289180Speterapr_pool_t *
128289180Spetersvn_dso__pool(void)
129289180Speter{
130289180Speter  return dso_pool;
131289180Speter}
132289180Speter
133251881Speter#endif /* APR_HAS_DSO */
134