1 /*
2     Copyright (c) 2012-2013 Martin Sustrik  All rights reserved.
3 
4     Permission is hereby granted, free of charge, to any person obtaining a copy
5     of this software and associated documentation files (the "Software"),
6     to deal in the Software without restriction, including without limitation
7     the rights to use, copy, modify, merge, publish, distribute, sublicense,
8     and/or sell copies of the Software, and to permit persons to whom
9     the Software is furnished to do so, subject to the following conditions:
10 
11     The above copyright notice and this permission notice shall be included
12     in all copies or substantial portions of the Software.
13 
14     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17     THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19     FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20     IN THE SOFTWARE.
21 */
22 
23 #include "excl.h"
24 
25 #include "../../utils/fast.h"
26 #include "../../utils/err.h"
27 #include "../../utils/attr.h"
28 
nn_excl_init(struct nn_excl * self)29 void nn_excl_init (struct nn_excl *self)
30 {
31     self->pipe = NULL;
32     self->inpipe = NULL;
33     self->outpipe = NULL;
34 }
35 
nn_excl_term(struct nn_excl * self)36 void nn_excl_term (struct nn_excl *self)
37 {
38     nn_assert (!self->pipe);
39     nn_assert (!self->inpipe);
40     nn_assert (!self->outpipe);
41 }
42 
nn_excl_add(struct nn_excl * self,struct nn_pipe * pipe)43 int nn_excl_add (struct nn_excl *self, struct nn_pipe *pipe)
44 {
45     /*  If there's a connection being used, reject any new connection. */
46     if (self->pipe)
47         return -EISCONN;
48 
49     /*  Remember that this pipe is the active one. */
50     self->pipe = pipe;
51 
52     return 0;
53 }
54 
nn_excl_rm(struct nn_excl * self,NN_UNUSED struct nn_pipe * pipe)55 void nn_excl_rm (struct nn_excl *self, NN_UNUSED struct nn_pipe *pipe)
56 {
57    nn_assert (self->pipe);
58    self->pipe = NULL;
59    self->inpipe = NULL;
60    self->outpipe = NULL;
61 }
62 
nn_excl_in(struct nn_excl * self,struct nn_pipe * pipe)63 void nn_excl_in (struct nn_excl *self, struct nn_pipe *pipe)
64 {
65     nn_assert (!self->inpipe);
66     nn_assert (pipe == self->pipe);
67     self->inpipe = pipe;
68 }
69 
nn_excl_out(struct nn_excl * self,struct nn_pipe * pipe)70 void nn_excl_out (struct nn_excl *self, struct nn_pipe *pipe)
71 {
72     nn_assert (!self->outpipe);
73     nn_assert (pipe == self->pipe);
74     self->outpipe = pipe;
75 }
76 
nn_excl_send(struct nn_excl * self,struct nn_msg * msg)77 int nn_excl_send (struct nn_excl *self, struct nn_msg *msg)
78 {
79     int rc;
80 
81     if (nn_slow (!self->outpipe))
82         return -EAGAIN;
83 
84     rc = nn_pipe_send (self->outpipe, msg);
85     errnum_assert (rc >= 0, -rc);
86 
87     if (rc & NN_PIPE_RELEASE)
88         self->outpipe = NULL;
89 
90     return rc & ~NN_PIPE_RELEASE;
91 }
92 
nn_excl_recv(struct nn_excl * self,struct nn_msg * msg)93 int nn_excl_recv (struct nn_excl *self, struct nn_msg *msg)
94 {
95     int rc;
96 
97     if (nn_slow (!self->inpipe))
98         return -EAGAIN;
99 
100     rc = nn_pipe_recv (self->inpipe, msg);
101     errnum_assert (rc >= 0, -rc);
102 
103     if (rc & NN_PIPE_RELEASE)
104         self->inpipe = NULL;
105 
106     return rc & ~NN_PIPE_RELEASE;
107 }
108 
nn_excl_can_send(struct nn_excl * self)109 int nn_excl_can_send (struct nn_excl *self)
110 {
111     return self->outpipe ? 1 : 0;
112 }
113 
nn_excl_can_recv(struct nn_excl * self)114 int nn_excl_can_recv (struct nn_excl *self)
115 {
116     return self->inpipe ? 1 : 0;
117 }
118 
119