* The device watcher now waits on a semaphore, and doesn't use snooze() anymore.

* This has the advantage that we can quit PowerStatus instantly, instead of
  having to wait for two seconds in the worst case.
* Automatic whitespace cleanup.


git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@32257 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Axel Dörfler 2009-08-11 18:34:33 +00:00
parent 95ea1dab5a
commit 780e0413a1
4 changed files with 48 additions and 33 deletions

View File

@ -6,6 +6,7 @@
* Clemens Zeidler, haiku@clemens-zeidler.de
*/
#include "ACPIDriverInterface.h"
#include <stdio.h>
@ -22,7 +23,7 @@ RateBuffer::RateBuffer()
fSize(kRateBufferSize),
fCurrentSize(0)
{
}
@ -33,7 +34,7 @@ RateBuffer::AddRate(int32 rate)
fPosition ++;
if (fPosition >= fSize)
fPosition = 0;
if (fCurrentSize < fSize)
fCurrentSize ++;
}
@ -46,7 +47,7 @@ RateBuffer::GetMeanRate()
for (int i = 0; i < fCurrentSize; i++) {
mean += fRateBuffer[i];
}
if (fCurrentSize == 0)
return -1;
@ -81,10 +82,10 @@ Battery::ReadBatteryInfo()
status_t status;
status = ioctl(fDriverHandler, GET_BATTERY_INFO, &fCachedAcpiInfo,
sizeof(acpi_battery_info));
if (status != B_OK)
return status;
return B_OK;
}
@ -113,7 +114,7 @@ Battery::GetExtendedBatteryInfo(acpi_extended_battery_info* info)
status_t status;
status = ioctl(fDriverHandler, GET_EXTENDED_BATTERY_INFO, info,
sizeof(acpi_extended_battery_info));
return status;
}
@ -126,17 +127,17 @@ Battery::_Init()
sizeof(uint32));
if (fInitStatus != B_OK)
return;
fInitStatus = ioctl(fDriverHandler, GET_EXTENDED_BATTERY_INFO,
&fExtendedBatteryInfo, sizeof(acpi_extended_battery_info));
if (fInitStatus != B_OK)
return;
fInitStatus = ioctl(fDriverHandler, GET_BATTERY_INFO, &fCachedAcpiInfo,
sizeof(acpi_battery_info));
if (fInitStatus != B_OK)
return;
printf("ACPI driver found\n");
}
@ -167,7 +168,7 @@ ACPIDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
BAutolock autolock(fInterfaceLocker);
if (index < 0 || index >= fDriverList.CountItems())
return B_ERROR;
status_t status;
status = fDriverList.ItemAt(index)->GetBatteryInfoCached(info);
return status;
@ -181,7 +182,7 @@ ACPIDriverInterface::GetExtendedBatteryInfo(acpi_extended_battery_info* info,
BAutolock autolock(fInterfaceLocker);
if (index < 0 || index >= fDriverList.CountItems())
return B_ERROR;
status_t status;
status = fDriverList.ItemAt(index)->GetExtendedBatteryInfo(info);
@ -201,7 +202,7 @@ ACPIDriverInterface::_ReadBatteryInfo()
{
for (int i = 0; i < fDriverList.CountItems(); i++)
fDriverList.ItemAt(i)->ReadBatteryInfo();
return B_OK;
}
@ -215,7 +216,7 @@ ACPIDriverInterface::_WatchPowerStatus()
while (atomic_get(&fIsWatching) > 0) {
_ReadBatteryInfo();
Broadcast(kMsgUpdate);
snooze(kUpdateInterval);
acquire_sem_etc(fWaitSem, 1, B_RELATIVE_TIMEOUT, kUpdateInterval);
}
}
@ -225,13 +226,13 @@ ACPIDriverInterface::_FindDrivers(const char* path)
{
BDirectory dir(path);
BEntry entry;
status_t status = B_ERROR;
while (dir.GetNextEntry(&entry) == B_OK) {
BPath path;
entry.GetPath(&path);
if (entry.IsDirectory()) {
if (_FindDrivers(path.Path()) == B_OK)
return B_OK;
@ -249,9 +250,9 @@ ACPIDriverInterface::_FindDrivers(const char* path)
delete battery;
}
}
}
return status;
return status;
}

View File

@ -6,6 +6,7 @@
* Clemens Zeidler, haiku@clemens-zeidler.de
*/
#include "APMDriverInterface.h"
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
@ -61,7 +62,7 @@ APMDriverInterface::Connect()
if (fDevice < 0) {
return B_ERROR;
}
return B_OK;
#endif
}
@ -72,7 +73,7 @@ APMDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
{
if (index != 0)
return B_BAD_VALUE;
info->current_rate = -1;
#ifdef HAIKU_TARGET_PLATFORM_HAIKU
@ -86,7 +87,7 @@ APMDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
info->full_capacity = 100;
info->time_left = apmInfo.time_left;
}
return status;
#else
if (fDevice < 0)
@ -108,7 +109,7 @@ APMDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
else if (info->time_left & 0x8000)
info->time_left = (info->time_left & 0x7fff) * 60;
}
return B_OK;
#endif
}
@ -134,7 +135,7 @@ APMDriverInterface::_WatchPowerStatus()
{
while (atomic_get(&fIsWatching) > 0) {
Broadcast(kMsgUpdate);
snooze(kUpdateInterval);
acquire_sem_etc(fWaitSem, 1, B_RELATIVE_TIMEOUT, kUpdateInterval);
}
}

View File

@ -46,18 +46,20 @@ Monitor::Broadcast(uint32 message)
}
// #pragma mark -
PowerStatusDriverInterface::PowerStatusDriverInterface()
:
fIsWatching(0),
fWaitSem(-1),
fThread(-1)
{
}
PowerStatusDriverInterface::~PowerStatusDriverInterface()
{
}
@ -65,8 +67,8 @@ status_t
PowerStatusDriverInterface::StartWatching(BHandler* target)
{
BAutolock autolock(fListLocker);
status_t status = Monitor::StartWatching(target);
status_t status = Monitor::StartWatching(target);
if (status != B_OK)
return status;
@ -76,13 +78,20 @@ PowerStatusDriverInterface::StartWatching(BHandler* target)
fThread = spawn_thread(&_ThreadWatchPowerFunction, "PowerStatusThread",
B_LOW_PRIORITY, this);
if (fThread >= 0) {
fWaitSem = create_sem(0, "power status wait");
atomic_set(&fIsWatching, 1);
status = resume_thread(fThread);
} else
return fThread;
if (status != B_OK && fWatcherList.CountItems() == 0)
if (status != B_OK && fWatcherList.CountItems() == 0) {
atomic_set(&fIsWatching, 0);
delete_sem(fWaitSem);
fThread = -1;
fWaitSem = -1;
}
return status;
}
@ -95,11 +104,8 @@ PowerStatusDriverInterface::StopWatching(BHandler* target)
if (fThread < 0)
return B_BAD_VALUE;
if (fWatcherList.CountItems() == 1) {
atomic_set(&fIsWatching, 0);
wait_for_thread(fThread, NULL);
fThread = -1;
}
if (fWatcherList.CountItems() == 1)
Disconnect();
return Monitor::StopWatching(target);
}
@ -116,9 +122,15 @@ PowerStatusDriverInterface::Broadcast(uint32 message)
void
PowerStatusDriverInterface::Disconnect()
{
if (fThread < 0)
return;
atomic_set(&fIsWatching, 0);
delete_sem(fWaitSem);
wait_for_thread(fThread, NULL);
fThread = -1;
fWaitSem = -1;
}

View File

@ -68,6 +68,7 @@ protected:
virtual void _WatchPowerStatus() = 0;
vint32 fIsWatching;
sem_id fWaitSem;
private:
static int32 _ThreadWatchPowerFunction(void* data);