Added DebugBreak.
[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
14 #include "handle.h"
15 #include "thread.h"
16
17 struct semaphore
18 {
19     struct object  obj;    /* object header */
20     unsigned int   count;  /* current count */
21     unsigned int   max;    /* maximum possible count */
22 };
23
24 static void semaphore_dump( struct object *obj, int verbose );
25 static int semaphore_signaled( struct object *obj, struct thread *thread );
26 static int semaphore_satisfied( struct object *obj, struct thread *thread );
27 static void semaphore_destroy( struct object *obj );
28
29 static const struct object_ops semaphore_ops =
30 {
31     semaphore_dump,
32     add_queue,
33     remove_queue,
34     semaphore_signaled,
35     semaphore_satisfied,
36     no_read_fd,
37     no_write_fd,
38     no_flush,
39     no_get_file_info,
40     semaphore_destroy
41 };
42
43
44 static struct object *create_semaphore( const char *name, unsigned int initial, unsigned int max )
45 {
46     struct semaphore *sem;
47
48     if (!max || (initial > max))
49     {
50         SET_ERROR( ERROR_INVALID_PARAMETER );
51         return NULL;
52     }
53     if (!(sem = (struct semaphore *)create_named_object( name, &semaphore_ops, sizeof(*sem) )))
54         return NULL;
55     if (GET_ERROR() != ERROR_ALREADY_EXISTS)
56     {
57         /* initialize it if it didn't already exist */
58         sem->count = initial;
59         sem->max   = max;
60     }
61     return &sem->obj;
62 }
63
64 static int release_semaphore( int handle, unsigned int count, unsigned int *prev_count )
65 {
66     struct semaphore *sem;
67
68     if (!(sem = (struct semaphore *)get_handle_obj( current->process, handle,
69                                                     SEMAPHORE_MODIFY_STATE, &semaphore_ops )))
70         return 0;
71
72     *prev_count = sem->count;
73     if (sem->count + count < sem->count || sem->count + count > sem->max)
74     {
75         SET_ERROR( ERROR_TOO_MANY_POSTS );
76         return 0;
77     }
78     if (sem->count)
79     {
80         /* there cannot be any thread waiting if the count is != 0 */
81         assert( !sem->obj.head );
82         sem->count += count;
83     }
84     else
85     {
86         sem->count = count;
87         wake_up( &sem->obj, count );
88     }
89     release_object( sem );
90     return 1;
91 }
92
93 static void semaphore_dump( struct object *obj, int verbose )
94 {
95     struct semaphore *sem = (struct semaphore *)obj;
96     assert( obj->ops == &semaphore_ops );
97     fprintf( stderr, "Semaphore count=%d max=%d name='%s'\n",
98              sem->count, sem->max, get_object_name( &sem->obj ) );
99 }
100
101 static int semaphore_signaled( struct object *obj, struct thread *thread )
102 {
103     struct semaphore *sem = (struct semaphore *)obj;
104     assert( obj->ops == &semaphore_ops );
105     return (sem->count > 0);
106 }
107
108 static int semaphore_satisfied( struct object *obj, struct thread *thread )
109 {
110     struct semaphore *sem = (struct semaphore *)obj;
111     assert( obj->ops == &semaphore_ops );
112     assert( sem->count );
113     sem->count--;
114     return 0;  /* not abandoned */
115 }
116
117 static void semaphore_destroy( struct object *obj )
118 {
119     struct semaphore *sem = (struct semaphore *)obj;
120     assert( obj->ops == &semaphore_ops );
121     free( sem );
122 }
123
124 /* create a semaphore */
125 DECL_HANDLER(create_semaphore)
126 {
127     struct create_semaphore_reply reply = { -1 };
128     struct object *obj;
129     char *name = (char *)data;
130     if (!len) name = NULL;
131     else CHECK_STRING( "create_semaphore", name, len );
132
133     obj = create_semaphore( name, req->initial, req->max );
134     if (obj)
135     {
136         reply.handle = alloc_handle( current->process, obj, SEMAPHORE_ALL_ACCESS, req->inherit );
137         release_object( obj );
138     }
139     send_reply( current, -1, 1, &reply, sizeof(reply) );
140 }
141
142 /* open a handle to a semaphore */
143 DECL_HANDLER(open_semaphore)
144 {
145     struct open_semaphore_reply reply;
146     char *name = (char *)data;
147     if (!len) name = NULL;
148     else CHECK_STRING( "open_semaphore", name, len );
149
150     reply.handle = open_object( name, &semaphore_ops, req->access, req->inherit );
151     send_reply( current, -1, 1, &reply, sizeof(reply) );
152 }
153
154 /* release a semaphore */
155 DECL_HANDLER(release_semaphore)
156 {
157     struct release_semaphore_reply reply;
158     if (release_semaphore( req->handle, req->count, &reply.prev_count )) CLEAR_ERROR();
159     send_reply( current, -1, 1, &reply, sizeof(reply) );
160 }