summaryrefslogtreecommitdiffstats
path: root/cpukit/posix/include/rtems/posix/posixapi.h
blob: b35874d8eaa4fac37203d3866613b9acd92fc568 (plain) (blame)
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
/**
 * @file
 *
 * @brief POSIX API Implementation
 *
 * This include file defines the top level interface to the POSIX API
 * implementation in RTEMS.
 */

/*
 *  COPYRIGHT (c) 1989-2011.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  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_POSIX_POSIXAPI_H
#define _RTEMS_POSIX_POSIXAPI_H

#include <rtems/config.h>
#include <rtems/score/assert.h>
#include <rtems/score/apimutex.h>
#include <rtems/score/objectimpl.h>
#include <rtems/score/threadimpl.h>
#include <rtems/seterr.h>

/**
 * @defgroup POSIXAPI RTEMS POSIX API
 *
 * RTEMS POSIX API definitions and modules.
 *
 */
/**@{**/

/**
 * @brief POSIX API Fatal domains.
 */
typedef enum {
  POSIX_FD_PTHREAD,      /**< A pthread thread error. */
  POSIX_FD_PTHREAD_ONCE  /**< A pthread once error. */
} POSIX_Fatal_domain;

/**
 * @brief POSIX API Fatal error.
 *
 * A common method of rasing a POSIX API fatal error.
 *
 * @param[in] domain The POSIX error domain.
 * @param[in] eno The error number as defined in errno.h.
 */
void _POSIX_Fatal_error( POSIX_Fatal_domain domain, int eno );

extern const int _POSIX_Get_by_name_error_table[ 3 ];

RTEMS_INLINE_ROUTINE int _POSIX_Get_by_name_error(
  Objects_Get_by_name_error error
)
{
  _Assert( (size_t) error < RTEMS_ARRAY_SIZE( _POSIX_Get_by_name_error_table ) );
  return _POSIX_Get_by_name_error_table[ error ];
}

RTEMS_INLINE_ROUTINE int _POSIX_Get_error( Status_Control status )
{
  return STATUS_GET_POSIX( status );
}

RTEMS_INLINE_ROUTINE int _POSIX_Get_error_after_wait(
  const Thread_Control *executing
)
{
  return _POSIX_Get_error( _Thread_Wait_get_status( executing ) );
}

RTEMS_INLINE_ROUTINE int _POSIX_Zero_or_minus_one_plus_errno(
  Status_Control status
)
{
  if ( status == STATUS_SUCCESSFUL ) {
    return 0;
  }

  rtems_set_errno_and_return_minus_one( _POSIX_Get_error( status ) );
}

/**
 * @brief Macro to generate a function body to get a POSIX object by
 * identifier.
 *
 * Generates a function body to get the object for the specified identifier.
 * Performs automatic initialization if requested and necessary.  This is an
 * ugly macro, since C lacks support for templates.
 */
#define _POSIX_Get_object_body( \
  type, \
  id, \
  queue_context, \
  info, \
  initializer, \
  init \
) \
  Objects_Control *the_object; \
  if ( id == NULL ) { \
    return NULL; \
  } \
  _Thread_queue_Initialize( queue_context ); \
  the_object = _Objects_Get( \
    (Objects_Id) *id, \
    &queue_context->Lock_context.Lock_context, \
    info \
  ); \
  if ( the_object == NULL ) { \
    _Once_Lock(); \
    if ( *id == initializer ) { \
      init( id, NULL ); \
    } \
    _Once_Unlock(); \
    the_object = _Objects_Get( \
      (Objects_Id) *id, \
      &queue_context->Lock_context.Lock_context, \
      info \
    ); \
  } \
  return (type *) the_object

/** @} */

#endif
/* end of include file */