/src/httpd/srclib/apr/locks/unix/thread_cond.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
19 | | #if APR_HAS_THREADS |
20 | | |
21 | | #include "apr_arch_thread_mutex.h" |
22 | | #include "apr_arch_thread_cond.h" |
23 | | |
24 | | static apr_status_t thread_cond_cleanup(void *data) |
25 | 0 | { |
26 | 0 | apr_thread_cond_t *cond = (apr_thread_cond_t *)data; |
27 | 0 | apr_status_t rv; |
28 | |
|
29 | 0 | rv = pthread_cond_destroy(&cond->cond); |
30 | | #ifdef HAVE_ZOS_PTHREADS |
31 | | if (rv) { |
32 | | rv = errno; |
33 | | } |
34 | | #endif |
35 | 0 | return rv; |
36 | 0 | } |
37 | | |
38 | | APR_DECLARE(apr_status_t) apr_thread_cond_create(apr_thread_cond_t **cond, |
39 | | apr_pool_t *pool) |
40 | 0 | { |
41 | 0 | apr_thread_cond_t *new_cond; |
42 | 0 | apr_status_t rv; |
43 | |
|
44 | 0 | new_cond = apr_palloc(pool, sizeof(apr_thread_cond_t)); |
45 | |
|
46 | 0 | new_cond->pool = pool; |
47 | |
|
48 | 0 | if ((rv = pthread_cond_init(&new_cond->cond, NULL))) { |
49 | | #ifdef HAVE_ZOS_PTHREADS |
50 | | rv = errno; |
51 | | #endif |
52 | 0 | return rv; |
53 | 0 | } |
54 | | |
55 | 0 | apr_pool_cleanup_register(new_cond->pool, |
56 | 0 | (void *)new_cond, thread_cond_cleanup, |
57 | 0 | apr_pool_cleanup_null); |
58 | |
|
59 | 0 | *cond = new_cond; |
60 | 0 | return APR_SUCCESS; |
61 | 0 | } |
62 | | |
63 | | APR_DECLARE(apr_status_t) apr_thread_cond_wait(apr_thread_cond_t *cond, |
64 | | apr_thread_mutex_t *mutex) |
65 | 0 | { |
66 | 0 | apr_status_t rv; |
67 | |
|
68 | 0 | rv = pthread_cond_wait(&cond->cond, &mutex->mutex); |
69 | | #ifdef HAVE_ZOS_PTHREADS |
70 | | if (rv) { |
71 | | rv = errno; |
72 | | } |
73 | | #endif |
74 | 0 | return rv; |
75 | 0 | } |
76 | | |
77 | | APR_DECLARE(apr_status_t) apr_thread_cond_timedwait(apr_thread_cond_t *cond, |
78 | | apr_thread_mutex_t *mutex, |
79 | | apr_interval_time_t timeout) |
80 | 0 | { |
81 | 0 | apr_status_t rv; |
82 | 0 | if (timeout < 0) { |
83 | 0 | rv = pthread_cond_wait(&cond->cond, &mutex->mutex); |
84 | | #ifdef HAVE_ZOS_PTHREADS |
85 | | if (rv) { |
86 | | rv = errno; |
87 | | } |
88 | | #endif |
89 | 0 | } |
90 | 0 | else { |
91 | 0 | apr_time_t then; |
92 | 0 | struct timespec abstime; |
93 | |
|
94 | 0 | then = apr_time_now() + timeout; |
95 | 0 | abstime.tv_sec = apr_time_sec(then); |
96 | 0 | abstime.tv_nsec = apr_time_usec(then) * 1000; /* nanoseconds */ |
97 | |
|
98 | 0 | rv = pthread_cond_timedwait(&cond->cond, &mutex->mutex, &abstime); |
99 | | #ifdef HAVE_ZOS_PTHREADS |
100 | | if (rv) { |
101 | | rv = errno; |
102 | | } |
103 | | #endif |
104 | 0 | if (ETIMEDOUT == rv) { |
105 | 0 | return APR_TIMEUP; |
106 | 0 | } |
107 | 0 | } |
108 | 0 | return rv; |
109 | 0 | } |
110 | | |
111 | | |
112 | | APR_DECLARE(apr_status_t) apr_thread_cond_signal(apr_thread_cond_t *cond) |
113 | 0 | { |
114 | 0 | apr_status_t rv; |
115 | |
|
116 | 0 | rv = pthread_cond_signal(&cond->cond); |
117 | | #ifdef HAVE_ZOS_PTHREADS |
118 | | if (rv) { |
119 | | rv = errno; |
120 | | } |
121 | | #endif |
122 | 0 | return rv; |
123 | 0 | } |
124 | | |
125 | | APR_DECLARE(apr_status_t) apr_thread_cond_broadcast(apr_thread_cond_t *cond) |
126 | 0 | { |
127 | 0 | apr_status_t rv; |
128 | |
|
129 | 0 | rv = pthread_cond_broadcast(&cond->cond); |
130 | | #ifdef HAVE_ZOS_PTHREADS |
131 | | if (rv) { |
132 | | rv = errno; |
133 | | } |
134 | | #endif |
135 | 0 | return rv; |
136 | 0 | } |
137 | | |
138 | | APR_DECLARE(apr_status_t) apr_thread_cond_destroy(apr_thread_cond_t *cond) |
139 | 0 | { |
140 | 0 | return apr_pool_cleanup_run(cond->pool, cond, thread_cond_cleanup); |
141 | 0 | } |
142 | | |
143 | | APR_POOL_IMPLEMENT_ACCESSOR(thread_cond) |
144 | | |
145 | | #endif /* APR_HAS_THREADS */ |