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 <nks/errno.h>
18
19#include "apr.h"
20#include "apr_private.h"
21#include "apr_general.h"
22#include "apr_strings.h"
23#include "apr_arch_thread_mutex.h"
24#include "apr_arch_thread_cond.h"
25#include "apr_portable.h"
26
27static apr_status_t thread_cond_cleanup(void *data)
28{
29    apr_thread_cond_t *cond = (apr_thread_cond_t *)data;
30
31    NXCondFree(cond->cond);
32    return APR_SUCCESS;
33}
34
35APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond,
36                                                 apr_pool_t *pool)
37{
38    apr_thread_cond_t *new_cond = NULL;
39
40    new_cond = (apr_thread_cond_t *)apr_pcalloc(pool, sizeof(apr_thread_cond_t));
41
42	if(new_cond ==NULL) {
43        return APR_ENOMEM;
44    }
45    new_cond->pool = pool;
46
47    new_cond->cond = NXCondAlloc(NULL);
48
49    if(new_cond->cond == NULL)
50        return APR_ENOMEM;
51
52    apr_pool_cleanup_register(new_cond->pool, new_cond,
53                                (void*)thread_cond_cleanup,
54                                apr_pool_cleanup_null);
55   *cond = new_cond;
56    return APR_SUCCESS;
57}
58
59APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond,
60                                               apr_thread_mutex_t *mutex)
61{
62    if (NXCondWait(cond->cond, mutex->mutex) != 0)
63        return APR_EINTR;
64    return APR_SUCCESS;
65}
66
67APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond,
68                                                    apr_thread_mutex_t *mutex,
69                                                    apr_interval_time_t timeout){
70    if (NXCondTimedWait(cond->cond, mutex->mutex,
71        (timeout*1000)/NXGetSystemTick()) == NX_ETIMEDOUT) {
72        return APR_TIMEUP;
73    }
74    return APR_SUCCESS;
75}
76
77APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
78{
79    NXCondSignal(cond->cond);
80    return APR_SUCCESS;
81}
82
83APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
84{
85    NXCondBroadcast(cond->cond);
86    return APR_SUCCESS;
87}
88
89APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
90{
91    apr_status_t stat;
92    if ((stat = thread_cond_cleanup(cond)) == APR_SUCCESS) {
93        apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
94        return APR_SUCCESS;
95    }
96    return stat;
97}
98
99APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
100
101