Apache HTTPD
thread_cond.c
Go to the documentation of this file.
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_general.h"
18#include "apr_lib.h"
19#include "apr_strings.h"
20#include "apr_portable.h"
21#include "apr_arch_thread_mutex.h"
22#include "apr_arch_thread_cond.h"
23#include "apr_arch_file_io.h"
24#include <string.h>
25
26#ifndef DCE_POSTONE
27#define DCE_POSTONE 0x0800 // Post one flag
28#endif
29
31{
33
34 if (cv->semaphore) {
35 DosCloseEventSem(cv->semaphore);
36 }
37
38 if (cv->mutex) {
39 DosCloseMutexSem(cv->mutex);
40 }
41
42 return APR_SUCCESS;
43}
44
45
46
49{
50 int rc;
52
53 cv = apr_pcalloc(pool, sizeof(**cond));
54 rc = DosCreateEventSem(NULL, &cv->semaphore, DCE_POSTONE, FALSE);
55
56 if (rc == 0) {
57 rc = DosCreateMutexSem(NULL, &cv->mutex, 0, FALSE);
58 }
59
60 *cond = cv;
61 cv->pool = pool;
64
65 return APR_FROM_OS_ERROR(rc);
66}
67
68
69
71 apr_thread_mutex_t *mutex,
72 ULONG timeout_ms )
73{
74 ULONG rc;
76 int wake = FALSE;
77 unsigned long generation;
78
80 cond->num_waiting++;
81 generation = cond->generation;
83
85
86 do {
87 rc = DosWaitEventSem(cond->semaphore, timeout_ms);
88
90
91 if (cond->num_wake) {
92 if (cond->generation != generation) {
93 cond->num_wake--;
94 cond->num_waiting--;
95 rv = APR_SUCCESS;
96 break;
97 } else {
98 wake = TRUE;
99 }
100 }
101 else if (rc != 0) {
102 cond->num_waiting--;
103 rv = APR_TIMEUP;
104 break;
105 }
106
108
109 if (wake) {
110 wake = FALSE;
112 }
113 } while (1);
114
117 return rv;
118}
119
120
121
123 apr_thread_mutex_t *mutex)
124{
125 return thread_cond_timedwait(cond, mutex, SEM_INDEFINITE_WAIT);
126}
127
128
129
131 apr_thread_mutex_t *mutex,
133{
134 ULONG timeout_ms = (timeout >= 0) ? apr_time_as_msec(timeout)
136 return thread_cond_timedwait(cond, mutex, timeout_ms);
137}
138
139
140
142{
143 int wake = FALSE;
144
146
147 if (cond->num_waiting > cond->num_wake) {
148 wake = TRUE;
149 cond->num_wake++;
150 cond->generation++;
151 }
152
154
155 if (wake) {
157 }
158
159 return APR_SUCCESS;
160}
161
162
163
165{
166 unsigned long num_wake = 0;
167
169
170 if (cond->num_waiting > cond->num_wake) {
171 num_wake = cond->num_waiting - cond->num_wake;
172 cond->num_wake = cond->num_waiting;
173 cond->generation++;
174 }
175
177
178 for (; num_wake; num_wake--) {
180 }
181
182 return APR_SUCCESS;
183}
184
185
186
188{
189 return apr_pool_cleanup_run(cond->pool, cond, thread_cond_cleanup);
190}
191
192
193
#define TRUE
Definition abts.h:38
#define FALSE
Definition abts.h:35
APR Miscellaneous library routines.
APR general purpose library routines.
APR Portability Routines.
APR Strings library.
static apr_status_t thread_cond_cleanup(void *data)
Definition thread_cond.c:22
#define APR_TIMEUP
Definition apr_errno.h:450
apr_redis_t * rc
Definition apr_redis.h:173
const void apr_status_t(*) apr_status_t(* APR_DECLARE)(void) apr_pool_pre_cleanup_register(apr_pool_t *p
Definition apr_pools.h:646
apr_size_t size
const char int apr_pool_t * pool
Definition apr_cstr.h:84
#define APR_FROM_OS_ERROR(e)
Definition apr_errno.h:1214
#define APR_SUCCESS
Definition apr_errno.h:225
int apr_status_t
Definition apr_errno.h:44
void * data
#define APR_POOL_IMPLEMENT_ACCESSOR(type)
Definition apr_pools.h:91
#define apr_pcalloc(p, size)
Definition apr_pools.h:465
#define apr_time_as_msec(time)
Definition apr_time.h:72
apr_int64_t apr_interval_time_t
Definition apr_time.h:55
return NULL
Definition mod_so.c:359
static apr_status_t thread_cond_timedwait(apr_thread_cond_t *cond, apr_thread_mutex_t *mutex, ULONG timeout_ms)
Definition thread_cond.c:70
#define DCE_POSTONE
Definition thread_cond.c:27
IN ULONG IN INT timeout