]> rtime.felk.cvut.cz Git - frescor/fosa.git/blobdiff - src_aquosa/fosa_threads_and_signals.c
Bugfixes and restyling.
[frescor/fosa.git] / src_aquosa / fosa_threads_and_signals.c
index 1cb47959fffe0fa3322250bc5460e10fef904faa..3f64a674dfd2c883ba99b58e0ec20e7b550bcbea 100644 (file)
@@ -135,6 +135,14 @@ fosa_thread_id_t fosa_thread_self()
  * Thread attributes
  *************************/
 
+static inline int __fosa_check_thread(const fosa_thread_id_t *tid)
+{
+       if (tid->linux_pid == tid->linux_tid)
+               return 0;
+
+       return 1;
+}
+
 /**
  * fosa_thread_attr_init()
  *
@@ -149,13 +157,6 @@ fosa_thread_id_t fosa_thread_self()
  **/
 int fosa_thread_attr_init(fosa_thread_attr_t *attr)
 {
-       fosa_thread_id_t self;
-
-       /* only POSIX threads have attributes */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
-
        return pthread_attr_init(attr);
 }
 
@@ -171,13 +172,6 @@ int fosa_thread_attr_init(fosa_thread_attr_t *attr)
  **/
 int fosa_thread_attr_destroy(fosa_thread_attr_t *attr)
 {
-       fosa_thread_id_t self;
-
-       /* only POSIX threads can have attributes */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
-
        return pthread_attr_destroy(attr);
 }
 
@@ -199,13 +193,6 @@ int fosa_thread_attr_destroy(fosa_thread_attr_t *attr)
 int fosa_thread_attr_set_stacksize(fosa_thread_attr_t *attr,
                                   size_t stacksize)
 {
-       fosa_thread_id_t self;
-
-       /* only POSIX threads can set the size of the stack */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
-
        return pthread_attr_setstacksize(attr, stacksize);
 }
 
@@ -222,13 +209,6 @@ int fosa_thread_attr_set_stacksize(fosa_thread_attr_t *attr,
 int fosa_thread_attr_get_stacksize(const fosa_thread_attr_t *attr,
                                   size_t *stacksize)
 {
-       fosa_thread_id_t self;
-
-       /* only POSIX threads can set the size of the stack */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
-
        return pthread_attr_getstacksize(attr, stacksize);
 }
 
@@ -301,12 +281,6 @@ int fosa_key_create(int *key)
 {
        int i, error = 0;
        bool found = false;
-       fosa_thread_id_t self;
-
-       /* only POSIX threads can have specific data */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
 
        if ((error = pthread_mutex_lock(&key_lock)) != 0)
                return error;
@@ -341,12 +315,6 @@ int fosa_key_create(int *key)
 int fosa_key_destroy(int key)
 {
        int error;
-       fosa_thread_id_t self;
-
-       /* only POSIX threads can have specific data */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
 
        if ((error = pthread_mutex_lock(&key_lock)) != 0)
                return error;
@@ -379,11 +347,8 @@ int fosa_key_destroy(int key)
                                   fosa_thread_id_t tid,
                                   const void * value)
 {
-       fosa_thread_id_t self;
-
        /* only POSIX threads can have specific data */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
+       if (!__fosa_check_thread(&tid))
                return EINVAL;
 
        return pthread_setspecific(key_list[key], value);
@@ -407,11 +372,8 @@ int fosa_thread_get_specific_data(int key,
                                  fosa_thread_id_t tid,
                                  void ** value)
 {
-       fosa_thread_id_t self;
-
        /* only POSIX threads can have specific data */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
+       if (!__fosa_check_thread(&tid))
                return EINVAL;
 
        if ((value = pthread_getspecific(key_list[key])) != NULL)
@@ -470,14 +432,8 @@ int fosa_get_priority_min()
 int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio)
 {
        int error;
-       fosa_thread_id_t self;
        struct sched_param param;
 
-       /* normal UNIX processes have no attributes */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
-       
        param.sched_priority = prio;
        if ((error = pthread_attr_setschedpolicy(attr, SCHED_RR)) == 0)
                return error;
@@ -498,14 +454,8 @@ int fosa_thread_attr_set_prio(fosa_thread_attr_t *attr, int prio)
 int fosa_thread_attr_get_prio(const fosa_thread_attr_t *attr, int *prio)
 {
        int error;
-       fosa_thread_id_t self;
        struct sched_param param;
 
-       /* normal UNIX processes have no attributes */
-       self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
-               return EINVAL;
-
        if ((error = pthread_attr_getschedparam(attr, &param)) == 0)
                *prio = param.sched_priority;
 
@@ -529,8 +479,7 @@ int fosa_thread_set_prio(fosa_thread_id_t tid, int prio)
 {
        struct sched_param param;
 
-       param.sched_priority=prio;
-
+       param.sched_priority = prio;
        return sched_setscheduler(0, SCHED_RR, &param);
 }
 
@@ -547,12 +496,12 @@ int fosa_thread_set_prio(fosa_thread_id_t tid, int prio)
 int fosa_thread_get_prio(fosa_thread_id_t tid, int *prio)
 {
        struct sched_param param;
-       int error;
+       int ret;
 
-       error = sched_getparam(0, &param);
+       ret = sched_getparam(0, &param);
        *prio = param.sched_priority;
 
-       return error;
+       return ret;
 }
 
 /*******************************************************************
@@ -609,7 +558,7 @@ int fosa_set_accepted_signals(fosa_signal_t set[], int size)
        }
 
        self = fosa_thread_self();
-       if (self.linux_pid == self.linux_tid)
+       if (__fosa_check_thread(&self))
                return pthread_sigmask(SIG_BLOCK, &sigset, NULL);
        else
                return sigprocmask(SIG_BLOCK, &sigset, NULL);