User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
c:thrsrv-kqueue [2019-06-01 14:58]
ziggi
c:thrsrv-kqueue [2019-06-04 12:14] (current)
Line 1: Line 1:
 +=====Threadpool web server with kqueue()/​kevent()===== ​
 +
 <code c thrsrv-kq.c>​ <code c thrsrv-kq.c>​
 /* /*
Line 70: Line 72:
 } }
  
-int queue_back(queue_t* q, int x) {+int queue_push(queue_t* q, int x) {
     if (q == NULL) return -1;     if (q == NULL) return -1;
     pthread_mutex_lock(&​q->​mutex);​     pthread_mutex_lock(&​q->​mutex);​
     int res = -1;     int res = -1;
-    if (> q->​capacity) {+    if ((q->​size ​1) > q->​capacity) {
         int newcapacity = q->​capacity * 2;         int newcapacity = q->​capacity * 2;
         void* items = realloc(q->​items,​ sizeof(int) * newcapacity);​         void* items = realloc(q->​items,​ sizeof(int) * newcapacity);​
Line 82: Line 84:
         }         }
     }     }
-    if (x < q->​capacity) {; +    if ((q->size + 1) q->​capacity) { 
-        q->rear = (q->rear + 1) q->​capacity; +        ​pthread_mutex_unlock(&​(q->mutex))
-        q->items[q->​rear] = x+        ​return ​-1;
-        ​q->​size++;​ +
-        res = 1;+
     }     }
 +    q->rear = (q->rear + 1) % q->​capacity;​
 +    q->​items[q->​rear] = x;
 +    q->​size++;​
 +    res = 1;
     pthread_mutex_unlock(&​(q->​mutex));​     pthread_mutex_unlock(&​(q->​mutex));​
     return res;     return res;
 } }
 +
  
 void queue_lock(queue_t* q) { void queue_lock(queue_t* q) {
Line 146: Line 151:
  
 int wrkpool_enqueue(wrkpool_t* wp, int newsock) { int wrkpool_enqueue(wrkpool_t* wp, int newsock) {
-    ​queue_back(wp->​queue,​ newsock);+    ​queue_push(wp->​queue,​ newsock);
     for (int i = 0; i < wp->​size;​ i++) {     for (int i = 0; i < wp->​size;​ i++) {
         worker_t* w = wp->​workers[i];​         worker_t* w = wp->​workers[i];​