blob: bd1579ae3c7bd4af3c0ffd8440ee7818583ee0ae [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 Julliardd30dfd21998-09-27 18:28:36 +000028#include "winnt.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 */
53 NULL, /* get_poll_events */
54 NULL, /* poll_event */
Alexandre Julliard1ab243b2000-12-19 02:12:45 +000055 no_get_fd, /* get_fd */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000056 no_flush, /* flush */
57 no_get_file_info, /* get_file_info */
Mike McCormack6f011c02001-12-20 00:07:05 +000058 NULL, /* queue_async */
Alexandre Julliard1dca5e22000-01-01 00:56:27 +000059 no_destroy /* destroy */
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000060};
61
62
Alexandre Julliardd16319c1999-11-25 21:30:24 +000063static struct semaphore *create_semaphore( const WCHAR *name, size_t len,
Alexandre Julliard5bc78081999-06-22 17:26:53 +000064 unsigned int initial, unsigned int max )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000065{
66 struct semaphore *sem;
67
68 if (!max || (initial > max))
69 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000070 set_error( STATUS_INVALID_PARAMETER );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000071 return NULL;
72 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +000073 if ((sem = create_named_object( &semaphore_ops, name, len )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000074 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000075 if (get_error() != STATUS_OBJECT_NAME_COLLISION)
Alexandre Julliard5bc78081999-06-22 17:26:53 +000076 {
77 /* initialize it if it didn't already exist */
78 sem->count = initial;
79 sem->max = max;
80 }
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000081 }
Alexandre Julliard5bc78081999-06-22 17:26:53 +000082 return sem;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000083}
84
Alexandre Julliard51885742002-05-30 20:12:58 +000085static unsigned int release_semaphore( obj_handle_t handle, unsigned int count )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000086{
87 struct semaphore *sem;
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000088 unsigned int prev = 0;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000089
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000090 if ((sem = (struct semaphore *)get_handle_obj( current->process, handle,
91 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
Alexandre Julliardd30dfd21998-09-27 18:28:36 +000092 {
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000093 prev = sem->count;
94 if (sem->count + count < sem->count || sem->count + count > sem->max)
95 {
Alexandre Julliardcb1fc732000-01-24 21:58:06 +000096 set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED );
Alexandre Julliardebe29ef1999-06-26 08:43:26 +000097 }
98 else if (sem->count)
99 {
100 /* there cannot be any thread waiting if the count is != 0 */
101 assert( !sem->obj.head );
102 sem->count += count;
103 }
104 else
105 {
106 sem->count = count;
107 wake_up( &sem->obj, count );
108 }
109 release_object( sem );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000110 }
Alexandre Julliardebe29ef1999-06-26 08:43:26 +0000111 return prev;
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000112}
113
Alexandre Julliard338e7571998-12-27 15:28:54 +0000114static void semaphore_dump( struct object *obj, int verbose )
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000115{
116 struct semaphore *sem = (struct semaphore *)obj;
117 assert( obj->ops == &semaphore_ops );
Alexandre Julliardd16319c1999-11-25 21:30:24 +0000118 fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max );
119 dump_object_name( &sem->obj );
120 fputc( '\n', stderr );
Alexandre Julliardd30dfd21998-09-27 18:28:36 +0000121}
122
123static int semaphore_signaled( struct object *obj, struct thread *thread )
124{
125 struct semaphore *sem = (struct semaphore *)obj;
126 assert( obj->ops == &semaphore_ops );
127 return (sem->count > 0);
128}
129
130static int semaphore_satisfied( struct object *obj, struct thread *thread )
131{
132 struct semaphore *sem = (struct semaphore *)obj;
133 assert( obj->ops == &semaphore_ops );
134 assert( sem->count );
135 sem->count--;
136 return 0; /* not abandoned */
137}
138
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000139/* create a semaphore */
140DECL_HANDLER(create_semaphore)
141{
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000142 struct semaphore *sem;
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000143
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000144 reply->handle = 0;
145 if ((sem = create_semaphore( get_req_data(), get_req_data_size(),
Alexandre Julliard9c2370b2000-08-30 00:00:48 +0000146 req->initial, req->max )))
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000147 {
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000148 reply->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit );
Alexandre Julliard5bc78081999-06-22 17:26:53 +0000149 release_object( sem );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000150 }
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000151}
152
153/* open a handle to a semaphore */
154DECL_HANDLER(open_semaphore)
155{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000156 reply->handle = open_object( get_req_data(), get_req_data_size(),
157 &semaphore_ops, req->access, req->inherit );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000158}
159
160/* release a semaphore */
161DECL_HANDLER(release_semaphore)
162{
Alexandre Julliard9caa71e2001-11-30 18:46:42 +0000163 reply->prev_count = release_semaphore( req->handle, req->count );
Alexandre Julliard43c190e1999-05-15 10:48:19 +0000164}