Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 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: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #ifndef included_clib_lock_h |
| 17 | #define included_clib_lock_h |
| 18 | |
| 19 | #include <vppinfra/clib.h> |
| 20 | |
| 21 | typedef struct |
| 22 | { |
| 23 | CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); |
| 24 | u32 lock; |
| 25 | #if CLIB_DEBUG > 0 |
| 26 | pid_t pid; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 27 | uword thread_index; |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 28 | void *frame_address; |
| 29 | #endif |
| 30 | } *clib_spinlock_t; |
| 31 | |
| 32 | static inline void |
| 33 | clib_spinlock_init (clib_spinlock_t * p) |
| 34 | { |
| 35 | *p = clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES); |
| 36 | memset ((void *) *p, 0, CLIB_CACHE_LINE_BYTES); |
| 37 | } |
| 38 | |
| 39 | static inline void |
| 40 | clib_spinlock_free (clib_spinlock_t * p) |
| 41 | { |
| 42 | if (*p) |
| 43 | { |
| 44 | clib_mem_free ((void *) *p); |
| 45 | *p = 0; |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | static_always_inline void |
| 50 | clib_spinlock_lock (clib_spinlock_t * p) |
| 51 | { |
| 52 | while (__sync_lock_test_and_set (&(*p)->lock, 1)) |
| 53 | #if __x86_64__ |
| 54 | __builtin_ia32_pause () |
| 55 | #endif |
| 56 | ; |
| 57 | #if CLIB_DEBUG > 0 |
| 58 | (*p)->frame_address = __builtin_frame_address (0); |
| 59 | (*p)->pid = getpid (); |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 60 | (*p)->thread_index = os_get_thread_index (); |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 61 | #endif |
| 62 | } |
| 63 | |
| 64 | static_always_inline void |
| 65 | clib_spinlock_lock_if_init (clib_spinlock_t * p) |
| 66 | { |
| 67 | if (PREDICT_FALSE (*p != 0)) |
| 68 | clib_spinlock_lock (p); |
| 69 | } |
| 70 | |
| 71 | static_always_inline void |
| 72 | clib_spinlock_unlock (clib_spinlock_t * p) |
| 73 | { |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 74 | #if CLIB_DEBUG > 0 |
| 75 | (*p)->frame_address = 0; |
| 76 | (*p)->pid = 0; |
Damjan Marion | f55f9b8 | 2017-05-10 21:06:28 +0200 | [diff] [blame] | 77 | (*p)->thread_index = 0; |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 78 | #endif |
Dave Barach | fa77e8f | 2017-10-15 17:20:40 -0400 | [diff] [blame] | 79 | /* Make sure all writes are complete before releasing the lock */ |
| 80 | CLIB_MEMORY_BARRIER (); |
| 81 | (*p)->lock = 0; |
Damjan Marion | 1927da2 | 2017-03-27 17:08:20 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static_always_inline void |
| 85 | clib_spinlock_unlock_if_init (clib_spinlock_t * p) |
| 86 | { |
| 87 | if (PREDICT_FALSE (*p != 0)) |
| 88 | clib_spinlock_unlock (p); |
| 89 | } |
| 90 | |
| 91 | #endif |
| 92 | |
| 93 | /* |
| 94 | * fd.io coding-style-patch-verification: ON |
| 95 | * |
| 96 | * Local Variables: |
| 97 | * eval: (c-set-style "gnu") |
| 98 | * End: |
| 99 | */ |