Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side semaphore management |
| 3 | * |
| 4 | * Copyright (C) 1998 Alexandre Julliard |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 5 | * |
| 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 Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 21 | #include "config.h" |
| 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 24 | #include <assert.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Robert Lunnon | 95414ef | 2005-11-22 12:01:05 +0000 | [diff] [blame] | 27 | #include <stdarg.h> |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 28 | |
Ge van Geldorp | 1a1583a | 2005-11-28 17:32:54 +0100 | [diff] [blame] | 29 | #include "ntstatus.h" |
| 30 | #define WIN32_NO_STATUS |
Alexandre Julliard | 435e2e6 | 2002-12-10 22:56:43 +0000 | [diff] [blame] | 31 | #include "windef.h" |
Vitaliy Margolen | a996000 | 2005-10-27 18:30:37 +0000 | [diff] [blame] | 32 | #include "winternl.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 33 | |
| 34 | #include "handle.h" |
| 35 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 36 | #include "request.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 37 | |
| 38 | struct semaphore |
| 39 | { |
| 40 | struct object obj; /* object header */ |
| 41 | unsigned int count; /* current count */ |
| 42 | unsigned int max; /* maximum possible count */ |
| 43 | }; |
| 44 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 45 | static void semaphore_dump( struct object *obj, int verbose ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 46 | static int semaphore_signaled( struct object *obj, struct thread *thread ); |
| 47 | static int semaphore_satisfied( struct object *obj, struct thread *thread ); |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 48 | static int semaphore_signal( struct object *obj, unsigned int access ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 49 | |
| 50 | static const struct object_ops semaphore_ops = |
| 51 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 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 */ |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 58 | semaphore_signal, /* signal */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 59 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 28beba3 | 2005-12-12 14:57:40 +0100 | [diff] [blame^] | 60 | no_map_access, /* map_access */ |
Vitaliy Margolen | baffcb9 | 2005-11-22 14:55:42 +0000 | [diff] [blame] | 61 | no_lookup_name, /* lookup_name */ |
Alexandre Julliard | b9b1ea9 | 2005-06-09 15:39:52 +0000 | [diff] [blame] | 62 | no_close_handle, /* close_handle */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 63 | no_destroy /* destroy */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 64 | }; |
| 65 | |
| 66 | |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 67 | static struct semaphore *create_semaphore( struct directory *root, const struct unicode_str *name, |
| 68 | unsigned int attr, unsigned int initial, unsigned int max ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 69 | { |
| 70 | struct semaphore *sem; |
| 71 | |
| 72 | if (!max || (initial > max)) |
| 73 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 74 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 77 | if ((sem = create_named_object_dir( root, name, attr, &semaphore_ops ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 78 | { |
Vitaliy Margolen | 893987b | 2005-11-21 16:27:03 +0000 | [diff] [blame] | 79 | if (get_error() != STATUS_OBJECT_NAME_EXISTS) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 80 | { |
| 81 | /* initialize it if it didn't already exist */ |
| 82 | sem->count = initial; |
| 83 | sem->max = max; |
| 84 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 85 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 86 | return sem; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 87 | } |
| 88 | |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 89 | static int release_semaphore( struct semaphore *sem, unsigned int count, |
| 90 | unsigned int *prev ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 91 | { |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 92 | if (prev) *prev = sem->count; |
| 93 | if (sem->count + count < sem->count || sem->count + count > sem->max) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 94 | { |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 95 | set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED ); |
| 96 | return 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 97 | } |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 98 | else if (sem->count) |
| 99 | { |
| 100 | /* there cannot be any thread to wake up if the count is != 0 */ |
| 101 | sem->count += count; |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | sem->count = count; |
| 106 | wake_up( &sem->obj, count ); |
| 107 | } |
| 108 | return 1; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 111 | static void semaphore_dump( struct object *obj, int verbose ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 112 | { |
| 113 | struct semaphore *sem = (struct semaphore *)obj; |
| 114 | assert( obj->ops == &semaphore_ops ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 115 | fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max ); |
| 116 | dump_object_name( &sem->obj ); |
| 117 | fputc( '\n', stderr ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static int semaphore_signaled( struct object *obj, struct thread *thread ) |
| 121 | { |
| 122 | struct semaphore *sem = (struct semaphore *)obj; |
| 123 | assert( obj->ops == &semaphore_ops ); |
| 124 | return (sem->count > 0); |
| 125 | } |
| 126 | |
| 127 | static int semaphore_satisfied( struct object *obj, struct thread *thread ) |
| 128 | { |
| 129 | struct semaphore *sem = (struct semaphore *)obj; |
| 130 | assert( obj->ops == &semaphore_ops ); |
| 131 | assert( sem->count ); |
| 132 | sem->count--; |
| 133 | return 0; /* not abandoned */ |
| 134 | } |
| 135 | |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 136 | static int semaphore_signal( struct object *obj, unsigned int access ) |
| 137 | { |
| 138 | struct semaphore *sem = (struct semaphore *)obj; |
| 139 | assert( obj->ops == &semaphore_ops ); |
| 140 | |
| 141 | if (!(access & SEMAPHORE_MODIFY_STATE)) |
| 142 | { |
| 143 | set_error( STATUS_ACCESS_DENIED ); |
| 144 | return 0; |
| 145 | } |
| 146 | return release_semaphore( sem, 1, NULL ); |
| 147 | } |
| 148 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 149 | /* create a semaphore */ |
| 150 | DECL_HANDLER(create_semaphore) |
| 151 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 152 | struct semaphore *sem; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 153 | struct unicode_str name; |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 154 | struct directory *root = NULL; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 155 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 156 | reply->handle = 0; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 157 | get_req_unicode_str( &name ); |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 158 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 159 | return; |
| 160 | |
| 161 | if ((sem = create_semaphore( root, &name, req->attributes, req->initial, req->max ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 162 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 163 | reply->handle = alloc_handle( current->process, sem, req->access, req->attributes ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 164 | release_object( sem ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 165 | } |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 166 | |
| 167 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | /* open a handle to a semaphore */ |
| 171 | DECL_HANDLER(open_semaphore) |
| 172 | { |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 173 | struct unicode_str name; |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 174 | struct directory *root = NULL; |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 175 | struct semaphore *sem; |
Alexandre Julliard | ead9b06 | 2005-11-18 16:31:18 +0000 | [diff] [blame] | 176 | |
| 177 | get_req_unicode_str( &name ); |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 178 | if (req->rootdir && !(root = get_directory_obj( current->process, req->rootdir, 0 ))) |
| 179 | return; |
| 180 | |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 181 | if ((sem = open_object_dir( root, &name, req->attributes, &semaphore_ops ))) |
| 182 | { |
Alexandre Julliard | 24560e7 | 2005-12-09 13:58:25 +0100 | [diff] [blame] | 183 | reply->handle = alloc_handle( current->process, &sem->obj, req->access, req->attributes ); |
Alexandre Julliard | 3764da6 | 2005-12-05 12:52:05 +0100 | [diff] [blame] | 184 | release_object( sem ); |
| 185 | } |
Vitaliy Margolen | 5daae3d | 2005-12-02 16:01:17 +0100 | [diff] [blame] | 186 | |
| 187 | if (root) release_object( root ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /* release a semaphore */ |
| 191 | DECL_HANDLER(release_semaphore) |
| 192 | { |
Mike McCormack | f92fff6 | 2005-04-24 17:35:52 +0000 | [diff] [blame] | 193 | struct semaphore *sem; |
| 194 | |
| 195 | if ((sem = (struct semaphore *)get_handle_obj( current->process, req->handle, |
| 196 | SEMAPHORE_MODIFY_STATE, &semaphore_ops ))) |
| 197 | { |
| 198 | release_semaphore( sem, req->count, &reply->prev_count ); |
| 199 | release_object( sem ); |
| 200 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 201 | } |