source: rtems/cpukit/rtems/src/semdelete.c

Last change on this file was b3b6d21e, checked in by Joel Sherrill <joel@…>, on 02/16/22 at 22:28:59

cpukit/rtems/src/[s-z]*.c: Change license to BSD-2

Updates #3053.

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/* SPDX-License-Identifier: BSD-2-Clause */
2
3/**
4 * @file
5 *
6 * @ingroup RTEMSImplClassicSemaphore
7 *
8 * @brief This source file contains the implementation of
9 *   rtems_semaphore_delete().
10 */
11
12/*
13 *  COPYRIGHT (c) 1989-2014.
14 *  On-Line Applications Research Corporation (OAR).
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 * 1. Redistributions of source code must retain the above copyright
20 *    notice, this list of conditions and the following disclaimer.
21 * 2. Redistributions in binary form must reproduce the above copyright
22 *    notice, this list of conditions and the following disclaimer in the
23 *    documentation and/or other materials provided with the distribution.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifdef HAVE_CONFIG_H
39#include "config.h"
40#endif
41
42#include <rtems/rtems/semimpl.h>
43#include <rtems/rtems/statusimpl.h>
44
45rtems_status_code rtems_semaphore_delete(
46  rtems_id   id
47)
48{
49  Semaphore_Control    *the_semaphore;
50  Thread_queue_Context  queue_context;
51  uintptr_t             flags;
52  Semaphore_Variant     variant;
53  Status_Control        status;
54
55  _Objects_Allocator_lock();
56  the_semaphore = _Semaphore_Get( id, &queue_context );
57
58  if ( the_semaphore == NULL ) {
59    _Objects_Allocator_unlock();
60
61#if defined(RTEMS_MULTIPROCESSING)
62    if ( _Semaphore_MP_Is_remote( id ) ) {
63      return RTEMS_ILLEGAL_ON_REMOTE_OBJECT;
64    }
65#endif
66
67    return RTEMS_INVALID_ID;
68  }
69
70  _Thread_queue_Acquire_critical(
71    &the_semaphore->Core_control.Wait_queue,
72    &queue_context
73  );
74  flags = _Semaphore_Get_flags( the_semaphore );
75  variant = _Semaphore_Get_variant( flags );
76
77  switch ( variant ) {
78    case SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY:
79    case SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING:
80    case SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL:
81      if (
82        _CORE_mutex_Is_locked(
83          &the_semaphore->Core_control.Mutex.Recursive.Mutex
84        )
85      ) {
86        status = STATUS_RESOURCE_IN_USE;
87      } else {
88        status = STATUS_SUCCESSFUL;
89      }
90
91      break;
92#if defined(RTEMS_SMP)
93    case SEMAPHORE_VARIANT_MRSP:
94      status = _MRSP_Can_destroy( &the_semaphore->Core_control.MRSP );
95      break;
96#endif
97    default:
98      _Assert(
99        variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
100          || variant == SEMAPHORE_VARIANT_COUNTING
101      );
102      status = STATUS_SUCCESSFUL;
103      break;
104  }
105
106  if ( status != STATUS_SUCCESSFUL ) {
107    _Thread_queue_Release(
108      &the_semaphore->Core_control.Wait_queue,
109      &queue_context
110    );
111    _Objects_Allocator_unlock();
112    return _Status_Get( status );
113  }
114
115  _Objects_Close( &_Semaphore_Information, &the_semaphore->Object );
116
117  switch ( variant ) {
118#if defined(RTEMS_SMP)
119    case SEMAPHORE_VARIANT_MRSP:
120      _MRSP_Destroy( &the_semaphore->Core_control.MRSP, &queue_context );
121      break;
122#endif
123    default:
124      _Assert(
125        variant == SEMAPHORE_VARIANT_MUTEX_INHERIT_PRIORITY
126          || variant == SEMAPHORE_VARIANT_MUTEX_PRIORITY_CEILING
127          || variant == SEMAPHORE_VARIANT_MUTEX_NO_PROTOCOL
128          || variant == SEMAPHORE_VARIANT_SIMPLE_BINARY
129          || variant == SEMAPHORE_VARIANT_COUNTING
130      );
131      _Thread_queue_Flush_critical(
132        &the_semaphore->Core_control.Wait_queue.Queue,
133        _Semaphore_Get_operations( flags ),
134        _Thread_queue_Flush_status_object_was_deleted,
135        &queue_context
136      );
137      _Thread_queue_Destroy( &the_semaphore->Core_control.Wait_queue );
138      break;
139  }
140
141#if defined(RTEMS_MULTIPROCESSING)
142  if ( _Semaphore_Is_global( flags ) ) {
143
144    _Objects_MP_Close( &_Semaphore_Information, id );
145
146    _Semaphore_MP_Send_process_packet(
147      SEMAPHORE_MP_ANNOUNCE_DELETE,
148      id,
149      0,                         /* Not used */
150      0                          /* Not used */
151    );
152  }
153#endif
154
155  _Semaphore_Free( the_semaphore );
156  _Objects_Allocator_unlock();
157  return RTEMS_SUCCESSFUL;
158}
Note: See TracBrowser for help on using the repository browser.