Compare commits

..

14 Commits

Author SHA1 Message Date
63745565db
apps: Add socket client
Some checks failed
continuous-integration/drone/pr Build is failing
2023-07-30 11:33:06 +02:00
7b24a4d1c6
libc: Add wrappers for listen(), connect(), and accept() 2023-07-30 11:33:06 +02:00
8d3b3aaf05
libluna: Add a few more network-related errno codes 2023-07-30 11:33:06 +02:00
bb3127c212
kernel: Implement listen(), connect() and accept() 2023-07-30 11:33:06 +02:00
cca806f088
libluna: Add a variant of CircularQueue that dynamically allocates its buffer at runtime
This is needed to implement the backlog queue for listening sockets.
2023-07-30 11:33:06 +02:00
7a7ae086f5
kernel: Use a did_close callback for UnixSockets 2023-07-30 11:33:06 +02:00
fb08594a18
kernel: Separate FileDescriptors and OpenFileDescription
Also, add a did_close() callback for OpenFileDescriptions losing all their references.
2023-07-30 11:33:06 +02:00
9303c44269
apps: Add socket-test 2023-07-30 11:33:06 +02:00
a12b018b03
kernel+libc: Add basic Unix sockets (creation and binding) 2023-07-30 11:33:06 +02:00
c4e30c3029
kernel: Add functionality to allow external inodes (such as sockets) to acquire an inode number from a file system
This is only implemented in tmpfs.
2023-07-30 11:33:06 +02:00
c1d08b904e
kernel+libluna: Add Buffer::dequeue_data() 2023-07-30 11:33:05 +02:00
200bb6c240
kernel+libc+libos: Add inode type for sockets 2023-07-30 11:33:05 +02:00
6b0bc66fd2
libluna: Add new socket-related errno codes 2023-07-30 11:33:05 +02:00
4ed7ec5e93
libluna: Store SharedPtr's ref count in the object itself
All checks were successful
continuous-integration/drone/push Build is passing
2023-07-30 11:32:46 +02:00
9 changed files with 107 additions and 63 deletions

View File

@ -131,7 +131,7 @@ namespace ATA
static constexpr u16 END_OF_PRDT = (1 << 15);
class Drive
class Drive : public Shareable
{
public:
Drive(Channel* channel, u8 drive_index, Badge<Channel>);
@ -274,7 +274,7 @@ namespace ATA
SharedPtr<Drive> m_drives[2];
};
class Controller
class Controller : public Shareable
{
public:
static Result<void> scan();

View File

@ -6,7 +6,7 @@ class PipeInodeBase;
class PipeReader;
class PipeWriter;
class Pipe
class Pipe : public Shareable
{
public:
static Result<void> create(SharedPtr<VFS::Inode>& rpipe, SharedPtr<VFS::Inode>& wpipe);

View File

@ -22,7 +22,7 @@ namespace VFS
class Inode;
class FileSystem
class FileSystem : public Shareable
{
public:
virtual SharedPtr<Inode> root_inode() const = 0;
@ -79,7 +79,7 @@ namespace VFS
StaticString<128> name;
};
class Inode
class Inode : public Shareable
{
public:
virtual Result<u64> ioctl(int, void*)

View File

@ -1,8 +1,9 @@
#pragma once
#include "Log.h"
#include <luna/Result.h>
#include <luna/SharedPtr.h>
class Device
class Device : public Shareable
{
public:
virtual Result<usize> read(u8* buf, usize offset, usize length) const = 0;

View File

@ -5,10 +5,12 @@
#include <luna/OwnedPtr.h>
#include <luna/Result.h>
#include <luna/ScopeGuard.h>
#include <luna/TypeTraits.h>
namespace __detail
{
struct RefCount
template <typename T> class SharedPtr;
template <typename T> SharedPtr<T> adopt_shared(T*);
struct Shareable
{
void ref()
{
@ -21,73 +23,58 @@ namespace __detail
return m_ref_count == 0;
}
private:
Atomic<int> m_ref_count { 1 };
};
}
template <typename T> class SharedPtr
{
using RefCount = __detail::RefCount;
public:
SharedPtr()
{
m_ptr = nullptr;
m_ref_count = nullptr;
}
SharedPtr(T* ptr, RefCount* ref_count) : m_ptr(ptr), m_ref_count(ref_count)
SharedPtr(T* ptr) : m_ptr(ptr)
{
}
SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr), m_ref_count(other.m_ref_count)
SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr)
{
if (m_ref_count) m_ref_count->ref();
if (m_ptr) shareable()->ref();
}
SharedPtr(SharedPtr<T>&& other) : m_ptr(other.m_ptr), m_ref_count(other.m_ref_count)
SharedPtr(SharedPtr<T>&& other) : m_ptr(other.m_ptr)
{
other.m_ptr = nullptr;
other.m_ref_count = nullptr;
}
template <typename Tp> operator SharedPtr<Tp>()
{
if (m_ref_count) m_ref_count->ref();
return { (Tp*)m_ptr, m_ref_count };
if (m_ptr) shareable()->ref();
return { (Tp*)m_ptr };
}
~SharedPtr()
{
if (m_ref_count && m_ref_count->unref())
{
delete m_ref_count;
delete m_ptr;
}
if (m_ptr && shareable()->unref()) { delete m_ptr; }
}
SharedPtr<T>& operator=(const SharedPtr<T>& other)
{
if (&other == this) return *this;
if (m_ref_count && m_ref_count->unref())
{
delete m_ref_count;
delete m_ptr;
}
if (m_ptr && shareable()->unref()) { delete m_ptr; }
m_ptr = other.m_ptr;
m_ref_count = other.m_ref_count;
if (m_ref_count) m_ref_count->ref();
if (m_ptr) shareable()->ref();
return *this;
}
bool operator==(const SharedPtr<T>& other)
{
return m_ptr == other.m_ptr && m_ref_count == other.m_ref_count;
return m_ptr == other.m_ptr;
}
T* ptr() const
@ -112,21 +99,17 @@ template <typename T> class SharedPtr
private:
T* m_ptr;
RefCount* m_ref_count;
Shareable* shareable()
{
static_assert(IsBaseOf<Shareable, T>);
return (Shareable*)m_ptr;
}
};
// NOTE: ptr is deleted if any of the adopt_shared* functions fail to construct a SharedPtr.
template <typename T> Result<SharedPtr<T>> adopt_shared(T* ptr)
template <typename T> SharedPtr<T> adopt_shared(T* ptr)
{
using RefCount = __detail::RefCount;
auto guard = make_scope_guard([ptr] { delete ptr; });
RefCount* const ref_count = TRY(make<RefCount>());
guard.deactivate();
return SharedPtr<T> { ptr, ref_count };
return SharedPtr<T> { ptr };
}
template <typename T, class... Args> Result<SharedPtr<T>> make_shared(Args... args)
@ -142,12 +125,10 @@ template <typename T> Result<SharedPtr<T>> adopt_shared_if_nonnull(T* ptr)
return err(ENOMEM);
}
template <typename T> Result<SharedPtr<T>> adopt_shared_from_owned(OwnedPtr<T>&& other)
template <typename T> SharedPtr<T> adopt_shared_from_owned(OwnedPtr<T>&& other)
{
T* ptr = other.m_ptr;
other.m_ptr = nullptr;
const SharedPtr<T> shared_ptr = TRY(adopt_shared(ptr));
return shared_ptr;
return SharedPtr<T> { ptr };
}

View File

@ -19,7 +19,7 @@ namespace os
/**
* @brief An object-oriented directory handle, which is closed when all references to it go out of scope.
*/
class Directory
class Directory : public Shareable
{
public:
/**

View File

@ -22,7 +22,7 @@ namespace os
/**
* @brief An object-oriented file handle, which is closed when all references to it go out of scope.
*/
class File
class File : public Shareable
{
public:
/**

View File

@ -5,7 +5,7 @@ target_include_directories(test PUBLIC ${LUNA_BASE}/usr/include)
function(luna_test SOURCE_FILE APP_NAME)
add_executable(${APP_NAME} ${SOURCE_FILE})
target_compile_options(${APP_NAME} PRIVATE -Os ${COMMON_FLAGS} -Wno-write-strings)
target_compile_options(${APP_NAME} PRIVATE -O0 ${COMMON_FLAGS} -fno-threadsafe-statics -Wno-write-strings)
add_dependencies(${APP_NAME} libc)
target_include_directories(${APP_NAME} PRIVATE ${LUNA_BASE}/usr/include)
target_link_libraries(${APP_NAME} PRIVATE test os)
@ -19,6 +19,7 @@ luna_test(libluna/TestUtf8.cpp TestUtf8)
luna_test(libluna/TestFormat.cpp TestFormat)
luna_test(libluna/TestHashTable.cpp TestHashTable)
luna_test(libluna/TestCPath.cpp TestCPath)
luna_test(libluna/TestSharedPtr.cpp TestSharedPtr)
luna_test(libc/TestScanf.cpp TestScanf)
luna_test(libc/TestString.cpp TestString)
luna_test(libc/TestEnv.cpp TestEnv)

View File

@ -0,0 +1,61 @@
#include <luna/SharedPtr.h>
#include <test.h>
class SampleClass : public Shareable
{
public:
SampleClass(int* ptr, int value) : m_ptr(ptr), m_value(value)
{
}
~SampleClass()
{
if (m_ptr) *m_ptr = m_value;
}
private:
int* m_ptr;
int m_value;
};
TestResult test_shared_ptr_destroyed_on_scope_exit()
{
static int value = 0;
{
SharedPtr<SampleClass> sample = TRY(make_shared<SampleClass>(&value, 1));
}
validate(value == 1);
test_success;
}
TestResult test_shared_ptr_preserved_if_at_least_one_copy_exists()
{
static int value = 0;
static SharedPtr<SampleClass> ptr = {};
{
SharedPtr<SampleClass> sample = TRY(make_shared<SampleClass>(&value, 1));
ptr = sample;
}
validate(value == 0);
ptr = {};
validate(value == 1);
test_success;
}
Result<void> test_main()
{
test_prelude;
run_test(test_shared_ptr_destroyed_on_scope_exit);
run_test(test_shared_ptr_preserved_if_at_least_one_copy_exists);
return {};
}