2 * Copyright (c) 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #define OVS_THREAD_H 1
23 /* glibc has some non-portable mutex types and initializers:
25 * - PTHREAD_MUTEX_ADAPTIVE_NP is a mutex type that works as a spinlock that
26 * falls back to a mutex after spinning for some number of iterations.
28 * - PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP is a non-portable initializer
29 * for an error-checking mutex.
31 * We use these definitions to fall back to PTHREAD_MUTEX_NORMAL instead in
34 * (glibc has other non-portable initializers, but we can't reasonably
35 * substitute for them here.) */
36 #ifdef PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
37 #define PTHREAD_MUTEX_ADAPTIVE PTHREAD_MUTEX_ADAPTIVE_NP
38 #define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER \
39 PTHREAD_ADAPTIVE_MUTEX_INITIALIZER_NP
41 #define PTHREAD_MUTEX_ADAPTIVE PTHREAD_MUTEX_NORMAL
42 #define PTHREAD_ADAPTIVE_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
45 #ifdef PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
46 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER \
47 PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP
49 #define PTHREAD_ERRORCHECK_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER
52 /* Simple wrappers for pthreads functions. Most of these functions abort the
53 * process with an error message on any error. The *_trylock() functions are
54 * exceptions: they pass through a 0 or EBUSY return value to the caller and
55 * abort on any other error. */
56 void xpthread_mutex_init(pthread_mutex_t *, pthread_mutexattr_t *);
57 void xpthread_mutex_lock(pthread_mutex_t *mutex) OVS_ACQUIRES(mutex);
58 void xpthread_mutex_unlock(pthread_mutex_t *mutex) OVS_RELEASES(mutex);
59 int xpthread_mutex_trylock(pthread_mutex_t *);
61 void xpthread_rwlock_init(pthread_rwlock_t *, pthread_rwlockattr_t *);
62 void xpthread_rwlock_rdlock(pthread_rwlock_t *rwlock) OVS_ACQUIRES(rwlock);
63 void xpthread_rwlock_wrlock(pthread_rwlock_t *rwlock) OVS_ACQUIRES(rwlock);
64 void xpthread_rwlock_unlock(pthread_rwlock_t *rwlock) OVS_RELEASES(rwlock);
65 int xpthread_rwlock_tryrdlock(pthread_rwlock_t *);
66 int xpthread_rwlock_trywrlock(pthread_rwlock_t *);
68 void xpthread_cond_init(pthread_cond_t *, pthread_condattr_t *);
69 void xpthread_cond_signal(pthread_cond_t *);
70 void xpthread_cond_broadcast(pthread_cond_t *);
71 void xpthread_cond_wait(pthread_cond_t *, pthread_mutex_t *mutex)
75 /* Replace these functions by the macros already defined in the <pthread.h>
76 * annotations, because the macro definitions have correct semantics for the
77 * conditional acquisition that can't be captured in a function annotation.
78 * The difference in semantics from pthread_*() to xpthread_*() does not matter
79 * because sparse is not a compiler. */
80 #define xpthread_mutex_trylock pthread_mutex_trylock
81 #define xpthread_rwlock_tryrdlock pthread_rwlock_tryrdlock
82 #define xpthread_rwlock_trywrlock pthread_rwlock_trywrlock
85 void xpthread_key_create(pthread_key_t *, void (*destructor)(void *));
87 void xpthread_create(pthread_t *, pthread_attr_t *, void *(*)(void *), void *);
89 #endif /* ovs-thread.h */