PX4 Firmware
PX4 Autopilot Software http://px4.io
thread.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #pragma once
6 
8 
9 #if UAVCAN_STM32_NUTTX
10 # include <nuttx/config.h>
11 # include <nuttx/fs/fs.h>
12 # include <poll.h>
13 # include <errno.h>
14 # include <cstdio>
15 # include <ctime>
16 # include <cstring>
17 #else
18 # error "Unknown OS"
19 #endif
20 
21 #include <uavcan/uavcan.hpp>
22 
23 namespace uavcan_stm32
24 {
25 
26 class CanDriver;
27 
28 #if UAVCAN_STM32_NUTTX
29 
30 /**
31  * All bus events are reported as POLLIN.
32  */
33 class BusEvent : uavcan::Noncopyable
34 {
35  using SignalCallbackHandler = void(*)();
36 
37  SignalCallbackHandler signal_cb_{nullptr};
38  sem_t sem_;
39 
40 public:
41  BusEvent(CanDriver &can_driver);
42  ~BusEvent();
43 
44  void registerSignalCallback(SignalCallbackHandler handler) { signal_cb_ = handler; }
45 
46  bool wait(uavcan::MonotonicDuration duration);
47 
48  void signalFromInterrupt();
49 };
50 
51 class Mutex
52 {
53  pthread_mutex_t mutex_;
54 
55 public:
56  Mutex()
57  {
58  init();
59  }
60 
61  int init()
62  {
63  return pthread_mutex_init(&mutex_, UAVCAN_NULLPTR);
64  }
65 
66  int deinit()
67  {
68  return pthread_mutex_destroy(&mutex_);
69  }
70 
71  void lock()
72  {
73  (void)pthread_mutex_lock(&mutex_);
74  }
75 
76  void unlock()
77  {
78  (void)pthread_mutex_unlock(&mutex_);
79  }
80 };
81 #endif
82 
83 
85 {
86  Mutex &mutex_;
87 
88 public:
89  MutexLocker(Mutex &mutex)
90  : mutex_(mutex)
91  {
92  mutex_.lock();
93  }
95  {
96  mutex_.unlock();
97  }
98 };
99 
100 }
void init()
Activates/configures the hardware registers.
MutexLocker(Mutex &mutex)
Definition: thread.hpp:89