Blame view

RIOT/sys/sema/sema.c 2.3 KB
a752c7ab   elopes   add first test an...
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
  /*
   * Copyright (C) 2016 TriaGnoSys GmbH
   *               2013-15 Freie Universität Berlin
   *
   * 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.
   */
  
  /**
   * @{
   *
   * @file
   *
   * @author  Víctor Ariño <victor.arino@zii.aero>
   */
  
  #include <errno.h>
  #include "irq.h"
  #include "assert.h"
  #include "sema.h"
  #include "xtimer.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  void sema_create(sema_t *sema, unsigned int value)
  {
      assert(sema != NULL);
  
      sema->value = value;
      sema->state = SEMA_OK;
      mutex_init(&sema->mutex);
      if (value == 0) {
          mutex_lock(&sema->mutex);
      }
  }
  
  void sema_destroy(sema_t *sema)
  {
      assert(sema != NULL);
  
      sema->state = SEMA_DESTROY;
      mutex_unlock(&sema->mutex);
  }
  
  int _sema_wait(sema_t *sema, int block, uint64_t us)
  {
      assert(sema != NULL);
  
      if (sema->state != SEMA_OK) {
          return -ECANCELED;
      }
  
      int did_block = block;
      unsigned old = irq_disable();
      while ((sema->value == 0) && block) {
          irq_restore(old);
          if (us == 0) {
              mutex_lock(&sema->mutex);
          }
          else {
              uint64_t start = xtimer_now_usec64();
              block = !xtimer_mutex_lock_timeout(&sema->mutex, us);
              uint64_t elapsed = xtimer_now_usec64() - start;
  
              if (elapsed < us) {
                  us -= elapsed;
              }
              else {
                  block = 0;
              }
          }
  
          if (sema->state != SEMA_OK) {
              mutex_unlock(&sema->mutex);
              return -ECANCELED;
          }
  
          old = irq_disable();
      }
  
      if (sema->value == 0) {
          irq_restore(old);
          return (did_block) ? -ETIMEDOUT : -EAGAIN;
      }
  
      unsigned int value = --sema->value;
      irq_restore(old);
  
      /* only unlock mutex if it was a blocking operation */
      if (did_block && value > 0) {
          mutex_unlock(&sema->mutex);
      }
  
      return 0;
  }
  
  int sema_post(sema_t *sema)
  {
      assert(sema != NULL);
  
      unsigned old = irq_disable();
      if (sema->value == UINT_MAX) {
          irq_restore(old);
          return -EOVERFLOW;
      }
  
      unsigned value = sema->value++;
      irq_restore(old);
  
      if (value == 0) {
          mutex_unlock(&sema->mutex);
      }
  
      return 0;
  }
  /** @} */