Skip to content

Commit

Permalink
c++: Add thread handling to OS funcs
Browse files Browse the repository at this point in the history
Signed-off-by: Corey Minyard <[email protected]>
  • Loading branch information
cminyard committed Sep 6, 2024
1 parent cda9f00 commit 6a507e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
14 changes: 13 additions & 1 deletion c++/include/gensio/gensioosh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ namespace gensios {
int err;
};

// Handle log calls from the low-level gensio system, generally for
// things that cannot be reported another way.
class GENSIOOSHCPP_DLL_PUBLIC Os_Funcs_Log_Handler {
public:
virtual void log(enum gensio_log_levels level, const std::string log)
Expand All @@ -53,6 +55,13 @@ namespace gensios {
GENSIOOSHCPP_DLL_PUBLIC
int get_log_mask();

// Used as the startup function for a new thread.
class GENSIOOSHCPP_DLL_PUBLIC Os_Funcs_Thread_Func {
public:
virtual void start() = 0;
virtual ~Os_Funcs_Thread_Func() = default;
};

// This is a wrapper for gensio_os_funcs that makes using it a lot
// cleaner. The default constructor automatically allocates the
// default os handler, destruction takes place automatically.
Expand Down Expand Up @@ -83,7 +92,7 @@ namespace gensios {
// gensio_os_funcs.3 for details on process setup.
void proc_setup();

// Do the setup for the thread. See gensio_os_funcs.c for details.
// Do the setup for the thread. See gensio_os_funcs.3 for details.
void thread_setup();

// If you allocate your own subclass of this, you must call
Expand Down Expand Up @@ -115,6 +124,9 @@ namespace gensios {

unsigned int get_refcount() { return *refcnt; }

struct gensio_thread *new_thread(Os_Funcs_Thread_Func *start_func);
void wait_thread(struct gensio_thread *thread_id);

private:
void refcount_from(const Os_Funcs *o);
Os_Funcs_Log_Handler *logger = NULL;
Expand Down
28 changes: 28 additions & 0 deletions c++/lib/gensioosh.cc
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,34 @@ namespace gensios {
throw gensio_error(err);
}

void
thread_start_func(void *data) {
Os_Funcs_Thread_Func *start_func =
static_cast<Os_Funcs_Thread_Func *>(data);

start_func->start();
}

struct gensio_thread *
Os_Funcs::new_thread(Os_Funcs_Thread_Func *start_func)
{
int err;
struct gensio_thread *id;

err = gensio_os_new_thread(*this, thread_start_func, start_func, &id);
if (err)
throw gensio_error(err);
return id;
}

void
Os_Funcs::wait_thread(struct gensio_thread *thread_id)
{
int err = gensio_os_wait_thread(thread_id);
if (err)
throw gensio_error(err);
}

void
Os_Funcs::refcount_from(const Os_Funcs *o)
{
Expand Down

0 comments on commit 6a507e0

Please sign in to comment.