Sharvil Nanavati | 3cf59ef | 2014-04-09 22:20:40 -0700 | [diff] [blame] | 1 | /****************************************************************************** |
| 2 | * |
| 3 | * Copyright (C) 2014 Google, Inc. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * 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, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | ******************************************************************************/ |
| 18 | |
| 19 | #pragma once |
| 20 | |
| 21 | #include "base.h" |
| 22 | |
| 23 | #include <semaphore.h> |
Sharvil Nanavati | 405b5c9 | 2016-06-17 14:15:46 -0700 | [diff] [blame] | 24 | #include <unistd.h> |
Sharvil Nanavati | 3cf59ef | 2014-04-09 22:20:40 -0700 | [diff] [blame] | 25 | |
| 26 | #define WAIT(callback) \ |
| 27 | do { \ |
| 28 | sem_t *semaphore = callbacks_get_semaphore(#callback); \ |
Sharvil Nanavati | 405b5c9 | 2016-06-17 14:15:46 -0700 | [diff] [blame] | 29 | TEMP_FAILURE_RETRY(sem_wait(semaphore)); \ |
Sharvil Nanavati | 3cf59ef | 2014-04-09 22:20:40 -0700 | [diff] [blame] | 30 | } while (0) |
| 31 | |
| 32 | #define CALL_AND_WAIT(expression, callback) \ |
| 33 | do { \ |
| 34 | sem_t *semaphore = callbacks_get_semaphore(#callback); \ |
| 35 | while (!sem_trywait(semaphore)); \ |
| 36 | expression; \ |
Sharvil Nanavati | 405b5c9 | 2016-06-17 14:15:46 -0700 | [diff] [blame] | 37 | TEMP_FAILURE_RETRY(sem_wait(semaphore)); \ |
Sharvil Nanavati | 3cf59ef | 2014-04-09 22:20:40 -0700 | [diff] [blame] | 38 | } while(0) |
| 39 | |
| 40 | // To be called from every exit point of the callback. This macro |
| 41 | // takes 0 or 1 arguments, the return value of the callback. |
| 42 | #define CALLBACK_RET(...) do { \ |
| 43 | sem_t *semaphore = callbacks_get_semaphore(__func__); \ |
| 44 | sem_post(semaphore); \ |
| 45 | return __VA_ARGS__; \ |
| 46 | } while (0) |
| 47 | |
| 48 | void callbacks_init(); |
| 49 | void callbacks_cleanup(); |
| 50 | |
| 51 | bt_callbacks_t *callbacks_get_adapter_struct(); |
| 52 | btpan_callbacks_t *callbacks_get_pan_struct(); |
| 53 | sem_t *callbacks_get_semaphore(const char *name); |