blob: 599ce6808adc66af138718b4a5044aa366d34ffa [file] [log] [blame]
Alexandre Julliardd30dfd21998-09-27 18:28:36 +00001/*
2 * Server-side semaphore management
3 *
4 * Copyright (C) 1998 Alexandre Julliard
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
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.
10 *
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.
15 *
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
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000019 */
20
Alexandre Julliard5769d1d2002-04-26 19:05:15 +000021#include "config.h"
22#include "wine/port.h"
23
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000024#include <assert.h>
25#include <stdio.h>
26#include <stdlib.h>
27
Alexandre Julliard435e2e62002-12-10 22:56:43 +000028#include "windef.h"
Alexandre Julliard43c190e1999-05-15 10:48:19 +000029
30#include "handle.h"
31#include "thread.h"
Alexandre Julliard5bc78081999-06-22 17:26:53 +000032#include "request.h"
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000033
34struct semaphore
35{
36 struct object obj; /* object header */
37 unsigned int count; /* current count */
38 unsigned int max; /* maximum possible count */
39};
40
Alexandre Julliard338e7571998-12-27 15:28:54 +000041static void semaphore_dump( struct object *obj, int verbose );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000042static int semaphore_signaled( struct object *obj, struct thread *thread );
43static int semaphore_satisfied( struct object *obj, struct thread *thread );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000044
45static const struct object_ops semaphore_ops =
46{
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000047 sizeof(struct semaphore), /* size */
48 semaphore_dump, /* dump */
49 add_queue, /* add_queue */
50 remove_queue, /* remove_queue */
51 semaphore_signaled, /* signaled */
52 semaphore_satisfied, /* satisfied */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000053 no_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000054 no_destroy /* destroy */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000055};
56
57
Alexandre Julliardd16319c1999-11-25 21:30:24 +000058static struct semaphore *create_semaphore( const WCHAR *name, size_t len,
Alexandre Julliard5bc78081999-06-22 17:26:53 +000059 unsigned int initial, unsigned int max )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000060{
61 struct semaphore *sem;
62
63 if (!max || (initial > max))
64 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000065 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000066 return NULL;
67 }
Alexandre Julliard526a28d2002-10-02 23:49:30 +000068 if ((sem = create_named_object( sync_namespace, &semaphore_ops, name, len )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000069 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000070 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
Alexandre Julliard5bc78081999-06-22 17:26:53 +000071 {
72 /* initialize it if it didn't already exist */
73 sem->count = initial;
74 sem->max = max;
75 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000076 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +000077 return sem;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000078}
79
Alexandre Julliard51885742002-05-30 20:12:58 +000080static unsigned int release_semaphore( obj_handle_t handle, unsigned int count )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000081{
82 struct semaphore *sem;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000083 unsigned int prev = 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000084
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000085 if ((sem = (struct semaphore *)get_handle_obj( current->process, handle,
86 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000087 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000088 prev = sem->count;
89 if (sem->count + count < sem->count || sem->count + count > sem->max)
90 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000091 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000092 }
93 else if (sem->count)
94 {
Alexandre Julliardd338b492003-05-02 20:18:09 +000095 /* there cannot be any thread to wake up if the count is != 0 */
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000096 sem->count += count;
97 }
98 else
99 {
100 sem->count = count;
101 wake_up( &sem->obj, count );
102 }
103 release_object( sem );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000104 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000105 return prev;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000106}
107
Alexandre Julliard338e7571998-12-27 15:28:54 +0000108static void semaphore_dump( struct object *obj, int verbose )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000109{
110 struct semaphore *sem = (struct semaphore *)obj;
111 assert( obj->ops == &semaphore_ops );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000112 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
113 dump_object_name( &sem->obj );
114 fputc( '\n', stderr );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000115}
116
117static int semaphore_signaled( struct object *obj, struct thread *thread )
118{
119 struct semaphore *sem = (struct semaphore *)obj;
120 assert( obj->ops == &semaphore_ops );
121 return (sem->count > 0);
122}
123
124static int semaphore_satisfied( struct object *obj, struct thread *thread )
125{
126 struct semaphore *sem = (struct semaphore *)obj;
127 assert( obj->ops == &semaphore_ops );
128 assert( sem->count );
129 sem->count--;
130 return 0; /* not abandoned */
131}
132
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000133/* create a semaphore */
134DECL_HANDLER(create_semaphore)
135{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000136 struct semaphore *sem;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000137
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000138 reply->handle = 0;
139 if ((sem = create_semaphore( get_req_data(), get_req_data_size(),
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000140 req->initial, req->max )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000141 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000142 reply->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000143 release_object( sem );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000144 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000145}
146
147/* open a handle to a semaphore */
148DECL_HANDLER(open_semaphore)
149{
Alexandre Julliard526a28d2002-10-02 23:49:30 +0000150 reply->handle = open_object( sync_namespace, get_req_data(), get_req_data_size(),
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000151 &semaphore_ops, req->access, req->inherit );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000152}
153
154/* release a semaphore */
155DECL_HANDLER(release_semaphore)
156{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000157 reply->prev_count = release_semaphore( req->handle, req->count );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000158}