Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #642, 645, 701, 702, 703 - OSAL global table management #704

Merged
merged 5 commits into from
Jan 11, 2021
Merged
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
44 changes: 44 additions & 0 deletions src/os/posix/inc/os-impl-idmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* NASA Docket No. GSC-18,370-1, and identified as "Operating System Abstraction Layer"
*
* Copyright (c) 2019 United States Government as represented by
* the Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/**
* \file os-impl-idmap.h
* \ingroup posix
* \author joseph.p.hickey@nasa.gov
*
*/

#ifndef OS_IMPL_IDMAP_H
#define OS_IMPL_IDMAP_H

#include "osconfig.h"
#include "osapi-idmap.h"
#include <pthread.h>

typedef struct
{
pthread_mutex_t mutex;
pthread_cond_t cond;
} OS_impl_objtype_lock_t;

/* Tables where the lock state information is stored */
extern OS_impl_objtype_lock_t *const OS_impl_objtype_lock_table[OS_OBJECT_TYPE_USER];

#endif /* OS_IMPL_IDMAP_H */
195 changes: 76 additions & 119 deletions src/os/posix/src/os-impl-idmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,45 +34,36 @@
#include <sched.h>

#include "os-shared-idmap.h"

typedef struct
#include "os-impl-idmap.h"

static OS_impl_objtype_lock_t OS_global_task_table_lock;
static OS_impl_objtype_lock_t OS_queue_table_lock;
static OS_impl_objtype_lock_t OS_bin_sem_table_lock;
static OS_impl_objtype_lock_t OS_mutex_table_lock;
static OS_impl_objtype_lock_t OS_count_sem_table_lock;
static OS_impl_objtype_lock_t OS_stream_table_lock;
static OS_impl_objtype_lock_t OS_dir_table_lock;
static OS_impl_objtype_lock_t OS_timebase_table_lock;
static OS_impl_objtype_lock_t OS_timecb_table_lock;
static OS_impl_objtype_lock_t OS_module_table_lock;
static OS_impl_objtype_lock_t OS_filesys_table_lock;
static OS_impl_objtype_lock_t OS_console_lock;

OS_impl_objtype_lock_t * const OS_impl_objtype_lock_table[OS_OBJECT_TYPE_USER] =
{
pthread_mutex_t mutex;
pthread_cond_t cond;
} POSIX_GlobalLock_t;

static POSIX_GlobalLock_t OS_global_task_table_mut;
static POSIX_GlobalLock_t OS_queue_table_mut;
static POSIX_GlobalLock_t OS_bin_sem_table_mut;
static POSIX_GlobalLock_t OS_mutex_table_mut;
static POSIX_GlobalLock_t OS_count_sem_table_mut;
static POSIX_GlobalLock_t OS_stream_table_mut;
static POSIX_GlobalLock_t OS_dir_table_mut;
static POSIX_GlobalLock_t OS_timebase_table_mut;
static POSIX_GlobalLock_t OS_timecb_table_mut;
static POSIX_GlobalLock_t OS_module_table_mut;
static POSIX_GlobalLock_t OS_filesys_table_mut;
static POSIX_GlobalLock_t OS_console_mut;

static POSIX_GlobalLock_t *const MUTEX_TABLE[] = {
[OS_OBJECT_TYPE_UNDEFINED] = NULL,
[OS_OBJECT_TYPE_OS_TASK] = &OS_global_task_table_mut,
[OS_OBJECT_TYPE_OS_QUEUE] = &OS_queue_table_mut,
[OS_OBJECT_TYPE_OS_COUNTSEM] = &OS_count_sem_table_mut,
[OS_OBJECT_TYPE_OS_BINSEM] = &OS_bin_sem_table_mut,
[OS_OBJECT_TYPE_OS_MUTEX] = &OS_mutex_table_mut,
[OS_OBJECT_TYPE_OS_STREAM] = &OS_stream_table_mut,
[OS_OBJECT_TYPE_OS_DIR] = &OS_dir_table_mut,
[OS_OBJECT_TYPE_OS_TIMEBASE] = &OS_timebase_table_mut,
[OS_OBJECT_TYPE_OS_TIMECB] = &OS_timecb_table_mut,
[OS_OBJECT_TYPE_OS_MODULE] = &OS_module_table_mut,
[OS_OBJECT_TYPE_OS_FILESYS] = &OS_filesys_table_mut,
[OS_OBJECT_TYPE_OS_CONSOLE] = &OS_console_mut,
};

enum
{
MUTEX_TABLE_SIZE = (sizeof(MUTEX_TABLE) / sizeof(MUTEX_TABLE[0]))
[OS_OBJECT_TYPE_OS_TASK] = &OS_global_task_table_lock,
[OS_OBJECT_TYPE_OS_QUEUE] = &OS_queue_table_lock,
[OS_OBJECT_TYPE_OS_COUNTSEM] = &OS_count_sem_table_lock,
[OS_OBJECT_TYPE_OS_BINSEM] = &OS_bin_sem_table_lock,
[OS_OBJECT_TYPE_OS_MUTEX] = &OS_mutex_table_lock,
[OS_OBJECT_TYPE_OS_STREAM] = &OS_stream_table_lock,
[OS_OBJECT_TYPE_OS_DIR] = &OS_dir_table_lock,
[OS_OBJECT_TYPE_OS_TIMEBASE] = &OS_timebase_table_lock,
[OS_OBJECT_TYPE_OS_TIMECB] = &OS_timecb_table_lock,
[OS_OBJECT_TYPE_OS_MODULE] = &OS_module_table_lock,
[OS_OBJECT_TYPE_OS_FILESYS] = &OS_filesys_table_lock,
[OS_OBJECT_TYPE_OS_CONSOLE] = &OS_console_lock,
};

/*---------------------------------------------------------------------------------------
Expand All @@ -92,31 +83,19 @@ void OS_Posix_ReleaseTableMutex(void *mut)
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_Lock_Global_Impl(osal_objtype_t idtype)
void OS_Lock_Global_Impl(osal_objtype_t idtype)
{
POSIX_GlobalLock_t *mut;
OS_impl_objtype_lock_t *impl;
int ret;

if (idtype < MUTEX_TABLE_SIZE)
{
mut = MUTEX_TABLE[idtype];
}
else
{
mut = NULL;
}
impl = OS_impl_objtype_lock_table[idtype];

if (mut != NULL)
ret = pthread_mutex_lock(&impl->mutex);
if (ret != 0)
{
ret = pthread_mutex_lock(&mut->mutex);
if (ret != 0)
{
OS_DEBUG("pthread_mutex_lock(&mut->mutex): %s", strerror(ret));
return OS_ERROR;
}
OS_DEBUG("pthread_mutex_lock(&impl->mutex): %s", strerror(ret));
}

return OS_SUCCESS;
} /* end OS_Lock_Global_Impl */

/*----------------------------------------------------------------
Expand All @@ -127,39 +106,27 @@ int32 OS_Lock_Global_Impl(osal_objtype_t idtype)
* See prototype for argument/return detail
*
*-----------------------------------------------------------------*/
int32 OS_Unlock_Global_Impl(osal_objtype_t idtype)
void OS_Unlock_Global_Impl(osal_objtype_t idtype)
{
POSIX_GlobalLock_t *mut;
OS_impl_objtype_lock_t *impl;
int ret;

if (idtype < MUTEX_TABLE_SIZE)
{
mut = MUTEX_TABLE[idtype];
}
else
impl = OS_impl_objtype_lock_table[idtype];

/* Notify any waiting threads that the state _may_ have changed */
ret = pthread_cond_broadcast(&impl->cond);
if (ret != 0)
{
mut = NULL;
OS_DEBUG("pthread_cond_broadcast(&impl->cond): %s", strerror(ret));
/* unexpected but keep going (not critical) */
}

if (mut != NULL)
ret = pthread_mutex_unlock(&impl->mutex);
if (ret != 0)
{
/* Notify any waiting threads that the state _may_ have changed */
ret = pthread_cond_broadcast(&mut->cond);
if (ret != 0)
{
OS_DEBUG("pthread_cond_broadcast(&mut->cond): %s", strerror(ret));
/* unexpected but keep going (not critical) */
}

ret = pthread_mutex_unlock(&mut->mutex);
if (ret != 0)
{
OS_DEBUG("pthread_mutex_unlock(&mut->mutex): %s", strerror(ret));
return OS_ERROR;
}
OS_DEBUG("pthread_mutex_unlock(&impl->mutex): %s", strerror(ret));
}

return OS_SUCCESS;
} /* end OS_Unlock_Global_Impl */

/*----------------------------------------------------------------
Expand All @@ -172,42 +139,39 @@ int32 OS_Unlock_Global_Impl(osal_objtype_t idtype)
*-----------------------------------------------------------------*/
void OS_WaitForStateChange_Impl(osal_objtype_t idtype, uint32 attempts)
{
POSIX_GlobalLock_t *impl;
OS_impl_objtype_lock_t *impl;
struct timespec ts;

impl = MUTEX_TABLE[idtype];
impl = OS_impl_objtype_lock_table[idtype];

if (impl != NULL)
{
/*
* because pthread_cond_timedwait() is also a cancellation point,
* this pushes a cleanup handler to ensure that if canceled during this call,
* the mutex will be released.
*/
pthread_cleanup_push(OS_Posix_ReleaseTableMutex, &impl->mutex);
/*
* because pthread_cond_timedwait() is also a cancellation point,
* this pushes a cleanup handler to ensure that if canceled during this call,
* the mutex will be released.
*/
pthread_cleanup_push(OS_Posix_ReleaseTableMutex, &impl->mutex);

clock_gettime(CLOCK_REALTIME, &ts);
clock_gettime(CLOCK_REALTIME, &ts);

if (attempts <= 10)
{
/* Wait an increasing amount of time, starting at 10ms */
ts.tv_nsec += attempts * attempts * 10000000;
if (ts.tv_nsec >= 1000000000)
{
ts.tv_nsec -= 1000000000;
++ts.tv_sec;
}
}
else
if (attempts <= 10)
{
/* Wait an increasing amount of time, starting at 10ms */
ts.tv_nsec += attempts * attempts * 10000000;
if (ts.tv_nsec >= 1000000000)
{
/* wait 1 second (max for polling) */
ts.tv_nsec -= 1000000000;
++ts.tv_sec;
}
}
else
{
/* wait 1 second (max for polling) */
++ts.tv_sec;
}

pthread_cond_timedwait(&impl->cond, &impl->mutex, &ts);
pthread_cond_timedwait(&impl->cond, &impl->mutex, &ts);

pthread_cleanup_pop(false);
}
pthread_cleanup_pop(false);
}

/*---------------------------------------------------------------------------------------
Expand All @@ -222,23 +186,16 @@ int32 OS_Posix_TableMutex_Init(osal_objtype_t idtype)
int ret;
int32 return_code = OS_SUCCESS;
pthread_mutexattr_t mutex_attr;
POSIX_GlobalLock_t *impl;
OS_impl_objtype_lock_t *impl;

do
impl = OS_impl_objtype_lock_table[idtype];
if (impl == NULL)
{
if (idtype >= MUTEX_TABLE_SIZE)
{
break;
}

impl = MUTEX_TABLE[idtype];

/* Initialize the table mutex for the given idtype */
if (impl == NULL)
{
break;
}
return OS_SUCCESS;
}

do
{
/*
* initialize the pthread mutex attribute structure with default values
*/
Expand Down
Loading