2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
|
2019-04-10 17:01:54 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <AK/Function.h>
|
2020-02-02 12:34:39 +01:00
|
|
|
#include <AK/String.h>
|
2019-04-10 17:01:54 +02:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
#include <AK/WeakPtr.h>
|
2020-02-14 22:29:06 +01:00
|
|
|
#include <LibCore/Forward.h>
|
2019-04-10 17:01:54 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
namespace Core {
|
2019-04-10 17:01:54 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class Event {
|
2019-04-10 17:01:54 +02:00
|
|
|
public:
|
2019-06-07 17:13:23 +02:00
|
|
|
enum Type {
|
2019-04-10 17:01:54 +02:00
|
|
|
Invalid = 0,
|
|
|
|
|
Quit,
|
|
|
|
|
Timer,
|
2019-07-16 20:31:14 +02:00
|
|
|
NotifierRead,
|
|
|
|
|
NotifierWrite,
|
2019-04-10 17:01:54 +02:00
|
|
|
DeferredInvoke,
|
|
|
|
|
ChildAdded,
|
|
|
|
|
ChildRemoved,
|
2019-07-13 18:35:13 +02:00
|
|
|
Custom,
|
2019-04-10 17:01:54 +02:00
|
|
|
};
|
|
|
|
|
|
2020-08-26 22:05:52 +02:00
|
|
|
Event() { }
|
2020-02-02 12:34:39 +01:00
|
|
|
explicit Event(unsigned type)
|
2019-05-28 11:53:16 +02:00
|
|
|
: m_type(type)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-08-26 22:05:52 +02:00
|
|
|
virtual ~Event() { }
|
2019-04-10 17:01:54 +02:00
|
|
|
|
|
|
|
|
unsigned type() const { return m_type; }
|
|
|
|
|
|
2019-09-20 20:37:31 +02:00
|
|
|
bool is_accepted() const { return m_accepted; }
|
|
|
|
|
void accept() { m_accepted = true; }
|
|
|
|
|
void ignore() { m_accepted = false; }
|
|
|
|
|
|
2019-04-10 17:01:54 +02:00
|
|
|
private:
|
|
|
|
|
unsigned m_type { Type::Invalid };
|
2019-09-20 20:37:31 +02:00
|
|
|
bool m_accepted { true };
|
2019-04-10 17:01:54 +02:00
|
|
|
};
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class DeferredInvocationEvent : public Event {
|
|
|
|
|
friend class EventLoop;
|
2019-05-28 11:53:16 +02:00
|
|
|
|
2019-04-10 17:01:54 +02:00
|
|
|
public:
|
2020-02-02 12:34:39 +01:00
|
|
|
DeferredInvocationEvent(Function<void(Object&)> invokee)
|
|
|
|
|
: Event(Event::Type::DeferredInvoke)
|
2019-04-10 17:01:54 +02:00
|
|
|
, m_invokee(move(invokee))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
2020-02-02 12:34:39 +01:00
|
|
|
Function<void(Object&)> m_invokee;
|
2019-04-10 17:01:54 +02:00
|
|
|
};
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class TimerEvent final : public Event {
|
2019-04-10 17:01:54 +02:00
|
|
|
public:
|
2020-02-02 12:34:39 +01:00
|
|
|
explicit TimerEvent(int timer_id)
|
|
|
|
|
: Event(Event::Timer)
|
2019-05-28 11:53:16 +02:00
|
|
|
, m_timer_id(timer_id)
|
2019-04-10 17:01:54 +02:00
|
|
|
{
|
|
|
|
|
}
|
2020-08-26 22:05:52 +02:00
|
|
|
~TimerEvent() { }
|
2019-04-10 17:01:54 +02:00
|
|
|
|
|
|
|
|
int timer_id() const { return m_timer_id; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int m_timer_id;
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class NotifierReadEvent final : public Event {
|
2019-07-16 20:31:14 +02:00
|
|
|
public:
|
2020-02-02 12:34:39 +01:00
|
|
|
explicit NotifierReadEvent(int fd)
|
|
|
|
|
: Event(Event::NotifierRead)
|
2019-07-16 20:31:14 +02:00
|
|
|
, m_fd(fd)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-08-26 22:05:52 +02:00
|
|
|
~NotifierReadEvent() { }
|
2019-07-16 20:31:14 +02:00
|
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int m_fd;
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class NotifierWriteEvent final : public Event {
|
2019-07-16 20:31:14 +02:00
|
|
|
public:
|
2020-02-02 12:34:39 +01:00
|
|
|
explicit NotifierWriteEvent(int fd)
|
|
|
|
|
: Event(Event::NotifierWrite)
|
2019-07-16 20:31:14 +02:00
|
|
|
, m_fd(fd)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-08-26 22:05:52 +02:00
|
|
|
~NotifierWriteEvent() { }
|
2019-07-16 20:31:14 +02:00
|
|
|
|
|
|
|
|
int fd() const { return m_fd; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int m_fd;
|
|
|
|
|
};
|
|
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class ChildEvent final : public Event {
|
2019-04-10 17:01:54 +02:00
|
|
|
public:
|
2020-02-02 12:34:39 +01:00
|
|
|
ChildEvent(Type, Object& child, Object* insertion_before_child = nullptr);
|
|
|
|
|
~ChildEvent();
|
2019-04-10 17:01:54 +02:00
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
Object* child();
|
|
|
|
|
const Object* child() const;
|
2019-04-10 17:01:54 +02:00
|
|
|
|
AK: Make RefPtr, NonnullRefPtr, WeakPtr thread safe
This makes most operations thread safe, especially so that they
can safely be used in the Kernel. This includes obtaining a strong
reference from a weak reference, which now requires an explicit
call to WeakPtr::strong_ref(). Another major change is that
Weakable::make_weak_ref() may require the explicit target type.
Previously we used reinterpret_cast in WeakPtr, assuming that it
can be properly converted. But WeakPtr does not necessarily have
the knowledge to be able to do this. Instead, we now ask the class
itself to deliver a WeakPtr to the type that we want.
Also, WeakLink is no longer specific to a target type. The reason
for this is that we want to be able to safely convert e.g. WeakPtr<T>
to WeakPtr<U>, and before this we just reinterpret_cast the internal
WeakLink<T> to WeakLink<U>, which is a bold assumption that it would
actually produce the correct code. Instead, WeakLink now operates
on just a raw pointer and we only make those constructors/operators
available if we can verify that it can be safely cast.
In order to guarantee thread safety, we now use the least significant
bit in the pointer for locking purposes. This also means that only
properly aligned pointers can be used.
2020-09-29 16:26:13 -06:00
|
|
|
Object* insertion_before_child();
|
|
|
|
|
const Object* insertion_before_child() const;
|
2019-11-05 20:41:27 +01:00
|
|
|
|
2019-04-10 17:01:54 +02:00
|
|
|
private:
|
2020-02-02 12:34:39 +01:00
|
|
|
WeakPtr<Object> m_child;
|
|
|
|
|
WeakPtr<Object> m_insertion_before_child;
|
2019-04-10 17:01:54 +02:00
|
|
|
};
|
2019-07-13 18:35:13 +02:00
|
|
|
|
2020-02-02 12:34:39 +01:00
|
|
|
class CustomEvent : public Event {
|
2019-07-13 18:35:13 +02:00
|
|
|
public:
|
2020-08-26 22:05:52 +02:00
|
|
|
CustomEvent(int custom_type)
|
2020-02-02 12:34:39 +01:00
|
|
|
: Event(Event::Type::Custom)
|
2019-07-13 18:35:13 +02:00
|
|
|
, m_custom_type(custom_type)
|
|
|
|
|
{
|
|
|
|
|
}
|
2020-08-26 22:05:52 +02:00
|
|
|
~CustomEvent() { }
|
2019-07-13 18:35:13 +02:00
|
|
|
|
|
|
|
|
int custom_type() const { return m_custom_type; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
int m_custom_type { 0 };
|
|
|
|
|
};
|
2020-02-02 12:34:39 +01:00
|
|
|
|
|
|
|
|
}
|