home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / klockfile.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  2.9 KB  |  124 lines

  1. /*
  2.    This file is part of the KDE libraries
  3.    Copyright (c) 2004 Waldo Bastian <bastian@kde.org>
  4.    
  5.    This library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License version 2 as published by the Free Software Foundation.
  8.    
  9.    This library is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  12.    Library General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU Library General Public License
  15.    along with this library; see the file COPYING.LIB.  If not, write to
  16.    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17.    Boston, MA 02110-1301, USA.
  18. */
  19.  
  20. #ifndef _KLOCKFILE_H_
  21. #define _KLOCKFILE_H_
  22.  
  23. #include <qstring.h>
  24. #include <qdatetime.h>
  25.  
  26. #include <ksharedptr.h>
  27. /**
  28.  * The KLockFile class provides NFS safe lockfiles.
  29.  *
  30.  * @author Waldo Bastian <bastian@kde.org>
  31.  * @since 3.3
  32.  */
  33. class KDECORE_EXPORT KLockFile : public KShared
  34. {
  35. public:
  36.    typedef KSharedPtr<KLockFile> Ptr;
  37.  
  38.    KLockFile(const QString &file);
  39.  
  40.    /**
  41.     * Destroys the object, releasing the lock if held
  42.     **/
  43.    ~KLockFile();
  44.  
  45.    /**
  46.     * Possible return values of the lock function.
  47.     */
  48.    enum LockResult {
  49.      /**
  50.       * Lock was acquired successfully
  51.       */
  52.      LockOK = 0, 
  53.  
  54.      /**
  55.       * The lock could not be acquired because it is held by another process
  56.       */
  57.      LockFail,
  58.      
  59.      /**
  60.       * The lock could not be acquired due to an error
  61.       */
  62.      LockError,
  63.  
  64.      /**
  65.       * A stale lock has been detected
  66.       */
  67.      LockStale
  68.    };
  69.  
  70.    enum LockOptions {
  71.      /**
  72.       * Return immediately, do not wait for the lock to become available
  73.       */
  74.      LockNoBlock = 1,
  75.      
  76.      /**
  77.       * Automatically remove a lock when a lock is detected that is stale
  78.       * for more than staleTime() seconds.
  79.       */
  80.      LockForce = 2
  81.    };
  82.  
  83.    /**
  84.     * Attempt to acquire the lock
  85.     *
  86.     * @param options A set of @ref LockOptions OR'ed together.
  87.     */
  88.    LockResult lock(int options=0);
  89.    
  90.    /**
  91.     * Returns whether the lock is held or not
  92.     */
  93.    bool isLocked() const;
  94.    
  95.    /**
  96.     * Release the lock
  97.     */
  98.    void unlock();
  99.  
  100.    /**
  101.     * Return the time in seconds after which a lock is considered stale
  102.     * The default is 30.
  103.     */
  104.    int staleTime() const;
  105.    
  106.    /**
  107.     * Set the time in seconds after which a lock is considered stale
  108.     */
  109.    void setStaleTime(int _staleTime);
  110.  
  111.    /**
  112.     * Returns the pid, hostname and appname of the process holding
  113.     * the lock after the lock functon has returned with LockStale.
  114.     * @returns false if the pid and hostname could not be determined
  115.     */
  116.    bool getLockInfo(int &pid, QString &hostname, QString &appname);
  117.  
  118. private:
  119.    class KLockFilePrivate;
  120.    KLockFilePrivate *d;
  121. };
  122.  
  123. #endif
  124.