1 /* 2 * Copyright 2013 MongoDB, Inc. 3 * 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 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 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. 15 */ 16 17 18 #ifndef BSON_THREAD_PRIVATE_H 19 #define BSON_THREAD_PRIVATE_H 20 21 22 #if !defined(BSON_INSIDE) && !defined(BSON_COMPILATION) 23 #error "Only <bson.h> can be included directly." 24 #endif 25 26 27 #include "bson-compat.h" 28 #include "bson-config.h" 29 #include "bson-macros.h" 30 31 32 BSON_BEGIN_DECLS 33 34 35 #if defined(BSON_OS_UNIX) 36 #include <pthread.h> 37 #define bson_mutex_t pthread_mutex_t 38 #define bson_mutex_init(_n) pthread_mutex_init ((_n), NULL) 39 #define bson_mutex_lock pthread_mutex_lock 40 #define bson_mutex_unlock pthread_mutex_unlock 41 #define bson_mutex_destroy pthread_mutex_destroy 42 #define bson_thread_t pthread_t 43 #define bson_thread_create(_t, _f, _d) pthread_create ((_t), NULL, (_f), (_d)) 44 #define bson_thread_join(_n) pthread_join ((_n), NULL) 45 #define bson_once_t pthread_once_t 46 #define bson_once pthread_once 47 #define BSON_ONCE_FUN(n) void n (void) 48 #define BSON_ONCE_RETURN return 49 #ifdef BSON_PTHREAD_ONCE_INIT_NEEDS_BRACES 50 #define BSON_ONCE_INIT \ 51 { \ 52 PTHREAD_ONCE_INIT \ 53 } 54 #else 55 #define BSON_ONCE_INIT PTHREAD_ONCE_INIT 56 #endif 57 #else 58 #define bson_mutex_t CRITICAL_SECTION 59 #define bson_mutex_init InitializeCriticalSection 60 #define bson_mutex_lock EnterCriticalSection 61 #define bson_mutex_unlock LeaveCriticalSection 62 #define bson_mutex_destroy DeleteCriticalSection 63 #define bson_thread_t HANDLE 64 #define bson_thread_create(_t, _f, _d) \ 65 (!(*(_t) = CreateThread (NULL, 0, (void *) _f, _d, 0, NULL))) 66 #define bson_thread_join(_n) WaitForSingleObject ((_n), INFINITE) 67 #define bson_once_t INIT_ONCE 68 #define BSON_ONCE_INIT INIT_ONCE_STATIC_INIT 69 #define bson_once(o, c) InitOnceExecuteOnce (o, c, NULL, NULL) 70 #define BSON_ONCE_FUN(n) \ 71 BOOL CALLBACK n (PINIT_ONCE _ignored_a, PVOID _ignored_b, PVOID *_ignored_c) 72 #define BSON_ONCE_RETURN return true 73 #endif 74 75 76 BSON_END_DECLS 77 78 79 #endif /* BSON_THREAD_PRIVATE_H */ 80