summaryrefslogtreecommitdiff
path: root/include/rtems/score/assert.h
blob: d4253f87da6439591017cc14e2626e4e73304144 (plain)
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
/*
 * Copyright (c) 2013-2014 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <rtems@embedded-brains.de>
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#ifndef _RTEMS_SCORE_ASSERT_H
#define _RTEMS_SCORE_ASSERT_H

#include <rtems/score/basedefs.h>

#if defined( RTEMS_DEBUG )
  #include <assert.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

/**
 * @brief Assertion similar to assert() controlled via RTEMS_DEBUG instead of
 * NDEBUG.
 */
#if defined( RTEMS_DEBUG )

  /**
   * @brief Macro with method name used in assert output
   *
   * Given the variations in compilers and standards, we have to poke a bit.
   *
   * @note This is based on the code in newlib's assert.h.
   */
  #ifndef __RTEMS_ASSERT_FUNCTION
    /* Use g++'s demangled names in C++.  */
    #if defined __cplusplus && defined __GNUC__
      #define __RTEMS_ASSERT_FUNCTION __PRETTY_FUNCTION__

    /* C99 requires the use of __func__.  */
    #elif __STDC_VERSION__ >= 199901L
      #define __RTEMS_ASSERT_FUNCTION __func__

    /* Older versions of gcc don't have __func__ but can use __FUNCTION__.  */
    #elif __GNUC__ >= 2
      #define __RTEMS_ASSERT_FUNCTION __FUNCTION__

    /* failed to detect __func__ support.  */
    #else
      #define __RTEMS_ASSERT_FUNCTION ((char *) 0)
    #endif
  #endif /* !__RTEMS_ASSERT_FUNCTION */

  #if !defined( RTEMS_SCHEDSIM )
    /* normal build is newlib. */

    void __assert_func(const char *, int, const char *, const char *)
      RTEMS_NO_RETURN;

    #define _Assert( _e ) \
       ( ( _e ) ? \
         ( void ) 0 : \
           __assert_func( __FILE__, __LINE__, __RTEMS_ASSERT_FUNCTION, #_e ) )

  #elif defined(__linux__)
    /* Scheduler simulator has only beed tested on glibc. */
    #define _Assert( _e ) \
     ( ( _e ) ? \
       ( void ) 0 : \
         __assert_fail( #_e, __FILE__, __LINE__, __RTEMS_ASSERT_FUNCTION ) )
  #else
    #error "Implement RTEMS assert support for this C Library"
  #endif

#else
  #define _Assert( _e ) ( ( void ) 0 )
#endif

/**
 * @brief Like _Assert(), but only armed if RTEMS_SMP is defined.
 */
#if defined( RTEMS_SMP )
  #define _SMP_Assert( _e ) _Assert( _e )
#else
  #define _SMP_Assert( _e ) ( ( void ) 0 )
#endif

/**
 * @brief Returns true if thread dispatching is allowed.
 *
 * Thread dispatching can be repressed via _Thread_Disable_dispatch() or
 * _ISR_Disable().
 */
#if defined( RTEMS_DEBUG )
  bool _Debug_Is_thread_dispatching_allowed( void );
#endif

/**
 * @brief Returns true if the current thread of execution owns the giant lock.
 */
#if defined( RTEMS_DEBUG )
  #if defined( RTEMS_SMP )
    bool _Debug_Is_owner_of_giant( void );
  #else
    #define _Debug_Is_owner_of_giant() (true)
  #endif
#endif

/**
 * @brief Returns true if the current thread of execution owns the allocator
 * mutex.
 */
#if defined( RTEMS_DEBUG )
  bool _Debug_Is_owner_of_allocator( void );
#endif

/**
 * @brief Asserts that this point is not reached during run-time.
 */
#if RTEMS_SCHEDSIM
#define _Assert_Not_reached()
#else
#define _Assert_Not_reached() _Assert( 0 )
#endif

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* _RTEMS_SCORE_ASSERT_H */