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