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"
36 struct object obj
; /* object header */
37 unsigned int count
; /* current count */
38 unsigned int max
; /* maximum possible count */
41 static void semaphore_dump( struct object
*obj
, int verbose
);
42 static int semaphore_signaled( struct object
*obj
, struct thread
*thread
);
43 static int semaphore_satisfied( struct object
*obj
, struct thread
*thread
);
44 static int semaphore_signal( struct object
*obj
, unsigned int access
);
46 static const struct object_ops semaphore_ops
=
48 sizeof(struct semaphore
), /* size */
49 semaphore_dump
, /* dump */
50 add_queue
, /* add_queue */
51 remove_queue
, /* remove_queue */
52 semaphore_signaled
, /* signaled */
53 semaphore_satisfied
, /* satisfied */
54 semaphore_signal
, /* signal */
55 no_get_fd
, /* get_fd */
56 no_close_handle
, /* close_handle */
57 no_destroy
/* destroy */
61 static struct semaphore
*create_semaphore( const WCHAR
*name
, size_t len
,
62 unsigned int initial
, unsigned int max
)
64 struct semaphore
*sem
;
66 if (!max
|| (initial
> max
))
68 set_error( STATUS_INVALID_PARAMETER
);
71 if ((sem
= create_named_object( sync_namespace
, &semaphore_ops
, name
, len
)))
73 if (get_error() != STATUS_OBJECT_NAME_COLLISION
)
75 /* initialize it if it didn't already exist */
83 static int release_semaphore( struct semaphore
*sem
, unsigned int count
,
86 if (prev
) *prev
= sem
->count
;
87 if (sem
->count
+ count
< sem
->count
|| sem
->count
+ count
> sem
->max
)
89 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED
);
94 /* there cannot be any thread to wake up if the count is != 0 */
100 wake_up( &sem
->obj
, count
);
105 static void semaphore_dump( struct object
*obj
, int verbose
)
107 struct semaphore
*sem
= (struct semaphore
*)obj
;
108 assert( obj
->ops
== &semaphore_ops
);
109 fprintf( stderr
, "Semaphore count=%d max=%d ", sem
->count
, sem
->max
);
110 dump_object_name( &sem
->obj
);
111 fputc( '\n', stderr
);
114 static int semaphore_signaled( struct object
*obj
, struct thread
*thread
)
116 struct semaphore
*sem
= (struct semaphore
*)obj
;
117 assert( obj
->ops
== &semaphore_ops
);
118 return (sem
->count
> 0);
121 static int semaphore_satisfied( struct object
*obj
, struct thread
*thread
)
123 struct semaphore
*sem
= (struct semaphore
*)obj
;
124 assert( obj
->ops
== &semaphore_ops
);
125 assert( sem
->count
);
127 return 0; /* not abandoned */
130 static int semaphore_signal( struct object
*obj
, unsigned int access
)
132 struct semaphore
*sem
= (struct semaphore
*)obj
;
133 assert( obj
->ops
== &semaphore_ops
);
135 if (!(access
& SEMAPHORE_MODIFY_STATE
))
137 set_error( STATUS_ACCESS_DENIED
);
140 return release_semaphore( sem
, 1, NULL
);
143 /* create a semaphore */
144 DECL_HANDLER(create_semaphore
)
146 struct semaphore
*sem
;
149 if ((sem
= create_semaphore( get_req_data(), get_req_data_size(),
150 req
->initial
, req
->max
)))
152 reply
->handle
= alloc_handle( current
->process
, sem
, req
->access
, req
->inherit
);
153 release_object( sem
);
157 /* open a handle to a semaphore */
158 DECL_HANDLER(open_semaphore
)
160 reply
->handle
= open_object( sync_namespace
, get_req_data(), get_req_data_size(),
161 &semaphore_ops
, req
->access
, req
->inherit
);
164 /* release a semaphore */
165 DECL_HANDLER(release_semaphore
)
167 struct semaphore
*sem
;
169 if ((sem
= (struct semaphore
*)get_handle_obj( current
->process
, req
->handle
,
170 SEMAPHORE_MODIFY_STATE
, &semaphore_ops
)))
172 release_semaphore( sem
, req
->count
, &reply
->prev_count
);
173 release_object( sem
);