struct timespec current_time_tspec;
ret = clock_gettime(clockid, ¤t_time_tspec);
+ if (ret) return errno;
+
*current_time = fosa_timespec_to_abs_time(current_time_tspec);
- return ret;
+ return 0;
}
/**
int fosa_thread_get_cputime_clock(fosa_thread_id_t tid,
fosa_clock_id_t *clockid)
{
- if (tid.linux_pid == tid.linux_tid) {
- /* standard UNIX process */
- return clock_getcpuclockid(tid.linux_pid, clockid);
- } else {
- /* POSIX thread */
- return pthread_getcpuclockid(tid.pthread_id, clockid);
- }
+ int ret;
+
+ if (tid.linux_pid == tid.linux_tid) /* standard UNIX process */
+ ret = clock_getcpuclockid(tid.linux_pid, clockid);
+ else /* POSIX thread */
+ ret = pthread_getcpuclockid(tid.pthread_id, clockid);
+
+ return ret ? errno : 0;
}
/*************************
fosa_signal_info_t info,
fosa_timer_id_t *timerid)
{
+ int ret;
struct sigevent event;
event.sigev_notify = SIGEV_SIGNAL;
event.sigev_signo = signal;
event.sigev_value = *((union sigval*) &info);
- return timer_create(clockid, &event, timerid);
+ ret = timer_create(clockid, &event, timerid);
+
+ return ret ? errno : 0;
}
/**
fosa_timer_id_t *timerid,
fosa_thread_id_t receiver)
{
+ int ret;
struct sigevent event;
event.sigev_notify = SIGEV_SIGNAL;
event.sigev_notify = SIGEV_THREAD_ID | SIGEV_SIGNAL;
event._sigev_un._tid = receiver.linux_tid;
- return timer_create(clockid, &event, timerid);
+ ret = timer_create(clockid, &event, timerid);
+
+ return ret ? errno : 0;
}
/**
**/
int fosa_timer_delete(fosa_timer_id_t timerid)
{
- return timer_delete(timerid);
+ int ret;
+
+ ret = timer_delete(timerid);
+
+ return ret ? errno : 0;
}
/**
**/
int fosa_rel_timer_arm(fosa_timer_id_t timerid, const fosa_rel_time_t *value)
{
+ int ret;
struct itimerspec when;
/* non-periodic one shot timer */
when.it_value = fosa_abs_time_to_timespec(*value);
when.it_interval = zero_time;
- return timer_settime(timerid, 0, &when, NULL);
+ ret = timer_settime(timerid, 0, &when, NULL);
+
+ return ret ? errno : 0;
}
**/
int fosa_abs_timer_arm(fosa_timer_id_t timerid, const fosa_abs_time_t *value)
{
+ int ret;
struct itimerspec when;
/* non-periodic one shot timer */
when.it_value = fosa_abs_time_to_timespec(*value);
when.it_interval = zero_time;
- return timer_settime(timerid, TIMER_ABSTIME, &when, NULL);
+ ret = timer_settime(timerid, TIMER_ABSTIME, &when, NULL);
+
+ return ret ? errno : 0;
}
/**
ret = timer_gettime(timerid, &time);
*remaining_time = fosa_timespec_to_rel_time(time.it_value);
- return ret;
+ return ret ? errno : 0;
}
/**
return FOSA_EINVAL;
ret = timer_gettime(timerid, &time);
+ if (ret) return errno;
+
*remaining_time = fosa_timespec_to_rel_time(time.it_value);
- if (ret < 0)
- return ret;
time.it_value = zero_time;
time.it_interval = zero_time;
-
- return timer_settime(timerid, 0, &time, NULL);
+
+ ret = timer_settime(timerid, 0, &time, NULL);
+
+ return ret ? errno : 0;
}
//================================================
#include "fosa_time.h"
+#include "fosa_configuration_parameters.h"
#include "fosa_mutexes_and_condvars.h"
#ifdef OMK_FOR_USER /* If compiled by OMK, use the config */
**/
int fosa_mutex_init(fosa_mutex_t *mutex, int prioceiling)
{
- int error;
+ int ret;
pthread_mutexattr_t attr;
- if ((error = pthread_mutexattr_init(&attr)) != 0)
- return error;
+ ret = pthread_mutexattr_init(&attr);
+ if (ret) return errno;
#ifndef CONFIG_NO_PRIO_INHERIT /* Valgrind doesn't support this attribute */
- if ((error = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)) != 0)
- return error;
+ ret = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
+ if (ret) return errno;
#endif
+
+ ret = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+ if (ret) return errno;
+
return pthread_mutex_init(mutex, &attr);
}
int new_ceiling,
int *old_ceiling)
{
- return -EINVAL;
+ return FOSA_EINVAL;
}
/**
**/
int fosa_mutex_get_prioceiling(const fosa_mutex_t *mutex, int *ceiling)
{
- return -EINVAL;
+ return FOSA_EINVAL;
}
/**
**/
int fosa_cond_init(fosa_cond_t *cond)
{
- return pthread_cond_init(cond, NULL);
+ int ret;
+ pthread_condattr_t attr;
+
+ ret = pthread_condattr_init(&attr);
+ if (ret) return errno;
+
+ ret = pthread_condattr_setclock(&attr, FOSA_CLOCK_REALTIME);
+ if (ret) return errno;
+
+ ret = pthread_condattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
+ if (ret) return errno;
+
+ return pthread_cond_init(cond, &attr);
}
/**
struct timespec abstime_tspec;
abstime_tspec = fosa_abs_time_to_timespec(*abstime);
-
return pthread_cond_timedwait(cond, mutex, &abstime_tspec);
}
+
/* Initialize the keys data structure */
int init_keys()
{
- int i, error;
+ int i, ret;
pthread_mutexattr_t attr;
for(i = 0; i < FOSA_MAX_KEYS; i++)
key_in_use[i] = false;
- if ((error = pthread_mutexattr_init(&attr)) != 0)
- return error;
+ ret = pthread_mutexattr_init(&attr);
+ if (ret) return errno;
- if ((error = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT)) != 0)
- return error;
+ ret = pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
+ if (ret) return errno;
- if ((error = pthread_mutex_init(&key_lock,&attr)) != 0)
- return error;
+ ret = pthread_mutex_init(&key_lock,&attr);
+ if (ret) return errno;
return 0;
}
**/
int fosa_key_create(int *key)
{
- int i, error = 0;
+ int i, ret;
bool found = false;
- if ((error = pthread_mutex_lock(&key_lock)) != 0)
- return error;
+ ret = pthread_mutex_lock(&key_lock);
+ if (ret) return ret;
/* find an unused key */
for (i = 0; i < FOSA_MAX_KEYS; i++) {
if (!key_in_use[i]) {
+ ret = pthread_key_create(&(key_list[i]), NULL);
+ if (ret) return ret;
+
*key = i;
key_in_use[i] = true;
- error = pthread_key_create(&(key_list[i]), NULL);
found = true;
+
break;
}
}
- if ((error = pthread_mutex_unlock(&key_lock))!= 0)
- return error;
+ ret = pthread_mutex_unlock(&key_lock);
+ if (ret) return ret;
- return (!found ? FOSA_EINVAL : error);
+ return (!found ? FOSA_EINVAL : ret);
}
/**
**/
int fosa_key_destroy(int key)
{
- int error;
+ int ret;
- if ((error = pthread_mutex_lock(&key_lock)) != 0)
- return error;
+ ret = pthread_mutex_lock(&key_lock);
+ if (ret) return ret;
- if ((error = pthread_key_delete(key_list[key])) != 0)
- key_in_use[key]=false;
+ ret = pthread_key_delete(key_list[key]);
+ if (ret) return ret;
- if ((error = pthread_mutex_unlock(&key_lock)) != 0)
- return error;
+ key_in_use[key]=false;
+
+ ret = pthread_mutex_unlock(&key_lock);
+ if (ret) return ret;
return 0;
}
fosa_thread_id_t tid,
const void * value)
{
+ int ret;
+
/* only POSIX threads can have specific data */
if (!__fosa_check_thread(&tid))
- return EINVAL;
+ return FOSA_EINVAL;
- return pthread_setspecific(key_list[key], value);
+ ret = pthread_setspecific(key_list[key], value);
+
+ return ret ? ret : 0;
}
/**
if (!__fosa_check_thread(&tid))
return EINVAL;
- if ((value = pthread_getspecific(key_list[key])) != NULL)
- return EINVAL;
- else
- return 0;
+ value = pthread_getspecific(key_list[key]);
+
+ return !value ? FOSA_EINVAL : 0;
}
/******************************************************************
**/
int fosa_get_priority_max()
{
- return sched_get_priority_max(SCHED_RR);
+ int ret;
+
+ ret = sched_get_priority_max(SCHED_RR);
+
+ return ret ? errno : 0;
}
/**
**/
int fosa_get_priority_min()
{
- return sched_get_priority_min(SCHED_RR);
+ int ret;
+
+ ret = sched_get_priority_min(SCHED_RR);
+
+ return ret ? errno : 0;
}
/**
**/
int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio)
{
- int error;
+ int ret;
struct sched_param param;
param.sched_priority = prio;
- if ((error = pthread_attr_setschedpolicy(attr, SCHED_RR)) == 0)
- return error;
+ ret = pthread_attr_setschedpolicy(attr, SCHED_RR);
+ if (ret) return ret;
return pthread_attr_setschedparam(attr, ¶m);
}
**/
int fosa_thread_attr_get_prio(const fosa_thread_attr_t *attr, int *prio)
{
- int error;
+ int ret;
struct sched_param param;
- if ((error = pthread_attr_getschedparam(attr, ¶m)) == 0)
- *prio = param.sched_priority;
+ ret = pthread_attr_getschedparam(attr, ¶m);
+ if (ret) return ret;
+
+ *prio = param.sched_priority;
- return error;
+ return 0;
}
/**
**/
int fosa_thread_set_prio(fosa_thread_id_t tid, int prio)
{
+ int ret;
struct sched_param param;
param.sched_priority = prio;
- return sched_setscheduler(0, SCHED_RR, ¶m);
+
+ ret = sched_setscheduler(0, SCHED_RR, ¶m);
+
+ return ret ? errno : 0;
}
/**
ret = sched_getparam(0, ¶m);
*prio = param.sched_priority;
- return ret;
+ return ret ? errno : 0;
}
/*******************************************************************
**/
int fosa_set_accepted_signals(fosa_signal_t set[], int size)
{
- int i, error;
+ int i, ret;
fosa_thread_id_t self;
sigset_t sigset;
struct sigaction action;
- if ((error = sigemptyset(&sigset)) != 0)
- return error;
+ ret = sigemptyset(&sigset);
+ if (ret) goto err;
action.sa_handler = SIG_DFL;
action.sa_mask = sigset;
action.sa_sigaction = NULL;
for (i = 0; i < size; i++) {
- if ((error = sigaddset(&sigset, set[i])) != 0)
- return error;
- if ((error = sigaction(set[i], &action, NULL)) != 0)
- return error;
+ ret = sigaddset(&sigset, set[i]);
+ if (ret) goto err;
+ ret = sigaction(set[i], &action, NULL);
+ if (ret) goto err;
}
self = fosa_thread_self();
- if (__fosa_check_thread(&self))
- return pthread_sigmask(SIG_BLOCK, &sigset, NULL);
- else
- return sigprocmask(SIG_BLOCK, &sigset, NULL);
+ if (__fosa_check_thread(&self)) {
+ ret = pthread_sigmask(SIG_BLOCK, &sigset, NULL);
+ if (ret) return ret;
+ } else {
+ ret = sigprocmask(SIG_BLOCK, &sigset, NULL);
+ if (ret) goto err;
+ }
+
+ return 0;
+err:
+ return errno;
}
/**
fosa_signal_info_t info,
fosa_thread_id_t receiver)
{
- int error;
+ int ret;
timer_t fake_timer;
struct itimerspec fake_time;
struct sigevent fake_event;
- timer_create(CLOCK_MONOTONIC, &fake_event, &fake_timer);
+ ret = timer_create(FOSA_CLOCK_REALTIME, &fake_event, &fake_timer);
+ if (ret) goto err;
fake_time.it_value.tv_sec = fake_time.it_value.tv_nsec = 0;
fake_time.it_interval.tv_sec = fake_time.it_interval.tv_nsec = 0;
fake_event.sigev_value.sival_int = info.sival_int;
fake_event._sigev_un._tid = receiver.linux_tid;
- error = timer_settime(fake_timer, TIMER_ABSTIME, &fake_time, NULL);
- timer_delete(fake_timer);
+ ret = timer_settime(fake_timer, TIMER_ABSTIME, &fake_time, NULL);
+ if (ret) {
+ timer_delete(fake_timer);
+ goto err;
+ }
- return error;
+ ret = timer_delete(fake_timer);
+ if (ret) goto err;
+
+ return 0;
+err:
+ return errno;
}
/**
fosa_signal_t *signal_received,
fosa_signal_info_t *info)
{
- int i, error;
+ int i, ret;
sigset_t sigset;
siginfo_t siginfo;
- if ((error = sigemptyset(&sigset)) != 0)
- return error;
+ ret = sigemptyset(&sigset);
+ if (ret) goto err;
- for (i = 0; i < size; i++)
- if ((error = sigaddset(&sigset,set[i])) != 0)
- return error;
+ for (i = 0; i < size; i++) {
+ ret = sigaddset(&sigset,set[i]);
+ if (ret) goto err;
+ }
- if ((error = sigwaitinfo(&sigset, &siginfo)) != 0)
- return error;
+ ret = sigwaitinfo(&sigset, &siginfo);
+ if (ret) goto err;
if (info != NULL && signal_received != NULL)
*signal_received = siginfo.si_signo;
*info = (fosa_signal_info_t) siginfo.si_value.sival_int;
return 0;
+err:
+ return errno;
}
/**
fosa_signal_info_t *info,
const struct timespec *timeout)
{
- int i, error;
+ int i, ret;
sigset_t signalset;
siginfo_t siginfo;
- if ((error = sigemptyset(&signalset)) != 0)
- return error;
+ ret = sigemptyset(&signalset);
+ if (ret) goto err;
- for (i = 0; i < size; i++)
- if ((error = sigaddset(&signalset,set[i])) != 0)
- return error;
+ for (i = 0; i < size; i++) {
+ ret = sigaddset(&signalset,set[i]);
+ if (ret) goto err;
+ }
- if ((error = sigtimedwait(&signalset,&siginfo,timeout)) != 0)
- return error;
+ ret = sigtimedwait(&signalset,&siginfo,timeout);
+ if (ret) goto err;
if (signal_received != NULL)
*signal_received = siginfo.si_signo;
*info = (fosa_signal_info_t) siginfo.si_value.sival_int;
return 0;
+err:
+ return errno;
}