1/*
2 * Copyright 2009-2010, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include "IOSchedulerRoster.h"
8
9#include <util/AutoLock.h>
10
11
12/*static*/ IOSchedulerRoster IOSchedulerRoster::sDefaultInstance;
13
14
15/*static*/ void
16IOSchedulerRoster::Init()
17{
18	new(&sDefaultInstance) IOSchedulerRoster;
19}
20
21
22void
23IOSchedulerRoster::AddScheduler(IOScheduler* scheduler)
24{
25	AutoLocker<IOSchedulerRoster> locker(this);
26	fSchedulers.Add(scheduler);
27	locker.Unlock();
28
29	Notify(IO_SCHEDULER_ADDED, scheduler);
30}
31
32
33void
34IOSchedulerRoster::RemoveScheduler(IOScheduler* scheduler)
35{
36	AutoLocker<IOSchedulerRoster> locker(this);
37	fSchedulers.Remove(scheduler);
38	locker.Unlock();
39
40	Notify(IO_SCHEDULER_REMOVED, scheduler);
41}
42
43
44void
45IOSchedulerRoster::Notify(uint32 eventCode, const IOScheduler* scheduler,
46	IORequest* request, IOOperation* operation)
47{
48	AutoLocker<DefaultNotificationService> locker(fNotificationService);
49
50	if (!fNotificationService.HasListeners())
51		return;
52
53	KMessage event;
54	event.SetTo(fEventBuffer, sizeof(fEventBuffer), IO_SCHEDULER_MONITOR);
55	event.AddInt32("event", eventCode);
56	event.AddPointer("scheduler", scheduler);
57	if (request != NULL) {
58		event.AddPointer("request", request);
59		if (operation != NULL)
60			event.AddPointer("operation", operation);
61	}
62
63	fNotificationService.NotifyLocked(event, eventCode);
64}
65
66
67int32
68IOSchedulerRoster::NextID()
69{
70	AutoLocker<IOSchedulerRoster> locker(this);
71	return fNextID++;
72}
73
74
75IOSchedulerRoster::IOSchedulerRoster()
76	:
77	fNextID(1),
78	fNotificationService("I/O")
79{
80	mutex_init(&fLock, "IOSchedulerRoster");
81	fNotificationService.Register();
82}
83
84
85IOSchedulerRoster::~IOSchedulerRoster()
86{
87	mutex_destroy(&fLock);
88	fNotificationService.Unregister();
89}
90