1*0a6a1f1dSLionel Sambuc /* $NetBSD: heim_threads.h,v 1.1.1.3 2014/04/24 12:45:27 pettai Exp $ */ 2ebfedea0SLionel Sambuc 3ebfedea0SLionel Sambuc /* 4ebfedea0SLionel Sambuc * Copyright (c) 2003 Kungliga Tekniska Högskolan 5ebfedea0SLionel Sambuc * (Royal Institute of Technology, Stockholm, Sweden). 6ebfedea0SLionel Sambuc * All rights reserved. 7ebfedea0SLionel Sambuc * 8ebfedea0SLionel Sambuc * Redistribution and use in source and binary forms, with or without 9ebfedea0SLionel Sambuc * modification, are permitted provided that the following conditions 10ebfedea0SLionel Sambuc * are met: 11ebfedea0SLionel Sambuc * 12ebfedea0SLionel Sambuc * 1. Redistributions of source code must retain the above copyright 13ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer. 14ebfedea0SLionel Sambuc * 15ebfedea0SLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright 16ebfedea0SLionel Sambuc * notice, this list of conditions and the following disclaimer in the 17ebfedea0SLionel Sambuc * documentation and/or other materials provided with the distribution. 18ebfedea0SLionel Sambuc * 19ebfedea0SLionel Sambuc * 3. Neither the name of the Institute nor the names of its contributors 20ebfedea0SLionel Sambuc * may be used to endorse or promote products derived from this software 21ebfedea0SLionel Sambuc * without specific prior written permission. 22ebfedea0SLionel Sambuc * 23ebfedea0SLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 24ebfedea0SLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25ebfedea0SLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26ebfedea0SLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 27ebfedea0SLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28ebfedea0SLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29ebfedea0SLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30ebfedea0SLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31ebfedea0SLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32ebfedea0SLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33ebfedea0SLionel Sambuc * SUCH DAMAGE. 34ebfedea0SLionel Sambuc */ 35ebfedea0SLionel Sambuc 36ebfedea0SLionel Sambuc /* Id */ 37ebfedea0SLionel Sambuc 38ebfedea0SLionel Sambuc /* 39ebfedea0SLionel Sambuc * Provide wrapper macros for thread synchronization primitives so we 40ebfedea0SLionel Sambuc * can use native thread functions for those operating system that 41ebfedea0SLionel Sambuc * supports it. 42ebfedea0SLionel Sambuc * 43ebfedea0SLionel Sambuc * This is so libkrb5.so (or more importantly, libgssapi.so) can have 44ebfedea0SLionel Sambuc * thread support while the program that that dlopen(3)s the library 45ebfedea0SLionel Sambuc * don't need to be linked to libpthread. 46ebfedea0SLionel Sambuc */ 47ebfedea0SLionel Sambuc 48ebfedea0SLionel Sambuc #ifndef HEIM_THREADS_H 49ebfedea0SLionel Sambuc #define HEIM_THREADS_H 1 50ebfedea0SLionel Sambuc 51ebfedea0SLionel Sambuc /* assume headers already included */ 52ebfedea0SLionel Sambuc 53ebfedea0SLionel Sambuc #if defined(__NetBSD__) && __NetBSD_Version__ >= 106120000 && __NetBSD_Version__< 299001200 && defined(ENABLE_PTHREAD_SUPPORT) 54ebfedea0SLionel Sambuc 55ebfedea0SLionel Sambuc /* 56ebfedea0SLionel Sambuc * NetBSD have a thread lib that we can use that part of libc that 57ebfedea0SLionel Sambuc * works regardless if application are linked to pthreads or not. 58ebfedea0SLionel Sambuc * NetBSD newer then 2.99.11 just use pthread.h, and the same thing 59ebfedea0SLionel Sambuc * will happen. 60ebfedea0SLionel Sambuc */ 61ebfedea0SLionel Sambuc #include <threadlib.h> 62ebfedea0SLionel Sambuc 63ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX mutex_t 64ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_INITIALIZER MUTEX_INITIALIZER 65ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_init(m) mutex_init(m, NULL) 66ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_lock(m) mutex_lock(m) 67ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_unlock(m) mutex_unlock(m) 68ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_destroy(m) mutex_destroy(m) 69ebfedea0SLionel Sambuc 70ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK rwlock_t 71ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER 72ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_init(l) rwlock_init(l, NULL) 73ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_rdlock(l) rwlock_rdlock(l) 74ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_wrlock(l) rwlock_wrlock(l) 75ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_tryrdlock(l) rwlock_tryrdlock(l) 76ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_trywrlock(l) rwlock_trywrlock(l) 77ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_unlock(l) rwlock_unlock(l) 78ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_destroy(l) rwlock_destroy(l) 79ebfedea0SLionel Sambuc 80ebfedea0SLionel Sambuc #define HEIMDAL_thread_key thread_key_t 81ebfedea0SLionel Sambuc #define HEIMDAL_key_create(k,d,r) do { r = thr_keycreate(k,d); } while(0) 82ebfedea0SLionel Sambuc #define HEIMDAL_setspecific(k,s,r) do { r = thr_setspecific(k,s); } while(0) 83ebfedea0SLionel Sambuc #define HEIMDAL_getspecific(k) thr_getspecific(k) 84ebfedea0SLionel Sambuc #define HEIMDAL_key_delete(k) thr_keydelete(k) 85ebfedea0SLionel Sambuc 86ebfedea0SLionel Sambuc #elif defined(ENABLE_PTHREAD_SUPPORT) && (!defined(__NetBSD__) || __NetBSD_Version__ >= 299001200) 87ebfedea0SLionel Sambuc 88ebfedea0SLionel Sambuc #include <pthread.h> 89ebfedea0SLionel Sambuc 90ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX pthread_mutex_t 91ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_INITIALIZER PTHREAD_MUTEX_INITIALIZER 92ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_init(m) pthread_mutex_init(m, NULL) 93ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_lock(m) pthread_mutex_lock(m) 94ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_unlock(m) pthread_mutex_unlock(m) 95ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_destroy(m) pthread_mutex_destroy(m) 96ebfedea0SLionel Sambuc 97ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK rwlock_t 98ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_INITIALIZER RWLOCK_INITIALIZER 99ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_init(l) pthread_rwlock_init(l, NULL) 100ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_rdlock(l) pthread_rwlock_rdlock(l) 101ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_wrlock(l) pthread_rwlock_wrlock(l) 102ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_tryrdlock(l) pthread_rwlock_tryrdlock(l) 103ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_trywrlock(l) pthread_rwlock_trywrlock(l) 104ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_unlock(l) pthread_rwlock_unlock(l) 105ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_destroy(l) pthread_rwlock_destroy(l) 106ebfedea0SLionel Sambuc 107ebfedea0SLionel Sambuc #define HEIMDAL_thread_key pthread_key_t 108ebfedea0SLionel Sambuc #define HEIMDAL_key_create(k,d,r) do { r = pthread_key_create(k,d); } while(0) 109ebfedea0SLionel Sambuc #define HEIMDAL_setspecific(k,s,r) do { r = pthread_setspecific(k,s); } while(0) 110ebfedea0SLionel Sambuc #define HEIMDAL_getspecific(k) pthread_getspecific(k) 111ebfedea0SLionel Sambuc #define HEIMDAL_key_delete(k) pthread_key_delete(k) 112ebfedea0SLionel Sambuc 113ebfedea0SLionel Sambuc #elif defined(HEIMDAL_DEBUG_THREADS) 114ebfedea0SLionel Sambuc 115ebfedea0SLionel Sambuc /* no threads support, just do consistency checks */ 116ebfedea0SLionel Sambuc #include <stdlib.h> 117ebfedea0SLionel Sambuc 118ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX int 119ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_INITIALIZER 0 120ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_init(m) do { (*(m)) = 0; } while(0) 121ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_lock(m) do { if ((*(m))++ != 0) abort(); } while(0) 122ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_unlock(m) do { if ((*(m))-- != 1) abort(); } while(0) 123ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_destroy(m) do {if ((*(m)) != 0) abort(); } while(0) 124ebfedea0SLionel Sambuc 125ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK rwlock_t int 126ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_INITIALIZER 0 127ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_init(l) do { } while(0) 128ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0) 129ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0) 130ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0) 131ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0) 132ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_unlock(l) do { } while(0) 133ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_destroy(l) do { } while(0) 134ebfedea0SLionel Sambuc 135ebfedea0SLionel Sambuc #define HEIMDAL_internal_thread_key 1 136ebfedea0SLionel Sambuc 137ebfedea0SLionel Sambuc #else /* no thread support, no debug case */ 138ebfedea0SLionel Sambuc 139ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX int 140ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_INITIALIZER 0 141ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_init(m) do { (void)(m); } while(0) 142ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_lock(m) do { (void)(m); } while(0) 143ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_unlock(m) do { (void)(m); } while(0) 144ebfedea0SLionel Sambuc #define HEIMDAL_MUTEX_destroy(m) do { (void)(m); } while(0) 145ebfedea0SLionel Sambuc 146ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK rwlock_t int 147ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_INITIALIZER 0 148ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_init(l) do { } while(0) 149ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_rdlock(l) do { } while(0) 150ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_wrlock(l) do { } while(0) 151ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_tryrdlock(l) do { } while(0) 152ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_trywrlock(l) do { } while(0) 153ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_unlock(l) do { } while(0) 154ebfedea0SLionel Sambuc #define HEIMDAL_RWLOCK_destroy(l) do { } while(0) 155ebfedea0SLionel Sambuc 156ebfedea0SLionel Sambuc #define HEIMDAL_internal_thread_key 1 157ebfedea0SLionel Sambuc 158ebfedea0SLionel Sambuc #endif /* no thread support */ 159ebfedea0SLionel Sambuc 160ebfedea0SLionel Sambuc #ifdef HEIMDAL_internal_thread_key 161ebfedea0SLionel Sambuc 162ebfedea0SLionel Sambuc typedef struct heim_thread_key { 163ebfedea0SLionel Sambuc void *value; 164ebfedea0SLionel Sambuc void (*destructor)(void *); 165ebfedea0SLionel Sambuc } heim_thread_key; 166ebfedea0SLionel Sambuc 167ebfedea0SLionel Sambuc #define HEIMDAL_thread_key heim_thread_key 168ebfedea0SLionel Sambuc #define HEIMDAL_key_create(k,d,r) \ 169ebfedea0SLionel Sambuc do { (k)->value = NULL; (k)->destructor = (d); r = 0; } while(0) 170ebfedea0SLionel Sambuc #define HEIMDAL_setspecific(k,s,r) do { (k).value = s ; r = 0; } while(0) 171ebfedea0SLionel Sambuc #define HEIMDAL_getspecific(k) ((k).value) 172ebfedea0SLionel Sambuc #define HEIMDAL_key_delete(k) do { (*(k).destructor)((k).value); } while(0) 173ebfedea0SLionel Sambuc 174ebfedea0SLionel Sambuc #undef HEIMDAL_internal_thread_key 175ebfedea0SLionel Sambuc #endif /* HEIMDAL_internal_thread_key */ 176ebfedea0SLionel Sambuc 177ebfedea0SLionel Sambuc #endif /* HEIM_THREADS_H */ 178