/Users/deen/code/yugabyte-db/src/yb/gutil/spinlock_posix-inl.h
Line | Count | Source |
1 | | // -*- Mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- |
2 | | /* Copyright (c) 2009, Google Inc. |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions are |
7 | | * met: |
8 | | * |
9 | | * * Redistributions of source code must retain the above copyright |
10 | | * notice, this list of conditions and the following disclaimer. |
11 | | * * Redistributions in binary form must reproduce the above |
12 | | * copyright notice, this list of conditions and the following disclaimer |
13 | | * in the documentation and/or other materials provided with the |
14 | | * distribution. |
15 | | * * Neither the name of Google Inc. nor the names of its |
16 | | * contributors may be used to endorse or promote products derived from |
17 | | * this software without specific prior written permission. |
18 | | * |
19 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 | | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 | | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 | | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 | | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 | | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 | | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | | * |
31 | | * The following only applies to changes made to this file as part of YugaByte development. |
32 | | * |
33 | | * Portions Copyright (c) YugaByte, Inc. |
34 | | * |
35 | | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
36 | | * in compliance with the License. You may obtain a copy of the License at |
37 | | * |
38 | | * http://www.apache.org/licenses/LICENSE-2.0 |
39 | | * |
40 | | * Unless required by applicable law or agreed to in writing, software distributed under the License |
41 | | * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
42 | | * or implied. See the License for the specific language governing permissions and limitations |
43 | | * under the License. |
44 | | * |
45 | | * |
46 | | * --- |
47 | | * This file is a Posix-specific part of spinlock_internal.cc |
48 | | */ |
49 | | |
50 | | #ifndef YB_GUTIL_SPINLOCK_POSIX_INL_H |
51 | | #define YB_GUTIL_SPINLOCK_POSIX_INL_H |
52 | | |
53 | | #include <errno.h> |
54 | | #if defined(HAVE_SCHED_H) || defined(__APPLE__) |
55 | | #include <sched.h> /* For sched_yield() */ |
56 | | #endif |
57 | | #include <time.h> /* For nanosleep() */ |
58 | | |
59 | | namespace yb { |
60 | | namespace base { |
61 | | namespace internal { |
62 | | |
63 | 8.57M | void SpinLockDelay(volatile Atomic32 *w, int32 value, int loop) { |
64 | 8.57M | int save_errno = errno; |
65 | 8.57M | if (loop == 0) { |
66 | 8.57M | } else if (loop == 1) { |
67 | 5.73M | sched_yield(); |
68 | 2.83M | } else { |
69 | 2.83M | struct timespec tm; |
70 | 2.83M | tm.tv_sec = 0; |
71 | 2.83M | tm.tv_nsec = yb::base::internal::SuggestedDelayNS(loop); |
72 | 2.83M | nanosleep(&tm, NULL); |
73 | 2.83M | } |
74 | 8.57M | errno = save_errno; |
75 | 8.57M | } |
76 | | |
77 | 18.1M | void SpinLockWake(volatile Atomic32 *w, bool all) { |
78 | 18.1M | } |
79 | | |
80 | | } // namespace internal |
81 | | } // namespace base |
82 | | } // namespace yb |
83 | | |
84 | | #endif // YB_GUTIL_SPINLOCK_POSIX_INL_H |