/src/open5gs/lib/core/ogs-thread.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (C) 2019 by Sukchan Lee <acetcom@gmail.com> |
3 | | * |
4 | | * This file is part of Open5GS. |
5 | | * |
6 | | * This program is free software: you can redistribute it and/or modify |
7 | | * it under the terms of the GNU Affero General Public License as published by |
8 | | * the Free Software Foundation, either version 3 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
18 | | */ |
19 | | |
20 | | #if !defined(OGS_CORE_INSIDE) && !defined(OGS_CORE_COMPILATION) |
21 | | #error "This header cannot be included directly." |
22 | | #endif |
23 | | |
24 | | #ifndef OGS_THREAD_H |
25 | | #define OGS_THREAD_H |
26 | | |
27 | | #ifdef __cplusplus |
28 | | extern "C" { |
29 | | #endif |
30 | | |
31 | | /* |
32 | | * The following code is stolen from mongodb-c-driver |
33 | | * https://github.com/mongodb/mongo-c-driver/blob/master/src/libmongoc/src/mongoc/mongoc-thread-private.h |
34 | | */ |
35 | | #if !defined(_WIN32) |
36 | | #define ogs_thread_mutex_t pthread_mutex_t |
37 | 2 | #define ogs_thread_mutex_init(_n) (void)pthread_mutex_init((_n), NULL) |
38 | 8.10k | #define ogs_thread_mutex_lock (void)pthread_mutex_lock |
39 | 8.10k | #define ogs_thread_mutex_unlock (void)pthread_mutex_unlock |
40 | 0 | #define ogs_thread_mutex_destroy (void)pthread_mutex_destroy |
41 | | #define ogs_thread_cond_t pthread_cond_t |
42 | 0 | #define ogs_thread_cond_init(_n) (void)pthread_cond_init((_n), NULL) |
43 | 0 | #define ogs_thread_cond_wait pthread_cond_wait |
44 | | static ogs_inline int ogs_thread_cond_timedwait( |
45 | | pthread_cond_t *cond, pthread_mutex_t *mutex, ogs_time_t timeout) |
46 | 0 | { |
47 | 0 | int r; |
48 | 0 | struct timespec to; |
49 | 0 | struct timeval tv; |
50 | 0 | ogs_time_t usec; |
51 | |
|
52 | 0 | ogs_gettimeofday(&tv); |
53 | |
|
54 | 0 | usec = ogs_time_from_sec(tv.tv_sec) + tv.tv_usec + timeout; |
55 | |
|
56 | 0 | to.tv_sec = ogs_time_sec(usec); |
57 | 0 | to.tv_nsec = ogs_time_usec(usec) * 1000; |
58 | |
|
59 | 0 | r = pthread_cond_timedwait(cond, mutex, &to); |
60 | 0 | if (r == 0) |
61 | 0 | return OGS_OK; |
62 | 0 | else if (r == OGS_ETIMEDOUT) |
63 | 0 | return OGS_TIMEUP; |
64 | 0 | else |
65 | 0 | return OGS_ERROR; |
66 | 0 | } Unexecuted instantiation: nas-message-fuzz.c:ogs_thread_cond_timedwait Unexecuted instantiation: decoder.c:ogs_thread_cond_timedwait Unexecuted instantiation: ies.c:ogs_thread_cond_timedwait Unexecuted instantiation: types.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-abort.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-strings.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-conv.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-log.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-pkbuf.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-memory.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-sockaddr.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-hash.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-core.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-errno.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-time.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-socket.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-tlv.c:ogs_thread_cond_timedwait Unexecuted instantiation: gtp-message-fuzz.c:ogs_thread_cond_timedwait Unexecuted instantiation: context.c:ogs_thread_cond_timedwait Unexecuted instantiation: xact.c:ogs_thread_cond_timedwait Unexecuted instantiation: conv.c:ogs_thread_cond_timedwait Unexecuted instantiation: message.c:ogs_thread_cond_timedwait Unexecuted instantiation: path.c:ogs_thread_cond_timedwait Unexecuted instantiation: build.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-timer.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-socknode.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-udp.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-poll.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-tlv-msg.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-epoll.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-rbtree.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-sockopt.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-notify.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-yaml.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-context.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-config.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-init.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-queue.c:ogs_thread_cond_timedwait Unexecuted instantiation: ogs-env.c:ogs_thread_cond_timedwait |
67 | 0 | #define ogs_thread_cond_signal (void)pthread_cond_signal |
68 | 0 | #define ogs_thread_cond_broadcast pthread_cond_broadcast |
69 | 0 | #define ogs_thread_cond_destroy (void)pthread_cond_destroy |
70 | | #define ogs_thread_id_t pthread_t |
71 | | #define ogs_thread_join(_n) pthread_join((_n), NULL) |
72 | | #else |
73 | | #define ogs_thread_mutex_t CRITICAL_SECTION |
74 | | #define ogs_thread_mutex_init InitializeCriticalSection |
75 | | #define ogs_thread_mutex_lock EnterCriticalSection |
76 | | #define ogs_thread_mutex_unlock LeaveCriticalSection |
77 | | #define ogs_thread_mutex_destroy DeleteCriticalSection |
78 | | #define ogs_thread_cond_t CONDITION_VARIABLE |
79 | | #define ogs_thread_cond_init InitializeConditionVariable |
80 | | #define ogs_thread_cond_wait(_c, _m) \ |
81 | | ogs_thread_cond_timedwait ((_c), (_m), INFINITE) |
82 | | static ogs_inline int ogs_thread_cond_timedwait( |
83 | | ogs_thread_cond_t *cond, ogs_thread_mutex_t *mutex, ogs_time_t timeout) |
84 | | { |
85 | | int r; |
86 | | |
87 | | if (SleepConditionVariableCS(cond, mutex, |
88 | | (DWORD)ogs_time_to_msec(timeout))) { |
89 | | return OGS_OK; |
90 | | } else { |
91 | | r = GetLastError(); |
92 | | |
93 | | if (r == WAIT_TIMEOUT || r == ERROR_TIMEOUT) { |
94 | | return OGS_TIMEUP; |
95 | | } else { |
96 | | return OGS_ERROR; |
97 | | } |
98 | | } |
99 | | } |
100 | | #define ogs_thread_cond_signal WakeConditionVariable |
101 | | #define ogs_thread_cond_broadcast WakeAllConditionVariable |
102 | | static ogs_inline int ogs_thread_cond_destroy(ogs_thread_cond_t *_ignored) |
103 | | { |
104 | | return 0; |
105 | | } |
106 | | #endif |
107 | | |
108 | | typedef struct ogs_thread_s ogs_thread_t; |
109 | | |
110 | | ogs_thread_t *ogs_thread_create(void (*func)(void *), void *data); |
111 | | void ogs_thread_destroy(ogs_thread_t *thread); |
112 | | |
113 | | #ifdef __cplusplus |
114 | | } |
115 | | #endif |
116 | | |
117 | | #endif /* OGS_THREAD_H */ |