thread_mutex.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "apr.h"
  17. #include "apr_private.h"
  18. #include "apr_general.h"
  19. #include "apr_strings.h"
  20. #include "apr_arch_thread_mutex.h"
  21. #include "apr_portable.h"
  22. static apr_status_t thread_mutex_cleanup(void *data)
  23. {
  24. apr_thread_mutex_t *mutex = (apr_thread_mutex_t *)data;
  25. NXMutexFree(mutex->mutex);
  26. return APR_SUCCESS;
  27. }
  28. APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
  29. unsigned int flags,
  30. apr_pool_t *pool)
  31. {
  32. apr_thread_mutex_t *new_mutex = NULL;
  33. /* XXX: Implement _UNNESTED flavor and favor _DEFAULT for performance
  34. */
  35. if (flags & APR_THREAD_MUTEX_UNNESTED) {
  36. return APR_ENOTIMPL;
  37. }
  38. new_mutex = (apr_thread_mutex_t *)apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
  39. if(new_mutex ==NULL) {
  40. return APR_ENOMEM;
  41. }
  42. new_mutex->pool = pool;
  43. new_mutex->mutex = NXMutexAlloc(NX_MUTEX_RECURSIVE, 0, NULL);
  44. if(new_mutex->mutex == NULL)
  45. return APR_ENOMEM;
  46. apr_pool_cleanup_register(new_mutex->pool, new_mutex,
  47. (void*)thread_mutex_cleanup,
  48. apr_pool_cleanup_null);
  49. *mutex = new_mutex;
  50. return APR_SUCCESS;
  51. }
  52. APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
  53. {
  54. NXLock(mutex->mutex);
  55. return APR_SUCCESS;
  56. }
  57. APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
  58. {
  59. if (!NXTryLock(mutex->mutex))
  60. return APR_EBUSY;
  61. return APR_SUCCESS;
  62. }
  63. APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
  64. {
  65. NXUnlock(mutex->mutex);
  66. return APR_SUCCESS;
  67. }
  68. APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
  69. {
  70. apr_status_t stat;
  71. if ((stat = thread_mutex_cleanup(mutex)) == APR_SUCCESS) {
  72. apr_pool_cleanup_kill(mutex->pool, mutex, thread_mutex_cleanup);
  73. return APR_SUCCESS;
  74. }
  75. return stat;
  76. }
  77. APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)