From 0c912e3bc8e325ca20137bb6b954e58e2f8e86d1 Mon Sep 17 00:00:00 2001 From: kirthika Date: Tue, 20 Jul 1999 21:24:50 +0000 Subject: ihandle gobbler class --- tests/Handle_Consumer.h | 120 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/Handle_Consumer.h diff --git a/tests/Handle_Consumer.h b/tests/Handle_Consumer.h new file mode 100644 index 00000000000..1c70b205e14 --- /dev/null +++ b/tests/Handle_Consumer.h @@ -0,0 +1,120 @@ +// $Id$ + +#ifndef _HANDLE_CONSUMER_H +#define _HANDLE_CONSUMER_H + +#include "ace/OS.h" +#include "ace/Containers_T.h" + +class Handle_Consumer +{ + // = TITLE + // This class gobbles up descriptors on demand. + // + // = DESCRIPTION + // This is useful when we need to control the number of + // descriptors available for a process. + + public: + + ~Handle_Consumer (void); + // Constructor. + + int consume_handles (size_t n_handles_to_keep_available); + // The descriptors are opened continously until the process runs out + // of them, and then only the number which have to be made available + // are closed thereby making them usable in the future. + + int keep_available (size_t n_handles); + // Denotes the number of handles to retain. + + void close_remaining_handles (void); + // All remaining handles are relased. + + private: + + typedef ACE_Unbounded_Set HANDLE_SET; + HANDLE_SET handle_set_; + // The container which holds the open descriptors. + +}; + + + +Handle_Consumer::~Handle_Consumer (void) +{ + this->close_remaining_handles (); +} + +int +Handle_Consumer::consume_handles (size_t n_handles_to_keep_available) +{ + int result = 0; + ACE_HANDLE handle = ACE_INVALID_HANDLE; + + while (1) + { + handle = ACE_OS::open (ACE_DEV_NULL, + O_WRONLY); + + if (handle == ACE_INVALID_HANDLE) + { + if (ACE::out_of_handles (errno)) + { + result = this->keep_available (n_handles_to_keep_available); + break; + } + else + { + result = -1; + break; + } + } + + result = this->handle_set_.insert (handle); + if (result == -1) + break; + } + + return result; +} + +int +Handle_Consumer::keep_available (size_t n_handles) +{ + HANDLE_SET::iterator iterator = + this->handle_set_.begin (); + + HANDLE_SET::iterator end = + this->handle_set_.end (); + + for (; + iterator != end && n_handles > 0; + ++iterator, --n_handles) + { + int result = ACE_OS::close (*iterator); + if (result != 0) + return result; + } + + return 0; +} + +void +Handle_Consumer::close_remaining_handles (void) +{ + HANDLE_SET::iterator iterator = + this->handle_set_.begin (); + + HANDLE_SET::iterator end = + this->handle_set_.end (); + + for (; + iterator != end; + ++iterator) + { + ACE_OS::close (*iterator); + } +} + +#endif /* _HANDLER_CONSUMER_H */ -- cgit v1.2.1