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