OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [cpukit/] [posix/] [src/] [semopen.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1026 ivang
/*
2
 *  semopen.c,v 1.8 2002/01/04 18:28:24 joel Exp
3
 */
4
 
5
#if HAVE_CONFIG_H
6
#include "config.h"
7
#endif
8
 
9
#include <stdarg.h>
10
 
11
#include <errno.h>
12
#include <fcntl.h>
13
#include <pthread.h>
14
#include <semaphore.h>
15
#include <limits.h>
16
 
17
#include <rtems/system.h>
18
#include <rtems/score/object.h>
19
#include <rtems/posix/semaphore.h>
20
#include <rtems/posix/time.h>
21
#include <rtems/seterr.h>
22
 
23
/*PAGE
24
 *
25
 *  sem_open
26
 *
27
 *  Opens a named semaphore.  Used in conjunction with the sem_close
28
 *  and sem_unlink commands.
29
 *
30
 *  11.2.3 Initialize/Open a Named Semaphore, P1003.1b-1993, p.221
31
 *
32
 *  NOTE: When oflag is O_CREAT, then optional third and fourth
33
 *        parameters must be present.
34
 */
35
 
36
sem_t *sem_open(
37
  const char *name,
38
  int         oflag,
39
  ...
40
  /* mode_t mode, */
41
  /* unsigned int value */
42
)
43
{
44
  va_list                    arg;
45
  mode_t                     mode;
46
  unsigned int               value = 0;
47
  int                        status;
48
  sem_t                                 the_semaphore_id;
49
  POSIX_Semaphore_Control   *the_semaphore;
50
  Objects_Locations          location;
51
 
52
  _Thread_Disable_dispatch();
53
 
54
  if ( oflag & O_CREAT ) {
55
    va_start(arg, oflag);
56
    mode = (mode_t) va_arg( arg, unsigned int );
57
    value = va_arg( arg, unsigned int );
58
    va_end(arg);
59
  }
60
 
61
  status = _POSIX_Semaphore_Name_to_id( name, &the_semaphore_id );
62
 
63
  /*
64
   *  If the name to id translation worked, then the semaphore exists
65
   *  and we can just return a pointer to the id.  Otherwise we may
66
   *  need to check to see if this is a "semaphore does not exist"
67
   *  or some other miscellaneous error on the name.
68
   */
69
 
70
  if ( status ) {
71
 
72
    /*
73
     * Unless provided a valid name that did not already exist
74
     * and we are willing to create then it is an error.
75
     */
76
 
77
    if ( !( status == ENOENT && (oflag & O_CREAT) ) ) {
78
      _Thread_Enable_dispatch();
79
      rtems_set_errno_and_return_minus_one_cast( status, sem_t * );
80
    }
81
  } else {
82
 
83
    /*
84
     * Check for existence with creation.
85
     */
86
 
87
    if ( (oflag & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL) ) {
88
      _Thread_Enable_dispatch();
89
      rtems_set_errno_and_return_minus_one_cast( EEXIST, sem_t * );
90
    }
91
 
92
    the_semaphore = _POSIX_Semaphore_Get( &the_semaphore_id, &location );
93
    the_semaphore->open_count += 1;
94
    _Thread_Enable_dispatch();
95
    _Thread_Enable_dispatch();
96
    return (sem_t *)&the_semaphore->Object.id;
97
 
98
  }
99
 
100
  /*
101
   *  At this point, the semaphore does not exist and everything has been
102
   *  checked. We should go ahead and create a semaphore.
103
   */
104
 
105
  status =_POSIX_Semaphore_Create_support(
106
    name,
107
    FALSE,         /* not shared across processes */
108
    value,
109
    &the_semaphore
110
  );
111
 
112
  /*
113
   * errno was set by Create_support, so don't set it again.
114
   */
115
 
116
  _Thread_Enable_dispatch();
117
 
118
  if ( status == -1 )
119
    return SEM_FAILED;
120
 
121
  return (sem_t *) &the_semaphore->Object.id;
122
}
123
 
124
 

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.