netdev-dpdk: fix mbuf leaks
[cascardo/ovs.git] / lib / ovs-atomic-locked.c
1 /*
2  * Copyright (c) 2013, 2014 Nicira, Inc.
3  *
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:
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 <config.h>
18
19 #include "ovs-atomic.h"
20 #include "hash.h"
21 #include "ovs-thread.h"
22
23 #ifdef OVS_ATOMIC_LOCKED_IMPL
24 static struct ovs_mutex *
25 mutex_for_pointer(void *p)
26 {
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)];
30     };
31
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,
37     };
38     BUILD_ASSERT_DECL(IS_POW2(ARRAY_SIZE(atomic_mutexes)));
39
40     uint32_t hash = hash_pointer(p, 0);
41     uint32_t indx = hash & (ARRAY_SIZE(atomic_mutexes) - 1);
42     return &atomic_mutexes[indx].mutex;
43 }
44
45 void
46 atomic_lock__(void *p)
47     OVS_ACQUIRES(mutex_for_pointer(p))
48 {
49     ovs_mutex_lock(mutex_for_pointer(p));
50 }
51
52 void
53 atomic_unlock__(void *p)
54     OVS_RELEASES(mutex_for_pointer(p))
55 {
56     ovs_mutex_unlock(mutex_for_pointer(p));
57 }
58 #endif /* OVS_ATOMIC_LOCKED_IMPL */