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
c:thrsrv-bs [2019-06-03 08:52]
ziggi
c:thrsrv-bs [2020-02-15 00:57] (current)
Line 76: Line 76:
 } }
  
-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 88: Line 88:
         }         }
     }     }
-    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;
Line 157: Line 159:
  
 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];​