Blame view

RIOT/core/mbox.c 3.36 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
119
120
121
122
  /*
   * Copyright (C) 2016 Kaspar Schleiser <kaspar@schleiser.de>
   *
   * 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     core_mbox
   * @{
   *
   * @file
   * @brief       mailbox implementation
   *
   * @author      Kaspar Schleiser <kaspar@schleiser.de>
   *
   * @}
   */
  
  #include <string.h>
  
  #include "mbox.h"
  #include "irq.h"
  #include "sched.h"
  #include "thread.h"
  
  #define ENABLE_DEBUG (0)
  #include "debug.h"
  
  static void _wake_waiter(thread_t *thread, unsigned irqstate)
  {
      sched_set_status(thread, STATUS_PENDING);
  
      DEBUG("mbox: Thread %"PRIkernel_pid": _wake_waiter(): waking up "
              "%"PRIkernel_pid".\n", sched_active_pid, thread->pid);
  
      uint16_t process_priority = thread->priority;
      irq_restore(irqstate);
      sched_switch(process_priority);
  }
  
  static void _wait(list_node_t *wait_list, unsigned irqstate)
  {
      DEBUG("mbox: Thread %"PRIkernel_pid" _wait(): going blocked.\n",
              sched_active_pid);
  
      thread_t *me = (thread_t*) sched_active_thread;
      sched_set_status(me, STATUS_MBOX_BLOCKED);
      thread_add_to_list(wait_list, me);
      irq_restore(irqstate);
      thread_yield();
  
      DEBUG("mbox: Thread %"PRIkernel_pid" _wait(): woke up.\n",
              sched_active_pid);
  }
  
  int _mbox_put(mbox_t *mbox, msg_t *msg, int blocking)
  {
      unsigned irqstate = irq_disable();
  
      list_node_t *next = (list_node_t*) list_remove_head(&mbox->readers);
      if (next) {
          DEBUG("mbox: Thread %"PRIkernel_pid" mbox 0x%08x: _tryput(): "
                  "there's a waiter.\n", sched_active_pid, (unsigned)mbox);
          thread_t *thread = container_of((clist_node_t*)next, thread_t, rq_entry);
          *(msg_t *)thread->wait_data = *msg;
          _wake_waiter(thread, irqstate);
          return 1;
      }
      else {
          if (cib_full(&mbox->cib)) {
              if (blocking) {
                  _wait(&mbox->writers, irqstate);
                  irqstate = irq_disable();
              }
              else {
                  irq_restore(irqstate);
                  return 0;
              }
          }
  
          DEBUG("mbox: Thread %"PRIkernel_pid" mbox 0x%08x: _tryput(): "
                  "queued message.\n", sched_active_pid, (unsigned)mbox);
          msg->sender_pid = sched_active_pid;
          /* copy msg into queue */
          mbox->msg_array[cib_put_unsafe(&mbox->cib)] = *msg;
          irq_restore(irqstate);
          return 1;
      }
  }
  
  int _mbox_get(mbox_t *mbox, msg_t *msg, int blocking)
  {
      unsigned irqstate = irq_disable();
  
      if (cib_avail(&mbox->cib)) {
          DEBUG("mbox: Thread %"PRIkernel_pid" mbox 0x%08x: _tryget(): "
                  "got queued message.\n", sched_active_pid, (unsigned)mbox);
          /* copy msg from queue */
          *msg = mbox->msg_array[cib_get_unsafe(&mbox->cib)];
          list_node_t *next = (list_node_t*) list_remove_head(&mbox->writers);
          if (next) {
              thread_t *thread = container_of((clist_node_t*)next, thread_t, rq_entry);
              _wake_waiter(thread, irqstate);
          }
          else {
              irq_restore(irqstate);
          }
          return 1;
      }
      else if (blocking) {
          sched_active_thread->wait_data = (void*)msg;
          _wait(&mbox->readers, irqstate);
          /* sender has copied message */
          return 1;
      }
      else {
          irq_restore(irqstate);
          return 0;
      }
  }