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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 int semaphore_signal( struct object
*obj
, unsigned int access
);
50 static const struct object_ops semaphore_ops
=
52 sizeof(struct semaphore
), /* size */
53 semaphore_dump
, /* dump */
54 add_queue
, /* add_queue */
55 remove_queue
, /* remove_queue */
56 semaphore_signaled
, /* signaled */
57 semaphore_satisfied
, /* satisfied */
58 semaphore_signal
, /* signal */
59 no_get_fd
, /* get_fd */
60 no_lookup_name
, /* lookup_name */
61 no_close_handle
, /* close_handle */
62 no_destroy
/* destroy */
66 static struct semaphore
*create_semaphore( const struct unicode_str
*name
, unsigned int attr
,
67 unsigned int initial
, unsigned int max
)
69 struct semaphore
*sem
;
71 if (!max
|| (initial
> max
))
73 set_error( STATUS_INVALID_PARAMETER
);
76 if ((sem
= create_named_object( sync_namespace
, &semaphore_ops
, name
, attr
)))
78 if (get_error() != STATUS_OBJECT_NAME_EXISTS
)
80 /* initialize it if it didn't already exist */
88 static int release_semaphore( struct semaphore
*sem
, unsigned int count
,
91 if (prev
) *prev
= sem
->count
;
92 if (sem
->count
+ count
< sem
->count
|| sem
->count
+ count
> sem
->max
)
94 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED
);
99 /* there cannot be any thread to wake up if the count is != 0 */
105 wake_up( &sem
->obj
, count
);
110 static void semaphore_dump( struct object
*obj
, int verbose
)
112 struct semaphore
*sem
= (struct semaphore
*)obj
;
113 assert( obj
->ops
== &semaphore_ops
);
114 fprintf( stderr
, "Semaphore count=%d max=%d ", sem
->count
, sem
->max
);
115 dump_object_name( &sem
->obj
);
116 fputc( '\n', stderr
);
119 static int semaphore_signaled( struct object
*obj
, struct thread
*thread
)
121 struct semaphore
*sem
= (struct semaphore
*)obj
;
122 assert( obj
->ops
== &semaphore_ops
);
123 return (sem
->count
> 0);
126 static int semaphore_satisfied( struct object
*obj
, struct thread
*thread
)
128 struct semaphore
*sem
= (struct semaphore
*)obj
;
129 assert( obj
->ops
== &semaphore_ops
);
130 assert( sem
->count
);
132 return 0; /* not abandoned */
135 static int semaphore_signal( struct object
*obj
, unsigned int access
)
137 struct semaphore
*sem
= (struct semaphore
*)obj
;
138 assert( obj
->ops
== &semaphore_ops
);
140 if (!(access
& SEMAPHORE_MODIFY_STATE
))
142 set_error( STATUS_ACCESS_DENIED
);
145 return release_semaphore( sem
, 1, NULL
);
148 /* create a semaphore */
149 DECL_HANDLER(create_semaphore
)
151 struct semaphore
*sem
;
152 struct unicode_str name
;
155 get_req_unicode_str( &name
);
156 if ((sem
= create_semaphore( &name
, req
->attributes
, req
->initial
, req
->max
)))
158 reply
->handle
= alloc_handle( current
->process
, sem
, req
->access
,
159 req
->attributes
& OBJ_INHERIT
);
160 release_object( sem
);
164 /* open a handle to a semaphore */
165 DECL_HANDLER(open_semaphore
)
167 struct unicode_str name
;
169 get_req_unicode_str( &name
);
170 reply
->handle
= open_object( sync_namespace
, &name
, &semaphore_ops
, req
->access
, req
->attributes
);
173 /* release a semaphore */
174 DECL_HANDLER(release_semaphore
)
176 struct semaphore
*sem
;
178 if ((sem
= (struct semaphore
*)get_handle_obj( current
->process
, req
->handle
,
179 SEMAPHORE_MODIFY_STATE
, &semaphore_ops
)))
181 release_semaphore( sem
, req
->count
, &reply
->prev_count
);
182 release_object( sem
);