source: rtems/cpukit/rtems/src/semdelete.c @ 3ca6e618

5
Last change on this file since 3ca6e618 was 3ca6e618, checked in by Sebastian Huber <sebastian.huber@…>, on 05/25/16 at 14:49:53

rtems: Simplify rtems_semaphore_delete()

  • Property mode set to 100644
File size: 3.0 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief RTEMS Delete Semaphore
5 *  @ingroup ClassicSem
6 */
7
8/*
9 *  COPYRIGHT (c) 1989-2014.
10 *  On-Line Applications Research Corporation (OAR).
11 *
12 *  The license and distribution terms for this file may be
13 *  found in the file LICENSE in this distribution or at
14 *  http://www.rtems.org/license/LICENSE.
15 */
16
17#if HAVE_CONFIG_H
18#include "config.h"
19#endif
20
21#include <rtems/rtems/semimpl.h>
22#include <rtems/rtems/attrimpl.h>
23#include <rtems/rtems/statusimpl.h>
24
25rtems_status_code rtems_semaphore_delete(
26  rtems_id   id
27)
28{
29  Semaphore_Control    *the_semaphore;
30  Thread_queue_Context  queue_context;
31  rtems_attribute       attribute_set;
32  Status_Control        status;
33
34  _Objects_Allocator_lock();
35  the_semaphore = _Semaphore_Get( id, &queue_context );
36
37  if ( the_semaphore == NULL ) {
38    _Objects_Allocator_unlock();
39
40#if defined(RTEMS_MULTIPROCESSING)
41    if ( _Semaphore_MP_Is_remote( id ) ) {
42      return RTEMS_ILLEGAL_ON_REMOTE_OBJECT;
43    }
44#endif
45
46    return RTEMS_INVALID_ID;
47  }
48
49  attribute_set = the_semaphore->attribute_set;
50
51  _Thread_queue_Acquire_critical(
52    &the_semaphore->Core_control.Wait_queue,
53    &queue_context.Lock_context
54  );
55
56#if defined(RTEMS_SMP)
57  if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) ) {
58    status = _MRSP_Can_destroy( &the_semaphore->Core_control.mrsp );
59  } else
60#endif
61  if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) {
62    if (
63      _CORE_mutex_Is_locked( &the_semaphore->Core_control.mutex )
64        && !_Attributes_Is_simple_binary_semaphore( attribute_set )
65    ) {
66      status = STATUS_RESOURCE_IN_USE;
67    } else {
68      status = STATUS_SUCCESSFUL;
69    }
70  } else {
71    status = STATUS_SUCCESSFUL;
72  }
73
74  if ( status != STATUS_SUCCESSFUL ) {
75    _Thread_queue_Release(
76      &the_semaphore->Core_control.Wait_queue,
77      &queue_context.Lock_context
78    );
79    _Objects_Allocator_unlock();
80    return _Status_Get( status );
81  }
82
83  _Objects_Close( &_Semaphore_Information, &the_semaphore->Object );
84
85#if defined(RTEMS_SMP)
86  if ( _Attributes_Is_multiprocessor_resource_sharing( attribute_set ) ) {
87    _MRSP_Destroy( &the_semaphore->Core_control.mrsp, &queue_context );
88  } else
89#endif
90  if ( !_Attributes_Is_counting_semaphore( attribute_set ) ) {
91    _CORE_mutex_Flush(
92      &the_semaphore->Core_control.mutex,
93      _Thread_queue_Flush_status_object_was_deleted,
94      &queue_context
95    );
96    _CORE_mutex_Destroy( &the_semaphore->Core_control.mutex );
97  } else {
98    _CORE_semaphore_Destroy(
99      &the_semaphore->Core_control.semaphore,
100      &queue_context
101    );
102  }
103
104#if defined(RTEMS_MULTIPROCESSING)
105  if ( _Attributes_Is_global( attribute_set ) ) {
106
107    _Objects_MP_Close( &_Semaphore_Information, id );
108
109    _Semaphore_MP_Send_process_packet(
110      SEMAPHORE_MP_ANNOUNCE_DELETE,
111      id,
112      0,                         /* Not used */
113      0                          /* Not used */
114    );
115  }
116#endif
117
118  _Semaphore_Free( the_semaphore );
119  _Objects_Allocator_unlock();
120  return RTEMS_SUCCESSFUL;
121}
Note: See TracBrowser for help on using the repository browser.