thread_rwlock.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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_rwlock.h"
  21. #include "fspr_portable.h"
  22. static fspr_status_t thread_rwlock_cleanup(void *data)
  23. {
  24. fspr_thread_rwlock_t *rwlock = (fspr_thread_rwlock_t *)data;
  25. NXRwLockFree (rwlock->rwlock);
  26. return APR_SUCCESS;
  27. }
  28. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_create(fspr_thread_rwlock_t **rwlock,
  29. fspr_pool_t *pool)
  30. {
  31. fspr_thread_rwlock_t *new_rwlock = NULL;
  32. NXHierarchy_t hierarchy = 1; //for libc NKS NXRwLockAlloc
  33. NXLockInfo_t *info; //for libc NKS NXRwLockAlloc
  34. new_rwlock = (fspr_thread_rwlock_t *)fspr_pcalloc(pool, sizeof(fspr_thread_rwlock_t));
  35. if(new_rwlock ==NULL) {
  36. return APR_ENOMEM;
  37. }
  38. new_rwlock->pool = pool;
  39. info = (NXLockInfo_t *)fspr_pcalloc(pool, sizeof(NXLockInfo_t));
  40. new_rwlock->rwlock = NXRwLockAlloc(hierarchy, info);
  41. if(new_rwlock->rwlock == NULL)
  42. return APR_ENOMEM;
  43. fspr_pool_cleanup_register(new_rwlock->pool, new_rwlock, thread_rwlock_cleanup,
  44. fspr_pool_cleanup_null);
  45. *rwlock = new_rwlock;
  46. return APR_SUCCESS;
  47. }
  48. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_rdlock(fspr_thread_rwlock_t *rwlock)
  49. {
  50. NXRdLock(rwlock->rwlock);
  51. return APR_SUCCESS;
  52. }
  53. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_tryrdlock(fspr_thread_rwlock_t *rwlock)
  54. {
  55. if (!NXTryRdLock(rwlock->rwlock))
  56. return APR_EBUSY;
  57. return APR_SUCCESS;
  58. }
  59. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_wrlock(fspr_thread_rwlock_t *rwlock)
  60. {
  61. NXWrLock(rwlock->rwlock);
  62. return APR_SUCCESS;
  63. }
  64. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_trywrlock(fspr_thread_rwlock_t *rwlock)
  65. {
  66. if (!NXTryWrLock(rwlock->rwlock))
  67. return APR_EBUSY;
  68. return APR_SUCCESS;
  69. }
  70. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_unlock(fspr_thread_rwlock_t *rwlock)
  71. {
  72. NXRwUnlock(rwlock->rwlock);
  73. return APR_SUCCESS;
  74. }
  75. APR_DECLARE(fspr_status_t) fspr_thread_rwlock_destroy(fspr_thread_rwlock_t *rwlock)
  76. {
  77. fspr_status_t stat;
  78. if ((stat = thread_rwlock_cleanup(rwlock)) == APR_SUCCESS) {
  79. fspr_pool_cleanup_kill(rwlock->pool, rwlock, thread_rwlock_cleanup);
  80. return APR_SUCCESS;
  81. }
  82. return stat;
  83. }
  84. APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)