Emulates ins/outs correctly for DOS programs.
[wine/multimedia.git] / server / semaphore.c
blobdeb06182969460cfd5b1bea630cc939665d7d976
1 /*
2 * Server-side semaphore management
4 * Copyright (C) 1998 Alexandre Julliard
5 */
7 #include <assert.h>
8 #include <stdio.h>
9 #include <stdlib.h>
11 #include "winerror.h"
12 #include "winnt.h"
13 #include "server/thread.h"
15 struct semaphore
17 struct object obj; /* object header */
18 unsigned int count; /* current count */
19 unsigned int max; /* maximum possible count */
22 static void semaphore_dump( 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 semaphore_destroy( struct object *obj );
27 static const struct object_ops semaphore_ops =
29 semaphore_dump,
30 add_queue,
31 remove_queue,
32 semaphore_signaled,
33 semaphore_satisfied,
34 no_read_fd,
35 no_write_fd,
36 no_flush,
37 no_get_file_info,
38 semaphore_destroy
42 struct object *create_semaphore( const char *name, unsigned int initial, unsigned int max )
44 struct semaphore *sem;
46 if (!max || (initial > max))
48 SET_ERROR( ERROR_INVALID_PARAMETER );
49 return NULL;
51 if (!(sem = (struct semaphore *)create_named_object( name, &semaphore_ops, sizeof(*sem) )))
52 return NULL;
53 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
55 /* initialize it if it didn't already exist */
56 sem->count = initial;
57 sem->max = max;
59 return &sem->obj;
62 int open_semaphore( unsigned int access, int inherit, const char *name )
64 return open_object( name, &semaphore_ops, access, inherit );
67 int release_semaphore( int handle, unsigned int count, unsigned int *prev_count )
69 struct semaphore *sem;
71 if (!(sem = (struct semaphore *)get_handle_obj( current->process, handle,
72 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
73 return 0;
75 *prev_count = sem->count;
76 if (sem->count + count < sem->count || sem->count + count > sem->max)
78 SET_ERROR( ERROR_TOO_MANY_POSTS );
79 return 0;
81 if (sem->count)
83 /* there cannot be any thread waiting if the count is != 0 */
84 assert( !sem->obj.head );
85 sem->count += count;
87 else
89 sem->count = count;
90 wake_up( &sem->obj, count );
92 release_object( sem );
93 return 1;
96 static void semaphore_dump( struct object *obj, int verbose )
98 struct semaphore *sem = (struct semaphore *)obj;
99 assert( obj->ops == &semaphore_ops );
100 fprintf( stderr, "Semaphore count=%d max=%d name='%s'\n",
101 sem->count, sem->max, get_object_name( &sem->obj ) );
104 static int semaphore_signaled( struct object *obj, struct thread *thread )
106 struct semaphore *sem = (struct semaphore *)obj;
107 assert( obj->ops == &semaphore_ops );
108 return (sem->count > 0);
111 static int semaphore_satisfied( struct object *obj, struct thread *thread )
113 struct semaphore *sem = (struct semaphore *)obj;
114 assert( obj->ops == &semaphore_ops );
115 assert( sem->count );
116 sem->count--;
117 return 0; /* not abandoned */
120 static void semaphore_destroy( struct object *obj )
122 struct semaphore *sem = (struct semaphore *)obj;
123 assert( obj->ops == &semaphore_ops );
124 free( sem );