Author: stefan2 Date: Fri Jul 1 19:04:29 2011 New Revision: 1142026 URL: http://svn.apache.org/viewvc?rev=1142026&view=rev Log: Introduce the svn_mutex API and implement it
* subversion/include/private/svn_mutex.h new private API header * subversion/libsvn_subr/svn_mutex.c implement the new API Added: subversion/branches/svn_mutex/subversion/include/private/svn_mutex.h subversion/branches/svn_mutex/subversion/libsvn_subr/svn_mutex.c Added: subversion/branches/svn_mutex/subversion/include/private/svn_mutex.h URL: http://svn.apache.org/viewvc/subversion/branches/svn_mutex/subversion/include/private/svn_mutex.h?rev=1142026&view=auto ============================================================================== --- subversion/branches/svn_mutex/subversion/include/private/svn_mutex.h (added) +++ subversion/branches/svn_mutex/subversion/include/private/svn_mutex.h Fri Jul 1 19:04:29 2011 @@ -0,0 +1,82 @@ +/** + * @copyright + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + * @endcopyright + * + * @file svn_mutex.h + * @brief Strutures and functions for mutual exclusion + */ + +#ifndef SVN_MUTEX_H +#define SVN_MUTEX_H + +#include <apr_thread_mutex.h> +#include "svn_error.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** + * This is a simple wrapper around @c apr_thread_mutex_t and will be a + * valid identifier even if APR does not support threading. + */ +typedef struct svn_mutex__t +{ +#if APR_HAS_THREADS + + /** A mutex for synchronization between threads. It may be NULL, in + * which case no synchronization will take place. The latter is useful, + * if implement some functionality where synchronization is optional. + */ + apr_thread_mutex_t *mutex; + +#endif +} svn_mutex__t; + +/** Initialize the @a *mutex with a lifetime defined by @a pool, if + * @a enable_mutex is TRUE and with @c NULL otherwise. If @a enable_mutex + * is set but threading is not supported by APR, this function returns an + * @c APR_ENOTIMPL error. + */ +svn_error_t * +svn_mutex__init(svn_mutex__t *mutex, + svn_boolean_t enable_mutex, + apr_pool_t *pool); + +/** Acquire the @a mutex, if that has been enabled in @ref svn_mutex__init. + * Make sure to call @ref svn_mutex__unlock some time later in the same + * thread to release the mutex again. Recursive locking are not supported. + */ +svn_error_t * +svn_mutex__lock(svn_mutex__t mutex); + +/** Release the @a mutex, previously acquired using @ref svn_mutex__lock + * that has been enabled in @ref svn_mutex__init. + */ +svn_error_t * +svn_mutex__unlock(svn_mutex__t mutex, + svn_error_t *err); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* SVN_MUTEX_H */ \ No newline at end of file Added: subversion/branches/svn_mutex/subversion/libsvn_subr/svn_mutex.c URL: http://svn.apache.org/viewvc/subversion/branches/svn_mutex/subversion/libsvn_subr/svn_mutex.c?rev=1142026&view=auto ============================================================================== --- subversion/branches/svn_mutex/subversion/libsvn_subr/svn_mutex.c (added) +++ subversion/branches/svn_mutex/subversion/libsvn_subr/svn_mutex.c Fri Jul 1 19:04:29 2011 @@ -0,0 +1,80 @@ +/* + * svn_mutex.c: in-memory caching for Subversion + * + * ==================================================================== + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * ==================================================================== + */ + +#include "svn_private_config.h" +#include "private/svn_mutex.h" + +svn_error_t * +svn_mutex__init(svn_mutex__t *mutex, + svn_boolean_t enable_mutex, + apr_pool_t *pool) +{ +#if APR_HAS_THREADS + mutex->mutex = NULL; + if (enable_mutex) + { + apr_status_t status = + apr_thread_mutex_create(&mutex->mutex, + APR_THREAD_MUTEX_DEFAULT, + pool); + if (status) + return svn_error_wrap_apr(status, _("Can't create mutex")); + } +#else + if (enable_mutex) + return svn_error_wrap_apr(APR_ENOTIMPL, _("APR doesn't support threads")); +#endif + + return SVN_NO_ERROR; +} + +svn_error_t * +svn_mutex__lock(svn_mutex__t mutex) +{ +#if APR_HAS_THREADS + if (mutex.mutex) + { + apr_status_t status = apr_thread_mutex_lock(mutex.mutex); + if (status) + return svn_error_wrap_apr(status, _("Can't lock mutex")); + } +#endif + + return SVN_NO_ERROR; +} + +svn_error_t * +svn_mutex__unlock(svn_mutex__t mutex, + svn_error_t *err) +{ +#if APR_HAS_THREADS + if (mutex.mutex) + { + apr_status_t status = apr_thread_mutex_unlock(mutex.mutex); + if (status && !err) + return svn_error_wrap_apr(status, _("Can't unlock mutex")); + } +#endif + + return err; +}