In the command-line client, forbid
[svn.git] / subversion / libsvn_subr / dso.c
blobc23ae8a2d14b31fdedad5d1dee62e1209647b05a
1 /*
2 * ====================================================================
3 * Copyright (c) 2006 CollabNet. All rights reserved.
5 * This software is licensed as described in the file COPYING, which
6 * you should have received as part of this distribution. The terms
7 * are also available at http://subversion.tigris.org/license-1.html.
8 * If newer versions of this license are posted there, you may use a
9 * newer version instead, at your option.
11 * This software consists of voluntary contributions made by many
12 * individuals. For exact contribution history, see the revision
13 * history and logs, available at http://subversion.tigris.org/.
14 * ====================================================================
17 #include <apr_thread_mutex.h>
18 #include <apr_hash.h>
20 #include "svn_dso.h"
21 #include "svn_pools.h"
22 #include "svn_private_config.h"
24 /* A mutex to protect our global pool and cache. */
25 #if APR_HAS_THREADS
26 static apr_thread_mutex_t *dso_mutex;
27 #endif
29 /* Global pool to allocate DSOs in. */
30 static apr_pool_t *dso_pool;
32 /* Global cache for storing DSO objects. */
33 static apr_hash_t *dso_cache;
35 /* Just an arbitrary location in memory... */
36 static int not_there_sentinel;
38 /* A specific value we store in the dso_cache to indicate that the
39 library wasn't found. This keeps us from allocating extra memory
40 from dso_pool when trying to find libraries we already know aren't
41 there. */
42 #define NOT_THERE ((void *) &not_there_sentinel)
44 void
45 svn_dso_initialize()
47 if (dso_pool)
48 return;
50 dso_pool = svn_pool_create(NULL);
52 #if APR_HAS_THREADS
53 apr_thread_mutex_create(&dso_mutex, APR_THREAD_MUTEX_DEFAULT, dso_pool);
54 #endif
56 dso_cache = apr_hash_make(dso_pool);
59 #if APR_HAS_DSO
60 svn_error_t *
61 svn_dso_load(apr_dso_handle_t **dso, const char *fname)
63 apr_status_t status;
65 if (! dso_pool)
66 svn_dso_initialize();
68 #if APR_HAS_THREADS
69 status = apr_thread_mutex_lock(dso_mutex);
70 if (status)
71 return svn_error_wrap_apr(status, _("Can't grab DSO mutex"));
72 #endif
74 *dso = apr_hash_get(dso_cache, fname, APR_HASH_KEY_STRING);
76 /* First check to see if we've been through this before... We do this
77 to avoid calling apr_dso_load multiple times for a given library,
78 which would result in wasting small amounts of memory each time. */
79 if (*dso == NOT_THERE)
81 *dso = NULL;
82 #if APR_HAS_THREADS
83 status = apr_thread_mutex_unlock(dso_mutex);
84 if (status)
85 return svn_error_wrap_apr(status, _("Can't ungrab DSO mutex"));
86 #endif
87 return SVN_NO_ERROR;
90 /* If we got nothing back from the cache, try and load the library. */
91 if (! *dso)
93 status = apr_dso_load(dso, fname, dso_pool);
94 if (status)
96 *dso = NULL;
98 /* It wasn't found, so set the special "we didn't find it" value. */
99 apr_hash_set(dso_cache,
100 apr_pstrdup(dso_pool, fname),
101 APR_HASH_KEY_STRING,
102 NOT_THERE);
104 #if APR_HAS_THREADS
105 status = apr_thread_mutex_unlock(dso_mutex);
106 if (status)
107 return svn_error_wrap_apr(status, _("Can't ungrab DSO mutex"));
108 #endif
109 return SVN_NO_ERROR;
112 /* Stash the dso so we can use it next time. */
113 apr_hash_set(dso_cache,
114 apr_pstrdup(dso_pool, fname),
115 APR_HASH_KEY_STRING,
116 *dso);
119 #if APR_HAS_THREADS
120 status = apr_thread_mutex_unlock(dso_mutex);
121 if (status)
122 return svn_error_wrap_apr(status, _("Can't ungrab DSO mutex"));
123 #endif
125 return SVN_NO_ERROR;
127 #endif /* APR_HAS_DSO */