blob: f7627fdf8efcf6ef89050e0ebea94fa99c9a846f [file] [log] [blame]
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -07001/******************************************************************************
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 Nanavati405b5c92016-06-17 14:15:46 -070024#include <unistd.h>
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070025
26#define WAIT(callback) \
27 do { \
28 sem_t *semaphore = callbacks_get_semaphore(#callback); \
Sharvil Nanavati405b5c92016-06-17 14:15:46 -070029 TEMP_FAILURE_RETRY(sem_wait(semaphore)); \
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070030 } 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 Nanavati405b5c92016-06-17 14:15:46 -070037 TEMP_FAILURE_RETRY(sem_wait(semaphore)); \
Sharvil Nanavati3cf59ef2014-04-09 22:20:40 -070038 } 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
48void callbacks_init();
49void callbacks_cleanup();
50
51bt_callbacks_t *callbacks_get_adapter_struct();
52btpan_callbacks_t *callbacks_get_pan_struct();
53sem_t *callbacks_get_semaphore(const char *name);