Skip to content

Commit

Permalink
ST: Always use unserialized accept for linux or darwin
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Apr 25, 2021
1 parent c3686f2 commit 8400115
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 109 deletions.
107 changes: 0 additions & 107 deletions trunk/3rdparty/st-srs/io.c
Expand Up @@ -262,7 +262,6 @@ int st_netfd_poll(_st_netfd_t *fd, int how, st_utime_t timeout)
}


#ifdef MD_ALWAYS_UNSERIALIZED_ACCEPT
/* No-op */
int st_netfd_serialize_accept(_st_netfd_t *fd)
{
Expand Down Expand Up @@ -310,112 +309,6 @@ _st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_
return newfd;
}

#else /* MD_ALWAYS_UNSERIALIZED_ACCEPT */
/*
* On some platforms accept() calls from different processes
* on the same listen socket must be serialized.
* The following code serializes accept()'s without process blocking.
* A pipe is used as an inter-process semaphore.
*/
int st_netfd_serialize_accept(_st_netfd_t *fd)
{
_st_netfd_t **p;
int osfd[2], err;

if (fd->aux_data) {
errno = EINVAL;
return -1;
}
if ((p = (_st_netfd_t **)calloc(2, sizeof(_st_netfd_t *))) == NULL)
return -1;
if (pipe(osfd) < 0) {
free(p);
return -1;
}
if ((p[0] = st_netfd_open(osfd[0])) != NULL && (p[1] = st_netfd_open(osfd[1])) != NULL && write(osfd[1], " ", 1) == 1) {
fd->aux_data = p;
return 0;
}
/* Error */
err = errno;
if (p[0])
st_netfd_free(p[0]);
if (p[1])
st_netfd_free(p[1]);
close(osfd[0]);
close(osfd[1]);
free(p);
errno = err;

return -1;
}

static void _st_netfd_free_aux_data(_st_netfd_t *fd)
{
_st_netfd_t **p = (_st_netfd_t **) fd->aux_data;

st_netfd_close(p[0]);
st_netfd_close(p[1]);
free(p);
fd->aux_data = NULL;
}

_st_netfd_t *st_accept(_st_netfd_t *fd, struct sockaddr *addr, int *addrlen, st_utime_t timeout)
{
int osfd, err;
_st_netfd_t *newfd;
_st_netfd_t **p = (_st_netfd_t **) fd->aux_data;
ssize_t n;
char c;

for ( ; ; ) {
if (p == NULL) {
osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
} else {
/* Get the lock */
n = st_read(p[0], &c, 1, timeout);
if (n < 0)
return NULL;
ST_ASSERT(n == 1);
/* Got the lock */
osfd = accept(fd->osfd, addr, (socklen_t *)addrlen);
/* Unlock */
err = errno;
n = st_write(p[1], &c, 1, timeout);
ST_ASSERT(n == 1);
errno = err;
}
if (osfd >= 0)
break;
if (errno == EINTR)
continue;
if (!_IO_NOT_READY_ERROR)
return NULL;
/* Wait until the socket becomes readable */
if (st_netfd_poll(fd, POLLIN, timeout) < 0)
return NULL;
}

/* On some platforms the new socket created by accept() inherits */
/* the nonblocking attribute of the listening socket */
#if defined (MD_ACCEPT_NB_INHERITED)
newfd = _st_netfd_new(osfd, 0, 1);
#elif defined (MD_ACCEPT_NB_NOT_INHERITED)
newfd = _st_netfd_new(osfd, 1, 1);
#else
#error Unknown OS
#endif

if (!newfd) {
err = errno;
close(osfd);
errno = err;
}

return newfd;
}
#endif /* MD_ALWAYS_UNSERIALIZED_ACCEPT */


int st_connect(_st_netfd_t *fd, const struct sockaddr *addr, int addrlen, st_utime_t timeout)
{
Expand Down
2 changes: 0 additions & 2 deletions trunk/3rdparty/st-srs/md.h
Expand Up @@ -62,7 +62,6 @@

#define MD_USE_BSD_ANON_MMAP
#define MD_ACCEPT_NB_INHERITED
#define MD_ALWAYS_UNSERIALIZED_ACCEPT
#define MD_HAVE_SOCKLEN_T

#define MD_USE_BUILTIN_SETJMP
Expand Down Expand Up @@ -102,7 +101,6 @@
*/
#define MD_USE_BSD_ANON_MMAP
#define MD_ACCEPT_NB_NOT_INHERITED
#define MD_ALWAYS_UNSERIALIZED_ACCEPT
/*
* Modern GNU/Linux is Posix.1g compliant.
*/
Expand Down

0 comments on commit 8400115

Please sign in to comment.