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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [exec/] [posix/] [src/] [semopen.c] - Blame information for rev 532

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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