1 /* Copyright (c) 2011, 2012 Nicira, Inc.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
23 /* Initializes 'session' as a session within 'server'. */
25 ovsdb_session_init(struct ovsdb_session *session, struct ovsdb_server *server)
27 session->server = server;
28 list_init(&session->completions);
29 hmap_init(&session->waiters);
32 /* Destroys 'session'. */
34 ovsdb_session_destroy(struct ovsdb_session *session)
36 ovs_assert(hmap_is_empty(&session->waiters));
37 hmap_destroy(&session->waiters);
40 /* Searches 'session' for an ovsdb_lock_waiter named 'lock_name' and returns
41 * it if it finds one, otherwise NULL. */
42 struct ovsdb_lock_waiter *
43 ovsdb_session_get_lock_waiter(const struct ovsdb_session *session,
44 const char *lock_name)
46 struct ovsdb_lock_waiter *waiter;
48 HMAP_FOR_EACH_WITH_HASH (waiter, session_node, hash_string(lock_name, 0),
50 if (!strcmp(lock_name, waiter->lock_name)) {
57 /* Returns the waiter that owns 'lock'.
59 * A lock always has an owner, so this function will never return NULL. */
60 struct ovsdb_lock_waiter *
61 ovsdb_lock_get_owner(const struct ovsdb_lock *lock)
63 return CONTAINER_OF(list_front(&lock->waiters),
64 struct ovsdb_lock_waiter, lock_node);
67 /* Removes 'waiter' from its lock's list. This means that, if 'waiter' was
68 * formerly the owner of its lock, then it no longer owns it.
70 * Returns the session that now owns 'waiter'. This is NULL if 'waiter' was
71 * the lock's owner and no other sessions were waiting for the lock. In this
72 * case, the lock has been destroyed, so the caller must be sure not to refer
73 * to it again. A nonnull return value reflects a change in the lock's
74 * ownership if and only if 'waiter' formerly owned the lock. */
75 struct ovsdb_session *
76 ovsdb_lock_waiter_remove(struct ovsdb_lock_waiter *waiter)
78 struct ovsdb_lock *lock = waiter->lock;
80 list_remove(&waiter->lock_node);
83 if (list_is_empty(&lock->waiters)) {
84 hmap_remove(&lock->server->locks, &lock->hmap_node);
90 return ovsdb_lock_get_owner(lock)->session;
93 /* Destroys 'waiter', which must have already been removed from its lock's
94 * waiting list with ovsdb_lock_waiter_remove().
96 * Removing and destroying locks are decoupled because a lock initially created
97 * by the "steal" request, that is later stolen by another client, remains in
98 * the database session until the database client sends an "unlock" request. */
100 ovsdb_lock_waiter_destroy(struct ovsdb_lock_waiter *waiter)
102 ovs_assert(!waiter->lock);
103 hmap_remove(&waiter->session->waiters, &waiter->session_node);
104 free(waiter->lock_name);
108 /* Returns true if 'waiter' owns its associated lock. */
110 ovsdb_lock_waiter_is_owner(const struct ovsdb_lock_waiter *waiter)
112 return waiter->lock && waiter == ovsdb_lock_get_owner(waiter->lock);
115 /* Initializes 'server'.
117 * The caller must call ovsdb_server_add_db() for each database to which
118 * 'server' should provide access. */
120 ovsdb_server_init(struct ovsdb_server *server)
122 shash_init(&server->dbs);
123 hmap_init(&server->locks);
126 /* Adds 'db' to the set of databases served out by 'server'. Returns true if
127 * successful, false if 'db''s name is the same as some database already in
130 ovsdb_server_add_db(struct ovsdb_server *server, struct ovsdb *db)
132 return shash_add_once(&server->dbs, db->schema->name, db);
135 /* Destroys 'server'. */
137 ovsdb_server_destroy(struct ovsdb_server *server)
139 shash_destroy(&server->dbs);
140 hmap_destroy(&server->locks);
143 static struct ovsdb_lock *
144 ovsdb_server_create_lock__(struct ovsdb_server *server, const char *lock_name,
147 struct ovsdb_lock *lock;
149 HMAP_FOR_EACH_WITH_HASH (lock, hmap_node, hash, &server->locks) {
150 if (!strcmp(lock->name, lock_name)) {
155 lock = xzalloc(sizeof *lock);
156 lock->server = server;
157 lock->name = xstrdup(lock_name);
158 hmap_insert(&server->locks, &lock->hmap_node, hash);
159 list_init(&lock->waiters);
164 /* Attempts to acquire the lock named 'lock_name' for 'session' within
165 * 'server'. Returns the new lock waiter.
167 * If 'mode' is OVSDB_LOCK_STEAL, then the new lock waiter is always the owner
168 * of the lock. '*victimp' receives the session of the previous owner or NULL
169 * if the lock was previously unowned. (If the victim itself originally
170 * obtained the lock through a "steal" operation, then this function also
171 * removes the victim from the lock's waiting list.)
173 * If 'mode' is OVSDB_LOCK_WAIT, then the new lock waiter is the owner of the
174 * lock only if this lock had no existing owner. '*victimp' is set to NULL. */
175 struct ovsdb_lock_waiter *
176 ovsdb_server_lock(struct ovsdb_server *server,
177 struct ovsdb_session *session,
178 const char *lock_name,
179 enum ovsdb_lock_mode mode,
180 struct ovsdb_session **victimp)
182 uint32_t hash = hash_string(lock_name, 0);
183 struct ovsdb_lock_waiter *waiter, *victim;
184 struct ovsdb_lock *lock;
186 lock = ovsdb_server_create_lock__(server, lock_name, hash);
187 victim = (mode == OVSDB_LOCK_STEAL && !list_is_empty(&lock->waiters)
188 ? ovsdb_lock_get_owner(lock)
191 waiter = xmalloc(sizeof *waiter);
193 waiter->lock_name = xstrdup(lock_name);
195 if (mode == OVSDB_LOCK_STEAL) {
196 list_push_front(&lock->waiters, &waiter->lock_node);
198 list_push_back(&lock->waiters, &waiter->lock_node);
200 waiter->session = session;
201 hmap_insert(&waiter->session->waiters, &waiter->session_node, hash);
203 if (victim && victim->mode == OVSDB_LOCK_STEAL) {
204 ovsdb_lock_waiter_remove(victim);
206 *victimp = victim ? victim->session : NULL;