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.
19 #include "ovs-atomic.h"
21 #include "ovs-thread.h"
23 #ifdef OVS_ATOMIC_LOCKED_IMPL
24 static struct ovs_mutex *
25 mutex_for_pointer(void *p)
27 OVS_ALIGNED_STRUCT(CACHE_LINE_SIZE, aligned_mutex) {
28 struct ovs_mutex mutex;
29 char pad[PAD_SIZE(sizeof(struct ovs_mutex), CACHE_LINE_SIZE)];
32 static struct aligned_mutex atomic_mutexes[] = {
33 #define MUTEX_INIT { .mutex = OVS_MUTEX_INITIALIZER }
34 #define MUTEX_INIT4 MUTEX_INIT, MUTEX_INIT, MUTEX_INIT, MUTEX_INIT
35 #define MUTEX_INIT16 MUTEX_INIT4, MUTEX_INIT4, MUTEX_INIT4, MUTEX_INIT4
36 MUTEX_INIT16, MUTEX_INIT16,
38 BUILD_ASSERT_DECL(IS_POW2(ARRAY_SIZE(atomic_mutexes)));
40 uint32_t hash = hash_pointer(p, 0);
41 uint32_t indx = hash & (ARRAY_SIZE(atomic_mutexes) - 1);
42 return &atomic_mutexes[indx].mutex;
46 atomic_lock__(void *p)
47 OVS_ACQUIRES(mutex_for_pointer(p))
49 ovs_mutex_lock(mutex_for_pointer(p));
53 atomic_unlock__(void *p)
54 OVS_RELEASES(mutex_for_pointer(p))
56 ovs_mutex_unlock(mutex_for_pointer(p));
58 #endif /* OVS_ATOMIC_LOCKED_IMPL */