Änderungen Gruppe 30_10_2022 V2

This commit is contained in:
2022-10-30 20:01:47 +01:00
parent 269f807b31
commit 2517e52e59
239 changed files with 54983 additions and 9 deletions
@@ -0,0 +1,46 @@
# MSVC5 does not fully support C99, enabling declaration-after-statement
# warnings allows a common MSVC5 build error to be detected in Linux builds.
if (CMAKE_COMPILER_IS_GNUCC)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror -Wdeclaration-after-statement")
add_definitions (-D_GNU_SOURCE)
endif ()
set (HEADERS
vcos_platform.h
vcos_platform_types.h
)
foreach (header ${HEADERS})
configure_file ("${header}" "${VCOS_HEADERS_BUILD_DIR}/${header}" COPYONLY)
endforeach ()
add_subdirectory (../generic generic)
set (SOURCES
vcos_pthreads.c
vcos_dlfcn.c
../glibc/vcos_backtrace.c
../generic/vcos_generic_event_flags.c
../generic/vcos_mem_from_malloc.c
../generic/vcos_generic_named_sem.c
../generic/vcos_generic_safe_string.c
../generic/vcos_generic_reentrant_mtx.c
../generic/vcos_abort.c
../generic/vcos_cmd.c
../generic/vcos_init.c
../generic/vcos_msgqueue.c
../generic/vcos_logcat.c
../generic/vcos_generic_blockpool.c
)
if (VCOS_PTHREADS_BUILD_SHARED)
add_library (vcos SHARED ${SOURCES})
target_link_libraries (vcos pthread dl rt)
else ()
add_library (vcos ${SOURCES})
target_link_libraries (vcos pthread rt)
endif ()
#install(FILES ${HEADERS} DESTINATION include)
install(TARGETS vcos DESTINATION lib)
@@ -0,0 +1,71 @@
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "interface/vcos/vcos.h"
#include "interface/vcos/vcos_dlfcn.h"
#include <dlfcn.h>
void *vcos_dlopen(const char *name, int mode)
{
return dlopen(name, mode);
}
void (*vcos_dlsym(void *handle, const char *name))(void)
{
return dlsym(handle, name);
}
int vcos_dlclose (void *handle)
{
return dlclose(handle);
}
int vcos_dlerror(int *err, char *buf, size_t buflen)
{
/* not really threadsafe! */
const char *errmsg = dlerror();
vcos_assert(buflen > 0);
if (errmsg)
{
*err = -1;
strncpy(buf, errmsg, buflen);
buf[buflen-1] = '\0';
}
else
{
*err = 0;
buf[0] = '\0';
}
return 0;
}
@@ -0,0 +1,102 @@
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*=============================================================================
FIXME: This code should be moved to 'linux', it is linux-specific and not generic
on 'pthreads'.
============================================================================*/
#ifndef VCOS_MUTEX_FROM_FUTEX_H
#define VCOS_MUTEX_FROM_FUTEX_H
#ifdef __cplusplus
extern "C" {
#endif
#include "interface/vcos/vcos_types.h"
#include "vcos_platform.h"
typedef struct VCOS_FUTEX_T
{
volatile int value;
} VCOS_FUTEX_T;
typedef VCOS_FUTEX_T VCOS_MUTEX_T;
VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_futex_init(VCOS_FUTEX_T *futex);
VCOSPRE_ void VCOSPOST_ vcos_futex_delete(VCOS_FUTEX_T *futex);
VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_futex_lock(VCOS_FUTEX_T *futex);
VCOSPRE_ void VCOSPOST_ vcos_futex_unlock(VCOS_FUTEX_T *futex);
VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_futex_trylock(VCOS_FUTEX_T *futex);
#if defined(VCOS_INLINE_BODIES)
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_mutex_create(VCOS_MUTEX_T *latch, const char *name) {
vcos_unused(name);
return vcos_futex_init(latch);
}
VCOS_INLINE_IMPL
void vcos_mutex_delete(VCOS_MUTEX_T *latch) {
vcos_futex_delete(latch);
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_mutex_lock(VCOS_MUTEX_T *latch) {
return vcos_futex_lock(latch);
}
VCOS_INLINE_IMPL
void vcos_mutex_unlock(VCOS_MUTEX_T *latch) {
vcos_futex_unlock(latch);
}
VCOS_INLINE_IMPL
int vcos_mutex_is_locked(VCOS_MUTEX_T *latch) {
int rc = latch->value;
if (!rc) {
/* it wasn't locked */
return 0;
}
else {
return 1; /* it was locked */
}
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_mutex_trylock(VCOS_MUTEX_T *m) {
return vcos_futex_trylock(m);
}
#endif /* VCOS_INLINE_BODIES */
#ifdef __cplusplus
}
#endif
#endif /* VCOS_MUTEX_FROM_FUTEX_H */
@@ -0,0 +1,749 @@
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*=============================================================================
VideoCore OS Abstraction Layer - pthreads types
=============================================================================*/
/* Do not include this file directly - instead include it via vcos.h */
/** @file
*
* Pthreads implementation of VCOS.
*
*/
#ifndef VCOS_PLATFORM_H
#define VCOS_PLATFORM_H
#ifdef __cplusplus
extern "C" {
#endif
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <sys/types.h>
#include <sched.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <signal.h>
#include <stddef.h>
#include <stdlib.h>
#include <dlfcn.h>
#define VCOS_HAVE_RTOS 1
#define VCOS_HAVE_SEMAPHORE 1
#define VCOS_HAVE_EVENT 1
#define VCOS_HAVE_QUEUE 0
#define VCOS_HAVE_LEGACY_ISR 0
#define VCOS_HAVE_TIMER 1
#define VCOS_HAVE_CANCELLATION_SAFE_TIMER 1
#define VCOS_HAVE_MEMPOOL 0
#define VCOS_HAVE_ISR 0
#define VCOS_HAVE_ATOMIC_FLAGS 1
#define VCOS_HAVE_THREAD_AT_EXIT 1
#define VCOS_HAVE_ONCE 1
#define VCOS_HAVE_BLOCK_POOL 1
#define VCOS_HAVE_FILE 0
#define VCOS_HAVE_PROC 0
#define VCOS_HAVE_CFG 0
#define VCOS_HAVE_ALIEN_THREADS 1
#define VCOS_HAVE_CMD 1
#define VCOS_HAVE_EVENT_FLAGS 1
#define VCOS_WANT_LOG_CMD 0 /* User apps should do their own thing */
#define VCOS_ALWAYS_WANT_LOGGING
#ifdef __linux__
#define VCOS_HAVE_BACKTRACE 1
#endif
#define VCOS_SO_EXT ".so"
/* Linux/pthreads seems to have different timer characteristics */
#define VCOS_TIMER_MARGIN_EARLY 0
#define VCOS_TIMER_MARGIN_LATE 15
typedef sem_t VCOS_SEMAPHORE_T;
typedef uint32_t VCOS_UNSIGNED;
typedef uint32_t VCOS_OPTION;
typedef pthread_key_t VCOS_TLS_KEY_T;
typedef pthread_once_t VCOS_ONCE_T;
typedef struct VCOS_LLTHREAD_T
{
pthread_t thread; // Must be first field.
} VCOS_LLTHREAD_T;
/* VCOS_CASSERT(offsetof(VCOS_LLTHREAD_T, thread) == 0); */
#ifndef VCOS_USE_VCOS_FUTEX
typedef pthread_mutex_t VCOS_MUTEX_T;
#else
#include "vcos_futex_mutex.h"
#endif /* VCOS_USE_VCOS_FUTEX */
typedef struct
{
VCOS_MUTEX_T mutex;
sem_t sem;
} VCOS_EVENT_T;
#define VCOS_ONCE_INIT PTHREAD_ONCE_INIT
typedef struct VCOS_TIMER_T
{
pthread_t thread; /**< id of the timer thread */
pthread_mutex_t lock; /**< lock protecting all other members of the struct */
pthread_cond_t settings_changed; /**< cond. var. for informing the timer thread about changes*/
int quit; /**< non-zero if the timer thread is requested to quit*/
struct timespec expires; /**< absolute time of next expiration, or 0 if disarmed*/
void (*orig_expiration_routine)(void*);/**< the expiration routine provided by the user of the timer*/
void *orig_context; /**< the context for exp. routine provided by the user*/
} VCOS_TIMER_T;
/** Thread attribute structure. Don't use pthread_attr directly, as
* the calls can fail, and inits must match deletes.
*/
typedef struct VCOS_THREAD_ATTR_T
{
void *ta_stackaddr;
VCOS_UNSIGNED ta_stacksz;
VCOS_UNSIGNED ta_priority;
VCOS_UNSIGNED ta_affinity;
VCOS_UNSIGNED ta_timeslice;
VCOS_UNSIGNED legacy;
} VCOS_THREAD_ATTR_T;
/** Called at thread exit.
*/
typedef struct VCOS_THREAD_EXIT_T
{
void (*pfn)(void *);
void *cxt;
} VCOS_THREAD_EXIT_T;
#define VCOS_MAX_EXIT_HANDLERS 4
typedef struct VCOS_THREAD_T
{
pthread_t thread; /**< The thread itself */
VCOS_THREAD_ENTRY_FN_T entry; /**< The thread entry point */
void *arg; /**< The argument to be passed to entry */
VCOS_SEMAPHORE_T suspend; /**< For support event groups and similar - a per thread semaphore */
VCOS_TIMER_T task_timer;
int task_timer_created; /**< non-zero if the task timer has already been created*/
void (*orig_task_timer_expiration_routine)(void*);
void *orig_task_timer_context;
VCOS_UNSIGNED legacy;
char name[16]; /**< Record the name of this thread, for diagnostics */
VCOS_UNSIGNED dummy; /**< Dummy thread created for non-vcos created threads */
/** Callback invoked at thread exit time */
VCOS_THREAD_EXIT_T at_exit[VCOS_MAX_EXIT_HANDLERS];
} VCOS_THREAD_T;
#ifdef VCOS_PTHREADS_WANT_HISR_EMULATION
typedef struct
{
VCOS_THREAD_T thread;
char stack[1024];
VCOS_SEMAPHORE_T waitsem;
} VCOS_HISR_T;
#endif
#define VCOS_SUSPEND -1
#define VCOS_NO_SUSPEND 0
#define VCOS_START 1
#define VCOS_NO_START 0
#define VCOS_THREAD_PRI_MIN (sched_get_priority_min(SCHED_OTHER))
#define VCOS_THREAD_PRI_MAX (sched_get_priority_max(SCHED_OTHER))
#define VCOS_THREAD_PRI_INCREASE (1)
#define VCOS_THREAD_PRI_HIGHEST VCOS_THREAD_PRI_MAX
#define VCOS_THREAD_PRI_LOWEST VCOS_THREAD_PRI_MIN
#define VCOS_THREAD_PRI_NORMAL ((VCOS_THREAD_PRI_MAX+VCOS_THREAD_PRI_MIN)/2)
#define VCOS_THREAD_PRI_BELOW_NORMAL (VCOS_THREAD_PRI_NORMAL-VCOS_THREAD_PRI_INCREASE)
#define VCOS_THREAD_PRI_ABOVE_NORMAL (VCOS_THREAD_PRI_NORMAL+VCOS_THREAD_PRI_INCREASE)
#define VCOS_THREAD_PRI_REALTIME VCOS_THREAD_PRI_MAX
#define _VCOS_AFFINITY_DEFAULT 0
#define _VCOS_AFFINITY_CPU0 0x100
#define _VCOS_AFFINITY_CPU1 0x200
#define _VCOS_AFFINITY_MASK 0x300
#define VCOS_CAN_SET_STACK_ADDR 0
#define VCOS_TICKS_PER_SECOND _vcos_get_ticks_per_second()
#include "generic/vcos_generic_event_flags.h"
#include "generic/vcos_generic_blockpool.h"
#include "generic/vcos_mem_from_malloc.h"
/** Convert errno values into the values recognized by vcos */
VCOSPRE_ VCOS_STATUS_T vcos_pthreads_map_error(int error);
VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_pthreads_map_errno(void);
/** Register a function to be called when the current thread exits.
*/
extern VCOS_STATUS_T vcos_thread_at_exit(void (*pfn)(void*), void *cxt);
extern uint32_t _vcos_get_ticks_per_second(void);
/**
* Set to 1 by default when ANDROID is defined. Allows runtime
* switching for console apps.
*/
extern int vcos_use_android_log;
typedef struct {
VCOS_MUTEX_T mutex;
uint32_t flags;
} VCOS_ATOMIC_FLAGS_T;
#if defined(VCOS_INLINE_BODIES)
#undef VCOS_ASSERT_LOGGING_DISABLE
#define VCOS_ASSERT_LOGGING_DISABLE 1
/*
* Counted Semaphores
*/
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_semaphore_wait(VCOS_SEMAPHORE_T *sem) {
int ret;
/* gdb causes sem_wait() to EINTR when a breakpoint is hit, retry here */
while ((ret = sem_wait(sem)) == -1 && errno == EINTR)
continue;
vcos_assert(ret==0);
return VCOS_SUCCESS;
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_semaphore_trywait(VCOS_SEMAPHORE_T *sem) {
int ret;
while ((ret = sem_trywait(sem)) == -1 && errno == EINTR)
continue;
if (ret == 0)
return VCOS_SUCCESS;
else if (errno == EAGAIN)
return VCOS_EAGAIN;
else {
vcos_assert(0);
return VCOS_EINVAL;
}
}
/**
* \brief Wait on a semaphore with a timeout.
*
* Note that this function may not be implemented on all
* platforms, and may not be efficient on all platforms
* (see comment in vcos_semaphore_wait)
*
* Try to obtain the semaphore. If it is already taken, return
* VCOS_EAGAIN.
* @param sem Semaphore to wait on
* @param timeout Number of milliseconds to wait before
* returning if the semaphore can't be acquired.
* @return VCOS_SUCCESS - semaphore was taken.
* VCOS_EAGAIN - could not take semaphore (i.e. timeout
* expired)
* VCOS_EINVAL - Some other error (most likely bad
* parameters).
*/
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_semaphore_wait_timeout(VCOS_SEMAPHORE_T *sem, VCOS_UNSIGNED timeout) {
struct timespec ts;
int ret;
if (clock_gettime(CLOCK_REALTIME, &ts) == -1)
return VCOS_EINVAL;
ts.tv_sec += timeout/1000;
ts.tv_nsec += (timeout%1000)*1000*1000;
if (ts.tv_nsec > 1000000000) {
ts.tv_sec++;
ts.tv_nsec -= 1000000000;
}
while (1) {
ret = sem_timedwait( sem, &ts );
if (ret == 0) {
return VCOS_SUCCESS;
} else {
if (errno == EINTR) {
continue;
} else if (errno == ETIMEDOUT) {
return VCOS_EAGAIN;
} else {
vcos_assert(0);
return VCOS_EINVAL;
}
}
}
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_semaphore_create(VCOS_SEMAPHORE_T *sem,
const char *name,
VCOS_UNSIGNED initial_count) {
int rc = sem_init(sem, 0, initial_count);
(void)name;
if (rc != -1) return VCOS_SUCCESS;
else return vcos_pthreads_map_errno();
}
VCOS_INLINE_IMPL
void vcos_semaphore_delete(VCOS_SEMAPHORE_T *sem) {
int rc = sem_destroy(sem);
vcos_assert(rc != -1);
(void)rc;
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_semaphore_post(VCOS_SEMAPHORE_T *sem) {
int rc = sem_post(sem);
vcos_assert(rc == 0);
(void)rc;
return VCOS_SUCCESS;
}
/***********************************************************
*
* Threads
*
***********************************************************/
extern VCOS_THREAD_T *vcos_dummy_thread_create(void);
extern pthread_key_t _vcos_thread_current_key;
extern uint64_t vcos_getmicrosecs64_internal(void);
VCOS_INLINE_IMPL
uint32_t vcos_getmicrosecs(void) { return (uint32_t)vcos_getmicrosecs64_internal(); }
VCOS_INLINE_IMPL
uint64_t vcos_getmicrosecs64(void) { return vcos_getmicrosecs64_internal(); }
VCOS_INLINE_IMPL
VCOS_THREAD_T *vcos_thread_current(void) {
void *ret = pthread_getspecific(_vcos_thread_current_key);
if (ret == NULL)
{
ret = vcos_dummy_thread_create();
}
#ifdef __cplusplus
return static_cast<VCOS_THREAD_T*>(ret);
#else
return (VCOS_THREAD_T *)ret;
#endif
}
VCOS_INLINE_IMPL
void vcos_sleep(uint32_t ms) {
struct timespec ts;
ts.tv_sec = ms/1000;
ts.tv_nsec = ms % 1000 * (1000000);
nanosleep(&ts, NULL);
}
VCOS_INLINE_IMPL
void vcos_thread_attr_setstack(VCOS_THREAD_ATTR_T *attr, void *addr, VCOS_UNSIGNED sz) {
attr->ta_stackaddr = addr;
attr->ta_stacksz = sz;
}
VCOS_INLINE_IMPL
void vcos_thread_attr_setstacksize(VCOS_THREAD_ATTR_T *attr, VCOS_UNSIGNED sz) {
attr->ta_stacksz = sz;
}
VCOS_INLINE_IMPL
void vcos_thread_attr_setpriority(VCOS_THREAD_ATTR_T *attr, VCOS_UNSIGNED pri) {
(void)attr;
(void)pri;
}
VCOS_INLINE_IMPL
void vcos_thread_set_priority(VCOS_THREAD_T *thread, VCOS_UNSIGNED p) {
/* not implemented */
(void)thread;
(void)p;
}
VCOS_INLINE_IMPL
VCOS_UNSIGNED vcos_thread_get_priority(VCOS_THREAD_T *thread) {
/* not implemented */
(void)thread;
return 0;
}
VCOS_INLINE_IMPL
void vcos_thread_set_affinity(VCOS_THREAD_T *thread, VCOS_UNSIGNED affinity) {
/* not implemented */
vcos_unused(thread);
vcos_unused(affinity);
}
VCOS_INLINE_IMPL
void vcos_thread_attr_setaffinity(VCOS_THREAD_ATTR_T *attrs, VCOS_UNSIGNED affinity) {
attrs->ta_affinity = affinity;
}
VCOS_INLINE_IMPL
void vcos_thread_attr_settimeslice(VCOS_THREAD_ATTR_T *attrs, VCOS_UNSIGNED ts) {
attrs->ta_timeslice = ts;
}
VCOS_INLINE_IMPL
void _vcos_thread_attr_setlegacyapi(VCOS_THREAD_ATTR_T *attrs, VCOS_UNSIGNED legacy) {
attrs->legacy = legacy;
}
VCOS_INLINE_IMPL
void vcos_thread_attr_setautostart(VCOS_THREAD_ATTR_T *attrs, VCOS_UNSIGNED autostart) {
(void)attrs;
(void)autostart;
}
VCOS_INLINE_IMPL
VCOS_LLTHREAD_T *vcos_llthread_current(void) {
return (VCOS_LLTHREAD_T *)pthread_self();
}
/*
* Mutexes
*/
#ifndef VCOS_USE_VCOS_FUTEX
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_mutex_create(VCOS_MUTEX_T *latch, const char *name) {
int rc = pthread_mutex_init(latch, NULL);
(void)name;
if (rc == 0) return VCOS_SUCCESS;
else return vcos_pthreads_map_errno();
}
VCOS_INLINE_IMPL
void vcos_mutex_delete(VCOS_MUTEX_T *latch) {
int rc = pthread_mutex_destroy(latch);
(void)rc;
vcos_assert(rc==0);
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_mutex_lock(VCOS_MUTEX_T *latch) {
int rc = pthread_mutex_lock(latch);
vcos_assert(rc==0);
(void)rc;
return VCOS_SUCCESS;
}
VCOS_INLINE_IMPL
void vcos_mutex_unlock(VCOS_MUTEX_T *latch) {
int rc = pthread_mutex_unlock(latch);
(void)rc;
vcos_assert(rc==0);
}
VCOS_INLINE_IMPL
int vcos_mutex_is_locked(VCOS_MUTEX_T *m) {
int rc = pthread_mutex_trylock(m);
if (rc == 0) {
pthread_mutex_unlock(m);
/* it wasn't locked */
return 0;
}
else {
return 1; /* it was locked */
}
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_mutex_trylock(VCOS_MUTEX_T *m) {
int rc = pthread_mutex_trylock(m);
(void)rc;
return (rc == 0) ? VCOS_SUCCESS : VCOS_EAGAIN;
}
#endif /* VCOS_USE_VCOS_FUTEX */
/*
* Events
*/
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_event_create(VCOS_EVENT_T *event, const char *debug_name)
{
VCOS_STATUS_T status;
int rc = sem_init(&event->sem, 0, 0);
if (rc != 0) return vcos_pthreads_map_errno();
status = vcos_mutex_create(&event->mutex, debug_name);
if (status != VCOS_SUCCESS) {
sem_destroy(&event->sem);
return status;
}
return VCOS_SUCCESS;
}
VCOS_INLINE_IMPL
void vcos_event_signal(VCOS_EVENT_T *event)
{
#if VCOS_ASSERT_ENABLED
int ok = 0;
#endif
int value;
if (vcos_mutex_lock(&event->mutex) != VCOS_SUCCESS)
goto fail_mtx;
if (sem_getvalue(&event->sem, &value) != 0)
goto fail_sem;
if (value == 0)
if (sem_post(&event->sem) != 0)
goto fail_sem;
#if VCOS_ASSERT_ENABLED
ok = 1;
#endif
fail_sem:
vcos_mutex_unlock(&event->mutex);
fail_mtx:
vcos_assert(ok);
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_event_wait(VCOS_EVENT_T *event)
{
int ret;
/* gdb causes sem_wait() to EINTR when a breakpoint is hit, retry here */
while ((ret = sem_wait(&event->sem)) == -1 && errno == EINTR)
continue;
vcos_assert(ret==0);
return ret == 0 ? VCOS_SUCCESS : (VCOS_STATUS_T)errno;
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_event_try(VCOS_EVENT_T *event)
{
int ret;
while ((ret = sem_trywait(&event->sem)) == -1 && errno == EINTR)
continue;
if (ret == -1 && errno == EAGAIN)
return VCOS_EAGAIN;
else
return VCOS_SUCCESS;
}
VCOS_INLINE_IMPL
void vcos_event_delete(VCOS_EVENT_T *event)
{
int rc = sem_destroy(&event->sem);
vcos_assert(rc != -1);
(void)rc;
vcos_mutex_delete(&event->mutex);
}
VCOS_INLINE_IMPL
VCOS_UNSIGNED vcos_process_id_current(void) {
return (VCOS_UNSIGNED) getpid();
}
VCOS_INLINE_IMPL
int vcos_strcasecmp(const char *s1, const char *s2) {
return strcasecmp(s1,s2);
}
VCOS_INLINE_IMPL
int vcos_strncasecmp(const char *s1, const char *s2, size_t n) {
return strncasecmp(s1,s2,n);
}
VCOS_INLINE_IMPL
int vcos_in_interrupt(void) {
return 0;
}
/* For support event groups - per thread semaphore */
VCOS_INLINE_IMPL
void _vcos_thread_sem_wait(void) {
VCOS_THREAD_T *t = vcos_thread_current();
vcos_semaphore_wait(&t->suspend);
}
VCOS_INLINE_IMPL
void _vcos_thread_sem_post(VCOS_THREAD_T *target) {
vcos_semaphore_post(&target->suspend);
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_tls_create(VCOS_TLS_KEY_T *key) {
int st = pthread_key_create(key, NULL);
return st == 0 ? VCOS_SUCCESS: VCOS_ENOMEM;
}
VCOS_INLINE_IMPL
void vcos_tls_delete(VCOS_TLS_KEY_T tls) {
pthread_key_delete(tls);
}
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_tls_set(VCOS_TLS_KEY_T tls, void *v) {
pthread_setspecific(tls, v);
return VCOS_SUCCESS;
}
VCOS_INLINE_IMPL
void *vcos_tls_get(VCOS_TLS_KEY_T tls) {
return pthread_getspecific(tls);
}
#if VCOS_HAVE_ATOMIC_FLAGS
/*
* Atomic flags
*/
/* TODO implement properly... */
VCOS_INLINE_IMPL
VCOS_STATUS_T vcos_atomic_flags_create(VCOS_ATOMIC_FLAGS_T *atomic_flags)
{
atomic_flags->flags = 0;
return vcos_mutex_create(&atomic_flags->mutex, "VCOS_ATOMIC_FLAGS_T");
}
VCOS_INLINE_IMPL
void vcos_atomic_flags_or(VCOS_ATOMIC_FLAGS_T *atomic_flags, uint32_t flags)
{
vcos_mutex_lock(&atomic_flags->mutex);
atomic_flags->flags |= flags;
vcos_mutex_unlock(&atomic_flags->mutex);
}
VCOS_INLINE_IMPL
uint32_t vcos_atomic_flags_get_and_clear(VCOS_ATOMIC_FLAGS_T *atomic_flags)
{
uint32_t flags;
vcos_mutex_lock(&atomic_flags->mutex);
flags = atomic_flags->flags;
atomic_flags->flags = 0;
vcos_mutex_unlock(&atomic_flags->mutex);
return flags;
}
VCOS_INLINE_IMPL
void vcos_atomic_flags_delete(VCOS_ATOMIC_FLAGS_T *atomic_flags)
{
vcos_mutex_delete(&atomic_flags->mutex);
}
#endif
#if defined(linux) || defined(_HAVE_SBRK)
/* not exactly the free memory, but a measure of it */
VCOS_INLINE_IMPL
unsigned long vcos_get_free_mem(void) {
return (unsigned long)sbrk(0);
}
#endif
#ifdef VCOS_PTHREADS_WANT_HISR_EMULATION
VCOS_STATUS_T vcos_legacy_hisr_create(VCOS_HISR_T *hisr, const char *name,
void (*entry)(void),
VCOS_UNSIGNED pri,
void *stack, VCOS_UNSIGNED stack_size);
void vcos_legacy_hisr_activate(VCOS_HISR_T *hisr);
void vcos_legacy_hisr_delete(VCOS_HISR_T *hisr);
#endif
#undef VCOS_ASSERT_LOGGING_DISABLE
#define VCOS_ASSERT_LOGGING_DISABLE 0
#endif /* VCOS_INLINE_BODIES */
#define vcos_log_platform_init() _vcos_log_platform_init()
VCOSPRE_ void VCOSPOST_ _vcos_log_platform_init(void);
VCOS_INLINE_DECL void _vcos_thread_sem_wait(void);
VCOS_INLINE_DECL void _vcos_thread_sem_post(VCOS_THREAD_T *);
#define VCOS_APPLICATION_ARGC vcos_get_argc()
#define VCOS_APPLICATION_ARGV vcos_get_argv()
#include "generic/vcos_generic_reentrant_mtx.h"
#include "generic/vcos_generic_named_sem.h"
#include "generic/vcos_generic_quickslow_mutex.h"
#include "generic/vcos_common.h"
#define _VCOS_LOG_LEVEL() getenv("VC_LOGLEVEL")
VCOS_STATIC_INLINE
char *vcos_strdup(const char *str)
{
return strdup(str);
}
typedef void (*VCOS_ISR_HANDLER_T)(VCOS_UNSIGNED vecnum);
#define VCOS_DL_LAZY RTLD_LAZY
#define VCOS_DL_NOW RTLD_NOW
#define VCOS_DL_LOCAL RTLD_LOCAL
#define VCOS_DL_GLOBAL RTLD_GLOBAL
#ifdef __cplusplus
}
#endif
#endif /* VCOS_PLATFORM_H */
@@ -0,0 +1,71 @@
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*=============================================================================
VideoCore OS Abstraction Layer - platform-specific types and defines
=============================================================================*/
#ifndef VCOS_PLATFORM_TYPES_H
#define VCOS_PLATFORM_TYPES_H
#include "vcos_inttypes.h"
#ifdef __cplusplus
extern "C" {
#endif
#define VCOSPRE_ extern
#define VCOSPOST_
#if defined(__GNUC__) && (( __GNUC__ > 2 ) || (( __GNUC__ == 2 ) && ( __GNUC_MINOR__ >= 3 )))
#define VCOS_FORMAT_ATTR_(ARCHETYPE, STRING_INDEX, FIRST_TO_CHECK) __attribute__ ((format (ARCHETYPE, STRING_INDEX, FIRST_TO_CHECK)))
#else
#define VCOS_FORMAT_ATTR_(ARCHETYPE, STRING_INDEX, FIRST_TO_CHECK)
#endif
#if defined(__linux__) && !defined(NDEBUG) && defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__))
#define VCOS_BKPT ({ __asm volatile ("int3":::"memory"); })
#endif
/*#define VCOS_BKPT vcos_abort() */
#define VCOS_ASSERT_LOGGING 1
#define VCOS_ASSERT_LOGGING_DISABLE 0
extern void
vcos_pthreads_logging_assert(const char *file, const char *func, unsigned int line, const char *fmt, ...);
#define VCOS_ASSERT_MSG(...) ((VCOS_ASSERT_LOGGING && !VCOS_ASSERT_LOGGING_DISABLE) ? vcos_pthreads_logging_assert(__FILE__, __func__, __LINE__, __VA_ARGS__) : (void)0)
#define VCOS_INLINE_BODIES
#define VCOS_INLINE_DECL extern __inline__
#define VCOS_INLINE_IMPL static __inline__
#ifdef __cplusplus
}
#endif
#endif
@@ -0,0 +1,902 @@
/*
Copyright (c) 2012, Broadcom Europe Ltd
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*#define VCOS_INLINE_BODIES */
#include "interface/vcos/vcos.h"
#include "interface/vcos/vcos_msgqueue.h"
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/time.h>
#include <linux/param.h>
/* Cygwin doesn't always have prctl.h and it doesn't have PR_SET_NAME */
#if defined( __linux__ )
# if !defined(HAVE_PRCTL)
# define HAVE_PRCTL
# endif
#include <sys/prctl.h>
#endif
#ifdef HAVE_CMAKE_CONFIG
#include "cmake_config.h"
#endif
#ifdef HAVE_MTRACE
#include <mcheck.h>
#endif
#if defined(ANDROID)
#include <android/log.h>
#endif
#ifndef VCOS_DEFAULT_STACK_SIZE
#define VCOS_DEFAULT_STACK_SIZE 4096
#endif
static int vcos_argc;
static const char **vcos_argv;
typedef void (*LEGACY_ENTRY_FN_T)(int, void *);
static VCOS_THREAD_ATTR_T default_attrs = {
.ta_stacksz = VCOS_DEFAULT_STACK_SIZE,
};
/** Singleton global lock used for vcos_global_lock/unlock(). */
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
#ifdef ANDROID
static VCOS_MUTEX_T printf_lock;
#endif
/* Create a per-thread key for faking up vcos access
* on non-vcos threads.
*/
pthread_key_t _vcos_thread_current_key;
static VCOS_UNSIGNED _vcos_thread_current_key_created = 0;
static VCOS_ONCE_T current_thread_key_once; /* init just once */
static void vcos_thread_cleanup(VCOS_THREAD_T *thread)
{
vcos_semaphore_delete(&thread->suspend);
if (thread->task_timer_created)
{
vcos_timer_delete(&thread->task_timer);
}
}
static void vcos_dummy_thread_cleanup(void *cxt)
{
VCOS_THREAD_T *thread = cxt;
if (thread->dummy)
{
int i;
/* call termination functions */
for (i=0; thread->at_exit[i].pfn != NULL; i++)
{
thread->at_exit[i].pfn(thread->at_exit[i].cxt);
}
vcos_thread_cleanup(thread);
vcos_free(thread);
}
}
static void current_thread_key_init(void)
{
vcos_assert(!_vcos_thread_current_key_created);
pthread_key_create (&_vcos_thread_current_key, vcos_dummy_thread_cleanup);
_vcos_thread_current_key_created = 1;
}
/* A VCOS wrapper for the thread which called vcos_init. */
static VCOS_THREAD_T vcos_thread_main;
static void *vcos_thread_entry(void *arg)
{
int i;
void *ret;
VCOS_THREAD_T *thread = (VCOS_THREAD_T *)arg;
vcos_assert(thread != NULL);
thread->dummy = 0;
pthread_setspecific(_vcos_thread_current_key, thread);
#if defined( HAVE_PRCTL ) && defined( PR_SET_NAME )
/* cygwin doesn't have PR_SET_NAME */
prctl( PR_SET_NAME, (unsigned long)thread->name, 0, 0, 0 );
#endif
if (thread->legacy)
{
LEGACY_ENTRY_FN_T fn = (LEGACY_ENTRY_FN_T)thread->entry;
(*fn)(0, thread->arg);
ret = 0;
}
else
{
ret = (*thread->entry)(thread->arg);
}
/* call termination functions */
for (i=0; thread->at_exit[i].pfn != NULL; i++)
{
thread->at_exit[i].pfn(thread->at_exit[i].cxt);
}
return ret;
}
static void _task_timer_expiration_routine(void *cxt)
{
VCOS_THREAD_T *thread = (VCOS_THREAD_T *)cxt;
vcos_assert(thread->orig_task_timer_expiration_routine);
thread->orig_task_timer_expiration_routine(thread->orig_task_timer_context);
thread->orig_task_timer_expiration_routine = NULL;
}
VCOS_STATUS_T vcos_thread_create(VCOS_THREAD_T *thread,
const char *name,
VCOS_THREAD_ATTR_T *attrs,
VCOS_THREAD_ENTRY_FN_T entry,
void *arg)
{
VCOS_STATUS_T st;
pthread_attr_t pt_attrs;
VCOS_THREAD_ATTR_T *local_attrs = attrs ? attrs : &default_attrs;
int rc;
vcos_assert(thread);
memset(thread, 0, sizeof(VCOS_THREAD_T));
rc = pthread_attr_init(&pt_attrs);
if (rc < 0)
return VCOS_ENOMEM;
st = vcos_semaphore_create(&thread->suspend, NULL, 0);
if (st != VCOS_SUCCESS)
{
pthread_attr_destroy(&pt_attrs);
return st;
}
pthread_attr_setstacksize(&pt_attrs, local_attrs->ta_stacksz);
#if VCOS_CAN_SET_STACK_ADDR
if (local_attrs->ta_stackaddr)
{
pthread_attr_setstackaddr(&pt_attrs, local_attrs->ta_stackaddr);
}
#else
vcos_demand(local_attrs->ta_stackaddr == 0);
#endif
/* pthread_attr_setpriority(&pt_attrs, local_attrs->ta_priority); */
vcos_assert(local_attrs->ta_stackaddr == 0); /* Not possible */
thread->entry = entry;
thread->arg = arg;
thread->legacy = local_attrs->legacy;
strncpy(thread->name, name, sizeof(thread->name));
thread->name[sizeof(thread->name)-1] = '\0';
memset(thread->at_exit, 0, sizeof(thread->at_exit));
rc = pthread_create(&thread->thread, &pt_attrs, vcos_thread_entry, thread);
pthread_attr_destroy(&pt_attrs);
if (rc < 0)
{
vcos_semaphore_delete(&thread->suspend);
return VCOS_ENOMEM;
}
else
{
return VCOS_SUCCESS;
}
}
void vcos_thread_join(VCOS_THREAD_T *thread,
void **pData)
{
pthread_join(thread->thread, pData);
vcos_thread_cleanup(thread);
}
VCOSPRE_ VCOS_STATUS_T VCOSPOST_ vcos_thread_create_classic(VCOS_THREAD_T *thread,
const char *name,
void *(*entry)(void *arg),
void *arg,
void *stack,
VCOS_UNSIGNED stacksz,
VCOS_UNSIGNED priaff,
VCOS_UNSIGNED timeslice,
VCOS_UNSIGNED autostart)
{
VCOS_THREAD_ATTR_T attrs;
vcos_thread_attr_init(&attrs);
vcos_thread_attr_setstacksize(&attrs, stacksz);
vcos_thread_attr_setpriority(&attrs, priaff & ~_VCOS_AFFINITY_MASK);
vcos_thread_attr_setaffinity(&attrs, priaff & _VCOS_AFFINITY_MASK);
(void)timeslice;
(void)autostart;
if (VCOS_CAN_SET_STACK_ADDR)
{
vcos_thread_attr_setstack(&attrs, stack, stacksz);
}
return vcos_thread_create(thread, name, &attrs, entry, arg);
}
uint64_t vcos_getmicrosecs64_internal(void)
{
struct timeval tv;
uint64_t tm = 0;
if (!gettimeofday(&tv, NULL))
{
tm = (tv.tv_sec * 1000000LL) + tv.tv_usec;
}
return tm;
}
#ifdef ANDROID
static int log_prio[] =
{
ANDROID_LOG_INFO, /* VCOS_LOG_UNINITIALIZED */
ANDROID_LOG_INFO, /* VCOS_LOG_NEVER */
ANDROID_LOG_ERROR, /* VCOS_LOG_ERROR */
ANDROID_LOG_WARN, /* VCOS_LOG_WARN */
ANDROID_LOG_INFO, /* VCOS_LOG_INFO */
ANDROID_LOG_DEBUG /* VCOS_LOG_TRACE */
};
int vcos_use_android_log = 1;
int vcos_log_to_file = 0;
#else
int vcos_use_android_log = 0;
int vcos_log_to_file = 0;
#endif
static FILE * log_fhandle = NULL;
void vcos_vlog_default_impl(const VCOS_LOG_CAT_T *cat, VCOS_LOG_LEVEL_T _level, const char *fmt, va_list args)
{
(void)_level;
#ifdef ANDROID
if ( vcos_use_android_log )
{
__android_log_vprint(log_prio[_level], cat->name, fmt, args);
}
else
{
vcos_mutex_lock(&printf_lock);
#endif
if(NULL != log_fhandle)
{
if (cat->flags.want_prefix)
fprintf( log_fhandle, "%s: ", cat->name );
vfprintf(log_fhandle, fmt, args);
fputs("\n", log_fhandle);
fflush(log_fhandle);
}
#ifdef ANDROID
vcos_mutex_unlock(&printf_lock);
}
#endif
}
void _vcos_log_platform_init(void)
{
if(vcos_log_to_file)
{
char log_fname[100];
#ifdef ANDROID
snprintf(log_fname, 100, "/data/log/vcos_log%u.txt", vcos_process_id_current());
#else
snprintf(log_fname, 100, "/var/log/vcos_log%u.txt", vcos_process_id_current());
#endif
log_fhandle = fopen(log_fname, "w");
}
else
log_fhandle = stderr;
}
/* Flags for init/deinit components */
enum
{
VCOS_INIT_NAMED_SEM = (1 << 0),
VCOS_INIT_PRINTF_LOCK = (1 << 1),
VCOS_INIT_MAIN_SEM = (1 << 2),
VCOS_INIT_MSGQ = (1 << 3),
VCOS_INIT_ALL = 0xffffffff
};
static void vcos_term(uint32_t flags)
{
if (flags & VCOS_INIT_MSGQ)
vcos_msgq_deinit();
if (flags & VCOS_INIT_MAIN_SEM)
vcos_semaphore_delete(&vcos_thread_main.suspend);
#ifdef ANDROID
if (flags & VCOS_INIT_PRINTF_LOCK)
vcos_mutex_delete(&printf_lock);
#endif
if (flags & VCOS_INIT_NAMED_SEM)
_vcos_named_semaphore_deinit();
}
VCOS_STATUS_T vcos_platform_init(void)
{
VCOS_STATUS_T st;
uint32_t flags = 0;
int pst;
st = _vcos_named_semaphore_init();
if (!vcos_verify(st == VCOS_SUCCESS))
goto end;
flags |= VCOS_INIT_NAMED_SEM;
#ifdef HAVE_MTRACE
/* enable glibc memory debugging, if the environment
* variable MALLOC_TRACE names a valid file.
*/
mtrace();
#endif
#ifdef ANDROID
st = vcos_mutex_create(&printf_lock, "printf");
if (!vcos_verify(st == VCOS_SUCCESS))
goto end;
flags |= VCOS_INIT_PRINTF_LOCK;
#endif
st = vcos_once(&current_thread_key_once, current_thread_key_init);
if (!vcos_verify(st == VCOS_SUCCESS))
goto end;
/* Initialise a VCOS wrapper for the thread which called vcos_init. */
st = vcos_semaphore_create(&vcos_thread_main.suspend, NULL, 0);
if (!vcos_verify(st == VCOS_SUCCESS))
goto end;
flags |= VCOS_INIT_MAIN_SEM;
vcos_thread_main.thread = pthread_self();
pst = pthread_setspecific(_vcos_thread_current_key, &vcos_thread_main);
if (!vcos_verify(pst == 0))
{
st = VCOS_EINVAL;
goto end;
}
st = vcos_msgq_init();
if (!vcos_verify(st == VCOS_SUCCESS))
goto end;
flags |= VCOS_INIT_MSGQ;
vcos_logging_init();
end:
if (st != VCOS_SUCCESS)
vcos_term(flags);
return st;
}
void vcos_platform_deinit(void)
{
vcos_term(VCOS_INIT_ALL);
}
void vcos_global_lock(void)
{
pthread_mutex_lock(&lock);
}
void vcos_global_unlock(void)
{
pthread_mutex_unlock(&lock);
}
void vcos_thread_exit(void *arg)
{
VCOS_THREAD_T *thread = vcos_thread_current();
if ( thread && thread->dummy )
{
vcos_free ( (void*) thread );
thread = NULL;
}
pthread_exit(arg);
}
void vcos_thread_attr_init(VCOS_THREAD_ATTR_T *attrs)
{
*attrs = default_attrs;
}
VCOS_STATUS_T vcos_pthreads_map_error(int error)
{
switch (error)
{
case ENOMEM:
return VCOS_ENOMEM;
case ENXIO:
return VCOS_ENXIO;
case EAGAIN:
return VCOS_EAGAIN;
case ENOSPC:
return VCOS_ENOSPC;
default:
return VCOS_EINVAL;
}
}
VCOS_STATUS_T vcos_pthreads_map_errno(void)
{
return vcos_pthreads_map_error(errno);
}
void _vcos_task_timer_set(void (*pfn)(void*), void *cxt, VCOS_UNSIGNED ms)
{
VCOS_THREAD_T *thread = vcos_thread_current();
if (thread == NULL)
return;
vcos_assert(thread->orig_task_timer_expiration_routine == NULL);
if (!thread->task_timer_created)
{
VCOS_STATUS_T st = vcos_timer_create(&thread->task_timer, NULL,
_task_timer_expiration_routine, thread);
(void)st;
vcos_assert(st == VCOS_SUCCESS);
thread->task_timer_created = 1;
}
thread->orig_task_timer_expiration_routine = pfn;
thread->orig_task_timer_context = cxt;
vcos_timer_set(&thread->task_timer, ms);
}
void _vcos_task_timer_cancel(void)
{
VCOS_THREAD_T *thread = vcos_thread_current();
if (thread == NULL || !thread->task_timer_created)
return;
vcos_timer_cancel(&thread->task_timer);
thread->orig_task_timer_expiration_routine = NULL;
}
int vcos_vsnprintf( char *buf, size_t buflen, const char *fmt, va_list ap )
{
return vsnprintf( buf, buflen, fmt, ap );
}
int vcos_snprintf(char *buf, size_t buflen, const char *fmt, ...)
{
int ret;
va_list ap;
va_start(ap,fmt);
ret = vsnprintf(buf, buflen, fmt, ap);
va_end(ap);
return ret;
}
int vcos_have_rtos(void)
{
return 1;
}
const char * vcos_thread_get_name(const VCOS_THREAD_T *thread)
{
return thread->name;
}
#ifdef VCOS_HAVE_BACKTRACK
void __attribute__((weak)) vcos_backtrace_self(void);
#endif
void vcos_pthreads_logging_assert(const char *file, const char *func, unsigned int line, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
fprintf(stderr, "assertion failure:%s:%d:%s():",
file, line, func);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
#ifdef VCOS_HAVE_BACKTRACK
if (vcos_backtrace_self)
vcos_backtrace_self();
#endif
abort();
}
extern VCOS_STATUS_T vcos_thread_at_exit(void (*pfn)(void*), void *cxt)
{
int i;
VCOS_THREAD_T *self = vcos_thread_current();
if (!self)
{
vcos_assert(0);
return VCOS_EINVAL;
}
for (i=0; i<VCOS_MAX_EXIT_HANDLERS; i++)
{
if (self->at_exit[i].pfn == NULL)
{
self->at_exit[i].pfn = pfn;
self->at_exit[i].cxt = cxt;
return VCOS_SUCCESS;
}
}
return VCOS_ENOSPC;
}
void vcos_set_args(int argc, const char **argv)
{
vcos_argc = argc;
vcos_argv = argv;
}
int vcos_get_argc(void)
{
return vcos_argc;
}
const char ** vcos_get_argv(void)
{
return vcos_argv;
}
/* we can't inline this, because HZ comes from sys/param.h which
* dumps all sorts of junk into the global namespace, notable MIN and
* MAX.
*/
uint32_t _vcos_get_ticks_per_second(void)
{
return HZ;
}
VCOS_STATUS_T vcos_once(VCOS_ONCE_T *once_control,
void (*init_routine)(void))
{
int rc = pthread_once(once_control, init_routine);
if (rc != 0)
{
switch (errno)
{
case EINVAL:
return VCOS_EINVAL;
default:
vcos_assert(0);
return VCOS_EACCESS;
}
}
else
{
return VCOS_SUCCESS;
}
}
VCOS_THREAD_T *vcos_dummy_thread_create(void)
{
VCOS_STATUS_T st;
VCOS_THREAD_T *thread_hndl = NULL;
int rc;
thread_hndl = (VCOS_THREAD_T *)vcos_malloc(sizeof(VCOS_THREAD_T), NULL);
vcos_assert(thread_hndl != NULL);
memset(thread_hndl, 0, sizeof(VCOS_THREAD_T));
thread_hndl->dummy = 1;
thread_hndl->thread = pthread_self();
st = vcos_semaphore_create(&thread_hndl->suspend, NULL, 0);
if (st != VCOS_SUCCESS)
{
vcos_free(thread_hndl);
return( thread_hndl );
}
vcos_once(&current_thread_key_once, current_thread_key_init);
rc = pthread_setspecific(_vcos_thread_current_key,
thread_hndl);
(void)rc;
return( thread_hndl );
}
/***********************************************************
*
* Timers
*
***********************************************************/
/* On Linux we could use POSIX timers with a bit of synchronization.
* Unfortunately POSIX timers on Bionic are NOT POSIX compliant
* what makes that option not viable.
* That's why we ended up with our own implementation of timers.
* NOTE: That condition variables on Bionic are also buggy and
* they work incorrectly with CLOCK_MONOTONIC, so we have to
* use CLOCK_REALTIME (and hope that no one will change the time
* significantly after the timer has been set up
*/
#define NSEC_IN_SEC (1000*1000*1000)
#define MSEC_IN_SEC (1000)
#define NSEC_IN_MSEC (1000*1000)
static int _timespec_is_zero(struct timespec *ts)
{
return ((ts->tv_sec == 0) && (ts->tv_nsec == 0));
}
static void _timespec_set_zero(struct timespec *ts)
{
ts->tv_sec = ts->tv_nsec = 0;
}
/* Adds left to right and stores the result in left */
static void _timespec_add(struct timespec *left, struct timespec *right)
{
left->tv_sec += right->tv_sec;
left->tv_nsec += right->tv_nsec;
if (left->tv_nsec >= (NSEC_IN_SEC))
{
left->tv_nsec -= NSEC_IN_SEC;
left->tv_sec++;
}
}
static int _timespec_is_larger(struct timespec *left, struct timespec *right)
{
if (left->tv_sec != right->tv_sec)
return left->tv_sec > right->tv_sec;
else
return left->tv_nsec > right->tv_nsec;
}
static void* _timer_thread(void *arg)
{
VCOS_TIMER_T *timer = (VCOS_TIMER_T*)arg;
pthread_mutex_lock(&timer->lock);
while (!timer->quit)
{
struct timespec now;
/* Wait until next expiry time, or until timer's settings are changed */
if (_timespec_is_zero(&timer->expires))
pthread_cond_wait(&timer->settings_changed, &timer->lock);
else
pthread_cond_timedwait(&timer->settings_changed, &timer->lock, &timer->expires);
/* See if the timer has expired - reloop if it didn't */
clock_gettime(CLOCK_REALTIME, &now);
if (_timespec_is_zero(&timer->expires) || _timespec_is_larger(&timer->expires, &now))
continue;
/* The timer has expired. Clear the expiry time and call the
* expiration routine
*/
_timespec_set_zero(&timer->expires);
timer->orig_expiration_routine(timer->orig_context);
}
pthread_mutex_unlock(&timer->lock);
return NULL;
}
VCOS_STATUS_T vcos_timer_init(void)
{
return VCOS_SUCCESS;
}
VCOS_STATUS_T vcos_timer_create(VCOS_TIMER_T *timer,
const char *name,
void (*expiration_routine)(void *context),
void *context)
{
pthread_mutexattr_t lock_attr;
VCOS_STATUS_T result = VCOS_SUCCESS;
int settings_changed_initialized = 0;
int lock_attr_initialized = 0;
int lock_initialized = 0;
(void)name;
vcos_assert(timer);
vcos_assert(expiration_routine);
memset(timer, 0, sizeof(VCOS_TIMER_T));
timer->orig_expiration_routine = expiration_routine;
timer->orig_context = context;
/* Create conditional variable for notifying the timer's thread
* when settings change.
*/
if (result == VCOS_SUCCESS)
{
int rc = pthread_cond_init(&timer->settings_changed, NULL);
if (rc == 0)
settings_changed_initialized = 1;
else
result = vcos_pthreads_map_error(rc);
}
/* Create attributes for the lock (we want it to be recursive) */
if (result == VCOS_SUCCESS)
{
int rc = pthread_mutexattr_init(&lock_attr);
if (rc == 0)
{
pthread_mutexattr_settype(&lock_attr, PTHREAD_MUTEX_RECURSIVE);
lock_attr_initialized = 1;
}
else
{
result = vcos_pthreads_map_error(rc);
}
}
/* Create lock for the timer structure */
if (result == VCOS_SUCCESS)
{
int rc = pthread_mutex_init(&timer->lock, &lock_attr);
if (rc == 0)
lock_initialized = 1;
else
result = vcos_pthreads_map_error(rc);
}
/* Lock attributes are no longer needed */
if (lock_attr_initialized)
pthread_mutexattr_destroy(&lock_attr);
/* Create the underlying thread */
if (result == VCOS_SUCCESS)
{
int rc = pthread_create(&timer->thread, NULL, _timer_thread, timer);
if (rc != 0)
result = vcos_pthreads_map_error(rc);
}
/* Clean up if anything went wrong */
if (result != VCOS_SUCCESS)
{
if (lock_initialized)
pthread_mutex_destroy(&timer->lock);
if (settings_changed_initialized)
pthread_cond_destroy(&timer->settings_changed);
}
return result;
}
void vcos_timer_set(VCOS_TIMER_T *timer, VCOS_UNSIGNED delay_ms)
{
struct timespec now;
vcos_assert(timer);
/* Other implementations of this function do undefined things
* when delay_ms is 0. This implementation will simply assert and return
*/
vcos_assert(delay_ms != 0);
if (delay_ms == 0)
return;
pthread_mutex_lock(&timer->lock);
/* Calculate the new absolute expiry time */
clock_gettime(CLOCK_REALTIME, &now);
timer->expires.tv_sec = delay_ms / MSEC_IN_SEC;
timer->expires.tv_nsec = (delay_ms % MSEC_IN_SEC) * NSEC_IN_MSEC;
_timespec_add(&timer->expires, &now);
/* Notify the timer's thread about the change */
pthread_cond_signal(&timer->settings_changed);
pthread_mutex_unlock(&timer->lock);
}
void vcos_timer_cancel(VCOS_TIMER_T *timer)
{
vcos_assert(timer);
pthread_mutex_lock(&timer->lock);
_timespec_set_zero(&timer->expires);
pthread_cond_signal(&timer->settings_changed);
pthread_mutex_unlock(&timer->lock);
}
void vcos_timer_reset(VCOS_TIMER_T *timer, VCOS_UNSIGNED delay_ms)
{
vcos_timer_set(timer, delay_ms);
}
void vcos_timer_delete(VCOS_TIMER_T *timer)
{
vcos_assert(timer);
pthread_mutex_lock(&timer->lock);
/* Other implementation of this function (e.g. ThreadX)
* disallow it being called from the expiration routine
*/
vcos_assert(pthread_self() != timer->thread);
/* Stop the timer and set flag telling the timer thread to quit */
_timespec_set_zero(&timer->expires);
timer->quit = 1;
/* Notify the timer's thread about the change */
pthread_cond_signal(&timer->settings_changed);
/* Release the lock, so that the timer's thread can quit */
pthread_mutex_unlock(&timer->lock);
/* Wait for the timer thread to finish */
pthread_join(timer->thread, NULL);
/* Free resources used by the timer */
pthread_mutex_destroy(&timer->lock);
pthread_cond_destroy(&timer->settings_changed);
}