• Main Page
  • Related Pages
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

EventNotify.cc

Go to the documentation of this file.
00001 /** @file
00002 
00003   A brief file description
00004 
00005   @section license License
00006 
00007   Licensed to the Apache Software Foundation (ASF) under one
00008   or more contributor license agreements.  See the NOTICE file
00009   distributed with this work for additional information
00010   regarding copyright ownership.  The ASF licenses this file
00011   to you under the Apache License, Version 2.0 (the
00012   "License"); you may not use this file except in compliance
00013   with the License.  You may obtain a copy of the License at
00014 
00015       http://www.apache.org/licenses/LICENSE-2.0
00016 
00017   Unless required by applicable law or agreed to in writing, software
00018   distributed under the License is distributed on an "AS IS" BASIS,
00019   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00020   See the License for the specific language governing permissions and
00021   limitations under the License.
00022  */
00023 
00024 /**************************************************************************
00025   EventNotify.cc
00026 
00027   Generic event notify mechanism among threads.
00028 **************************************************************************/
00029 
00030 #include "EventNotify.h"
00031 #include "ink_hrtime.h"
00032 #include "ink_defs.h"
00033 
00034 #ifdef HAVE_EVENTFD
00035 #include <sys/eventfd.h>
00036 #include <sys/fcntl.h>
00037 #include <sys/epoll.h>
00038 #endif
00039 
00040 EventNotify::EventNotify()
00041 {
00042 #ifdef HAVE_EVENTFD
00043   int ret;
00044   struct epoll_event ev;
00045 
00046   m_event_fd = eventfd(0, EFD_NONBLOCK | EFD_CLOEXEC);
00047   if (m_event_fd < 0) {
00048     // EFD_NONBLOCK/EFD_CLOEXEC invalid in <= Linux 2.6.27
00049     m_event_fd = eventfd(0, 0);
00050 
00051     fcntl(m_event_fd, F_SETFD, FD_CLOEXEC);
00052     fcntl(m_event_fd, F_SETFL, O_NONBLOCK);
00053   }
00054   ink_release_assert(m_event_fd != -1);
00055 
00056   ev.events = EPOLLIN;
00057   ev.data.fd = m_event_fd;
00058 
00059   m_epoll_fd = epoll_create(1);
00060   ink_release_assert(m_epoll_fd != -1);
00061 
00062   ret = epoll_ctl(m_epoll_fd, EPOLL_CTL_ADD, m_event_fd, &ev);
00063   ink_release_assert(ret != -1);
00064 #else
00065   ink_cond_init(&m_cond);
00066   ink_mutex_init(&m_mutex, NULL);
00067 #endif
00068 }
00069 
00070 void
00071 EventNotify::signal(void)
00072 {
00073 #ifdef HAVE_EVENTFD
00074   uint64_t value = 1;
00075   //
00076   // If the addition would cause the counter’s value of eventfd
00077   // to exceed the maximum, write() will fail with the errno EAGAIN,
00078   // which is acceptable as the receiver will be notified eventually.
00079   //
00080   ATS_UNUSED_RETURN(write(m_event_fd, &value, sizeof(uint64_t)));
00081 #else
00082   ink_cond_signal(&m_cond);
00083 #endif
00084 }
00085 
00086 int
00087 EventNotify::wait(void)
00088 {
00089 #ifdef HAVE_EVENTFD
00090   ssize_t nr, nr_fd;
00091   uint64_t value = 0;
00092   struct epoll_event ev;
00093 
00094   do {
00095     nr_fd = epoll_wait(m_epoll_fd, &ev, 1, -1);
00096   } while (nr_fd == -1 && errno == EINTR);
00097 
00098   if (nr_fd == -1)
00099     return errno;
00100 
00101   nr = read(m_event_fd, &value, sizeof(uint64_t));
00102   if (nr == sizeof(uint64_t))
00103     return 0;
00104   else
00105     return errno;
00106 #else
00107   ink_cond_wait(&m_cond, &m_mutex);
00108   return 0;
00109 #endif
00110 }
00111 
00112 int
00113 EventNotify::timedwait(int timeout) // milliseconds
00114 {
00115 #ifdef HAVE_EVENTFD
00116   ssize_t nr, nr_fd = 0;
00117   uint64_t value = 0;
00118   struct epoll_event ev;
00119 
00120   //
00121   // When timeout < 0, epoll_wait() will wait indefinitely, but
00122   // pthread_cond_timedwait() will return ETIMEDOUT immediately.
00123   // We should keep compatible with pthread_cond_timedwait() here.
00124   //
00125   if (timeout < 0)
00126     return ETIMEDOUT;
00127 
00128   do {
00129     nr_fd = epoll_wait(m_epoll_fd, &ev, 1, timeout);
00130   } while (nr_fd == -1 && errno == EINTR);
00131 
00132   if (nr_fd == 0)
00133     return ETIMEDOUT;
00134   else if (nr_fd == -1)
00135     return errno;
00136 
00137   nr = read(m_event_fd, &value, sizeof(uint64_t));
00138   if (nr == sizeof(uint64_t))
00139     return 0;
00140   else
00141     return errno;
00142 #else
00143   ink_timestruc abstime;
00144 
00145   abstime = ink_hrtime_to_timespec(ink_get_hrtime_internal() + HRTIME_SECONDS(timeout));
00146   return ink_cond_timedwait(&m_cond, &m_mutex, &abstime);
00147 #endif
00148 }
00149 
00150 void
00151 EventNotify::lock(void)
00152 {
00153 #ifdef HAVE_EVENTFD
00154   // do nothing
00155 #else
00156   ink_mutex_acquire(&m_mutex);
00157 #endif
00158 }
00159 
00160 bool
00161 EventNotify::trylock(void)
00162 {
00163 #ifdef HAVE_EVENTFD
00164   return true;
00165 #else
00166   return ink_mutex_try_acquire(&m_mutex);
00167 #endif
00168 }
00169 
00170 void
00171 EventNotify::unlock(void)
00172 {
00173 #ifdef HAVE_EVENTFD
00174   // do nothing
00175 #else
00176   ink_mutex_release(&m_mutex);
00177 #endif
00178 }
00179 
00180 EventNotify::~EventNotify()
00181 {
00182 #ifdef HAVE_EVENTFD
00183   close(m_event_fd);
00184   close(m_epoll_fd);
00185 #else
00186   ink_cond_destroy(&m_cond);
00187   ink_mutex_destroy(&m_mutex);
00188 #endif
00189 }

Generated by  doxygen 1.7.1