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 |
| 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 semaphore |
| 19 | { |
| 20 | struct object obj; /* object header */ |
| 21 | unsigned int count; /* current count */ |
| 22 | unsigned int max; /* maximum possible count */ |
| 23 | }; |
| 24 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 25 | static void semaphore_dump( struct object *obj, int verbose ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 26 | static int semaphore_signaled( struct object *obj, struct thread *thread ); |
| 27 | static int semaphore_satisfied( struct object *obj, struct thread *thread ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 28 | |
| 29 | static const struct object_ops semaphore_ops = |
| 30 | { |
Alexandre Julliard | 1dca5e2 | 2000-01-01 00:56:27 +0000 | [diff] [blame] | 31 | sizeof(struct semaphore), /* size */ |
| 32 | semaphore_dump, /* dump */ |
| 33 | add_queue, /* add_queue */ |
| 34 | remove_queue, /* remove_queue */ |
| 35 | semaphore_signaled, /* signaled */ |
| 36 | semaphore_satisfied, /* satisfied */ |
| 37 | NULL, /* get_poll_events */ |
| 38 | NULL, /* poll_event */ |
| 39 | no_read_fd, /* get_read_fd */ |
| 40 | no_write_fd, /* get_write_fd */ |
| 41 | no_flush, /* flush */ |
| 42 | no_get_file_info, /* get_file_info */ |
| 43 | no_destroy /* destroy */ |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 47 | static struct semaphore *create_semaphore( const WCHAR *name, size_t len, |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 48 | unsigned int initial, unsigned int max ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 49 | { |
| 50 | struct semaphore *sem; |
| 51 | |
| 52 | if (!max || (initial > max)) |
| 53 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 54 | set_error( ERROR_INVALID_PARAMETER ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 57 | if ((sem = create_named_object( &semaphore_ops, name, len ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 58 | { |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 59 | if (get_error() != ERROR_ALREADY_EXISTS) |
| 60 | { |
| 61 | /* initialize it if it didn't already exist */ |
| 62 | sem->count = initial; |
| 63 | sem->max = max; |
| 64 | } |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 65 | } |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 66 | return sem; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 69 | static unsigned int release_semaphore( int handle, unsigned int count ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 70 | { |
| 71 | struct semaphore *sem; |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 72 | unsigned int prev = 0; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 73 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 74 | if ((sem = (struct semaphore *)get_handle_obj( current->process, handle, |
| 75 | SEMAPHORE_MODIFY_STATE, &semaphore_ops ))) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 76 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 77 | prev = sem->count; |
| 78 | if (sem->count + count < sem->count || sem->count + count > sem->max) |
| 79 | { |
| 80 | set_error( ERROR_TOO_MANY_POSTS ); |
| 81 | } |
| 82 | else if (sem->count) |
| 83 | { |
| 84 | /* there cannot be any thread waiting if the count is != 0 */ |
| 85 | assert( !sem->obj.head ); |
| 86 | sem->count += count; |
| 87 | } |
| 88 | else |
| 89 | { |
| 90 | sem->count = count; |
| 91 | wake_up( &sem->obj, count ); |
| 92 | } |
| 93 | release_object( sem ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 94 | } |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 95 | return prev; |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Alexandre Julliard | 338e757 | 1998-12-27 15:28:54 +0000 | [diff] [blame] | 98 | static void semaphore_dump( struct object *obj, int verbose ) |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 99 | { |
| 100 | struct semaphore *sem = (struct semaphore *)obj; |
| 101 | assert( obj->ops == &semaphore_ops ); |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 102 | fprintf( stderr, "Semaphore count=%d max=%d ", sem->count, sem->max ); |
| 103 | dump_object_name( &sem->obj ); |
| 104 | fputc( '\n', stderr ); |
Alexandre Julliard | d30dfd2 | 1998-09-27 18:28:36 +0000 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static int semaphore_signaled( struct object *obj, struct thread *thread ) |
| 108 | { |
| 109 | struct semaphore *sem = (struct semaphore *)obj; |
| 110 | assert( obj->ops == &semaphore_ops ); |
| 111 | return (sem->count > 0); |
| 112 | } |
| 113 | |
| 114 | static int semaphore_satisfied( struct object *obj, struct thread *thread ) |
| 115 | { |
| 116 | struct semaphore *sem = (struct semaphore *)obj; |
| 117 | assert( obj->ops == &semaphore_ops ); |
| 118 | assert( sem->count ); |
| 119 | sem->count--; |
| 120 | return 0; /* not abandoned */ |
| 121 | } |
| 122 | |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 123 | /* create a semaphore */ |
| 124 | DECL_HANDLER(create_semaphore) |
| 125 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 126 | size_t len = get_req_strlenW( req->name ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 127 | struct semaphore *sem; |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 128 | |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 129 | req->handle = -1; |
| 130 | if ((sem = create_semaphore( req->name, len, req->initial, req->max ))) |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 131 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 132 | req->handle = alloc_handle( current->process, sem, SEMAPHORE_ALL_ACCESS, req->inherit ); |
Alexandre Julliard | 5bc7808 | 1999-06-22 17:26:53 +0000 | [diff] [blame] | 133 | release_object( sem ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 134 | } |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | /* open a handle to a semaphore */ |
| 138 | DECL_HANDLER(open_semaphore) |
| 139 | { |
Alexandre Julliard | d16319c | 1999-11-25 21:30:24 +0000 | [diff] [blame] | 140 | size_t len = get_req_strlenW( req->name ); |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 141 | req->handle = open_object( req->name, len, &semaphore_ops, req->access, req->inherit ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /* release a semaphore */ |
| 145 | DECL_HANDLER(release_semaphore) |
| 146 | { |
Alexandre Julliard | ebe29ef | 1999-06-26 08:43:26 +0000 | [diff] [blame] | 147 | req->prev_count = release_semaphore( req->handle, req->count ); |
Alexandre Julliard | 43c190e | 1999-05-15 10:48:19 +0000 | [diff] [blame] | 148 | } |