Skip to content

Commit

Permalink
Bug 1251179 - Ensure that the lock protecting the CPU sleep functiona…
Browse files Browse the repository at this point in the history
…lity is always valid r=dhylands
  • Loading branch information
gabrielesvelto committed Feb 27, 2016
1 parent 9feeb71 commit bebcf88
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions hal/gonk/GonkHal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "mozilla/Monitor.h"
#include "mozilla/RefPtr.h"
#include "mozilla/Services.h"
#include "mozilla/StaticMutex.h"
#include "mozilla/StaticPtr.h"
#include "mozilla/Preferences.h"
#include "nsAlgorithm.h"
Expand Down Expand Up @@ -720,7 +721,7 @@ bool sScreenEnabled = true;
bool sCpuSleepAllowed = true;

// Some CPU wake locks may be acquired internally in HAL. We use a counter to
// keep track of these needs. Note we have to hold |sInternalLockCpuMonitor|
// keep track of these needs. Note we have to hold |sInternalLockCpuMutex|
// when reading or writing this variable to ensure thread-safe.
int32_t sInternalLockCpuCount = 0;

Expand Down Expand Up @@ -818,29 +819,29 @@ SetScreenBrightness(double brightness)
}
}

static Monitor* sInternalLockCpuMonitor = nullptr;
static StaticMutex sInternalLockCpuMutex;

static void
UpdateCpuSleepState()
{
const char *wakeLockFilename = "/sys/power/wake_lock";
const char *wakeUnlockFilename = "/sys/power/wake_unlock";

sInternalLockCpuMonitor->AssertCurrentThreadOwns();
sInternalLockCpuMutex.AssertCurrentThreadOwns();
bool allowed = sCpuSleepAllowed && !sInternalLockCpuCount;
WriteSysFile(allowed ? wakeUnlockFilename : wakeLockFilename, "gecko");
}

static void
InternalLockCpu() {
MonitorAutoLock monitor(*sInternalLockCpuMonitor);
StaticMutexAutoLock lock(sInternalLockCpuMutex);
++sInternalLockCpuCount;
UpdateCpuSleepState();
}

static void
InternalUnlockCpu() {
MonitorAutoLock monitor(*sInternalLockCpuMonitor);
StaticMutexAutoLock lock(sInternalLockCpuMutex);
--sInternalLockCpuCount;
UpdateCpuSleepState();
}
Expand All @@ -854,7 +855,7 @@ GetCpuSleepAllowed()
void
SetCpuSleepAllowed(bool aAllowed)
{
MonitorAutoLock monitor(*sInternalLockCpuMonitor);
StaticMutexAutoLock lock(sInternalLockCpuMutex);
sCpuSleepAllowed = aAllowed;
UpdateCpuSleepState();
}
Expand Down Expand Up @@ -1141,14 +1142,10 @@ EnableAlarm()
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);

// Initialize the monitor for internally locking CPU to ensure thread-safe
// before running the alarm-watcher thread.
sInternalLockCpuMonitor = new Monitor("sInternalLockCpuMonitor");
int status = pthread_create(&sAlarmFireWatcherThread, &attr, WaitForAlarm,
alarmData.get());
if (status) {
alarmData = nullptr;
delete sInternalLockCpuMonitor;
HAL_LOG("Failed to create alarm-watcher thread. Status: %d.", status);
return false;
}
Expand All @@ -1172,8 +1169,6 @@ DisableAlarm()
// data pointed at by sAlarmData.
DebugOnly<int> err = pthread_kill(sAlarmFireWatcherThread, SIGUSR1);
MOZ_ASSERT(!err);

delete sInternalLockCpuMonitor;
}

bool
Expand Down

0 comments on commit bebcf88

Please sign in to comment.