feisty meow concerns codebase 2.140
singleton_application.cpp
Go to the documentation of this file.
1/*****************************************************************************\
2* *
3* Name : singleton_application *
4* Author : Chris Koeritz *
5* *
6*******************************************************************************
7* Copyright (c) 2006-$now By Author. This program is free software; you can *
8* redistribute it and/or modify it under the terms of the GNU General Public *
9* License as published by the Free Software Foundation; either version 2 of *
10* the License or (at your option) any later version. This is online at: *
11* http://www.fsf.org/copyleft/gpl.html *
12* Please send any updates to: fred@gruntose.com *
13\*****************************************************************************/
14
16
17#include <basis/functions.h>
18#include <filesystem/filename.h>
19
20using namespace basis;
21using namespace filesystem;
22using namespace processes;
23
24namespace application {
25
27 const astring &user_name)
28: c_initial_try(0),
29 _app_lock(new rendezvous(filename(application_name).basename().raw()
30 + "__" + user_name)),
31 _got_lock(false)
32{
33}
34
40
43
45{ return c_initial_try > 0; }
46
48{
49 if (_got_lock) {
50 _app_lock->unlock();
51 _got_lock = false;
52 }
53}
54
56{
57 if (_got_lock) return true; // already grabbed it.
58
59 if (!already_tried()) {
60 _got_lock = _app_lock->lock(rendezvous::IMMEDIATE_RETURN);
61 if (_got_lock) c_initial_try = 1; // succeeded in locking.
62 else c_initial_try = 2; // failure.
63 }
64
65 return _got_lock;
66}
67
68} //namespace.
69
bool allow_this_instance()
the application must check this before starting up.
void release_lock()
let's go of the application lock, if we had previously gotten it.
bool already_running()
returns false if this program is not already running.
bool already_tried() const
returns true if the singleton has already tried to lock.
singleton_application(const basis::astring &application_name, const basis::astring &user_name=basis::astring::empty_string())
constructs a singleton guardian for the "application_name".
Provides a dynamically resizable ASCII character string.
Definition astring.h:35
Provides operations commonly needed on file names.
Definition filename.h:64
An inter-process synchronization primitive.
Definition rendezvous.h:34
bool lock(locking_methods how=ENDLESS_WAIT)
grab the lock, if possible.
void unlock()
releases a previously acquired lock.
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
Definition array.h:30
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
Definition functions.h:121
A platform independent way to obtain the timestamp of a file.