* 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
+20 -19
View File
@@ -6,6 +6,7 @@
* Clemens Zeidler, [email protected] * Clemens Zeidler, [email protected]
*/ */
#include "ACPIDriverInterface.h" #include "ACPIDriverInterface.h"
#include <stdio.h> #include <stdio.h>
@@ -22,7 +23,7 @@ RateBuffer::RateBuffer()
fSize(kRateBufferSize), fSize(kRateBufferSize),
fCurrentSize(0) fCurrentSize(0)
{ {
} }
@@ -33,7 +34,7 @@ RateBuffer::AddRate(int32 rate)
fPosition ++; fPosition ++;
if (fPosition >= fSize) if (fPosition >= fSize)
fPosition = 0; fPosition = 0;
if (fCurrentSize < fSize) if (fCurrentSize < fSize)
fCurrentSize ++; fCurrentSize ++;
} }
@@ -46,7 +47,7 @@ RateBuffer::GetMeanRate()
for (int i = 0; i < fCurrentSize; i++) { for (int i = 0; i < fCurrentSize; i++) {
mean += fRateBuffer[i]; mean += fRateBuffer[i];
} }
if (fCurrentSize == 0) if (fCurrentSize == 0)
return -1; return -1;
@@ -81,10 +82,10 @@ Battery::ReadBatteryInfo()
status_t status; status_t status;
status = ioctl(fDriverHandler, GET_BATTERY_INFO, &fCachedAcpiInfo, status = ioctl(fDriverHandler, GET_BATTERY_INFO, &fCachedAcpiInfo,
sizeof(acpi_battery_info)); sizeof(acpi_battery_info));
if (status != B_OK) if (status != B_OK)
return status; return status;
return B_OK; return B_OK;
} }
@@ -113,7 +114,7 @@ Battery::GetExtendedBatteryInfo(acpi_extended_battery_info* info)
status_t status; status_t status;
status = ioctl(fDriverHandler, GET_EXTENDED_BATTERY_INFO, info, status = ioctl(fDriverHandler, GET_EXTENDED_BATTERY_INFO, info,
sizeof(acpi_extended_battery_info)); sizeof(acpi_extended_battery_info));
return status; return status;
} }
@@ -126,17 +127,17 @@ Battery::_Init()
sizeof(uint32)); sizeof(uint32));
if (fInitStatus != B_OK) if (fInitStatus != B_OK)
return; return;
fInitStatus = ioctl(fDriverHandler, GET_EXTENDED_BATTERY_INFO, fInitStatus = ioctl(fDriverHandler, GET_EXTENDED_BATTERY_INFO,
&fExtendedBatteryInfo, sizeof(acpi_extended_battery_info)); &fExtendedBatteryInfo, sizeof(acpi_extended_battery_info));
if (fInitStatus != B_OK) if (fInitStatus != B_OK)
return; return;
fInitStatus = ioctl(fDriverHandler, GET_BATTERY_INFO, &fCachedAcpiInfo, fInitStatus = ioctl(fDriverHandler, GET_BATTERY_INFO, &fCachedAcpiInfo,
sizeof(acpi_battery_info)); sizeof(acpi_battery_info));
if (fInitStatus != B_OK) if (fInitStatus != B_OK)
return; return;
printf("ACPI driver found\n"); printf("ACPI driver found\n");
} }
@@ -167,7 +168,7 @@ ACPIDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
BAutolock autolock(fInterfaceLocker); BAutolock autolock(fInterfaceLocker);
if (index < 0 || index >= fDriverList.CountItems()) if (index < 0 || index >= fDriverList.CountItems())
return B_ERROR; return B_ERROR;
status_t status; status_t status;
status = fDriverList.ItemAt(index)->GetBatteryInfoCached(info); status = fDriverList.ItemAt(index)->GetBatteryInfoCached(info);
return status; return status;
@@ -181,7 +182,7 @@ ACPIDriverInterface::GetExtendedBatteryInfo(acpi_extended_battery_info* info,
BAutolock autolock(fInterfaceLocker); BAutolock autolock(fInterfaceLocker);
if (index < 0 || index >= fDriverList.CountItems()) if (index < 0 || index >= fDriverList.CountItems())
return B_ERROR; return B_ERROR;
status_t status; status_t status;
status = fDriverList.ItemAt(index)->GetExtendedBatteryInfo(info); status = fDriverList.ItemAt(index)->GetExtendedBatteryInfo(info);
@@ -201,7 +202,7 @@ ACPIDriverInterface::_ReadBatteryInfo()
{ {
for (int i = 0; i < fDriverList.CountItems(); i++) for (int i = 0; i < fDriverList.CountItems(); i++)
fDriverList.ItemAt(i)->ReadBatteryInfo(); fDriverList.ItemAt(i)->ReadBatteryInfo();
return B_OK; return B_OK;
} }
@@ -215,7 +216,7 @@ ACPIDriverInterface::_WatchPowerStatus()
while (atomic_get(&fIsWatching) > 0) { while (atomic_get(&fIsWatching) > 0) {
_ReadBatteryInfo(); _ReadBatteryInfo();
Broadcast(kMsgUpdate); 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); BDirectory dir(path);
BEntry entry; BEntry entry;
status_t status = B_ERROR; status_t status = B_ERROR;
while (dir.GetNextEntry(&entry) == B_OK) { while (dir.GetNextEntry(&entry) == B_OK) {
BPath path; BPath path;
entry.GetPath(&path); entry.GetPath(&path);
if (entry.IsDirectory()) { if (entry.IsDirectory()) {
if (_FindDrivers(path.Path()) == B_OK) if (_FindDrivers(path.Path()) == B_OK)
return B_OK; return B_OK;
@@ -249,9 +250,9 @@ ACPIDriverInterface::_FindDrivers(const char* path)
delete battery; delete battery;
} }
} }
} }
return status; return status;
} }
+6 -5
View File
@@ -6,6 +6,7 @@
* Clemens Zeidler, [email protected] * Clemens Zeidler, [email protected]
*/ */
#include "APMDriverInterface.h" #include "APMDriverInterface.h"
#ifdef HAIKU_TARGET_PLATFORM_HAIKU #ifdef HAIKU_TARGET_PLATFORM_HAIKU
@@ -61,7 +62,7 @@ APMDriverInterface::Connect()
if (fDevice < 0) { if (fDevice < 0) {
return B_ERROR; return B_ERROR;
} }
return B_OK; return B_OK;
#endif #endif
} }
@@ -72,7 +73,7 @@ APMDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
{ {
if (index != 0) if (index != 0)
return B_BAD_VALUE; return B_BAD_VALUE;
info->current_rate = -1; info->current_rate = -1;
#ifdef HAIKU_TARGET_PLATFORM_HAIKU #ifdef HAIKU_TARGET_PLATFORM_HAIKU
@@ -86,7 +87,7 @@ APMDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
info->full_capacity = 100; info->full_capacity = 100;
info->time_left = apmInfo.time_left; info->time_left = apmInfo.time_left;
} }
return status; return status;
#else #else
if (fDevice < 0) if (fDevice < 0)
@@ -108,7 +109,7 @@ APMDriverInterface::GetBatteryInfo(battery_info* info, int32 index)
else if (info->time_left & 0x8000) else if (info->time_left & 0x8000)
info->time_left = (info->time_left & 0x7fff) * 60; info->time_left = (info->time_left & 0x7fff) * 60;
} }
return B_OK; return B_OK;
#endif #endif
} }
@@ -134,7 +135,7 @@ APMDriverInterface::_WatchPowerStatus()
{ {
while (atomic_get(&fIsWatching) > 0) { while (atomic_get(&fIsWatching) > 0) {
Broadcast(kMsgUpdate); Broadcast(kMsgUpdate);
snooze(kUpdateInterval); acquire_sem_etc(fWaitSem, 1, B_RELATIVE_TIMEOUT, kUpdateInterval);
} }
} }
+21 -9
View File
@@ -46,18 +46,20 @@ Monitor::Broadcast(uint32 message)
} }
// #pragma mark -
PowerStatusDriverInterface::PowerStatusDriverInterface() PowerStatusDriverInterface::PowerStatusDriverInterface()
: :
fIsWatching(0), fIsWatching(0),
fWaitSem(-1),
fThread(-1) fThread(-1)
{ {
} }
PowerStatusDriverInterface::~PowerStatusDriverInterface() PowerStatusDriverInterface::~PowerStatusDriverInterface()
{ {
} }
@@ -65,8 +67,8 @@ status_t
PowerStatusDriverInterface::StartWatching(BHandler* target) PowerStatusDriverInterface::StartWatching(BHandler* target)
{ {
BAutolock autolock(fListLocker); BAutolock autolock(fListLocker);
status_t status = Monitor::StartWatching(target);
status_t status = Monitor::StartWatching(target);
if (status != B_OK) if (status != B_OK)
return status; return status;
@@ -76,13 +78,20 @@ PowerStatusDriverInterface::StartWatching(BHandler* target)
fThread = spawn_thread(&_ThreadWatchPowerFunction, "PowerStatusThread", fThread = spawn_thread(&_ThreadWatchPowerFunction, "PowerStatusThread",
B_LOW_PRIORITY, this); B_LOW_PRIORITY, this);
if (fThread >= 0) { if (fThread >= 0) {
fWaitSem = create_sem(0, "power status wait");
atomic_set(&fIsWatching, 1); atomic_set(&fIsWatching, 1);
status = resume_thread(fThread); status = resume_thread(fThread);
} else } else
return fThread; return fThread;
if (status != B_OK && fWatcherList.CountItems() == 0) if (status != B_OK && fWatcherList.CountItems() == 0) {
atomic_set(&fIsWatching, 0); atomic_set(&fIsWatching, 0);
delete_sem(fWaitSem);
fThread = -1;
fWaitSem = -1;
}
return status; return status;
} }
@@ -95,11 +104,8 @@ PowerStatusDriverInterface::StopWatching(BHandler* target)
if (fThread < 0) if (fThread < 0)
return B_BAD_VALUE; return B_BAD_VALUE;
if (fWatcherList.CountItems() == 1) { if (fWatcherList.CountItems() == 1)
atomic_set(&fIsWatching, 0); Disconnect();
wait_for_thread(fThread, NULL);
fThread = -1;
}
return Monitor::StopWatching(target); return Monitor::StopWatching(target);
} }
@@ -116,9 +122,15 @@ PowerStatusDriverInterface::Broadcast(uint32 message)
void void
PowerStatusDriverInterface::Disconnect() PowerStatusDriverInterface::Disconnect()
{ {
if (fThread < 0)
return;
atomic_set(&fIsWatching, 0); atomic_set(&fIsWatching, 0);
delete_sem(fWaitSem);
wait_for_thread(fThread, NULL); wait_for_thread(fThread, NULL);
fThread = -1; fThread = -1;
fWaitSem = -1;
} }
+1
View File
@@ -68,6 +68,7 @@ protected:
virtual void _WatchPowerStatus() = 0; virtual void _WatchPowerStatus() = 0;
vint32 fIsWatching; vint32 fIsWatching;
sem_id fWaitSem;
private: private:
static int32 _ThreadWatchPowerFunction(void* data); static int32 _ThreadWatchPowerFunction(void* data);