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

Commit a17e14bf authored by William Escande's avatar William Escande Committed by Gerrit Code Review
Browse files

Merge "Replace semaphore with future"

parents cec70ff5 ceab092a
Loading
Loading
Loading
Loading
+32 −30
Original line number Diff line number Diff line
@@ -34,7 +34,6 @@
#include "main/shim/shim.h"
#include "osi/include/log.h"
#include "osi/include/osi.h"
#include "osi/include/semaphore.h"
#include "stack/include/acl_api.h"
#include "stack/include/btm_client_interface.h"
#include "stack/include/btu.h"
@@ -135,7 +134,7 @@ static bool stack_is_initialized;
// If running, the stack is fully up and able to bluetooth.
static bool stack_is_running;

static void event_init_stack(semaphore_t* semaphore,
static void event_init_stack(std::promise<void> promise,
                             bluetooth::core::CoreInterface* interface);
static void event_start_up_stack(bluetooth::core::CoreInterface* interface,
                                 ProfileStartCallback startProfiles,
@@ -165,12 +164,12 @@ static void init_stack(bluetooth::core::CoreInterface* interface) {
  // state modification only happens there. Using the thread to perform
  // all stack operations ensures that the operations are done serially
  // and do not overlap.
  semaphore_t* semaphore = semaphore_new(0);
  std::promise<void> promise;
  auto future = promise.get_future();
  management_thread.DoInThread(
      FROM_HERE,
      base::Bind(event_init_stack, semaphore, base::Unretained(interface)));
  semaphore_wait(semaphore);
  semaphore_free(semaphore);
      FROM_HERE, base::BindOnce(event_init_stack, std::move(promise),
                                base::Unretained(interface)));
  future.wait();
}

static void start_up_stack_async(bluetooth::core::CoreInterface* interface,
@@ -249,14 +248,7 @@ inline const module_t* get_local_module(const char* name) {
  return nullptr;
}

// Synchronous function to initialize the stack
static void event_init_stack(semaphore_t* semaphore,
                             bluetooth::core::CoreInterface* interface) {
  LOG_INFO("is initializing the stack");

  if (stack_is_initialized) {
    LOG_INFO("found the stack already in initialized state");
  } else {
static void init_stack_internal(bluetooth::core::CoreInterface* interface) {
  // all callbacks out of libbluetooth-core happen via this interface
  interfaceToProfiles = interface;

@@ -276,18 +268,28 @@ static void event_init_stack(semaphore_t* semaphore,
  stack_is_initialized = true;
}

// Synchronous function to initialize the stack
static void event_init_stack(std::promise<void> promise,
                             bluetooth::core::CoreInterface* interface) {
  LOG_INFO("is initializing the stack");

  if (stack_is_initialized) {
    LOG_INFO("found the stack already in initialized state");
  } else {
    init_stack_internal(interface);
  }

  LOG_INFO("finished");

  if (semaphore) semaphore_post(semaphore);
  promise.set_value();
}

static void ensure_stack_is_initialized(
    bluetooth::core::CoreInterface* interface) {
  if (!stack_is_initialized) {
    LOG_WARN("%s found the stack was uninitialized. Initializing now.",
             __func__);
    // No semaphore needed since we are calling it directly
    event_init_stack(nullptr, interface);
    LOG_WARN("found the stack was uninitialized. Initializing now.");
    // No future needed since we are calling it directly
    init_stack_internal(interface);
  }
}