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 
thread_cond_cleanup(void * data)27 static 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 
apr_thread_cond_create(apr_thread_cond_t ** cond,apr_pool_t * pool)35 APR_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 
apr_thread_cond_wait(apr_thread_cond_t * cond,apr_thread_mutex_t * mutex)59 APR_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 
apr_thread_cond_timedwait(apr_thread_cond_t * cond,apr_thread_mutex_t * mutex,apr_interval_time_t timeout)67 APR_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 {
71     int rc;
72     if (timeout < 0) {
73         rc = NXCondWait(cond->cond, mutex->mutex);
74     }
75     else {
76         timeout = timeout * 1000 / NXGetSystemTick();
77         rc = NXCondTimedWait(cond->cond, mutex->mutex, timeout);
78         if (rc == NX_ETIMEDOUT) {
79             return APR_TIMEUP;
80         }
81     }
82     if (rc != 0) {
83         return APR_EINTR;
84     }
85     return APR_SUCCESS;
86 }
87 
apr_thread_cond_signal(apr_thread_cond_t * cond)88 APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond)
89 {
90     NXCondSignal(cond->cond);
91     return APR_SUCCESS;
92 }
93 
apr_thread_cond_broadcast(apr_thread_cond_t * cond)94 APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond)
95 {
96     NXCondBroadcast(cond->cond);
97     return APR_SUCCESS;
98 }
99 
apr_thread_cond_destroy(apr_thread_cond_t * cond)100 APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond)
101 {
102     apr_status_t stat;
103     if ((stat = thread_cond_cleanup(cond)) == APR_SUCCESS) {
104         apr_pool_cleanup_kill(cond->pool, cond, thread_cond_cleanup);
105         return APR_SUCCESS;
106     }
107     return stat;
108 }
109 
110 APR_POOL_IMPLEMENT_ACCESSOR(thread_cond)
111 
112