Added beginnings of server-side file handling.
[wine/multimedia.git] / server / semaphore.c
blobcd77d166b209797b3d316f8902f8da24a1b879ab
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 semaphore_destroy
38 struct object *create_semaphore( const char *name, unsigned int initial, unsigned int max )
40 struct semaphore *sem;
42 if (!max || (initial > max))
44 SET_ERROR( ERROR_INVALID_PARAMETER );
45 return NULL;
47 if (!(sem = (struct semaphore *)create_named_object( name, &semaphore_ops, sizeof(*sem) )))
48 return NULL;
49 if (GET_ERROR() != ERROR_ALREADY_EXISTS)
51 /* initialize it if it didn't already exist */
52 sem->count = initial;
53 sem->max = max;
55 return &sem->obj;
58 int open_semaphore( unsigned int access, int inherit, const char *name )
60 return open_object( name, &semaphore_ops, access, inherit );
63 int release_semaphore( int handle, unsigned int count, unsigned int *prev_count )
65 struct semaphore *sem;
67 if (!(sem = (struct semaphore *)get_handle_obj( current->process, handle,
68 SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
69 return 0;
71 *prev_count = sem->count;
72 if (sem->count + count < sem->count || sem->count + count > sem->max)
74 SET_ERROR( ERROR_TOO_MANY_POSTS );
75 return 0;
77 if (sem->count)
79 /* there cannot be any thread waiting if the count is != 0 */
80 assert( !sem->obj.head );
81 sem->count += count;
83 else
85 sem->count = count;
86 wake_up( &sem->obj, count );
88 release_object( sem );
89 return 1;
92 static void semaphore_dump( struct object *obj, int verbose )
94 struct semaphore *sem = (struct semaphore *)obj;
95 assert( obj->ops == &semaphore_ops );
96 printf( "Semaphore count=%d max=%d\n", sem->count, sem->max );
99 static int semaphore_signaled( struct object *obj, struct thread *thread )
101 struct semaphore *sem = (struct semaphore *)obj;
102 assert( obj->ops == &semaphore_ops );
103 return (sem->count > 0);
106 static int semaphore_satisfied( struct object *obj, struct thread *thread )
108 struct semaphore *sem = (struct semaphore *)obj;
109 assert( obj->ops == &semaphore_ops );
110 assert( sem->count );
111 sem->count--;
112 return 0; /* not abandoned */
115 static void semaphore_destroy( struct object *obj )
117 struct semaphore *sem = (struct semaphore *)obj;
118 assert( obj->ops == &semaphore_ops );
119 free( sem );