libc: use pthread mutex in getifaddrs()

This commit is contained in:
Christian Helmuth 2020-11-06 11:12:41 +01:00
parent ff429a8056
commit 5dfca79bcc
2 changed files with 39 additions and 8 deletions

View File

@ -37,6 +37,7 @@ namespace Libc {
struct Pthread_registry;
struct Pthread_blockade;
struct Pthread_job;
struct Pthread_mutex;
}
@ -373,4 +374,36 @@ struct Libc::Pthread_job : Monitor::Job
};
struct Libc::Pthread_mutex
{
public:
class Guard
{
private:
Pthread_mutex &_mutex;
public:
explicit Guard(Pthread_mutex &mutex) : _mutex(mutex) { _mutex.lock(); }
~Guard() { _mutex.unlock(); }
};
private:
pthread_mutex_t _mutex;
public:
Pthread_mutex() { pthread_mutex_init(&_mutex, nullptr); }
~Pthread_mutex() { pthread_mutex_destroy(&_mutex); }
void lock() { pthread_mutex_lock(&_mutex); }
void unlock() { pthread_mutex_unlock(&_mutex); }
};
#endif /* _LIBC__INTERNAL__PTHREAD_H_ */

View File

@ -41,6 +41,7 @@
#include <internal/errno.h>
#include <internal/init.h>
#include <internal/suspend.h>
#include <internal/pthread.h>
#include <internal/unconfirmed.h>
@ -248,7 +249,7 @@ struct Libc::Socket_fs::Context : Plugin_context
ssize_t connect_status_len;
connect_status_len = read(connect_fd(), connect_status,
sizeof(connect_status));
sizeof(connect_status));
if (connect_status_len <= 0) {
error("socket_fs: reading from the connect file failed");
@ -1070,10 +1071,9 @@ static int read_ifaddr_file(sockaddr_in &sockaddr, Socket_fs::Absolute_path cons
extern "C" int getifaddrs(struct ifaddrs **ifap)
{
/* FIXME this should be a pthread_mutex because function uses blocking operations */
static Mutex mutex;
static Pthread_mutex mutex;
Mutex::Guard guard(mutex);
Pthread_mutex::Guard guard(mutex);
static sockaddr_in address;
static sockaddr_in netmask { 0 };
@ -1170,14 +1170,12 @@ bool Socket_fs::Plugin::poll(File_descriptor &fdo, struct pollfd &pfd)
bool res { false };
if ((pfd.events & POLLIN_MASK) && context->read_ready())
{
if ((pfd.events & POLLIN_MASK) && context->read_ready()) {
pfd.revents |= pfd.events & POLLIN_MASK;
res = true;
}
if ((pfd.events & POLLOUT_MASK) && context->write_ready())
{
if ((pfd.events & POLLOUT_MASK) && context->write_ready()) {
pfd.revents |= pfd.events & POLLOUT_MASK;
res = true;
}