Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions lib/platform/linux/platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "logger.h"
#include "platform.h"
#include "reassembly.h"
#include "ssr.h"
#include "wpc_proto.h"

// Maximum number of indication to be retrieved from a single poll
Expand All @@ -29,6 +30,8 @@

// Mutex for sending, ie serial access
static pthread_mutex_t sending_mutex;
// Mutex for the in-process SSR routing state.
static pthread_mutex_t ssr_mutex;

// This thread is used to poll for indication
static pthread_t thread_polling;
Expand Down Expand Up @@ -309,6 +312,30 @@ void Platform_unlock_request()
pthread_mutex_unlock(&sending_mutex);
}

bool Platform_lock_ssr()
{
int res = pthread_mutex_lock(&ssr_mutex);
if (res != 0)
{
if (res == EINVAL)
{
LOGW("SSR mutex no longer exists (destroyed)\n");
}
else
{
LOGE("SSR mutex lock failed %d\n", res);
}
return false;
}

return true;
}

void Platform_unlock_ssr()
{
pthread_mutex_unlock(&ssr_mutex);
}

unsigned long long Platform_get_timestamp_ms_epoch()
{
struct timespec spec;
Expand Down Expand Up @@ -377,25 +404,33 @@ bool Platform_init(Platform_get_indication_f get_indication_f,
goto error2;
}

if (pthread_mutex_init(&ssr_mutex, &attr) != 0)
{
LOGE("SSR Mutex init failed\n");
goto error3;
}

// Start a thread to poll for indication
if (pthread_create(&thread_polling, NULL, poll_for_indication, NULL) != 0)
{
LOGE("Cannot create polling thread\n");
goto error3;
goto error4;
}

m_dispatch_thread_running = true;
// Start a thread to dispatch indication
if (pthread_create(&thread_dispatch, NULL, dispatch_indication, NULL) != 0)
{
LOGE("Cannot create dispatch thread\n");
goto error4;
goto error5;
}

return true;

error4:
error5:
pthread_kill(thread_polling, SIGKILL);
error4:
pthread_mutex_destroy(&ssr_mutex);
error3:
pthread_mutex_destroy(&m_queue_mutex);
error2:
Expand Down Expand Up @@ -431,6 +466,7 @@ void Platform_close()
}

// Destroy our mutexes
pthread_mutex_destroy(&ssr_mutex);
pthread_mutex_destroy(&m_queue_mutex);
pthread_mutex_destroy(&sending_mutex);
}
12 changes: 12 additions & 0 deletions lib/platform/platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ bool Platform_lock_request();
*/
void Platform_unlock_request();

/**
* \brief Call at the beginning of an SSR critical section.
* \note This lock protects the in-process SSR routing state, which is
* accessed both from the dispatch thread and from caller threads.
*/
bool Platform_lock_ssr();

/**
* \brief Called at the end of an SSR critical section.
*/
void Platform_unlock_ssr();

/**
* \brief Dynamic memory allocation
* \param size
Expand Down
3 changes: 2 additions & 1 deletion lib/wpc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ add_library(wpc STATIC
${CMAKE_CURRENT_LIST_DIR}/wpc.c
${CMAKE_CURRENT_LIST_DIR}/wpc_internal.c
${CMAKE_CURRENT_LIST_DIR}/reassembly/reassembly.c
${CMAKE_CURRENT_LIST_DIR}/ssr/fht.c
${CMAKE_CURRENT_LIST_DIR}/ssr/ssr.c
)

target_include_directories(wpc PUBLIC
Expand All @@ -17,4 +19,3 @@ target_include_directories(wpc PRIVATE
${PROJECT_SOURCE_DIR}/platform
${CMAKE_CURRENT_LIST_DIR}/include
)

Loading
Loading