[mocom] Lock access to the workers list with a mutex.
This commit is contained in:
@@ -47,10 +47,12 @@ mocom_app::mocom_app(transport &t)
|
||||
: m_transport(t),
|
||||
m_mux(m_transport)
|
||||
{
|
||||
mutex_init(&m_workers_mutex);
|
||||
}
|
||||
|
||||
mocom_app::~mocom_app()
|
||||
{
|
||||
mutex_destroy(&m_workers_mutex);
|
||||
}
|
||||
|
||||
status_t mocom_app::worker()
|
||||
@@ -66,12 +68,14 @@ status_t mocom_app::worker()
|
||||
t = temp;
|
||||
|
||||
// call all of the registered workers
|
||||
mutex_acquire(&m_workers_mutex);
|
||||
worker_callback *cb;
|
||||
list_for_every_entry(&m_workers, cb, worker_callback, node) {
|
||||
temp = cb->work(cb->context);
|
||||
if (temp < t)
|
||||
t = temp;
|
||||
}
|
||||
mutex_release(&m_workers_mutex);
|
||||
|
||||
// wait a cycle
|
||||
if (t > 0) {
|
||||
@@ -85,12 +89,16 @@ status_t mocom_app::worker()
|
||||
|
||||
void mocom_app::register_worker(worker_callback &cb)
|
||||
{
|
||||
mutex_acquire(&m_workers_mutex);
|
||||
list_add_head(&m_workers, &cb.node);
|
||||
mutex_release(&m_workers_mutex);
|
||||
}
|
||||
|
||||
void mocom_app::unregister_worker(worker_callback &cb)
|
||||
{
|
||||
mutex_acquire(&m_workers_mutex);
|
||||
list_delete(&cb.node);
|
||||
mutex_release(&m_workers_mutex);
|
||||
}
|
||||
|
||||
status_t mocom_app::init()
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include <list.h>
|
||||
#include <sys/types.h>
|
||||
#include <kernel/event.h>
|
||||
#include <kernel/mutex.h>
|
||||
#include <lib/cpputils/nocopy.hpp>
|
||||
#include "transport.hpp"
|
||||
#include "mux.hpp"
|
||||
@@ -63,6 +64,9 @@ private:
|
||||
// main app event
|
||||
event_t m_event;
|
||||
|
||||
// mutex to protect list of workers.
|
||||
mutex_t m_workers_mutex;
|
||||
|
||||
struct list_node m_workers = LIST_INITIAL_VALUE(m_workers);
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user