2 * Copyright 2009-2015, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Clemens Zeidler, haiku@Clemens-Zeidler.de
10 #include "DriverInterface.h"
13 #include <Messenger.h>
22 Monitor::StartWatching(BHandler
* target
)
24 if (fWatcherList
.HasItem(target
))
27 fWatcherList
.AddItem(target
);
33 Monitor::StopWatching(BHandler
* target
)
35 return fWatcherList
.RemoveItem(target
);
40 Monitor::Broadcast(uint32 message
)
42 for (int i
= 0; i
< fWatcherList
.CountItems(); i
++) {
43 BMessenger
messenger(fWatcherList
.ItemAt(i
));
44 messenger
.SendMessage(message
);
52 PowerStatusDriverInterface::PowerStatusDriverInterface()
57 fListLocker("driver list")
62 PowerStatusDriverInterface::~PowerStatusDriverInterface()
68 PowerStatusDriverInterface::StartWatching(BHandler
* target
)
70 BAutolock
autolock(fListLocker
);
72 status_t status
= Monitor::StartWatching(target
);
79 fThread
= spawn_thread(&_ThreadWatchPowerFunction
, "PowerStatusThread",
80 B_LOW_PRIORITY
, this);
82 fWaitSem
= create_sem(0, "power status wait");
84 atomic_set(&fIsWatching
, 1);
85 status
= resume_thread(fThread
);
89 if (status
!= B_OK
&& fWatcherList
.CountItems() == 0) {
90 atomic_set(&fIsWatching
, 0);
102 PowerStatusDriverInterface::StopWatching(BHandler
* target
)
109 if (fWatcherList
.CountItems() == 1) {
110 fListLocker
.Unlock();
113 fListLocker
.Unlock();
115 return Monitor::StopWatching(target
);
120 PowerStatusDriverInterface::Broadcast(uint32 message
)
122 BAutolock
autolock(fListLocker
);
123 Monitor::Broadcast(message
);
128 PowerStatusDriverInterface::Disconnect()
133 atomic_set(&fIsWatching
, 0);
134 delete_sem(fWaitSem
);
136 wait_for_thread(fThread
, NULL
);
143 PowerStatusDriverInterface::_ThreadWatchPowerFunction(void* data
)
145 PowerStatusDriverInterface
* that
= (PowerStatusDriverInterface
*)data
;
146 that
->_WatchPowerStatus();