Blame view

RIOT/sys/posix/semaphore/posix_semaphore.c 1.5 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
  /*
   * Copyright (C) 2013 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  Christian Mehlis <mehlis@inf.fu-berlin.de>
   * @author  Martine Lenders <mlenders@inf.fu-berlin.de>
   * @author  René Kijewski <kijewski@inf.fu-berlin.de>
   */
  
  #include <errno.h>
  #include <inttypes.h>
  
  #include "irq.h"
  #include "sched.h"
  #include "sema.h"
  #include "timex.h"
  #include "thread.h"
  #include "xtimer.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  #include "semaphore.h"
  
  int sem_timedwait(sem_t *sem, const struct timespec *abstime)
  {
      uint64_t timeout = (((uint64_t)abstime->tv_sec) * SEC_IN_USEC) +
                              (abstime->tv_nsec / USEC_IN_NS);
      uint64_t now = xtimer_now_usec64();
      if (now > timeout) {
          errno = ETIMEDOUT;
          return -1;
      }
      timeout = timeout - now;
      int res = sema_wait_timed((sema_t *)sem, timeout);
      if (res < 0) {
          errno = -res;
          return -1;
      }
      return 0;
  }
  
  int sem_trywait(sem_t *sem)
  {
      unsigned int old_state, value;
      int result;
      if (sem == NULL) {
          errno = EINVAL;
          return -1;
      }
      old_state = irq_disable();
      value = sem->value;
      if (value == 0) {
          errno = EAGAIN;
          result = -1;
      }
      else {
          result = 0;
          sem->value = value - 1;
      }
  
      irq_restore(old_state);
      return result;
  }
  
  /** @} */