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" |
| 13 | #include "server/thread.h" |
| 14 | |
| 15 | struct semaphore |
| 16 | { |
| 17 | struct object obj; /* object header */ |
| 18 | unsigned int count; /* current count */ |
| 19 | unsigned int max; /* maximum possible count */ |
| 20 | }; |
| 21 | |
| 22 | static void dump_semaphore( struct object *obj, int verbose ); |
| 23 | static int semaphore_signaled( struct object *obj, struct thread *thread ); |
| 24 | static int semaphore_satisfied( struct object *obj, struct thread *thread ); |
| 25 | static void destroy_semaphore( struct object *obj ); |
| 26 | |
| 27 | static const struct object_ops semaphore_ops = |
| 28 | { |
| 29 | dump_semaphore, |
| 30 | semaphore_signaled, |
| 31 | semaphore_satisfied, |
| 32 | destroy_semaphore |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | struct object *create_semaphore( const char *name, unsigned int initial, unsigned int max ) |
| 37 | { |
| 38 | struct semaphore *sem; |
| 39 | |
| 40 | if (!max || (initial > max)) |
| 41 | { |
| 42 | SET_ERROR( ERROR_INVALID_PARAMETER ); |
| 43 | return NULL; |
| 44 | } |
| 45 | if (!(sem = (struct semaphore *)create_named_object( name, &semaphore_ops, sizeof(*sem) ))) |
| 46 | return NULL; |
| 47 | if (GET_ERROR() != ERROR_ALREADY_EXISTS) |
| 48 | { |
| 49 | /* initialize it if it didn't already exist */ |
| 50 | sem->count = initial; |
| 51 | sem->max = max; |
| 52 | } |
| 53 | return &sem->obj; |
| 54 | } |
| 55 | |
| 56 | int open_semaphore( unsigned int access, int inherit, const char *name ) |
| 57 | { |
| 58 | return open_object( name, &semaphore_ops, access, inherit ); |
| 59 | } |
| 60 | |
| 61 | int release_semaphore( int handle, unsigned int count, unsigned int *prev_count ) |
| 62 | { |
| 63 | struct semaphore *sem; |
| 64 | |
| 65 | if (!(sem = (struct semaphore *)get_handle_obj( current->process, handle, |
| 66 | SEMAPHORE_MODIFY_STATE, &semaphore_ops ))) |
| 67 | return 0; |
| 68 | |
| 69 | *prev_count = sem->count; |
| 70 | if (sem->count + count < sem->count || sem->count + count > sem->max) |
| 71 | { |
| 72 | SET_ERROR( ERROR_TOO_MANY_POSTS ); |
| 73 | return 0; |
| 74 | } |
| 75 | if (sem->count) |
| 76 | { |
| 77 | /* there cannot be any thread waiting if the count is != 0 */ |
| 78 | assert( !sem->obj.head ); |
| 79 | sem->count += count; |
| 80 | } |
| 81 | else |
| 82 | { |
| 83 | sem->count = count; |
| 84 | wake_up( &sem->obj, count ); |
| 85 | } |
| 86 | release_object( sem ); |
| 87 | return 1; |
| 88 | } |
| 89 | |
| 90 | static void dump_semaphore( struct object *obj, int verbose ) |
| 91 | { |
| 92 | struct semaphore *sem = (struct semaphore *)obj; |
| 93 | assert( obj->ops == &semaphore_ops ); |
| 94 | printf( "Semaphore count=%d max=%d\n", sem->count, sem->max ); |
| 95 | } |
| 96 | |
| 97 | static int semaphore_signaled( struct object *obj, struct thread *thread ) |
| 98 | { |
| 99 | struct semaphore *sem = (struct semaphore *)obj; |
| 100 | assert( obj->ops == &semaphore_ops ); |
| 101 | return (sem->count > 0); |
| 102 | } |
| 103 | |
| 104 | static int semaphore_satisfied( struct object *obj, struct thread *thread ) |
| 105 | { |
| 106 | struct semaphore *sem = (struct semaphore *)obj; |
| 107 | assert( obj->ops == &semaphore_ops ); |
| 108 | assert( sem->count ); |
| 109 | sem->count--; |
| 110 | return 0; /* not abandoned */ |
| 111 | } |
| 112 | |
| 113 | static void destroy_semaphore( struct object *obj ) |
| 114 | { |
| 115 | struct semaphore *sem = (struct semaphore *)obj; |
| 116 | assert( obj->ops == &semaphore_ops ); |
| 117 | free( sem ); |
| 118 | } |