~ubuntu-branches/ubuntu/vivid/mpich/vivid-proposed

« back to all changes in this revision

Viewing changes to src/pm/smpd/sock/poll/smpd_util_sock.c

  • Committer: Package Import Robot
  • Author(s): Anton Gladky
  • Date: 2014-04-01 20:24:20 UTC
  • mfrom: (5.2.4 sid)
  • Revision ID: package-import@ubuntu.com-20140401202420-t5ey1ia2klt5dkq3
Tags: 3.1-4
* [c3e3398] Disable test_primitives, which is unreliable on some platforms.
            (Closes: #743047)
* [265a699] Add minimal autotest.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: C; c-basic-offset:4 ; indent-tabs-mode:nil ; -*- */
2
 
 
3
 
/*
4
 
 *  (C) 2001 by Argonne National Laboratory.
5
 
 *      See COPYRIGHT in top-level directory.
6
 
 */
7
 
 
8
 
#include "smpd_util_sock.h"
9
 
/*#include "mpiimpl.h"*/
10
 
#ifdef HAVE_STRING_H
11
 
/* Include for memcpy and memset */
12
 
#include <string.h>
13
 
#endif
14
 
 
15
 
#include "mpishared.h"
16
 
 
17
 
#include <unistd.h>
18
 
#include <sys/types.h>
19
 
#include <sys/socket.h>
20
 
#include <sys/uio.h>
21
 
#include <netinet/tcp.h>
22
 
#include <netinet/in.h>
23
 
#include <fcntl.h>
24
 
#if defined(HAVE_POLL_H)
25
 
#include <sys/poll.h>
26
 
#elif defined(HAVE_SYS_POLL_H)
27
 
#include <sys/poll.h>
28
 
#endif
29
 
#include <netdb.h>
30
 
#include <errno.h>
31
 
#include <stdio.h>
32
 
#include "smpd.h"
33
 
 
34
 
 
35
 
/* FIXME: What do these mean?  Why is 32 a good size (e.g., is it because
36
 
   32*32 = 1024 if these are bits in a 4-byte int?  In that case, should
37
 
   these be related to a maximum processor count or an OS-defined fd limit? */
38
 
#if !defined(SMPDU_SOCK_SET_DEFAULT_SIZE)
39
 
#define SMPDU_SOCK_SET_DEFAULT_SIZE 32
40
 
#endif
41
 
 
42
 
#if !defined(SMPDU_SOCK_EVENTQ_POOL_SIZE)
43
 
#define SMPDU_SOCK_EVENTQ_POOL_SIZE 32
44
 
#endif
45
 
 
46
 
 
47
 
enum SMPDU_Socki_state
48
 
{
49
 
    SMPDU_SOCKI_STATE_FIRST = 0,
50
 
    SMPDU_SOCKI_STATE_CONNECTING,
51
 
    SMPDU_SOCKI_STATE_CONNECTED_RW,
52
 
    SMPDU_SOCKI_STATE_CONNECTED_RO,
53
 
    SMPDU_SOCKI_STATE_DISCONNECTED,
54
 
    SMPDU_SOCKI_STATE_CLOSING,
55
 
    SMPDU_SOCKI_STATE_LAST
56
 
};
57
 
 
58
 
enum SMPDU_Socki_type
59
 
{
60
 
    SMPDU_SOCKI_TYPE_FIRST = 0,
61
 
    SMPDU_SOCKI_TYPE_COMMUNICATION,
62
 
    SMPDU_SOCKI_TYPE_LISTENER,
63
 
    SMPDU_SOCKI_TYPE_INTERRUPTER,
64
 
    SMPDU_SOCKI_TYPE_LAST
65
 
};
66
 
 
67
 
/*
68
 
 * struct pollinfo
69
 
 * 
70
 
 * sock_id - an integer id comprised of the sock_set id and the element number
71
 
 *           in the pollfd/info arrays
72
 
 * 
73
 
 * sock_set - a pointer to the sock set to which this connection belongs
74
 
 * 
75
 
 * elem - the element number of this connection in the pollfd/info arrays
76
 
 * 
77
 
 * sock - at present this is only used to free the sock structure when the 
78
 
 *        close is completed by SMPDIU_Sock_wait()
79
 
 * 
80
 
 * fd - this file descriptor is used whenever the file descriptor is needed. 
81
 
 *      this descriptor remains open until the sock, and
82
 
 *      thus the socket, are actually closed.  the fd in the pollfd structure
83
 
 *      should only be used for telling poll() if it should
84
 
 *      check for events on that descriptor.
85
 
 * 
86
 
 * user_ptr - a user supplied pointer that is included with event associated 
87
 
 *            with this connection
88
 
 * 
89
 
 * state - state of the connection
90
 
 *
91
 
 */
92
 
struct pollinfo
93
 
{
94
 
    int sock_id;
95
 
    struct SMPDU_Sock_set * sock_set;
96
 
    int elem;
97
 
    struct SMPDU_Sock * sock;
98
 
    int fd;
99
 
    void * user_ptr;
100
 
    enum SMPDU_Socki_type type;
101
 
    enum SMPDU_Socki_state state;
102
 
    int os_errno;
103
 
    union
104
 
    {
105
 
        struct
106
 
        {
107
 
            SMPD_IOV * ptr;
108
 
            int count;
109
 
            int offset;
110
 
        } iov;
111
 
        struct
112
 
        {
113
 
            char * ptr;
114
 
            SMPDU_Sock_size_t min;
115
 
            SMPDU_Sock_size_t max;
116
 
        } buf;
117
 
    } read;
118
 
    int read_iov_flag;
119
 
    SMPDU_Sock_size_t read_nb;
120
 
    SMPDU_Sock_progress_update_func_t read_progress_update_fn;
121
 
    union
122
 
    {
123
 
        struct
124
 
        {
125
 
            SMPD_IOV * ptr;
126
 
            int count;
127
 
            int offset;
128
 
        } iov;
129
 
        struct
130
 
        {
131
 
            char * ptr;
132
 
            SMPDU_Sock_size_t min;
133
 
            SMPDU_Sock_size_t max;
134
 
        } buf;
135
 
    } write;
136
 
    int write_iov_flag;
137
 
    SMPDU_Sock_size_t write_nb;
138
 
    SMPDU_Sock_progress_update_func_t write_progress_update_fn;
139
 
};
140
 
 
141
 
struct SMPDU_Socki_eventq_elem
142
 
{
143
 
    struct SMPDU_Sock_event event;
144
 
    int set_elem;
145
 
    struct SMPDU_Socki_eventq_elem * next;
146
 
};
147
 
 
148
 
struct SMPDU_Sock_set
149
 
{
150
 
    int id;
151
 
 
152
 
    /* when the pollfds array is scanned for activity, start with this element.
153
 
       this is used to prevent favoring a particular
154
 
       element, such as the first. */
155
 
    int starting_elem;
156
 
 
157
 
    /* pointers to the pollfd and pollinfo that make up the logical poll array,
158
 
       along with the current size of the array and last
159
 
       allocated element */
160
 
    int poll_array_sz;
161
 
    int poll_array_elems;
162
 
    struct pollfd * pollfds;
163
 
    struct pollinfo * pollinfos;
164
 
 
165
 
    /* head and tail pointers for the event queue */
166
 
    struct SMPDU_Socki_eventq_elem * eventq_head;
167
 
    struct SMPDU_Socki_eventq_elem * eventq_tail;
168
 
};
169
 
 
170
 
struct SMPDU_Sock
171
 
{
172
 
    struct SMPDU_Sock_set * sock_set;
173
 
    int elem;
174
 
};
175
 
 
176
 
/* FIXME: Why aren't these static */
177
 
int SMPDU_Socki_initialized = 0;
178
 
 
179
 
static struct SMPDU_Socki_eventq_elem * SMPDU_Socki_eventq_pool = NULL;
180
 
 
181
 
/* MT: needs to be atomically incremented */
182
 
static int SMPDU_Socki_set_next_id = 0;
183
 
 
184
 
/* Prototypes for functions used only within the socket code. */
185
 
 
186
 
/* Set the buffer size on the socket fd from the environment variable
187
 
   or other option; if "firm" is true, fail if the buffer size is not
188
 
   successfully set */
189
 
int SMPDU_Sock_SetSockBufferSize( int fd, int firm );
190
 
/* Get a string version of the address in ifaddr*/
191
 
int SMPDU_Sock_AddrToStr( SMPDU_Sock_ifaddr_t *ifaddr, char *str, int maxlen );
192
 
 
193
 
/* FIXME: Why are these files included in this way?  Why not make them either
194
 
   separate files or simply part of (one admittedly large) source file? */
195
 
#include "smpd_socki_util.i"
196
 
 
197
 
#include "smpd_sock_init.i"
198
 
#include "smpd_sock_set.i"
199
 
#include "smpd_sock_post.i"
200
 
#include "smpd_sock_immed.i"
201
 
#include "smpd_sock_misc.i"
202
 
#include "smpd_sock_wait.i"