blob: ee6c92c9dd9ffa7be15c4f19cd2e895f4da55b19 [file] [log] [blame]
Willy Tarreau982b6e32009-01-25 13:49:53 +01001/*
2 * Pipe management
3 *
4 * Copyright 2000-2009 Willy Tarreau <w@1wt.eu>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 *
11 */
12
13#include <unistd.h>
14
15#include <common/config.h>
16#include <common/memory.h>
17
18#include <types/global.h>
19#include <types/pipe.h>
20
21struct pool_head *pool2_pipe = NULL;
22struct pipe *pipes_live = NULL; /* pipes which are still ready to use */
23int pipes_used = 0; /* # of pipes in use (2 fds each) */
24int pipes_free = 0; /* # of pipes unused */
25
26/* allocate memory for the pipes */
27static void init_pipe()
28{
29 pool2_pipe = create_pool("pipe", sizeof(struct pipe), MEM_F_SHARED);
30 pipes_used = 0;
31 pipes_free = 0;
32}
33
34/* return a pre-allocated empty pipe. Try to allocate one if there isn't any
35 * left. NULL is returned if a pipe could not be allocated.
36 */
37struct pipe *get_pipe()
38{
39 struct pipe *ret;
40 int pipefd[2];
41
42 if (likely(pipes_live)) {
43 ret = pipes_live;
44 pipes_live = pipes_live->next;
45 pipes_free--;
46 pipes_used++;
47 return ret;
48 }
49
50 if (pipes_used >= global.maxpipes)
51 return NULL;
52
53 ret = pool_alloc2(pool2_pipe);
54 if (!ret)
55 return NULL;
56
57 if (pipe(pipefd) < 0) {
58 pool_free2(pool2_pipe, ret);
59 return NULL;
60 }
61 ret->data = 0;
62 ret->prod = pipefd[1];
63 ret->cons = pipefd[0];
64 ret->next = NULL;
65 pipes_used++;
66 return ret;
67}
68
69/* destroy a pipe, possibly because an error was encountered on it. Its FDs
70 * will be closed and it will not be reinjected into the live pool.
71 */
72void kill_pipe(struct pipe *p)
73{
74 close(p->prod);
75 close(p->cons);
76 pool_free2(pool2_pipe, p);
77 pipes_used--;
78 return;
79}
80
81/* put back a unused pipe into the live pool. If it still has data in it, it is
82 * closed and not reinjected into the live pool. The caller is not allowed to
83 * use it once released.
84 */
85void put_pipe(struct pipe *p)
86{
87 if (p->data) {
88 kill_pipe(p);
89 return;
90 }
91 p->next = pipes_live;
92 pipes_live = p;
93 pipes_free++;
94 pipes_used--;
95}
96
97
98__attribute__((constructor))
99static void __pipe_module_init(void)
100{
101 init_pipe();
102}
103
104/*
105 * Local variables:
106 * c-indent-level: 8
107 * c-basic-offset: 8
108 * End:
109 */