root/unix/posix_locking.cpp

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. lock

   1 /******************************************************************************
   2 ** $Header: svn+ssh://jmmcg@svn.code.sf.net/p/libjmmcg/code/trunk/libjmmcg/unix/posix_locking_impl.hpp 1833 2016-01-15 19:24:28Z jmmcg $
   3 **
   4 ** Copyright (c) 2004 by J.M.McGuiness, coder@hussar.me.uk
   5 **
   6 ** This library is free software; you can redistribute it and/or
   7 ** modify it under the terms of the GNU Lesser General Public
   8 ** License as published by the Free Software Foundation; either
   9 ** version 2.1 of the License, or (at your option) any later version.
  10 **
  11 ** This library is distributed in the hope that it will be useful,
  12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14 ** Lesser General Public License for more details.
  15 **
  16 ** You should have received a copy of the GNU Lesser General Public
  17 ** License along with this library; if not, write to the Free Software
  18 ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19 */
  20 
  21 #include "../core/hp_timer.hpp"
  22 #include "posix_locking.hpp"
  23 
  24 namespace jmmcg { namespace ppd { namespace pthreads {
  25 
  26 anon_semaphore::lock_result_type
  27 anon_semaphore::lock(const timeout_type period) noexcept(false) {
  28         using timer_t=hp_timer<ppd::generic_traits::api_type::posix_pthreads, heavyweight_threading>;
  29 
  30         if (period==0) {
  31                 return try_lock();
  32         } else if (period<lock_traits::infinite_timeout()) {
  33                 static const timer_t time;
  34                 timer_t::time_utc_t now=time.current_time();
  35                 now.tv_nsec+=(period%1000)*1000000;
  36                 now.tv_sec+=period/1000+now.tv_nsec/1000000000;
  37                 now.tv_nsec=now.tv_nsec%1000000000;
  38                 assert(now.tv_nsec>=0);
  39                 assert(now.tv_nsec<1000000000);
  40                 if (sem_timedwait(&sem, &now)==-1) {
  41                         switch (errno) {
  42                         case ETIMEDOUT: return atom_unset;
  43                         case EINTR: return atom_interrupted;
  44                         case EAGAIN: return atom_max_recurse;
  45                         case EINVAL:
  46                         default: return atom_abandoned;
  47                         };
  48                 }
  49                 return atom_set;
  50         } else {
  51                 return lock();
  52         }
  53 }
  54 
  55 } } }

/* [<][>][^][v][top][bottom][index][help] */