[kernel] Re-design thread blocking
In preparation for the new mailbox IPC model, blocking threads needed an overhaul. The `wait_on_*` and `wake_on_*` methods are gone, and the `block()` and `wake()` calls on threads now pass a value between the waker and the blocked thread. As part of this change, the concept of signals on the base kobject class was removed, along with the queue of blocked threads waiting on any given object. Signals are now exclusively the domain of the event object type, and the new wait_queue utility class helps manage waiting threads when an object does actually need this functionality. In some cases (eg, logger) an event object is used instead of the lower-level wait_queue. Since this change has a lot of ramifications, this large commit includes the following additional changes: - The j6_object_wait, j6_object_wait_many, and j6_thread_pause syscalls have been removed. - The j6_event_clear syscall has been removed - events are "cleared" by reading them now. A new j6_event_wait syscall has been added to read events. - The generic close() method on kobject has been removed. - The on_no_handles() method on kobject now deletes the object by default, and needs to be overridden by classes that should not be. - The j6_system_bind_irq syscall now takes an event handle, as well as a signal that the IRQ should set on the event. IRQs will cause a waiting thread to be woken with the appropriate bit set. - Threads waking due to timeout is simplified to just having a wake_timeout() accessor that returns a timestamp. - The new wait_queue uses util::deque, which caused the disovery of two bugs in the deque implementation: empty deques could still have a single array allocated and thus return true for empty(), and new arrays getting allocated were not being zeroed first. - Exposed a new erase() method on util::map that takes a node pointer instead of a key, skipping lookup.
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
/// A generic templatized linked list.
|
||||
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <util/linked_list.h>
|
||||
|
||||
namespace util {
|
||||
@@ -42,7 +43,9 @@ public:
|
||||
|
||||
inline void push_front(const T& item) {
|
||||
if (!m_first) { // need a new block at the start
|
||||
m_list.push_front(new node_type);
|
||||
node_type *n = new node_type;
|
||||
memset(n, 0, sizeof(node_type));
|
||||
m_list.push_front(n);
|
||||
m_first = N;
|
||||
}
|
||||
m_list.front()->items[--m_first] = item;
|
||||
@@ -50,7 +53,9 @@ public:
|
||||
|
||||
inline void push_back(const T& item) {
|
||||
if (m_next == N) { // need a new block at the end
|
||||
m_list.push_back(new node_type);
|
||||
node_type *n = new node_type;
|
||||
memset(n, 0, sizeof(node_type));
|
||||
m_list.push_back(n);
|
||||
m_next = 0;
|
||||
}
|
||||
m_list.back()->items[m_next++] = item;
|
||||
@@ -80,7 +85,10 @@ public:
|
||||
return value;
|
||||
}
|
||||
|
||||
inline bool empty() const { return m_list.empty(); }
|
||||
inline bool empty() const {
|
||||
return m_list.empty() ||
|
||||
m_list.length() == 1 && m_first == m_next;
|
||||
}
|
||||
|
||||
inline T& first() {
|
||||
assert(!empty() && "Calling first() on an empty deque");
|
||||
|
||||
@@ -119,7 +119,22 @@ public:
|
||||
{
|
||||
node *n = lookup(k);
|
||||
if (!n) return false;
|
||||
erase(n);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline size_t count() const { return m_count; }
|
||||
inline size_t capacity() const { return m_capacity; }
|
||||
inline size_t threshold() const { return (m_capacity * max_load) / 100; }
|
||||
|
||||
protected:
|
||||
inline size_t mod(uint64_t i) const { return i & (m_capacity - 1); }
|
||||
inline size_t offset(uint64_t h, size_t i) const {
|
||||
return mod(i + m_capacity - mod(h));
|
||||
}
|
||||
|
||||
void erase(node *n)
|
||||
{
|
||||
n->~node();
|
||||
--m_count;
|
||||
|
||||
@@ -132,18 +147,6 @@ public:
|
||||
m.~node();
|
||||
i = mod(++i);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
inline size_t count() const { return m_count; }
|
||||
inline size_t capacity() const { return m_capacity; }
|
||||
inline size_t threshold() const { return (m_capacity * max_load) / 100; }
|
||||
|
||||
protected:
|
||||
inline size_t mod(uint64_t i) const { return i & (m_capacity - 1); }
|
||||
inline size_t offset(uint64_t h, size_t i) const {
|
||||
return mod(i + m_capacity - mod(h));
|
||||
}
|
||||
|
||||
void set_capacity(size_t capacity) {
|
||||
@@ -263,7 +266,8 @@ public:
|
||||
|
||||
V * find(const K &k) {
|
||||
node *n = this->lookup(k);
|
||||
return n ? &n->val : nullptr;
|
||||
V *val = n ? &n->val : nullptr;
|
||||
return val;
|
||||
}
|
||||
|
||||
const V * find(const K &k) const {
|
||||
@@ -285,6 +289,12 @@ public:
|
||||
map(size_t capacity = 0) :
|
||||
base(capacity) {}
|
||||
|
||||
V * find(const K &k) {
|
||||
node *n = this->lookup(k);
|
||||
V *val = n ? n->val : nullptr;
|
||||
return val;
|
||||
}
|
||||
|
||||
V * find(const K &k) const {
|
||||
const node *n = this->lookup(k);
|
||||
return n ? n->val : nullptr;
|
||||
|
||||
Reference in New Issue
Block a user