Blame view

RIOT/sys/posix/pthread/pthread_cond.c 4.31 KB
fb11e647   vrobic   reseau statique a...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
  /*
   * Copyright (C) 2014 Hamburg University of Applied Sciences (HAW)
   *
   * This file is subject to the terms and conditions of the GNU Lesser
   * General Public License v2.1. See the file LICENSE in the top level
   * directory for more details.
   */
  
  /**
   * @ingroup     sys
   * @{
   *
   * @file
   * @brief       Condition variable implementation
   *
   * @author      Martin Landsmann <martin.landsmann@haw-hamburg.de>
   * @author      René Kijewski <rene.kijewski@fu-berlin.de>
   *
   * @}
   */
  
  #include "pthread_cond.h"
  #include "thread.h"
  #include "xtimer.h"
  #include "sched.h"
  #include "irq.h"
  #include "debug.h"
  
  int pthread_cond_condattr_destroy(pthread_condattr_t *attr)
  {
      if (attr != NULL) {
          DEBUG("pthread_cond_condattr_destroy: currently attributes are not supported.\n");
      }
  
      return 0;
  }
  
  int pthread_cond_condattr_init(pthread_condattr_t *attr)
  {
      if (attr != NULL) {
          DEBUG("pthread_cond_condattr_init: currently attributes are not supported.\n");
      }
  
      return 0;
  }
  
  int pthread_condattr_getpshared(const pthread_condattr_t *attr, int *pshared)
  {
      (void)attr;
      (void)pshared;
  
      return 0;
  }
  
  int pthread_condattr_setpshared(pthread_condattr_t *attr, int pshared)
  {
      (void)attr;
      (void)pshared;
  
      return 0;
  }
  
  int pthread_condattr_getclock(const pthread_condattr_t *attr, clockid_t *clock_id)
  {
      (void)attr;
      (void)clock_id;
  
      return 0;
  }
  
  int pthread_condattr_setclock(pthread_condattr_t *attr, clockid_t clock_id)
  {
      (void)attr;
      (void)clock_id;
  
      return 0;
  }
  
  int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *attr)
  {
      if (attr != NULL) {
          DEBUG("pthread_cond_init: currently attributes are not supported.\n");
      }
  
      cond->queue.first = NULL;
      return 0;
  }
  
  int pthread_cond_destroy(pthread_cond_t *cond)
  {
      pthread_cond_init(cond, NULL);
      return 0;
  }
  
  int pthread_cond_wait(pthread_cond_t *cond, mutex_t *mutex)
  {
      priority_queue_node_t n;
      n.priority = sched_active_thread->priority;
      n.data = sched_active_pid;
      n.next = NULL;
  
      /* the signaling thread may not hold the mutex, the queue is not thread safe */
      unsigned old_state = irq_disable();
      priority_queue_add(&(cond->queue), &n);
      irq_restore(old_state);
  
      mutex_unlock_and_sleep(mutex);
  
      if (n.data != -1u) {
          /* on signaling n.data is set to -1u */
          /* if it isn't set, then the wakeup is either spurious or a timer wakeup */
          old_state = irq_disable();
          priority_queue_remove(&(cond->queue), &n);
          irq_restore(old_state);
      }
  
      mutex_lock(mutex);
      return 0;
  }
  
  int pthread_cond_timedwait(pthread_cond_t *cond, mutex_t *mutex, const struct timespec *abstime)
  {
      timex_t now, then, reltime;
  
      xtimer_now_timex(&now);
      then.seconds = abstime->tv_sec;
      then.microseconds = abstime->tv_nsec / 1000u;
      reltime = timex_sub(then, now);
  
      xtimer_t timer;
      xtimer_set_wakeup64(&timer, timex_uint64(reltime) , sched_active_pid);
      int result = pthread_cond_wait(cond, mutex);
      xtimer_remove(&timer);
  
      return result;
  }
  
  int pthread_cond_signal(pthread_cond_t *cond)
  {
      unsigned old_state = irq_disable();
  
      priority_queue_node_t *head = priority_queue_remove_head(&(cond->queue));
      int other_prio = -1;
      if (head != NULL) {
          thread_t *other_thread = (thread_t *) sched_threads[head->data];
          if (other_thread) {
              other_prio = other_thread->priority;
              sched_set_status(other_thread, STATUS_PENDING);
          }
          head->data = -1u;
      }
  
      irq_restore(old_state);
  
      if (other_prio >= 0) {
          sched_switch(other_prio);
      }
  
      return 0;
  }
  
  static int max_prio(int a, int b)
  {
      return (a < 0) ? b : ((a < b) ? a : b);
  }
  
  int pthread_cond_broadcast(pthread_cond_t *cond)
  {
      unsigned old_state = irq_disable();
  
      int other_prio = -1;
  
      while (1) {
          priority_queue_node_t *head = priority_queue_remove_head(&(cond->queue));
          if (head == NULL) {
              break;
          }
  
          thread_t *other_thread = (thread_t *) sched_threads[head->data];
          if (other_thread) {
              other_prio = max_prio(other_prio, other_thread->priority);
              sched_set_status(other_thread, STATUS_PENDING);
          }
          head->data = -1u;
      }
  
      irq_restore(old_state);
  
      if (other_prio >= 0) {
          sched_switch(other_prio);
      }
  
      return 0;
  }