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 "fspr.h"
  17. #include "fspr_private.h"
  18. #include "fspr_general.h"
  19. #include "fspr_strings.h"
  20. #include "fspr_arch_thread_mutex.h"
  21. #include "fspr_portable.h"
  22. static fspr_status_t thread_mutex_cleanup(void *data)
  23. {
  24. fspr_thread_mutex_t *mutex = (fspr_thread_mutex_t *)data;
  25. NXMutexFree(mutex->mutex);
  26. return APR_SUCCESS;
  27. }
  28. APR_DECLARE(fspr_status_t) fspr_thread_mutex_create(fspr_thread_mutex_t **mutex,
  29. unsigned int flags,
  30. fspr_pool_t *pool)
  31. {
  32. fspr_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 = (fspr_thread_mutex_t *)fspr_pcalloc(pool, sizeof(fspr_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. fspr_pool_cleanup_register(new_mutex->pool, new_mutex,
  47. (void*)thread_mutex_cleanup,
  48. fspr_pool_cleanup_null);
  49. *mutex = new_mutex;
  50. return APR_SUCCESS;
  51. }
  52. APR_DECLARE(fspr_status_t) fspr_thread_mutex_lock(fspr_thread_mutex_t *mutex)
  53. {
  54. NXLock(mutex->mutex);
  55. return APR_SUCCESS;
  56. }
  57. APR_DECLARE(fspr_status_t) fspr_thread_mutex_trylock(fspr_thread_mutex_t *mutex)
  58. {
  59. if (!NXTryLock(mutex->mutex))
  60. return APR_EBUSY;
  61. return APR_SUCCESS;
  62. }
  63. APR_DECLARE(fspr_status_t) fspr_thread_mutex_unlock(fspr_thread_mutex_t *mutex)
  64. {
  65. NXUnlock(mutex->mutex);
  66. return APR_SUCCESS;
  67. }
  68. APR_DECLARE(fspr_status_t) fspr_thread_mutex_destroy(fspr_thread_mutex_t *mutex)
  69. {
  70. fspr_status_t stat;
  71. if ((stat = thread_mutex_cleanup(mutex)) == APR_SUCCESS) {
  72. fspr_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)