Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
evrhel committed Jun 11, 2024
2 parents 98dd50c + fa71652 commit 72a1335
Show file tree
Hide file tree
Showing 24 changed files with 1,754 additions and 514 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ set(LYSYS_SOURCES
${src}/ls_core.c
${src}/ls_event.c
${src}/ls_file.c
${src}/ls_file_priv.c
${src}/ls_font.c
${src}/ls_ioutils.c
${src}/ls_handle.c
Expand Down Expand Up @@ -55,6 +56,8 @@ if (WIN32)
elseif(APPLE)
find_library(COCOA_LIBRARY Cocoa)
target_link_libraries(liblysys PUBLIC ${COCOA_LIBRARY})
add_definitions(-D_XOPEN_SOURCE=500) # for ucontext.h
add_definitions(-D_DARWIN_C_SOURCE) # _XOPEN_SOURCE excludes some functions/types
elseif(UNIX)
target_link_libraries(liblysys PRIVATE xcb)
endif()
Expand Down
2 changes: 2 additions & 0 deletions include/lysys/ls_defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@
#define LS_LIKELY(x) (x)
#define LS_UNLIKELY(x) (x)
#define LS_UNREACHABLE __assume(0)
#define LS_NORETURN __declspec(noreturn)
#else
#define LS_THREADLOCAL __thread
#define LS_RESTRICT restrict
#define LS_LIKELY(x) __builtin_expect(!!(x), 1)
#define LS_UNLIKELY(x) __builtin_expect(!!(x), 0)
#define LS_UNREACHABLE __builtin_unreachable()
#define LS_NORETURN __attribute__((noreturn))
#endif // LS_WINDOWS

typedef void *ls_handle;
Expand Down
92 changes: 41 additions & 51 deletions include/lysys/ls_file.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@
// Psuedo file handles
//

// Path to the null device (e.g. /dev/null on Unix, NUL on Windows)
#define LS_DEVNULL ((ls_handle)-9)

// Standard input
#define LS_STDIN ((ls_handle)1)
#define LS_STDIN ((ls_handle)-10)

// Standard output
#define LS_STDOUT ((ls_handle)2)
#define LS_STDOUT ((ls_handle)-11)

// Standard error
#define LS_STDERR ((ls_handle)3)
#define LS_STDERR ((ls_handle)-12)

//
/////////////////////////////////////////////////////////////////////
Expand All @@ -36,16 +39,25 @@

// Open the file for asynchronous I/O
// On Windows, this will cause synchronous I/O operations to fail
#define LS_FLAG_ASYNC 0x10000
#define LS_FLAG_ASYNC 0x1000

// Optimize the file for random access
#define LS_FLAG_RANDOM 0x20000
#define LS_FLAG_RANDOM 0x2000

// Optimize the file for sequential access
#define LS_FLAG_SEQUENTIAL 0x40000
#define LS_FLAG_SEQUENTIAL 0x4000

// Child processes inherit the file handle
#define LS_FLAG_INHERIT 0x80000
#define LS_FLAG_INHERIT 0x8000

// Create the read end of an anonymous pipe for asynchronous I/O
#define LS_ANON_PIPE_READ_ASYNC 0x10000

// Create the write end of an anonymous pipe for asynchronous I/O
#define LS_ANON_PIPE_WRITE_ASYNC 0x20000

// Create both ends of an anonymous pipe for asynchronous I/O
#define LS_ANON_PIPE_ASYNC (LS_ANON_PIPE_READ_ASYNC | LS_ANON_PIPE_WRITE_ASYNC)

//
/////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -211,31 +223,6 @@ size_t ls_write(ls_handle fh, const void *buffer, size_t size);
//! occurred.
int ls_flush(ls_handle fh);

//! \brief Open an asynchronous I/O handle
//!
//! Opens an asynchronous I/O handle for the specified file or I/O
//! device. The handle can be used to perform asynchronous read and
//! write operations on the file or device.
//!
//! The file or I/O device must be opened with the LS_FLAG_ASYNC flag
//! to use asynchronous I/O operations and must remain open for the
//! duration of the asynchronous I/O operations. Close the
//! handle with ls_close when the asynchronous I/O operations are
//! complete.
//!
//! The asynchronous I/O handle is not valid for synchronous I/O
//! operations (e.g. ls_read, ls_write).
//!
//! Use ls_aio_read and ls_aio_write to queue asynchronous read and
//! write operations on the file or device. Only one asynchronous
//! I/O operation can be pending on a file or device at a time.
//!
//! \param fh The handle to the file or I/O device
//!
//! \return A handle to the asynchronous I/O request, or NULL if an
//! error occurred.
ls_handle ls_aio_open(ls_handle fh);

//! \brief Queue an asynchronous read operation
//!
//! Queues an asynchronous read operation on the specified file or
Expand Down Expand Up @@ -369,40 +356,43 @@ int ls_createdirs(const char *path);

//! \brief Create an anonymous pipe
//!
//! If a pipe end is opened for asynchronous I/O, it cannot be used
//! for process I/O redirection.
//!
//! \param read A pointer to a handle that will receive the read end
//! of the pipe
//! \param write A pointer to a handle that will receive the write
//! end of the pipe
//! \param flags Flags for the pipe creation
//! \param flags Flags for the pipe creation. Use a combination of
//! LS_ANON_PIPE_WRITE_ASYNC and LS_ANON_PIPE_READ_ASYNC to create
//! an asynchronous pipe.
//!
//! \return 0 if the pipe was successfully created, -1 if an error
//! occurred.
int ls_pipe(ls_handle *read, ls_handle *write, int flags);

//! \brief Create a named pipe.
//!
//! Creates a named pipe with the specified name. The pipe is created
//! in a system-specific location and can be opened by other processes
//! using the same name. The pipe may be used as if it were a file. All
//! pipes are read/write and blocking unless LS_FLAG_ASYNC is specified.
//!
//! \param name Name of the pipe.
//! \param flags Pipe creation flags.
//!
//! \return Handle to the pipe or NULL on failure.
ls_handle ls_mkfifo(const char *name, int flags);
//! \param wait Whether to wait for a client to connect to the pipe.
//! If the pipe is asynchronous, this parameter is ignored.
//!
//! \return Handle to the named pipe, or NULL if an error occurred.
ls_handle ls_named_pipe(const char *name, int flags, int wait);

//! \brief Open a named pipe.
//! \brief Wait for a named pipe to connect.
//!
//! The semantics of this function are the same as ls_open, but the name
//! parameter is used to specify the name of the pipe rather than a file
//! path.
//! \param fh Handle to the named pipe.
//! \param timeout Timeout in milliseconds. Use 0 to check if the
//! pipe is connected without waiting. If the pipe is not asynchronous,
//! and this parameter is nonzero, the function will block indefinitely
//! until the pipe is connected or an error occurs.
//!
//! \param name Name of the pipe.
//! \param flags Pipe access flags. Implicitly LS_FILE_READ | LS_FILE_WRITE. Optionally
//! may include LS_FLAG_ASYNC for non-blocking I/O.
//!
//! \return Handle to the pipe or NULL on failure.
ls_handle ls_pipe_open(const char *name, int access);
//! \return 0 if the pipe is connected, -1 if an error occurred,
//! or 1 if the timeout expired.
int ls_named_pipe_wait(ls_handle fh, unsigned long timeout);

ls_handle ls_pipe_open(const char *name, int access, unsigned long timeout);

#endif // _LS_FILE_H_
72 changes: 72 additions & 0 deletions include/lysys/ls_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,76 @@ int ls_tls_set(ls_handle tlsh, void *value);
//! occurred. Note that NULL is also a valid value.
void *ls_tls_get(ls_handle tlsh);

//! \brief Convert the calling thread to a fiber.
//!
//! Converts the calling thread to a fiber. The thread can now be
//! used to create fibers and switch between them.
//!
//! \param up User data to associate with the fiber. This data can be
//! retrieved with ls_fiber_get_data().
//!
//! \return 0 if successful, or -1 if an error occurred.
int ls_convert_to_fiber(void *up);

//! \brief Convert the calling fiber to a thread.
//!
//! Converts the calling fiber to a thread. The thread cannot do
//! any operations that are specific to fibers, including closing
//! the handle. Close handles before converting to a thread.
//!
//! \return 0 if successful, or -1 if an error occurred.
int ls_convert_to_thread(void);

//! \brief Create a new fiber.
//!
//! Fibers may only be created by a fiber. Use ls_convert_to_fiber() to
//! convert the calling thread to a fiber.
//!
//! If a fiber was not created with ls_convert_to_fiber() or
//! ls_fiber_create(), use of the fiber functions within that fiber
//! will result in undefined behavior.
//!
//! \param func The function to run in the new fiber.
//! \param up User data to pass to the fiber function.
//!
//! \return A handle to the new fiber, or NULL if an error occurred.
ls_handle ls_fiber_create(ls_thread_func_t func, void *up);

//! \brief Switch to the specified fiber.
//!
//! Do not switch to a already running fiber. If the current thread
//! is not a fiber, the function returns immediately.
//!
//! \param fiber The fiber to switch to.
void ls_fiber_switch(ls_handle fiber);

//! \brief Switch to the main fiber on the current thread.
void ls_fiber_sched(void);

//! \brief Retrieve the handle of the calling fiber.
//!
//! The returned handle is a psuedo-handle to the calling fiber and
//! will always refer to the calling fiber, regardless of the thread
//! or fiber that receives the handle.
//!
//! \return A handle to the calling fiber.
ls_handle ls_fiber_self(void);

//! \brief Get the user data associated with a fiber.
//!
//! \param fiber The fiber handle.
//!
//! \return The user data associated with the fiber. If the current
//! thread is not a fiber, the function returns NULL and sets ls_errno.
void *ls_fiber_get_data(ls_handle fiber);

//! \brief Exits the current fiber.
//!
//! If the caller is a fiber, the thread that created the fiber will be
//! scheduled. If the caller is a thread or is the creator of the fiber,
//! the thread will exit. Regardless, the call will not return.
//!
//! \param code The exit code.
LS_NORETURN void ls_fiber_exit(int code);

#endif // _LS_THREAD_H_
6 changes: 6 additions & 0 deletions include/lysys/ls_time.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,14 @@ void ls_get_time(struct ls_timespec *ts);

void ls_get_local_time(struct ls_timespec *ts);

//! \brief Sleep for a number of milliseconds.
//!
//! \param ms The minimum number of milliseconds to sleep.
void ls_sleep(unsigned long ms);

//! \brief Sleep for a number of nanoseconds.
//!
//! \param ns The minimum number of nanoseconds to sleep.
void ls_nanosleep(long long ns);

#endif // _LYSYS_H_
1 change: 1 addition & 0 deletions include/lysys/lysys.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "ls_memory.h"
#include "ls_mmap.h"
#include "ls_proc.h"
#include "ls_random.h"
#include "ls_shell.h"
#include "ls_stat.h"
#include "ls_string.h"
Expand Down
2 changes: 2 additions & 0 deletions src/ls_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ typedef struct ls_buffer
uint8_t *end;
} ls_buffer_t;

#define ls_buffer_size(b) ((b)->end - (b)->data)

void ls_buffer_release(ls_buffer_t *buffer);

void ls_buffer_clear(ls_buffer_t *buffer);
Expand Down
Loading

0 comments on commit 72a1335

Please sign in to comment.