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> |
| 27 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 28 | #include "winnt.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 29 | |
| 30 | #include "handle.h" |
| 31 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 32 | #include "request.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 33 | |
| 34 | struct semaphore |
| 35 | { |
| 36 | struct object obj; /* object header */ |
| 37 | unsigned int count; /* current count */ |
| 38 | unsigned int max; /* maximum possible count */ |
| 39 | }; |
| 40 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 41 | static void semaphore_dump( struct object *obj, int verbose ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 42 | static int semaphore_signaled( struct object *obj, struct thread *thread ); |
| 43 | static int semaphore_satisfied( struct object *obj, struct thread *thread ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 44 | |
| 45 | static const struct object_ops semaphore_ops = |
| 46 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 47 | 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 Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 55 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 56 | no_flush, /* flush */ |
| 57 | no_get_file_info, /* get_file_info */ |
Mike McCormack | 6f011c0 | 2001-12-20 00:07:05 +0000 | [diff] [blame] | 58 | NULL, /* queue_async */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 59 | no_destroy /* destroy */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 63 | static struct semaphore *create_semaphore( const WCHAR *name, size_t len, |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 64 | unsigned int initial, unsigned int max ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 65 | { |
| 66 | struct semaphore *sem; |
| 67 | |
| 68 | if (!max || (initial > max)) |
| 69 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 70 | set_error( STATUS_INVALID_PARAMETER ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 71 | return NULL; |
| 72 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 73 | if ((sem = create_named_object( &semaphore_ops, name, len ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 74 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 75 | if (get_error() != STATUS_OBJECT_NAME_COLLISION) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 76 | { |
| 77 | /* initialize it if it didn't already exist */ |
| 78 | sem->count = initial; |
| 79 | sem->max = max; |
| 80 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 81 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 82 | return sem; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Alexandre Julliard | 5188574 | 2002-05-30 20:12:58 +0000 | [diff] [blame] | 85 | static unsigned int release_semaphore( obj_handle_t handle, unsigned int count ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 86 | { |
| 87 | struct semaphore *sem; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 88 | unsigned int prev = 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 89 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 90 | if ((sem = (struct semaphore *)get_handle_obj( current->process, handle, |
| 91 | SEMAPHORE_MODIFY_STATE, &semaphore_ops ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 92 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 93 | prev = sem->count; |
| 94 | if (sem->count + count < sem->count || sem->count + count > sem->max) |
| 95 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 96 | set_error( STATUS_SEMAPHORE_LIMIT_EXCEEDED ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 97 | } |
| 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 Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 110 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 111 | return prev; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 114 | static void semaphore_dump( struct object *obj, int verbose ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 115 | { |
| 116 | struct semaphore *sem = (struct semaphore *)obj; |
| 117 | assert( obj->ops == &semaphore_ops ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 118 | fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max ); |
| 119 | dump_object_name( &sem->obj ); |
| 120 | fputc( '\n', stderr ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | static 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 | |
| 130 | static 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 Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 139 | /* create a semaphore */ |
| 140 | DECL_HANDLER(create_semaphore) |
| 141 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 142 | struct semaphore *sem; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 143 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 144 | reply->handle = 0; |
| 145 | if ((sem = create_semaphore( get_req_data(), get_req_data_size(), |
Alexandre Julliard | 9c2370b | 2000-08-30 00:00:48 +0000 | [diff] [blame] | 146 | req->initial, req->max ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 147 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 148 | reply->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 149 | release_object( sem ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 150 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /* open a handle to a semaphore */ |
| 154 | DECL_HANDLER(open_semaphore) |
| 155 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 156 | reply->handle = open_object( get_req_data(), get_req_data_size(), |
| 157 | &semaphore_ops, req->access, req->inherit ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | /* release a semaphore */ |
| 161 | DECL_HANDLER(release_semaphore) |
| 162 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 163 | reply->prev_count = release_semaphore( req->handle, req->count ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 164 | } |