comparison src/c/urweb.c @ 689:b6a8425e1b1f

Make sure only one pull request runs at a time for each client
author Adam Chlipala <adamc@hcoop.net>
date Thu, 02 Apr 2009 13:48:59 -0400
parents a3ddf05fb3e3
children 655bcc9b77e0
comparison
equal deleted inserted replaced
688:829887ca47a6 689:b6a8425e1b1f
92 typedef struct client { 92 typedef struct client {
93 unsigned id; 93 unsigned id;
94 usage mode; 94 usage mode;
95 int pass; 95 int pass;
96 struct client *next; 96 struct client *next;
97 pthread_mutex_t lock; 97 pthread_mutex_t lock, pull_lock;
98 buf msgs; 98 buf msgs;
99 int sock; 99 int sock;
100 time_t last_contact; 100 time_t last_contact;
101 unsigned n_channels; 101 unsigned n_channels;
102 unsigned refcount; 102 unsigned refcount;
123 ++n_clients; 123 ++n_clients;
124 clients = realloc(clients, sizeof(client) * n_clients); 124 clients = realloc(clients, sizeof(client) * n_clients);
125 c = malloc(sizeof(client)); 125 c = malloc(sizeof(client));
126 c->id = n_clients-1; 126 c->id = n_clients-1;
127 pthread_mutex_init(&c->lock, NULL); 127 pthread_mutex_init(&c->lock, NULL);
128 pthread_mutex_init(&c->pull_lock, NULL);
128 buf_init(&c->msgs, 0); 129 buf_init(&c->msgs, 0);
129 clients[n_clients-1] = c; 130 clients[n_clients-1] = c;
130 } 131 }
131 132
132 pthread_mutex_lock(&c->lock); 133 pthread_mutex_lock(&c->lock);
149 150
150 static void use_client(client *c) { 151 static void use_client(client *c) {
151 pthread_mutex_lock(&c->lock); 152 pthread_mutex_lock(&c->lock);
152 ++c->refcount; 153 ++c->refcount;
153 pthread_mutex_unlock(&c->lock); 154 pthread_mutex_unlock(&c->lock);
155 pthread_mutex_lock(&c->pull_lock);
154 } 156 }
155 157
156 static void release_client(client *c) { 158 static void release_client(client *c) {
159 pthread_mutex_unlock(&c->pull_lock);
157 pthread_mutex_lock(&c->lock); 160 pthread_mutex_lock(&c->lock);
158 --c->refcount; 161 --c->refcount;
159 pthread_mutex_unlock(&c->lock); 162 pthread_mutex_unlock(&c->lock);
163
160 } 164 }
161 165
162 static const char begin_msgs[] = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\n"; 166 static const char begin_msgs[] = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\n\r\n";
163 167
164 static client *find_client(unsigned id) { 168 static client *find_client(unsigned id) {