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
17#include "apr.h"
18#include "apr_private.h"
19#include "apr_general.h"
20#include "apr_strings.h"
21#include "apr_arch_thread_rwlock.h"
22#include "apr_portable.h"
23
24static apr_status_t thread_rwlock_cleanup(void *data)
25{
26    apr_thread_rwlock_t *rwlock = (apr_thread_rwlock_t *)data;
27
28    NXRwLockFree (rwlock->rwlock);
29    return APR_SUCCESS;
30}
31
32APR_DECLARE(apr_status_t) apr_thread_rwlock_create(apr_thread_rwlock_t **rwlock,
33                                                   apr_pool_t *pool)
34{
35    apr_thread_rwlock_t *new_rwlock = NULL;
36
37    NXHierarchy_t hierarchy = 1;   /* for libc NKS NXRwLockAlloc */
38    NXLockInfo_t *info;            /* for libc NKS NXRwLockAlloc */
39
40    new_rwlock = (apr_thread_rwlock_t *)apr_pcalloc(pool, sizeof(apr_thread_rwlock_t));
41
42    if(new_rwlock ==NULL) {
43        return APR_ENOMEM;
44    }
45    new_rwlock->pool = pool;
46
47    info = (NXLockInfo_t *)apr_pcalloc(pool, sizeof(NXLockInfo_t));
48    new_rwlock->rwlock = NXRwLockAlloc(hierarchy, info);
49    if(new_rwlock->rwlock == NULL)
50        return APR_ENOMEM;
51
52    apr_pool_cleanup_register(new_rwlock->pool, new_rwlock, thread_rwlock_cleanup,
53                              apr_pool_cleanup_null);
54    *rwlock = new_rwlock;
55
56    return APR_SUCCESS;
57}
58
59APR_DECLARE(apr_status_t) apr_thread_rwlock_rdlock(apr_thread_rwlock_t *rwlock)
60{
61    NXRdLock(rwlock->rwlock);
62    return APR_SUCCESS;
63}
64
65APR_DECLARE(apr_status_t) apr_thread_rwlock_tryrdlock(apr_thread_rwlock_t *rwlock)
66{
67    if (!NXTryRdLock(rwlock->rwlock))
68        return APR_EBUSY;
69    return APR_SUCCESS;
70}
71
72APR_DECLARE(apr_status_t) apr_thread_rwlock_wrlock(apr_thread_rwlock_t *rwlock)
73{
74    NXWrLock(rwlock->rwlock);
75    return APR_SUCCESS;
76}
77
78APR_DECLARE(apr_status_t) apr_thread_rwlock_trywrlock(apr_thread_rwlock_t *rwlock)
79{
80    if (!NXTryWrLock(rwlock->rwlock))
81        return APR_EBUSY;
82    return APR_SUCCESS;
83}
84
85APR_DECLARE(apr_status_t) apr_thread_rwlock_unlock(apr_thread_rwlock_t *rwlock)
86{
87    NXRwUnlock(rwlock->rwlock);
88    return APR_SUCCESS;
89}
90
91APR_DECLARE(apr_status_t) apr_thread_rwlock_destroy(apr_thread_rwlock_t *rwlock)
92{
93    apr_status_t stat;
94    if ((stat = thread_rwlock_cleanup(rwlock)) == APR_SUCCESS) {
95        apr_pool_cleanup_kill(rwlock->pool, rwlock, thread_rwlock_cleanup);
96        return APR_SUCCESS;
97    }
98    return stat;
99}
100
101APR_POOL_IMPLEMENT_ACCESSOR(thread_rwlock)
102
103