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