]> git.sesse.net Git - cubemap/blob - markpool.cpp
Fix 100% CPU usage when there is no access_log.
[cubemap] / markpool.cpp
1 #include "log.h"
2 #include "markpool.h"
3 #include "mutexlock.h"
4 #include <stdio.h>
5 #include <assert.h>
6 #include <pthread.h>
7 #include <queue>
8
9 MarkPool::MarkPool(int start, int end)
10         : start(start), end(end)
11 {
12         assert(start > 0 && start < 65536);
13         assert(end > 0 && end < 65536);
14
15         for (int i = start; i < end; ++i) {
16                 free_marks.push(i);
17         }
18
19         pthread_mutex_init(&mutex, NULL);
20 }
21
22 int MarkPool::get_mark()
23 {
24         MutexLock lock(&mutex);
25         if (free_marks.empty()) {
26                 log(WARNING, "Out of free marks in mark pool %d-%d, session will not be marked. "
27                              "To fix, increase the pool size and HUP the server.",
28                              start, end);
29                 return 0;
30         }
31         int mark = free_marks.front();
32         free_marks.pop();
33         return mark;
34 }
35
36 void MarkPool::release_mark(int mark)
37 {
38         if (mark == 0) {
39                 return;
40         }
41
42         MutexLock lock(&mutex);
43         free_marks.push(mark);
44 }