Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Server-side mutex 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 | |
| 21 | #include <assert.h> |
| 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
| 24 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 25 | #include "winnt.h" |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 26 | |
| 27 | #include "handle.h" |
| 28 | #include "thread.h" |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 29 | #include "request.h" |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 30 | |
| 31 | struct mutex |
| 32 | { |
| 33 | struct object obj; /* object header */ |
| 34 | struct thread *owner; /* mutex owner */ |
| 35 | unsigned int count; /* recursion count */ |
| 36 | int abandoned; /* has it been abandoned? */ |
| 37 | struct mutex *next; |
| 38 | struct mutex *prev; |
| 39 | }; |
| 40 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 41 | static void mutex_dump( struct object *obj, int verbose ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 42 | static int mutex_signaled( struct object *obj, struct thread *thread ); |
| 43 | static int mutex_satisfied( struct object *obj, struct thread *thread ); |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 44 | static void mutex_destroy( struct object *obj ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 45 | |
| 46 | static const struct object_ops mutex_ops = |
| 47 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 48 | sizeof(struct mutex), /* size */ |
| 49 | mutex_dump, /* dump */ |
| 50 | add_queue, /* add_queue */ |
| 51 | remove_queue, /* remove_queue */ |
| 52 | mutex_signaled, /* signaled */ |
| 53 | mutex_satisfied, /* satisfied */ |
| 54 | NULL, /* get_poll_events */ |
| 55 | NULL, /* poll_event */ |
Alexandre Julliard | 1ab243b | 2000-12-19 02:12:45 +0000 | [diff] [blame] | 56 | no_get_fd, /* get_fd */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 57 | no_flush, /* flush */ |
| 58 | no_get_file_info, /* get_file_info */ |
Mike McCormack | 6f011c0 | 2001-12-20 00:07:05 +0000 | [diff] [blame] | 59 | NULL, /* queue_async */ |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 60 | mutex_destroy /* destroy */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 64 | static struct mutex *create_mutex( const WCHAR *name, size_t len, int owned ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 65 | { |
| 66 | struct mutex *mutex; |
| 67 | |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 68 | if ((mutex = create_named_object( &mutex_ops, name, len ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 69 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 70 | if (get_error() != STATUS_OBJECT_NAME_COLLISION) |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 71 | { |
| 72 | /* initialize it if it didn't already exist */ |
| 73 | mutex->count = 0; |
| 74 | mutex->owner = NULL; |
| 75 | mutex->abandoned = 0; |
| 76 | mutex->next = mutex->prev = NULL; |
| 77 | if (owned) mutex_satisfied( &mutex->obj, current ); |
| 78 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 79 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 80 | return mutex; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 83 | /* release a mutex once the recursion count is 0 */ |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 84 | static void do_release( struct mutex *mutex ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 85 | { |
| 86 | assert( !mutex->count ); |
| 87 | /* remove the mutex from the thread list of owned mutexes */ |
| 88 | if (mutex->next) mutex->next->prev = mutex->prev; |
| 89 | if (mutex->prev) mutex->prev->next = mutex->next; |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 90 | else mutex->owner->mutex = mutex->next; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 91 | mutex->owner = NULL; |
| 92 | mutex->next = mutex->prev = NULL; |
| 93 | wake_up( &mutex->obj, 0 ); |
| 94 | } |
| 95 | |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 96 | void abandon_mutexes( struct thread *thread ) |
| 97 | { |
| 98 | while (thread->mutex) |
| 99 | { |
| 100 | struct mutex *mutex = thread->mutex; |
| 101 | assert( mutex->owner == thread ); |
| 102 | mutex->count = 0; |
| 103 | mutex->abandoned = 1; |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 104 | do_release( mutex ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 108 | static void mutex_dump( struct object *obj, int verbose ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 109 | { |
| 110 | struct mutex *mutex = (struct mutex *)obj; |
| 111 | assert( obj->ops == &mutex_ops ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 112 | fprintf( stderr, "Mutex count=%u owner=%p ", mutex->count, mutex->owner ); |
| 113 | dump_object_name( &mutex->obj ); |
| 114 | fputc( '\n', stderr ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static int mutex_signaled( struct object *obj, struct thread *thread ) |
| 118 | { |
| 119 | struct mutex *mutex = (struct mutex *)obj; |
| 120 | assert( obj->ops == &mutex_ops ); |
| 121 | return (!mutex->count || (mutex->owner == thread)); |
| 122 | } |
| 123 | |
| 124 | static int mutex_satisfied( struct object *obj, struct thread *thread ) |
| 125 | { |
| 126 | struct mutex *mutex = (struct mutex *)obj; |
| 127 | assert( obj->ops == &mutex_ops ); |
| 128 | assert( !mutex->count || (mutex->owner == thread) ); |
| 129 | |
| 130 | if (!mutex->count++) /* FIXME: avoid wrap-around */ |
| 131 | { |
| 132 | assert( !mutex->owner ); |
| 133 | mutex->owner = thread; |
| 134 | mutex->prev = NULL; |
| 135 | if ((mutex->next = thread->mutex)) mutex->next->prev = mutex; |
| 136 | thread->mutex = mutex; |
| 137 | } |
| 138 | if (!mutex->abandoned) return 0; |
| 139 | mutex->abandoned = 0; |
| 140 | return 1; |
| 141 | } |
| 142 | |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 143 | static void mutex_destroy( struct object *obj ) |
| 144 | { |
| 145 | struct mutex *mutex = (struct mutex *)obj; |
| 146 | assert( obj->ops == &mutex_ops ); |
| 147 | |
| 148 | if (!mutex->count) return; |
| 149 | mutex->count = 0; |
| 150 | do_release( mutex ); |
| 151 | } |
| 152 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 153 | /* create a mutex */ |
| 154 | DECL_HANDLER(create_mutex) |
| 155 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 156 | struct mutex *mutex; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 157 | |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 158 | reply->handle = 0; |
| 159 | if ((mutex = create_mutex( get_req_data(), get_req_data_size(), req->owned ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 160 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 161 | reply->handle = alloc_handle( current->process, mutex, MUTEX_ALL_ACCESS, req->inherit ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 162 | release_object( mutex ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 163 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* open a handle to a mutex */ |
| 167 | DECL_HANDLER(open_mutex) |
| 168 | { |
Alexandre Julliard | 9caa71e | 2001-11-30 18:46:42 +0000 | [diff] [blame] | 169 | reply->handle = open_object( get_req_data(), get_req_data_size(), |
| 170 | &mutex_ops, req->access, req->inherit ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* release a mutex */ |
| 174 | DECL_HANDLER(release_mutex) |
| 175 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 176 | struct mutex *mutex; |
| 177 | |
| 178 | if ((mutex = (struct mutex *)get_handle_obj( current->process, req->handle, |
| 179 | MUTEX_MODIFY_STATE, &mutex_ops ))) |
| 180 | { |
Alexandre Julliard | cb1fc73 | 2000-01-24 21:58:06 +0000 | [diff] [blame] | 181 | if (!mutex->count || (mutex->owner != current)) set_error( STATUS_MUTANT_NOT_OWNED ); |
Alexandre Julliard | dbab5e2 | 1999-08-04 09:52:33 +0000 | [diff] [blame] | 182 | else if (!--mutex->count) do_release( mutex ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 183 | release_object( mutex ); |
| 184 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 185 | } |