2 * Copyright (c) 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
27 #include "openvswitch/list.h"
28 #include "ovs-thread.h"
29 #include "poll-loop.h"
31 COVERAGE_DEFINE(seq_change
);
33 /* A sequence number object. */
35 uint64_t value OVS_GUARDED
;
36 struct hmap waiters OVS_GUARDED
; /* Contains 'struct seq_waiter's. */
39 /* A thread waiting on a particular seq. */
41 struct seq
*seq OVS_GUARDED
; /* Seq being waited for. */
42 struct hmap_node hmap_node OVS_GUARDED
; /* In 'seq->waiters'. */
43 unsigned int ovsthread_id OVS_GUARDED
; /* Key in 'waiters' hmap. */
45 struct seq_thread
*thread OVS_GUARDED
; /* Thread preparing to wait. */
46 struct ovs_list list_node OVS_GUARDED
; /* In 'thread->waiters'. */
48 uint64_t value OVS_GUARDED
; /* seq->value we're waiting to change. */
51 /* A thread that might be waiting on one or more seqs. */
53 struct ovs_list waiters OVS_GUARDED
; /* Contains 'struct seq_waiter's. */
54 struct latch latch OVS_GUARDED
; /* Wakeup latch for this thread. */
55 bool waiting OVS_GUARDED
; /* True if latch_wait() already called. */
58 static struct ovs_mutex seq_mutex
= OVS_MUTEX_INITIALIZER
;
60 static uint64_t seq_next
OVS_GUARDED_BY(seq_mutex
) = 1;
62 static pthread_key_t seq_thread_key
;
64 static void seq_init(void);
65 static struct seq_thread
*seq_thread_get(void) OVS_REQUIRES(seq_mutex
);
66 static void seq_thread_exit(void *thread_
) OVS_EXCLUDED(seq_mutex
);
67 static void seq_thread_woke(struct seq_thread
*) OVS_REQUIRES(seq_mutex
);
68 static void seq_waiter_destroy(struct seq_waiter
*) OVS_REQUIRES(seq_mutex
);
69 static void seq_wake_waiters(struct seq
*) OVS_REQUIRES(seq_mutex
);
71 /* Creates and returns a new 'seq' object. */
72 struct seq
* OVS_EXCLUDED(seq_mutex
)
79 seq
= xmalloc(sizeof *seq
);
81 COVERAGE_INC(seq_change
);
83 ovs_mutex_lock(&seq_mutex
);
84 seq
->value
= seq_next
++;
85 hmap_init(&seq
->waiters
);
86 ovs_mutex_unlock(&seq_mutex
);
91 /* Destroys 'seq', waking up threads that were waiting on it, if any. */
93 seq_destroy(struct seq
*seq
)
94 OVS_EXCLUDED(seq_mutex
)
96 ovs_mutex_lock(&seq_mutex
);
97 seq_wake_waiters(seq
);
98 hmap_destroy(&seq
->waiters
);
100 ovs_mutex_unlock(&seq_mutex
);
103 /* Increments 'seq''s sequence number, waking up any threads that are waiting
106 seq_change(struct seq
*seq
)
107 OVS_EXCLUDED(seq_mutex
)
109 COVERAGE_INC(seq_change
);
111 ovs_mutex_lock(&seq_mutex
);
112 seq
->value
= seq_next
++;
113 seq_wake_waiters(seq
);
114 ovs_mutex_unlock(&seq_mutex
);
117 /* Returns 'seq''s current sequence number (which could change immediately).
119 * seq_read() and seq_wait() can be used together to yield a race-free wakeup
120 * when an object changes, even without an ability to lock the object. See
121 * Usage in seq.h for details. */
123 seq_read(const struct seq
*seq
)
124 OVS_EXCLUDED(seq_mutex
)
128 ovs_mutex_lock(&seq_mutex
);
130 ovs_mutex_unlock(&seq_mutex
);
136 seq_wait__(struct seq
*seq
, uint64_t value
, const char *where
)
137 OVS_REQUIRES(seq_mutex
)
139 unsigned int id
= ovsthread_id_self();
140 uint32_t hash
= hash_int(id
, 0);
141 struct seq_waiter
*waiter
;
143 HMAP_FOR_EACH_IN_BUCKET (waiter
, hmap_node
, hash
, &seq
->waiters
) {
144 if (waiter
->ovsthread_id
== id
) {
145 if (waiter
->value
!= value
) {
146 /* The current value is different from the value we've already
148 poll_immediate_wake_at(where
);
150 /* Already waiting on 'value', nothing more to do. */
156 waiter
= xmalloc(sizeof *waiter
);
158 hmap_insert(&seq
->waiters
, &waiter
->hmap_node
, hash
);
159 waiter
->ovsthread_id
= id
;
160 waiter
->value
= value
;
161 waiter
->thread
= seq_thread_get();
162 list_push_back(&waiter
->thread
->waiters
, &waiter
->list_node
);
164 if (!waiter
->thread
->waiting
) {
165 latch_wait_at(&waiter
->thread
->latch
, where
);
166 waiter
->thread
->waiting
= true;
170 /* Causes the following poll_block() to wake up when 'seq''s sequence number
171 * changes from 'value'. (If 'seq''s sequence number isn't 'value', then
172 * poll_block() won't block at all.)
174 * seq_read() and seq_wait() can be used together to yield a race-free wakeup
175 * when an object changes, even without an ability to lock the object. See
176 * Usage in seq.h for details.
178 * ('where' is used in debug logging. Commonly one would use seq_wait() to
179 * automatically provide the caller's source file and line number for
182 seq_wait_at(const struct seq
*seq_
, uint64_t value
, const char *where
)
183 OVS_EXCLUDED(seq_mutex
)
185 struct seq
*seq
= CONST_CAST(struct seq
*, seq_
);
187 ovs_mutex_lock(&seq_mutex
);
188 if (value
== seq
->value
) {
189 seq_wait__(seq
, value
, where
);
191 poll_immediate_wake_at(where
);
193 ovs_mutex_unlock(&seq_mutex
);
196 /* Called by poll_block() just before it returns, this function destroys any
197 * seq_waiter objects associated with the current thread. */
200 OVS_EXCLUDED(seq_mutex
)
202 struct seq_thread
*thread
;
206 thread
= pthread_getspecific(seq_thread_key
);
208 ovs_mutex_lock(&seq_mutex
);
209 seq_thread_woke(thread
);
210 thread
->waiting
= false;
211 ovs_mutex_unlock(&seq_mutex
);
218 static struct ovsthread_once once
= OVSTHREAD_ONCE_INITIALIZER
;
220 if (ovsthread_once_start(&once
)) {
221 xpthread_key_create(&seq_thread_key
, seq_thread_exit
);
222 ovsthread_once_done(&once
);
226 static struct seq_thread
*
228 OVS_REQUIRES(seq_mutex
)
230 struct seq_thread
*thread
= pthread_getspecific(seq_thread_key
);
232 thread
= xmalloc(sizeof *thread
);
233 list_init(&thread
->waiters
);
234 latch_init(&thread
->latch
);
235 thread
->waiting
= false;
237 xpthread_setspecific(seq_thread_key
, thread
);
243 seq_thread_exit(void *thread_
)
244 OVS_EXCLUDED(seq_mutex
)
246 struct seq_thread
*thread
= thread_
;
248 ovs_mutex_lock(&seq_mutex
);
249 seq_thread_woke(thread
);
250 latch_destroy(&thread
->latch
);
252 ovs_mutex_unlock(&seq_mutex
);
256 seq_thread_woke(struct seq_thread
*thread
)
257 OVS_REQUIRES(seq_mutex
)
259 struct seq_waiter
*waiter
, *next_waiter
;
261 LIST_FOR_EACH_SAFE (waiter
, next_waiter
, list_node
, &thread
->waiters
) {
262 ovs_assert(waiter
->thread
== thread
);
263 seq_waiter_destroy(waiter
);
265 latch_poll(&thread
->latch
);
269 seq_waiter_destroy(struct seq_waiter
*waiter
)
270 OVS_REQUIRES(seq_mutex
)
272 hmap_remove(&waiter
->seq
->waiters
, &waiter
->hmap_node
);
273 list_remove(&waiter
->list_node
);
278 seq_wake_waiters(struct seq
*seq
)
279 OVS_REQUIRES(seq_mutex
)
281 struct seq_waiter
*waiter
, *next_waiter
;
283 HMAP_FOR_EACH_SAFE (waiter
, next_waiter
, hmap_node
, &seq
->waiters
) {
284 latch_set(&waiter
->thread
->latch
);
285 seq_waiter_destroy(waiter
);