Implemented makepath, rand() returns correct range, fixed fputc.
[wine] / server / semaphore.c
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/process.h"
14 #include "server/thread.h"
15
16 struct semaphore
17 {
18     struct object  obj;    /* object header */
19     unsigned int   count;  /* current count */
20     unsigned int   max;    /* maximum possible count */
21 };
22
23 static void semaphore_dump( struct object *obj, int verbose );
24 static int semaphore_signaled( struct object *obj, struct thread *thread );
25 static int semaphore_satisfied( struct object *obj, struct thread *thread );
26 static void semaphore_destroy( struct object *obj );
27
28 static const struct object_ops semaphore_ops =
29 {
30     semaphore_dump,
31     add_queue,
32     remove_queue,
33     semaphore_signaled,
34     semaphore_satisfied,
35     no_read_fd,
36     no_write_fd,
37     no_flush,
38     no_get_file_info,
39     semaphore_destroy
40 };
41
42
43 struct object *create_semaphore( const char *name, unsigned int initial, unsigned int max )
44 {
45     struct semaphore *sem;
46
47     if (!max || (initial > max))
48     {
49         SET_ERROR( ERROR_INVALID_PARAMETER );
50         return NULL;
51     }
52     if (!(sem = (struct semaphore *)create_named_object( name, &semaphore_ops, sizeof(*sem) )))
53         return NULL;
54     if (GET_ERROR() != ERROR_ALREADY_EXISTS)
55     {
56         /* initialize it if it didn't already exist */
57         sem->count = initial;
58         sem->max   = max;
59     }
60     return &sem->obj;
61 }
62
63 int open_semaphore( unsigned int access, int inherit, const char *name )
64 {
65     return open_object( name, &semaphore_ops, access, inherit );
66 }
67
68 int release_semaphore( int handle, unsigned int count, unsigned int *prev_count )
69 {
70     struct semaphore *sem;
71
72     if (!(sem = (struct semaphore *)get_handle_obj( current->process, handle,
73                                                     SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
74         return 0;
75
76     *prev_count = sem->count;
77     if (sem->count + count < sem->count || sem->count + count > sem->max)
78     {
79         SET_ERROR( ERROR_TOO_MANY_POSTS );
80         return 0;
81     }
82     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 );
94     return 1;
95 }
96
97 static void semaphore_dump( struct object *obj, int verbose )
98 {
99     struct semaphore *sem = (struct semaphore *)obj;
100     assert( obj->ops == &semaphore_ops );
101     fprintf( stderr, "Semaphore count=%d max=%d name='%s'\n",
102              sem->count, sem->max, get_object_name( &sem->obj ) );
103 }
104
105 static int semaphore_signaled( struct object *obj, struct thread *thread )
106 {
107     struct semaphore *sem = (struct semaphore *)obj;
108     assert( obj->ops == &semaphore_ops );
109     return (sem->count > 0);
110 }
111
112 static int semaphore_satisfied( struct object *obj, struct thread *thread )
113 {
114     struct semaphore *sem = (struct semaphore *)obj;
115     assert( obj->ops == &semaphore_ops );
116     assert( sem->count );
117     sem->count--;
118     return 0;  /* not abandoned */
119 }
120
121 static void semaphore_destroy( struct object *obj )
122 {
123     struct semaphore *sem = (struct semaphore *)obj;
124     assert( obj->ops == &semaphore_ops );
125     free( sem );
126 }