2 * Server-side semaphore management
4 * Copyright (C) 1998 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "wine/port.h"
30 #define WIN32_NO_STATUS
40 struct object obj
; /* object header */
41 unsigned int count
; /* current count */
42 unsigned int max
; /* maximum possible count */
45 static void semaphore_dump( struct object
*obj
, int verbose
);
46 static int semaphore_signaled( struct object
*obj
, struct thread
*thread
);
47 static int semaphore_satisfied( struct object
*obj
, struct thread
*thread
);
48 static unsigned int semaphore_map_access( struct object
*obj
, unsigned int access
);
49 static int semaphore_signal( struct object
*obj
, unsigned int access
);
51 static const struct object_ops semaphore_ops
=
53 sizeof(struct semaphore
), /* size */
54 semaphore_dump
, /* dump */
55 add_queue
, /* add_queue */
56 remove_queue
, /* remove_queue */
57 semaphore_signaled
, /* signaled */
58 semaphore_satisfied
, /* satisfied */
59 semaphore_signal
, /* signal */
60 no_get_fd
, /* get_fd */
61 semaphore_map_access
, /* map_access */
62 no_lookup_name
, /* lookup_name */
63 no_open_file
, /* open_file */
64 no_close_handle
, /* close_handle */
65 no_destroy
/* destroy */
69 static struct semaphore
*create_semaphore( struct directory
*root
, const struct unicode_str
*name
,
70 unsigned int attr
, unsigned int initial
, unsigned int max
)
72 struct semaphore
*sem
;
74 if (!max
|| (initial
> max
))
76 set_error( STATUS_INVALID_PARAMETER
);
79 if ((sem
= create_named_object_dir( root
, name
, attr
, &semaphore_ops
)))
81 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
83 /* initialize it if it didn't already exist */
91 static int release_semaphore( struct semaphore
*sem
, unsigned int count
,
94 if (prev
) *prev
= sem
->count
;
95 if (sem
->count
+ count
< sem
->count
|| sem
->count
+ count
> sem
->max
)
97 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED
);
102 /* there cannot be any thread to wake up if the count is != 0 */
108 wake_up( &sem
->obj
, count
);
113 static void semaphore_dump( struct object
*obj
, int verbose
)
115 struct semaphore
*sem
= (struct semaphore
*)obj
;
116 assert( obj
->ops
== &semaphore_ops
);
117 fprintf( stderr
, "Semaphore count=%d max=%d ", sem
->count
, sem
->max
);
118 dump_object_name( &sem
->obj
);
119 fputc( '\n', stderr
);
122 static int semaphore_signaled( struct object
*obj
, struct thread
*thread
)
124 struct semaphore
*sem
= (struct semaphore
*)obj
;
125 assert( obj
->ops
== &semaphore_ops
);
126 return (sem
->count
> 0);
129 static int semaphore_satisfied( struct object
*obj
, struct thread
*thread
)
131 struct semaphore
*sem
= (struct semaphore
*)obj
;
132 assert( obj
->ops
== &semaphore_ops
);
133 assert( sem
->count
);
135 return 0; /* not abandoned */
138 static unsigned int semaphore_map_access( struct object
*obj
, unsigned int access
)
140 if (access
& GENERIC_READ
) access
|= STANDARD_RIGHTS_READ
| SYNCHRONIZE
;
141 if (access
& GENERIC_WRITE
) access
|= STANDARD_RIGHTS_WRITE
| SEMAPHORE_MODIFY_STATE
;
142 if (access
& GENERIC_EXECUTE
) access
|= STANDARD_RIGHTS_EXECUTE
;
143 if (access
& GENERIC_ALL
) access
|= STANDARD_RIGHTS_ALL
| SEMAPHORE_ALL_ACCESS
;
144 return access
& ~(GENERIC_READ
| GENERIC_WRITE
| GENERIC_EXECUTE
| GENERIC_ALL
);
147 static int semaphore_signal( struct object
*obj
, unsigned int access
)
149 struct semaphore
*sem
= (struct semaphore
*)obj
;
150 assert( obj
->ops
== &semaphore_ops
);
152 if (!(access
& SEMAPHORE_MODIFY_STATE
))
154 set_error( STATUS_ACCESS_DENIED
);
157 return release_semaphore( sem
, 1, NULL
);
160 /* create a semaphore */
161 DECL_HANDLER(create_semaphore
)
163 struct semaphore
*sem
;
164 struct unicode_str name
;
165 struct directory
*root
= NULL
;
168 get_req_unicode_str( &name
);
169 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
172 if ((sem
= create_semaphore( root
, &name
, req
->attributes
, req
->initial
, req
->max
)))
174 reply
->handle
= alloc_handle( current
->process
, sem
, req
->access
, req
->attributes
);
175 release_object( sem
);
178 if (root
) release_object( root
);
181 /* open a handle to a semaphore */
182 DECL_HANDLER(open_semaphore
)
184 struct unicode_str name
;
185 struct directory
*root
= NULL
;
186 struct semaphore
*sem
;
188 get_req_unicode_str( &name
);
189 if (req
->rootdir
&& !(root
= get_directory_obj( current
->process
, req
->rootdir
, 0 )))
192 if ((sem
= open_object_dir( root
, &name
, req
->attributes
, &semaphore_ops
)))
194 reply
->handle
= alloc_handle( current
->process
, &sem
->obj
, req
->access
, req
->attributes
);
195 release_object( sem
);
198 if (root
) release_object( root
);
201 /* release a semaphore */
202 DECL_HANDLER(release_semaphore
)
204 struct semaphore
*sem
;
206 if ((sem
= (struct semaphore
*)get_handle_obj( current
->process
, req
->handle
,
207 SEMAPHORE_MODIFY_STATE
, &semaphore_ops
)))
209 release_semaphore( sem
, req
->count
, &reply
->prev_count
);
210 release_object( sem
);