Coverage Report

Created: 2026-03-19 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/brpc/src/butil/compat.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
19
#ifndef BUTIL_COMPAT_H
20
#define BUTIL_COMPAT_H
21
22
#include "butil/build_config.h"
23
#include <pthread.h>
24
25
#if defined(OS_MACOSX)
26
27
#include <sys/cdefs.h>
28
#include <stdint.h>
29
#include <dispatch/dispatch.h>    // dispatch_semaphore
30
#include <errno.h>                // EINVAL
31
32
__BEGIN_DECLS
33
34
// Implement pthread_spinlock_t for MAC.
35
struct pthread_spinlock_t {
36
    dispatch_semaphore_t sem;
37
};
38
inline int pthread_spin_init(pthread_spinlock_t *__lock, int __pshared) {
39
    if (__pshared != 0) {
40
        return EINVAL;
41
    }
42
    __lock->sem = dispatch_semaphore_create(1);
43
    return 0;
44
}
45
inline int pthread_spin_destroy(pthread_spinlock_t *__lock) {
46
    // TODO(gejun): Not see any destructive API on dispatch_semaphore
47
    (void)__lock;
48
    return 0;
49
}
50
inline int pthread_spin_lock(pthread_spinlock_t *__lock) {
51
    return (int)dispatch_semaphore_wait(__lock->sem, DISPATCH_TIME_FOREVER);
52
}
53
inline int pthread_spin_trylock(pthread_spinlock_t *__lock) {
54
    if (dispatch_semaphore_wait(__lock->sem, DISPATCH_TIME_NOW) == 0) {
55
        return 0;
56
    }
57
    return EBUSY;
58
}
59
inline int pthread_spin_unlock(pthread_spinlock_t *__lock) {
60
    return dispatch_semaphore_signal(__lock->sem);
61
}
62
63
__END_DECLS
64
65
#elif defined(OS_LINUX)
66
67
#include <sys/epoll.h>
68
69
#else
70
71
#error "The platform does not support epoll-like APIs"
72
73
#endif // defined(OS_MACOSX)
74
75
__BEGIN_DECLS
76
77
0
inline uint64_t pthread_numeric_id() {
78
#if defined(OS_MACOSX)
79
    uint64_t id;
80
    if (pthread_threadid_np(pthread_self(), &id) == 0) {
81
        return id;
82
    }
83
    return -1;
84
#else
85
0
    return pthread_self();
86
0
#endif
87
0
}
88
89
__END_DECLS
90
91
#endif // BUTIL_COMPAT_H