singleton_application.cpp

Go to the documentation of this file.
00001 /*****************************************************************************\
00002 *                                                                             *
00003 *  Name   : singleton_application                                             *
00004 *  Author : Chris Koeritz                                                     *
00005 *                                                                             *
00006 *******************************************************************************
00007 * Copyright (c) 2006-$now By Author.  This program is free software; you can  *
00008 * redistribute it and/or modify it under the terms of the GNU General Public  *
00009 * License as published by the Free Software Foundation; either version 2 of   *
00010 * the License or (at your option) any later version.  This is online at:      *
00011 *     http://www.fsf.org/copyleft/gpl.html                                    *
00012 * Please send any updates to: fred@gruntose.com                               *
00013 \*****************************************************************************/
00014 
00015 #include "singleton_application.h"
00016 
00017 #include <basis/functions.h>
00018 #include <filesystem/filename.h>
00019 
00020 using namespace basis;
00021 using namespace filesystem;
00022 using namespace processes;
00023 
00024 namespace application {
00025 
00026 singleton_application::singleton_application(const astring &application_name,
00027     const astring &user_name)
00028 : c_initial_try(0),
00029   _app_lock(new rendezvous(filename(application_name).basename().raw()
00030       + "__" + user_name)),
00031   _got_lock(false)
00032 {
00033 }
00034 
00035 singleton_application::~singleton_application()
00036 {
00037   release_lock();
00038   WHACK(_app_lock);
00039 }
00040 
00041 bool singleton_application::already_running()
00042 { return !allow_this_instance(); }
00043 
00044 bool singleton_application::already_tried() const
00045 { return c_initial_try > 0; }
00046 
00047 void singleton_application::release_lock()
00048 {
00049   if (_got_lock) {
00050     _app_lock->unlock();
00051     _got_lock = false;
00052   }
00053 }
00054 
00055 bool singleton_application::allow_this_instance()
00056 {
00057   if (_got_lock) return true;  // already grabbed it.
00058 
00059   if (!already_tried()) {
00060     _got_lock = _app_lock->lock(rendezvous::IMMEDIATE_RETURN);
00061     if (_got_lock) c_initial_try = 1;  // succeeded in locking.
00062     else c_initial_try = 2;  // failure.
00063   }
00064 
00065   return _got_lock;
00066 }
00067 
00068 } //namespace.
00069 
Generated on Sat Jan 28 04:22:09 2012 for hoople2 project by  doxygen 1.6.3