summaryrefslogtreecommitdiffstats
path: root/c/src/exec/itron/src/cre_sem.c
blob: 6b5bd6c82b8fb3501f7a4d5d901d8adc4d0ca2e0 (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
/*
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.OARcorp.com/rtems/license.html.
 *
 *  $Id$
 */

#include <itron.h>

#include <rtems/itron/semaphore.h>
#include <rtems/itron/task.h>
#include <rtems/score/tod.h>

/*
 *  cre_sem - Create Semaphore
 *
 *  This function implements the ITRON 3.0 cre_sem() service.
 */

ER cre_sem(
  ID      semid,
  T_CSEM *pk_csem
)
{
  CORE_semaphore_Attributes   the_semaphore_attributes;
  ITRON_Semaphore_Control    *the_semaphore;

  /*
   *  Bad pointer to the attributes structure
   */

  if ( !pk_csem )
    return E_PAR;

  /*
   *  Bits were set that were note defined.
   */

  if ( pk_csem->sematr & _ITRON_SEMAPHORE_UNUSED_ATTRIBUTES )
    return E_RSATR;

  /*
   *  Initial semaphore count exceeds the maximum.
   */

  if ( pk_csem->isemcnt > pk_csem->maxsem )
    return E_PAR;

  /*
   *  This error is not in the specification but this condition
   *  does not make sense.
   */

  if ( pk_csem->maxsem == 0 )
    return E_PAR;

  _Thread_Disable_dispatch();             /* prevents deletion */

  the_semaphore = _ITRON_Semaphore_Allocate( semid );
  if ( !the_semaphore ) {
    _Thread_Enable_dispatch();
    return _ITRON_Semaphore_Clarify_allocation_id_error( semid );
  }

  if ( pk_csem->sematr & TA_TPRI )
    the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_PRIORITY;
  else
    the_semaphore_attributes.discipline = CORE_SEMAPHORE_DISCIPLINES_FIFO;

  the_semaphore_attributes.maximum_count = pk_csem->maxsem;

  _CORE_semaphore_Initialize(
    &the_semaphore->semaphore,
    OBJECTS_ITRON_SEMAPHORES,
    &the_semaphore_attributes,
    pk_csem->isemcnt,
    NULL                           /* Multiprocessing not supported */
  );

  _ITRON_Objects_Open( &_ITRON_Semaphore_Information, &the_semaphore->Object );

  /*
   *  If multiprocessing were supported, this is where we would announce
   *  the existence of the semaphore to the rest of the system.
   */

#if defined(RTEMS_MULTIPROCESSING)
#endif

  _Thread_Enable_dispatch();
  return E_OK;
}