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

Commit 62267f72 authored by Ajay Panicker's avatar Ajay Panicker Committed by android-build-merger
Browse files

Merge "Keep original thread name when attaching JNI thread for easier...

Merge "Keep original thread name when attaching JNI thread for easier debugging" am: 7a198efe am: 48a8fd13
am: d6e02aab

Change-Id: Ia732845cf2af43a117c20c4596ef8ccb087c4cbf
parents 33b12898 d6e02aab
Loading
Loading
Loading
Loading
+62 −46
Original line number Original line Diff line number Diff line
@@ -28,6 +28,7 @@
#include <string.h>
#include <string.h>


#include <fcntl.h>
#include <fcntl.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/stat.h>


namespace android {
namespace android {
@@ -447,23 +448,60 @@ static alarm_cb sAlarmCallback;
// The data to pass to the wake alarm callback.
// The data to pass to the wake alarm callback.
static void* sAlarmCallbackData;
static void* sAlarmCallbackData;


static JavaVMAttachArgs sAttachArgs = {
class JNIThreadAttacher {
    .version = JNI_VERSION_1_6, .name = "bluetooth wake", .group = NULL};
 public:
  JNIThreadAttacher() : vm_(nullptr), env_(nullptr) {
    vm_ = AndroidRuntime::getJavaVM();
    status_ = vm_->GetEnv((void**)&env_, JNI_VERSION_1_6);

    if (status_ != JNI_OK && status_ != JNI_EDETACHED) {
      ALOGE(
          "JNIThreadAttacher: unable to get environment for JNI CALL, "
          "status: %d",
          status_);
      env_ = nullptr;
      return;
    }


static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake,
    if (status_ == JNI_EDETACHED) {
                                   alarm_cb cb, void* data) {
      char name[17] = {0};
  JNIEnv* env;
      if (prctl(PR_GET_NAME, (unsigned long)name) != 0) {
  JavaVM* vm = AndroidRuntime::getJavaVM();
        ALOGE(
  jint status = vm->GetEnv((void**)&env, JNI_VERSION_1_6);
            "JNIThreadAttacher: unable to grab previous thread name, error: %s",
            strerror(errno));
        env_ = nullptr;
        return;
      }


  if (status != JNI_OK && status != JNI_EDETACHED) {
      JavaVMAttachArgs args = {
    ALOGE("%s unable to get environment for JNI call", __func__);
          .version = JNI_VERSION_1_6, .name = name, .group = nullptr};
    return false;
      if (vm_->AttachCurrentThread(&env_, &args) != 0) {
        ALOGE("JNIThreadAttacher: unable to attach thread to VM");
        env_ = nullptr;
        return;
      }
    }
  }

  ~JNIThreadAttacher() {
    if (status_ == JNI_EDETACHED) vm_->DetachCurrentThread();
  }
  }


  if (status == JNI_EDETACHED &&
  JNIEnv* getEnv() { return env_; }
      vm->AttachCurrentThread(&env, &sAttachArgs) != 0) {

    ALOGE("%s unable to attach thread to VM", __func__);
 private:
  JavaVM* vm_;
  JNIEnv* env_;
  jint status_;
};

static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake,
                                   alarm_cb cb, void* data) {
  JNIThreadAttacher attacher;
  JNIEnv* env = attacher.getEnv();

  if (env == nullptr) {
    ALOGE("%s: Unable to get JNI Env", __func__);
    return false;
    return false;
  }
  }


@@ -479,24 +517,15 @@ static bool set_wake_alarm_callout(uint64_t delay_millis, bool should_wake,
    sAlarmCallbackData = NULL;
    sAlarmCallbackData = NULL;
  }
  }


  if (status == JNI_EDETACHED) {
  return (ret == JNI_TRUE);
    vm->DetachCurrentThread();
  }

  return !!ret;
}
}


static int acquire_wake_lock_callout(const char* lock_name) {
static int acquire_wake_lock_callout(const char* lock_name) {
  JNIEnv* env;
  JNIThreadAttacher attacher;
  JavaVM* vm = AndroidRuntime::getJavaVM();
  JNIEnv* env = attacher.getEnv();
  jint status = vm->GetEnv((void**)&env, JNI_VERSION_1_6);

  if (status != JNI_OK && status != JNI_EDETACHED) {
  if (env == nullptr) {
    ALOGE("%s unable to get environment for JNI call", __func__);
    ALOGE("%s: Unable to get JNI Env", __func__);
    return BT_STATUS_JNI_ENVIRONMENT_ERROR;
  }
  if (status == JNI_EDETACHED &&
      vm->AttachCurrentThread(&env, &sAttachArgs) != 0) {
    ALOGE("%s unable to attach thread to VM", __func__);
    return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
    return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
  }
  }


@@ -513,24 +542,15 @@ static int acquire_wake_lock_callout(const char* lock_name) {
    }
    }
  }
  }


  if (status == JNI_EDETACHED) {
    vm->DetachCurrentThread();
  }

  return ret;
  return ret;
}
}


static int release_wake_lock_callout(const char* lock_name) {
static int release_wake_lock_callout(const char* lock_name) {
  JNIEnv* env;
  JNIThreadAttacher attacher;
  JavaVM* vm = AndroidRuntime::getJavaVM();
  JNIEnv* env = attacher.getEnv();
  jint status = vm->GetEnv((void**)&env, JNI_VERSION_1_6);

  if (status != JNI_OK && status != JNI_EDETACHED) {
  if (env == nullptr) {
    ALOGE("%s unable to get environment for JNI call", __func__);
    ALOGE("%s: Unable to get JNI Env", __func__);
    return BT_STATUS_JNI_ENVIRONMENT_ERROR;
  }
  if (status == JNI_EDETACHED &&
      vm->AttachCurrentThread(&env, &sAttachArgs) != 0) {
    ALOGE("%s unable to attach thread to VM", __func__);
    return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
    return BT_STATUS_JNI_THREAD_ATTACH_ERROR;
  }
  }


@@ -547,10 +567,6 @@ static int release_wake_lock_callout(const char* lock_name) {
    }
    }
  }
  }


  if (status == JNI_EDETACHED) {
    vm->DetachCurrentThread();
  }

  return ret;
  return ret;
}
}