Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit d5bcc5d6 authored by Sharvil Nanavati's avatar Sharvil Nanavati Committed by Android (Google) Code Review
Browse files

Merge "A simple, thread-safe timer API for bluedroid." into lmp-dev

parents d467290a 61ae5b8f
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -6,6 +6,7 @@ LOCAL_C_INCLUDES := \
    $(LOCAL_PATH)/include

LOCAL_SRC_FILES := \
    ./src/alarm.c \
    ./src/config.c \
    ./src/fixed_queue.c \
    ./src/list.c \
@@ -29,6 +30,7 @@ LOCAL_C_INCLUDES := \
    $(LOCAL_PATH)/include

LOCAL_SRC_FILES := \
    ./test/alarm_test.cpp \
    ./test/config_test.cpp \
    ./test/list_test.cpp \
    ./test/reactor_test.cpp \
+47 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2014 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#pragma once

typedef struct alarm_t alarm_t;
typedef uint64_t period_ms_t;

// Prototype for the callback function.
typedef void (*alarm_callback_t)(void *data);

// Creates a new alarm object. The returned object must be freed by calling
// |alarm_free|. Returns NULL on failure.
alarm_t *alarm_new(void);

// Frees an alarm object created by |alarm_new|. |alarm| may be NULL. If the
// alarm is pending, it will be cancelled. It is not safe to call |alarm_free|
// from inside the callback of |alarm|.
void alarm_free(alarm_t *alarm);

// Sets an alarm to fire |cb| after the given |deadline|. Note that |deadline| is the
// number of milliseconds relative to the current time. |data| is a context variable
// for the callback and may be NULL. |cb| will be called back in the context of an
// unspecified thread (i.e. it will not be called back in the same thread as the caller).
// |alarm| and |cb| may not be NULL.
void alarm_set(alarm_t *alarm, period_ms_t deadline, alarm_callback_t cb, void *data);

// This function cancels the |alarm| if it was previously set. When this call
// returns, the caller has a guarantee that the callback is not in progress and
// will not be called if it hasn't already been called. This function is idempotent.
// |alarm| may not be NULL.
void alarm_cancel(alarm_t *alarm);
+4 −3
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ void *list_front(const list_t *list);
void *list_back(const list_t *list);

// Mutators.
bool list_insert_after(list_t *list, list_node_t *prev_node, void *data);
bool list_prepend(list_t *list, void *data);
bool list_append(list_t *list, void *data);
bool list_remove(list_t *list, void *data);
@@ -31,7 +32,7 @@ void list_clear(list_t *list);
// Iteration.
void list_foreach(const list_t *list, list_iter_cb callback);

const list_node_t *list_begin(const list_t *list);
const list_node_t *list_end(const list_t *list);
const list_node_t *list_next(const list_node_t *node);
list_node_t *list_begin(const list_t *list);
list_node_t *list_end(const list_t *list);
list_node_t *list_next(const list_node_t *node);
void *list_node(const list_node_t *node);

system/osi/src/alarm.c

0 → 100644
+282 −0
Original line number Diff line number Diff line
/******************************************************************************
 *
 *  Copyright (C) 2014 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#define LOG_TAG "bt_osi_alarm"

#include <assert.h>
#include <errno.h>
#include <hardware/bluetooth.h>
#include <time.h>
#include <utils/Log.h>

#include "alarm.h"
#include "list.h"
#include "osi.h"

struct alarm_t {
  // The lock is held while the callback for this alarm is being executed.
  // It allows us to release the coarse-grained monitor lock while a potentially
  // long-running callback is executing. |alarm_cancel| uses this lock to provide
  // a guarantee to its caller that the callback will not be in progress when it
  // returns.
  pthread_mutex_t callback_lock;
  period_ms_t deadline;
  alarm_callback_t callback;
  void *data;
};

extern bt_os_callouts_t *bt_os_callouts;

// If the next wakeup time is less than this threshold, we should acquire
// a wakelock instead of setting a wake alarm so we're not bouncing in
// and out of suspend frequently. This value is externally visible to allow
// unit tests to run faster. It should not be modified by production code.
int64_t TIMER_INTERVAL_FOR_WAKELOCK_IN_MS = 3000;
static const clockid_t CLOCK_ID = CLOCK_BOOTTIME;
static const char *WAKE_LOCK_ID = "bluedroid_timer";

// This mutex ensures that the |alarm_set|, |alarm_cancel|, and alarm callback
// functions execute serially and not concurrently. As a result, this mutex also
// protects the |alarms| list.
static pthread_mutex_t monitor;
static list_t *alarms;
static timer_t timer;
static bool timer_set;

static bool lazy_initialize(void);
static period_ms_t now(void);
static void timer_callback(void *data);
static void reschedule(void);

alarm_t *alarm_new(void) {
  // Make sure we have a list we can insert alarms into.
  if (!alarms && !lazy_initialize())
    return NULL;

  pthread_mutexattr_t attr;
  pthread_mutexattr_init(&attr);

  alarm_t *ret = calloc(1, sizeof(alarm_t));
  if (!ret) {
    ALOGE("%s unable to allocate memory for alarm.", __func__);
    goto error;
  }

  // Make this a recursive mutex to make it safe to call |alarm_cancel| from
  // within the callback function of the alarm.
  int error = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
  if (error) {
    ALOGE("%s unable to create a recursive mutex: %s", __func__, strerror(error));
    goto error;
  }

  error = pthread_mutex_init(&ret->callback_lock, &attr);
  if (error) {
    ALOGE("%s unable to initialize mutex: %s", __func__, strerror(error));
    goto error;
  }

  pthread_mutexattr_destroy(&attr);
  return ret;

error:;
  pthread_mutexattr_destroy(&attr);
  free(ret);
  return NULL;
}

void alarm_free(alarm_t *alarm) {
  if (!alarm)
    return;

  alarm_cancel(alarm);
  pthread_mutex_destroy(&alarm->callback_lock);
  free(alarm);
}

// Runs in exclusion with alarm_cancel and timer_callback.
void alarm_set(alarm_t *alarm, period_ms_t deadline, alarm_callback_t cb, void *data) {
  assert(alarms != NULL);
  assert(alarm != NULL);
  assert(cb != NULL);

  pthread_mutex_lock(&monitor);

  // If the alarm is currently set and it's at the start of the list,
  // we'll need to re-schedule since we've adjusted the earliest deadline.
  bool needs_reschedule = (!list_is_empty(alarms) && list_front(alarms) == alarm);
  if (alarm->callback)
    list_remove(alarms, alarm);

  alarm->deadline = now() + deadline;
  alarm->callback = cb;
  alarm->data = data;

  // Add it into the timer list sorted by deadline (earliest deadline first).
  if (list_is_empty(alarms))
    list_prepend(alarms, alarm);
  else
    for (list_node_t *node = list_begin(alarms); node != list_end(alarms); node = list_next(node)) {
      list_node_t *next = list_next(node);
      if (next == list_end(alarms) || ((alarm_t *)list_node(next))->deadline >= alarm->deadline) {
        list_insert_after(alarms, node, alarm);
        break;
      }
    }

  // If the new alarm has the earliest deadline, we need to re-evaluate our schedule.
  if (needs_reschedule || (!list_is_empty(alarms) && list_front(alarms) == alarm))
    reschedule();

  pthread_mutex_unlock(&monitor);
}

void alarm_cancel(alarm_t *alarm) {
  assert(alarms != NULL);
  assert(alarm != NULL);

  pthread_mutex_lock(&monitor);

  bool needs_reschedule = (!list_is_empty(alarms) && list_front(alarms) == alarm);

  list_remove(alarms, alarm);
  alarm->deadline = 0;
  alarm->callback = NULL;
  alarm->data = NULL;

  if (needs_reschedule)
    reschedule();

  pthread_mutex_unlock(&monitor);

  // If the callback for |alarm| is in progress, wait here until it completes.
  pthread_mutex_lock(&alarm->callback_lock);
  pthread_mutex_unlock(&alarm->callback_lock);
}

static bool lazy_initialize(void) {
  assert(alarms == NULL);

  pthread_mutex_init(&monitor, NULL);

  alarms = list_new(NULL);
  if (!alarms) {
    ALOGE("%s unable to allocate alarm list.", __func__);
    return false;
  }

  return true;
}

static period_ms_t now(void) {
  assert(alarms != NULL);

  struct timespec ts;
  if (clock_gettime(CLOCK_ID, &ts) == -1) {
    ALOGE("%s unable to get current time: %s", __func__, strerror(errno));
    return 0;
  }

  return (ts.tv_sec * 1000LL) + (ts.tv_nsec / 1000000LL);
}

// Warning: this function is called in the context of an unknown thread.
// As a result, it must be thread-safe relative to other operations on
// the alarm list.
static void timer_callback(void *ptr) {
  alarm_t *alarm = (alarm_t *)ptr;
  assert(alarm != NULL);

  pthread_mutex_lock(&monitor);

  bool alarm_valid = list_remove(alarms, alarm);
  alarm_callback_t callback = alarm->callback;
  void *data = alarm->data;

  alarm->deadline = 0;
  alarm->callback = NULL;
  alarm->data = NULL;

  reschedule();

  // The alarm was cancelled before we got to it. Release the monitor
  // lock and exit right away since there's nothing left to do.
  if (!alarm_valid) {
    pthread_mutex_unlock(&monitor);
    return;
  }

  // Downgrade lock.
  pthread_mutex_lock(&alarm->callback_lock);
  pthread_mutex_unlock(&monitor);

  callback(data);

  pthread_mutex_unlock(&alarm->callback_lock);
}

// NOTE: must be called with monitor lock.
static void reschedule(void) {
  assert(alarms != NULL);

  if (timer_set) {
    timer_delete(timer);
    timer_set = false;
  }

  if (list_is_empty(alarms)) {
    bt_os_callouts->release_wake_lock(WAKE_LOCK_ID);
    return;
  }

  alarm_t *next = list_front(alarms);
  int64_t next_exp = next->deadline - now();
  if (next_exp < TIMER_INTERVAL_FOR_WAKELOCK_IN_MS) {
    int status = bt_os_callouts->acquire_wake_lock(WAKE_LOCK_ID);
    if (status != BT_STATUS_SUCCESS) {
      ALOGE("%s unable to acquire wake lock: %d", __func__, status);
      return;
    }

    struct sigevent sigevent;
    memset(&sigevent, 0, sizeof(sigevent));
    sigevent.sigev_notify = SIGEV_THREAD;
    sigevent.sigev_notify_function = (void (*)(union sigval))timer_callback;
    sigevent.sigev_value.sival_ptr = next;
    if (timer_create(CLOCK_ID, &sigevent, &timer) == -1) {
      ALOGE("%s unable to create timer: %s", __func__, strerror(errno));
      return;
    }

    struct itimerspec wakeup_time;
    memset(&wakeup_time, 0, sizeof(wakeup_time));
    wakeup_time.it_value.tv_sec = (next->deadline / 1000);
    wakeup_time.it_value.tv_nsec = (next->deadline % 1000) * 1000000LL;
    if (timer_settime(timer, TIMER_ABSTIME, &wakeup_time, NULL) == -1) {
      ALOGE("%s unable to set timer: %s", __func__, strerror(errno));
      timer_delete(timer);
      return;
    }
    timer_set = true;
  } else {
    if (!bt_os_callouts->set_wake_alarm(next_exp, true, timer_callback, next))
      ALOGE("%s unable to set wake alarm for %lldms.", __func__, next_exp);

    bt_os_callouts->release_wake_lock(WAKE_LOCK_ID);
  }
}
+21 −3
Original line number Diff line number Diff line
@@ -71,6 +71,24 @@ void *list_back(const list_t *list) {
  return list->tail->data;
}

bool list_insert_after(list_t *list, list_node_t *prev_node, void *data) {
  assert(list != NULL);
  assert(node != NULL);
  assert(data != NULL);

  list_node_t *node = (list_node_t *)malloc(sizeof(list_node_t));
  if (!node)
    return false;

  node->next = prev_node->next;
  node->data = data;
  prev_node->next = node;
  if (list->tail == prev_node)
    list->tail = node;
  ++list->length;
  return true;
}

// Inserts |data| at the beginning of |list|. Neither |data| nor |list| may be NULL.
// This function does not make a copy of |data| so the pointer must remain valid
// at least until the element is removed from the list or the list is freed.
@@ -176,7 +194,7 @@ void list_foreach(const list_t *list, list_iter_cb callback) {
// Returns an iterator to the first element in |list|. |list| may not be NULL.
// The returned iterator is valid as long as it does not equal the value returned
// by |list_end|.
const list_node_t *list_begin(const list_t *list) {
list_node_t *list_begin(const list_t *list) {
  assert(list != NULL);
  return list->head;
}
@@ -185,7 +203,7 @@ const list_node_t *list_begin(const list_t *list) {
// this function returns the value of an invalid iterator for the given list.
// When an iterator has the same value as what's returned by this function, you
// may no longer call |list_next| with the iterator. |list| may not be NULL.
const list_node_t *list_end(UNUSED_ATTR const list_t *list) {
list_node_t *list_end(UNUSED_ATTR const list_t *list) {
  assert(list != NULL);
  return NULL;
}
@@ -194,7 +212,7 @@ const list_node_t *list_end(UNUSED_ATTR const list_t *list) {
// iterator. If the returned value equals the value returned by |list_end|, the
// iterator has reached the end of the list and may no longer be used for any
// purpose.
const list_node_t *list_next(const list_node_t *node) {
list_node_t *list_next(const list_node_t *node) {
  assert(node != NULL);
  return node->next;
}
Loading