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_mutex.h"
22#include "apr_portable.h"
23
24static apr_status_t thread_mutex_cleanup(void *data)
25{
26    apr_thread_mutex_t *mutex = (apr_thread_mutex_t *)data;
27
28    NXMutexFree(mutex->mutex);
29    return APR_SUCCESS;
30}
31
32APR_DECLARE(apr_status_t) apr_thread_mutex_create(apr_thread_mutex_t **mutex,
33                                                  unsigned int flags,
34                                                  apr_pool_t *pool)
35{
36    apr_thread_mutex_t *new_mutex = NULL;
37
38    /* XXX: Implement _UNNESTED flavor and favor _DEFAULT for performance
39     */
40    if (flags & APR_THREAD_MUTEX_UNNESTED) {
41        return APR_ENOTIMPL;
42    }
43    new_mutex = (apr_thread_mutex_t *)apr_pcalloc(pool, sizeof(apr_thread_mutex_t));
44
45	if(new_mutex ==NULL) {
46        return APR_ENOMEM;
47    }
48    new_mutex->pool = pool;
49
50    new_mutex->mutex = NXMutexAlloc(NX_MUTEX_RECURSIVE, 0, NULL);
51
52    if(new_mutex->mutex == NULL)
53        return APR_ENOMEM;
54
55    apr_pool_cleanup_register(new_mutex->pool, new_mutex,
56                                (void*)thread_mutex_cleanup,
57                                apr_pool_cleanup_null);
58   *mutex = new_mutex;
59    return APR_SUCCESS;
60}
61
62APR_DECLARE(apr_status_t) apr_thread_mutex_lock(apr_thread_mutex_t *mutex)
63{
64    NXLock(mutex->mutex);
65    return APR_SUCCESS;
66}
67
68APR_DECLARE(apr_status_t) apr_thread_mutex_trylock(apr_thread_mutex_t *mutex)
69{
70    if (!NXTryLock(mutex->mutex))
71        return APR_EBUSY;
72    return APR_SUCCESS;
73}
74
75APR_DECLARE(apr_status_t) apr_thread_mutex_unlock(apr_thread_mutex_t *mutex)
76{
77    NXUnlock(mutex->mutex);
78    return APR_SUCCESS;
79}
80
81APR_DECLARE(apr_status_t) apr_thread_mutex_destroy(apr_thread_mutex_t *mutex)
82{
83    apr_status_t stat;
84    if ((stat = thread_mutex_cleanup(mutex)) == APR_SUCCESS) {
85        apr_pool_cleanup_kill(mutex->pool, mutex, thread_mutex_cleanup);
86        return APR_SUCCESS;
87    }
88    return stat;
89}
90
91APR_POOL_IMPLEMENT_ACCESSOR(thread_mutex)
92
93